Index: src/Classes/Store.h =================================================================== --- src/Classes/Store.h (revision 1418) +++ src/Classes/Store.h (working copy) @@ -13,6 +13,9 @@ @interface Store : UIViewController { + NSInteger mode; + BOOL reloadRequested; + Reachability* reachability; SKRequest* request; NSArray* products; Index: src/Classes/Item.h =================================================================== --- src/Classes/Item.h (revision 1418) +++ src/Classes/Item.h (working copy) @@ -19,6 +19,8 @@ UIView* loadingView; } +@property (nonatomic, retain) SKProduct* product; + @property (nonatomic, retain) IBOutlet UITextView* desc; @property (nonatomic, retain) IBOutlet UIButton* button; @property (nonatomic, retain) IBOutlet UIView* loadingView; Index: src/Classes/Store.m =================================================================== --- src/Classes/Store.m (revision 1418) +++ src/Classes/Store.m (working copy) @@ -10,6 +10,14 @@ #import "Store.h" +enum Modes +{ + Mode_Load, + Mode_Restore, + Mode_Display, +}; + + @interface Store () @property (nonatomic, retain) Reachability* reachability; @@ -18,6 +26,8 @@ @property (nonatomic, retain, readonly) NSNumberFormatter* priceFormatter; - (void)reachabilityChanged:(NSNotification*)note; +- (void)localeChanged:(NSNotification*)note; +- (void)load; - (void)restore; - (void)dismiss; @@ -73,11 +83,14 @@ self.reachability = [Reachability reachabilityWithHostName:@"www.apple.com"]; [self.reachability startNotifer]; + // Register for locale changes + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(localeChanged:) + name:NSCurrentLocaleDidChangeNotification + object:nil]; + // Populate store - NSArray* a = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Products" ofType:@"plist"]]; - self.request = [[[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithArray:a]] autorelease]; - self.request.delegate = self; - [self.request start]; + [self load]; // Observe restore operations [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; @@ -234,6 +247,8 @@ - (void)productsRequest:(SKProductsRequest*)aRequest didReceiveResponse:(SKProductsResponse*)response { + mode = Mode_Display; + self.request = nil; self.products = response.products; @@ -241,6 +256,29 @@ [self.tableView reloadData]; self.loadingView.hidden = YES; + + // Is there an Item on display? + UIViewController* topVC = self.navigationController.topViewController; + if (topVC != self) + { + // What Product is it displaying? + Item* item = (Item*) topVC; + NSString* oldProductId = item.product.productIdentifier; + for (SKProduct* newProduct in products) + { + if ([oldProductId isEqualToString:newProduct.productIdentifier]) + { + // Push an update! + item.product = newProduct; + break; + } + } + } + + if (reloadRequested) + { + [self load]; + } } #pragma mark Store kit observer methods @@ -253,21 +291,33 @@ - (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue*)queue { - if (self.products) + if (mode == Mode_Restore) { + mode = Mode_Display; + self.navigationItem.rightBarButtonItem.enabled = YES; self.loadingView.hidden = YES; } + if (reloadRequested && (mode == Mode_Display)) + { + [self load]; + } } - (void)paymentQueue:(SKPaymentQueue*)queue restoreCompletedTransactionsFailedWithError:(NSError*)error { - if (self.products) + if (mode == Mode_Restore) { + mode = Mode_Display; + self.navigationItem.rightBarButtonItem.enabled = YES; self.loadingView.hidden = YES; } + if (reloadRequested && (mode == Mode_Display)) + { + [self load]; + } } #pragma mark Alert view delegate methods @@ -298,14 +348,51 @@ } -- (void)restore +- (void)localeChanged:(NSNotification*)note { - self.loadingLabel.text = @"Restoring"; + if (mode == Mode_Display) + { + // If the store is currently displaying, then reload it + [self load]; + } + else + { + // O/w, queue a reload request + reloadRequested = YES; + } +} + +- (void)load +{ + mode = Mode_Load; + reloadRequested = NO; + + self.loadingLabel.text = @"Loading"; + self.navigationItem.rightBarButtonItem.enabled = NO; self.loadingView.hidden = NO; + + NSArray* a = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Products" ofType:@"plist"]]; + self.request = [[[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithArray:a]] autorelease]; + self.request.delegate = self; + [self.request start]; +} + + +- (void)restore +{ + if (mode == Mode_Display) + { + mode = Mode_Restore; + + self.loadingLabel.text = @"Restoring"; + + self.navigationItem.rightBarButtonItem.enabled = NO; + self.loadingView.hidden = NO; - [[SKPaymentQueue defaultQueue] restoreCompletedTransactions]; + [[SKPaymentQueue defaultQueue] restoreCompletedTransactions]; + } } Index: src/Classes/Item.m =================================================================== --- src/Classes/Item.m (revision 1418) +++ src/Classes/Item.m (working copy) @@ -10,13 +10,31 @@ #import "Item.h" +@interface Item () + +- (void)updateDisplay; + +@end + + @implementation Item +@synthesize product; @synthesize desc; @synthesize button; @synthesize loadingView; +- (void)setProduct:(SKProduct*)newProduct +{ + if (newProduct != product) + { + [product autorelease]; + product = [newProduct retain]; + [self updateDisplay]; + } +} + // The parent's designated initializer. Overriden to call the new DI. - (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil { @@ -31,7 +49,7 @@ { if (aProduct) { - product = [aProduct retain]; + self.product = aProduct; } else { @@ -61,15 +79,8 @@ background.frame = CGRectMake(0, 0, 320, 416); [self.view insertSubview:background atIndex:0]; - // Helper for price formatting - NSNumberFormatter* pf = [[[NSNumberFormatter alloc] init] autorelease]; - pf.formatterBehavior = NSNumberFormatterBehavior10_4; - pf.numberStyle = NSNumberFormatterCurrencyStyle; - pf.locale = product.priceLocale; - // Title and description - self.title = [NSString stringWithFormat:@"%@ %@",product.localizedTitle,[pf stringFromNumber:product.price]]; - desc.text = product.localizedDescription; + [self updateDisplay]; if (![button backgroundImageForState:UIControlStateNormal]) { @@ -153,4 +164,17 @@ } } +#pragma mark Extension methods + +- (void)updateDisplay +{ + NSNumberFormatter* pf = [[[NSNumberFormatter alloc] init] autorelease]; + pf.formatterBehavior = NSNumberFormatterBehavior10_4; + pf.numberStyle = NSNumberFormatterCurrencyStyle; + pf.locale = product.priceLocale; + + self.title = [NSString stringWithFormat:@"%@ %@",product.localizedTitle,[pf stringFromNumber:product.price]]; + desc.text = product.localizedDescription; +} + @end