Permalink
Browse files

Separated platform-dependant concerns into new ILSimTransactionSimula…

…tor protocol. Mac support.
  • Loading branch information...
1 parent 8ae8cbe commit d61584b0dc0d52ff2276fd089992aed579500361 @millenomi committed Jul 13, 2011
Showing with 397 additions and 50 deletions.
  1. +5 −0 ILSimSKPaymentQueue.h
  2. +12 −33 ILSimSKPaymentQueue.m
  3. +23 −0 ILSimTransactionSimulator.h
  4. +125 −0 ILSimTransactionSimulator.m
  5. +232 −17 SimStoreKit.xcodeproj/project.pbxproj
View
@@ -14,6 +14,8 @@
#import "ILSimSKPayment.h"
#import "ILSimSKPaymentTransaction.h"
+@protocol ILSimTransactionSimulator;
+
// ENVIRONMENT VARIABLES THAT CHANGE THIS CLASS'S BEHAVIOR:
// if -boolValue == NO, the user cannot make payments.
@@ -58,6 +60,7 @@ enum {
NSMutableArray* transactions;
ILSimSKPaymentTransaction* currentTransaction;
+ id <ILSimTransactionSimulator> transactionSimulator;
}
+ (BOOL) canMakePayments;
@@ -74,6 +77,8 @@ enum {
@property(nonatomic, readonly) NSArray* transactions;
+@property(nonatomic, assign) id <ILSimTransactionSimulator> transactionSimulator;
+
@end
View
@@ -9,8 +9,7 @@
#import "ILSimStoreKit.h"
#if kILSimAllowSimulatedStoreKit
-
-#import <UIKit/UIKit.h>
+#import "ILSimTransactionSimulator.h"
#import "ILSimSKPaymentQueue.h"
#import "ILSimSKPaymentTransaction_Private.h"
@@ -19,7 +18,7 @@
NSString* const kILSimSKErrorDomain = @"net.infinite-labs.SimulatedStoreKit";
-@interface ILSimSKPaymentQueue () <UIAlertViewDelegate>
+@interface ILSimSKPaymentQueue ()
@property(nonatomic, retain) ILSimSKPaymentTransaction* currentTransaction;
@@ -32,6 +31,8 @@ - (void) fail:(NSError *)e;
@implementation ILSimSKPaymentQueue
+@synthesize transactionSimulator;
+
+ (BOOL) canMakePayments;
{
id o = [[[NSProcessInfo processInfo] environment] objectForKey:kILSimSKCanMakePaymentsEnvironmentVariable];
@@ -44,6 +45,8 @@ - (id) init
if (self != nil) {
observers = [NSMutableSet new];
transactions = [NSMutableArray new];
+
+ transactionSimulator = ILSimDefaultTransactionSimulator();
}
return self;
}
@@ -117,36 +120,12 @@ - (void) ask;
if (!p)
[self fail:[NSError errorWithDomain:kILSimSKErrorDomain code:kILSimSKErrorPaymentInvalid userInfo:nil]];
else {
- UIAlertView* a = [[UIAlertView new] autorelease];
- a.delegate = self;
-
- NSNumberFormatter* numberFormatter = [[NSNumberFormatter new] autorelease];
- [numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
- [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
- [numberFormatter setLocale:p.priceLocale];
- NSString* formattedPrice = [numberFormatter stringFromNumber:p.price];
-
- a.title = [NSString stringWithFormat:@"SIMULATED: Would you like to buy %d x '%@' at %@?", self.currentTransaction.payment.quantity, p.localizedTitle, formattedPrice];
- [a addButtonWithTitle:@"Succeed Transaction"];
- [a addButtonWithTitle:@"Fail Transaction"];
- a.cancelButtonIndex = [a addButtonWithTitle:@"Cancel"];
- [a show];
- }
-}
-
-- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
-{
- switch (buttonIndex) {
- case 0:
- [self succeed];
- break;
- case 1:
- [self fail:[NSError errorWithDomain:kILSimSKErrorDomain code:kILSimSKErrorUnknown userInfo:nil]];
- break;
- case 2:
- default:
- [self fail:[NSError errorWithDomain:kILSimSKErrorDomain code:kILSimSKErrorPaymentCancelled userInfo:nil]];
- break;
+ [self.transactionSimulator simulateTransaction:self.currentTransaction product:p forQueue:self completionHandler:^(NSError* e) {
+ if (!e)
+ [self succeed];
+ else
+ [self fail:e];
+ }];
}
}
@@ -0,0 +1,23 @@
+//
+// ILSimTransactionSimulator.h
+// SimStoreKit
+//
+// Created by ∞ on 13/07/11.
+// Copyright 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#if kILSimAllowSimulatedStoreKit
+
+#import "ILSimSKPaymentQueue.h"
+#import "ILSimSKPaymentTransaction.h"
+#import "ILSimSKProduct.h"
+
+@protocol ILSimTransactionSimulator <NSObject>
+
+- (void) simulateTransaction:(ILSimSKPaymentTransaction*)transaction product:(ILSimSKProduct*) product forQueue:(ILSimSKPaymentQueue*)queue completionHandler:(void (^)(NSError *))block;
+
+@end
+
+extern id <ILSimTransactionSimulator> ILSimDefaultTransactionSimulator();
+#endif // kILSimAllowSimulatedStoreKit
View
@@ -0,0 +1,125 @@
+//
+// ILSimTransactionSimulator.m
+// SimStoreKit
+//
+// Created by ∞ on 13/07/11.
+// Copyright 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import "ILSimTransactionSimulator.h"
+#if kILSimAllowSimulatedStoreKit
+
+#if TARGET_OS_IPHONE
+
+#import <UIKit/UIKit.h>
+#import <objc/runtime.h>
+
+static char kILSimDefaultTransactionSimulator_iOSCompletionHandlerToken = 0;
+static void* const kILSimDefaultTransactionSimulator_iOSCompletionHandlerKey = &kILSimDefaultTransactionSimulator_iOSCompletionHandlerToken;
+
+@interface ILSimDefaultTransactionSimulator_iOS : NSObject <ILSimTransactionSimulator, UIAlertViewDelegate>
+@end
+
+@implementation ILSimDefaultTransactionSimulator_iOS
+
+- (void) simulateTransaction:(ILSimSKPaymentTransaction *)transaction product:(ILSimSKProduct*) p forQueue:(ILSimSKPaymentQueue *)queue completionHandler:(void (^)(NSError *))block;
+{
+ UIAlertView* a = [[UIAlertView new] autorelease];
+ a.delegate = self;
+ objc_setAssociatedObject(a, kILSimDefaultTransactionSimulator_iOSCompletionHandlerKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
+
+ NSNumberFormatter* numberFormatter = [[NSNumberFormatter new] autorelease];
+ [numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
+ [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
+ [numberFormatter setLocale:p.priceLocale];
+ NSString* formattedPrice = [numberFormatter stringFromNumber:p.price];
+
+ a.title = [NSString stringWithFormat:@"SIMULATED: Would you like to buy %d x '%@' at %@?", transaction.payment.quantity, p.localizedTitle, formattedPrice];
+ [a addButtonWithTitle:@"Succeed Transaction"];
+ [a addButtonWithTitle:@"Fail Transaction"];
+ a.cancelButtonIndex = [a addButtonWithTitle:@"Cancel"];
+ [a show];
+}
+
+- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
+{
+ void (^completionHandler)(NSError*) = objc_getAssociatedObject(alertView, kILSimDefaultTransactionSimulator_iOSCompletionHandlerKey);
+ if (!completionHandler)
+ return;
+
+ switch (buttonIndex) {
+ case 0:
+ completionHandler(nil);
+ break;
+ case 1:
+ completionHandler([NSError errorWithDomain:kILSimSKErrorDomain code:kILSimSKErrorUnknown userInfo:nil]);
+ break;
+ case 2:
+ default:
+ completionHandler([NSError errorWithDomain:kILSimSKErrorDomain code:kILSimSKErrorPaymentCancelled userInfo:nil]);
+ break;
+ }
+}
+
+@end
+
+#elif TARGET_OS_MAC && !TARGET_OS_IPHONE
+
+#import <Cocoa/Cocoa.h>
+
+@interface ILSimDefaultTransactionSimulator_Mac : NSObject <ILSimTransactionSimulator>
+@end
+
+@implementation ILSimDefaultTransactionSimulator_Mac
+
+- (void) simulateTransaction:(ILSimSKPaymentTransaction *)transaction product:(ILSimSKProduct*) p forQueue:(ILSimSKPaymentQueue *)queue completionHandler:(void (^)(NSError *))block;
+{
+ NSAlert* a = [[NSAlert new] autorelease];
+
+ NSNumberFormatter* numberFormatter = [[NSNumberFormatter new] autorelease];
+ [numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
+ [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
+ [numberFormatter setLocale:p.priceLocale];
+ NSString* formattedPrice = [numberFormatter stringFromNumber:p.price];
+
+ [a setMessageText:[NSString stringWithFormat:@"Simulated StoreKit: Would you like to buy %d x '%@' at %@?", transaction.payment.quantity, p.localizedTitle, formattedPrice]];
+
+ [a addButtonWithTitle:@"Succeed Transaction"];
+ [a addButtonWithTitle:@"Cancel"];
+ [a addButtonWithTitle:@"Fail Transaction"];
+
+ NSInteger result = [a runModal];
+ switch (result) {
+ case NSAlertFirstButtonReturn:
+ block(nil);
+ break;
+
+ case NSAlertSecondButtonReturn:
+ block([NSError errorWithDomain:kILSimSKErrorDomain code:kILSimSKErrorUnknown userInfo:nil]);
+ break;
+
+ case NSAlertThirdButtonReturn:
+ block([NSError errorWithDomain:kILSimSKErrorDomain code:kILSimSKErrorUnknown userInfo:nil]);
+ break;
+ }
+}
+
+@end
+
+#endif
+
+
+id <ILSimTransactionSimulator> ILSimDefaultTransactionSimulator() {
+ static id simulator = nil; if (!simulator) {
+#if TARGET_OS_IPHONE
+ simulator = [ILSimDefaultTransactionSimulator_iOS new];
+#elif TARGET_OS_MAC && !TARGET_OS_IPHONE
+ simulator = [ILSimDefaultTransactionSimulator_Mac new];
+#endif
+ }
+
+ return simulator;
+}
+
+
+#endif // kILSimAllowSimulatedStoreKit
Oops, something went wrong.

0 comments on commit d61584b

Please sign in to comment.