Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit eedda91

Browse files
committed
Processed feedback on pull request.
1 parent 1790118 commit eedda91

File tree

6 files changed

+80
-25
lines changed

6 files changed

+80
-25
lines changed

packages/in_app_purchase/in_app_purchase_storekit/example/ios/RunnerTests/FIATransactionCacheTests.m

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,14 @@ - (void)testGetObjectsForNonExistingKey {
3838
XCTAssertNil([cache getObjectsForKey:TransactionCacheKeyUpdatedTransactions]);
3939
}
4040

41-
- (void)testRemoveObjectsForNonExistingKey {
42-
FIATransactionCache *cache = [[FIATransactionCache alloc] init];
43-
[cache removeObjectsForKey:TransactionCacheKeyUpdatedTransactions];
44-
}
45-
46-
- (void)testRemoveObjectsForExistingKey {
41+
- (void)testClear {
4742
NSArray *dummyArray = @[ @1, @2, @3 ];
4843
FIATransactionCache *cache = [[FIATransactionCache alloc] init];
4944
[cache addObjects:dummyArray forKey:TransactionCacheKeyUpdatedTransactions];
5045

5146
XCTAssertEqual(dummyArray, [cache getObjectsForKey:TransactionCacheKeyUpdatedTransactions]);
5247

53-
[cache removeObjectsForKey:TransactionCacheKeyUpdatedTransactions];
48+
[cache clear];
5449
XCTAssertNil([cache getObjectsForKey:TransactionCacheKeyUpdatedTransactions]);
5550
}
5651
@end

packages/in_app_purchase/in_app_purchase_storekit/example/ios/RunnerTests/PaymentQueueTests.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,9 @@ - (void)testStartObservingPaymentQueueShouldProcessTransactionsForItemsInCache {
297297
]
298298
timeout:5];
299299
OCMVerify(times(1), [mockCache getObjectsForKey:TransactionCacheKeyUpdatedTransactions]);
300-
OCMVerify(times(1), [mockCache removeObjectsForKey:TransactionCacheKeyUpdatedTransactions]);
301300
OCMVerify(times(1), [mockCache getObjectsForKey:TransactionCacheKeyUpdatedDownloads]);
302-
OCMVerify(times(1), [mockCache removeObjectsForKey:TransactionCacheKeyUpdatedDownloads]);
303301
OCMVerify(times(1), [mockCache getObjectsForKey:TransactionCacheKeyRemovedTransactions]);
304-
OCMVerify(times(1), [mockCache removeObjectsForKey:TransactionCacheKeyRemovedTransactions]);
302+
OCMVerify(times(1), [mockCache clear]);
305303
}
306304

307305
- (void)testTransactionsShouldBeCachedWhenNotObserving {

packages/in_app_purchase/in_app_purchase_storekit/ios/Classes/FIAPaymentQueueHandler.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,27 @@ typedef void (^UpdatedDownloads)(NSArray<SKDownload *> *downloads);
2222
@property(NS_NONATOMIC_IOSONLY, weak, nullable) id<SKPaymentQueueDelegate> delegate API_AVAILABLE(
2323
ios(13.0), macos(10.15), watchos(6.2));
2424

25+
/// Creates a new FIAPaymentQueueHandler initialized with an empty
26+
/// FIATransactionCache.
27+
///
28+
/// @param queue The SKPaymentQueue instance connected to the App Store and
29+
/// responsible for processing transactions.
30+
/// @param transactionsUpdated Callback method that is called each time the App
31+
/// Store indicates transactions are updated.
32+
/// @param transactionsRemoved Callback method that is called each time the App
33+
/// Store indicates transactions are removed.
34+
/// @param restoreTransactionFailed Callback method that is called each time
35+
/// the App Store indicates transactions failed
36+
/// to restore.
37+
/// @param restoreCompletedTransactionsFinished Callback method that is called
38+
/// each time the App Store
39+
/// indicates restoring of
40+
/// transactions has finished.
41+
/// @param shouldAddStorePayment Callback method that is called each time an
42+
/// in-app purchase has been initiated from the
43+
/// App Store.
44+
/// @param updatedDownloads Callback method that is called each time the App
45+
/// Store indicates downloads are updated.
2546
- (instancetype)initWithQueue:(nonnull SKPaymentQueue *)queue
2647
transactionsUpdated:(nullable TransactionsUpdated)transactionsUpdated
2748
transactionRemoved:(nullable TransactionsRemoved)transactionsRemoved
@@ -35,6 +56,42 @@ typedef void (^UpdatedDownloads)(NSArray<SKDownload *> *downloads);
3556
"'initWithQueue:transactionsUpdated:transactionsRemoved:restoreTransactionsFinished:"
3657
"shouldAddStorePayment:updatedDownloads:transactionCache:' message instead.");
3758

59+
/// Creates a new FIAPaymentQueueHandler.
60+
///
61+
/// The "transactionsUpdated", "transactionsRemoved" and "updatedDownloads"
62+
/// callbacks are only called while actively observing transactions. To start
63+
/// observing transactions send the "startObservingPaymentQueue" message.
64+
/// Sending the "stopObservingPaymentQueue" message will stop actively
65+
/// observing transactions. When transactions are not observed they are cached
66+
/// to the "transactionCache" and will be delivered via the
67+
/// "transactionsUpdated", "transactionsRemoved" and "updatedDownloads"
68+
/// callbacks as soon as the "startObservingPaymentQueue" message arrives.
69+
///
70+
/// Note: cached transactions that are not processed when the application is
71+
/// killed will be delivered again by the App Store as soon as the application
72+
/// starts again.
73+
///
74+
/// @param queue The SKPaymentQueue instance connected to the App Store and
75+
/// responsible for processing transactions.
76+
/// @param transactionsUpdated Callback method that is called each time the App
77+
/// Store indicates transactions are updated.
78+
/// @param transactionsRemoved Callback method that is called each time the App
79+
/// Store indicates transactions are removed.
80+
/// @param restoreTransactionFailed Callback method that is called each time
81+
/// the App Store indicates transactions failed
82+
/// to restore.
83+
/// @param restoreCompletedTransactionsFinished Callback method that is called
84+
/// each time the App Store
85+
/// indicates restoring of
86+
/// transactions has finished.
87+
/// @param shouldAddStorePayment Callback method that is called each time an
88+
/// in-app purchase has been initiated from the
89+
/// App Store.
90+
/// @param updatedDownloads Callback method that is called each time the App
91+
/// Store indicates downloads are updated.
92+
/// @param transactionCache An empty [FIATransactionCache] instance that is
93+
/// responsible for keeping track of transactions that
94+
/// arrive when not actively observing transactions.
3895
- (instancetype)initWithQueue:(nonnull SKPaymentQueue *)queue
3996
transactionsUpdated:(nullable TransactionsUpdated)transactionsUpdated
4097
transactionRemoved:(nullable TransactionsRemoved)transactionsRemoved

packages/in_app_purchase/in_app_purchase_storekit/ios/Classes/FIAPaymentQueueHandler.m

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ @interface FIAPaymentQueueHandler ()
4545
/// client as soon as it starts observing. The Flutter client can start
4646
/// observing by sending a startObservingPaymentQueue message and stop by
4747
/// sending a stopObservingPaymentQueue message.
48-
@property(atomic, assign, readwrite) BOOL isObservingTransactions;
48+
@property(atomic, assign, readwrite, getter=isObservingTransactions) BOOL observingTransactions;
4949

5050
@end
5151

@@ -98,34 +98,39 @@ - (instancetype)initWithQueue:(nonnull SKPaymentQueue *)queue
9898
}
9999

100100
- (void)startObservingPaymentQueue {
101-
self.isObservingTransactions = true;
101+
self.observingTransactions = YES;
102102

103103
[self processCachedTransactions];
104104
}
105105

106106
- (void)stopObservingPaymentQueue {
107-
self.isObservingTransactions = false;
107+
// Don't remove the transaction observer from the SKPaymentQueue instance as
108+
// that will result in transactions being lost during the current application
109+
// lifetime. Only setting the "observingTransactions" to "NO" ensures
110+
// incoming transactions from the App Store are cached and delivered to the
111+
// client as soon as it indicates it is ready to receive transactions by
112+
// sending the "startObservingPaymentQueue" message.
113+
self.observingTransactions = NO;
108114
}
109115

110116
- (void)processCachedTransactions {
111117
NSArray *cachedObjects =
112118
[self.transactionCache getObjectsForKey:TransactionCacheKeyUpdatedTransactions];
113119
if (cachedObjects) {
114120
self.transactionsUpdated(cachedObjects);
115-
[self.transactionCache removeObjectsForKey:TransactionCacheKeyUpdatedTransactions];
116121
}
117122

118123
cachedObjects = [self.transactionCache getObjectsForKey:TransactionCacheKeyUpdatedDownloads];
119124
if (cachedObjects) {
120125
self.updatedDownloads(cachedObjects);
121-
[self.transactionCache removeObjectsForKey:TransactionCacheKeyUpdatedDownloads];
122126
}
123127

124128
cachedObjects = [self.transactionCache getObjectsForKey:TransactionCacheKeyRemovedTransactions];
125129
if (cachedObjects) {
126130
self.transactionsRemoved(cachedObjects);
127-
[self.transactionCache removeObjectsForKey:TransactionCacheKeyRemovedTransactions];
128131
}
132+
133+
[self.transactionCache clear];
129134
}
130135

131136
- (BOOL)addPayment:(SKPayment *)payment {
@@ -168,7 +173,7 @@ - (void)showPriceConsentIfNeeded {
168173
// state of transactions and finish as appropriate.
169174
- (void)paymentQueue:(SKPaymentQueue *)queue
170175
updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions {
171-
if (!self.isObservingTransactions) {
176+
if (!self.observingTransactions) {
172177
[_transactionCache addObjects:transactions forKey:TransactionCacheKeyUpdatedTransactions];
173178
return;
174179
}
@@ -180,7 +185,7 @@ - (void)paymentQueue:(SKPaymentQueue *)queue
180185
// Sent when transactions are removed from the queue (via finishTransaction:).
181186
- (void)paymentQueue:(SKPaymentQueue *)queue
182187
removedTransactions:(NSArray<SKPaymentTransaction *> *)transactions {
183-
if (!self.isObservingTransactions) {
188+
if (!self.observingTransactions) {
184189
[_transactionCache addObjects:transactions forKey:TransactionCacheKeyRemovedTransactions];
185190
return;
186191
}
@@ -202,7 +207,7 @@ - (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
202207

203208
// Sent when the download state has changed.
204209
- (void)paymentQueue:(SKPaymentQueue *)queue updatedDownloads:(NSArray<SKDownload *> *)downloads {
205-
if (!self.isObservingTransactions) {
210+
if (!self.observingTransactions) {
206211
[_transactionCache addObjects:downloads forKey:TransactionCacheKeyUpdatedDownloads];
207212
return;
208213
}

packages/in_app_purchase/in_app_purchase_storekit/ios/Classes/FIATransactionCache.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ typedef NS_ENUM(NSUInteger, TransactionCacheKey) {
1212

1313
@interface FIATransactionCache : NSObject
1414

15-
/// Add objects to the transaction cache.
15+
/// Adds objects to the transaction cache.
1616
///
17-
/// If the cache already contain an array of objects on the specified key, the supplied
17+
/// If the cache already contains an array of objects on the specified key, the supplied
1818
/// array will be appended to the existing array.
1919
- (void)addObjects:(NSArray *)objects forKey:(TransactionCacheKey)key;
2020

@@ -23,8 +23,8 @@ typedef NS_ENUM(NSUInteger, TransactionCacheKey) {
2323
/// If there are no objects associated with the given key nil is returned.
2424
- (NSArray *)getObjectsForKey:(TransactionCacheKey)key;
2525

26-
/// Remove objects to the transaction cache for the supplied key.
27-
- (void)removeObjectsForKey:(TransactionCacheKey)key;
26+
/// Removes all objects from the transaction cache.
27+
- (void)clear;
2828

2929
@end
3030

packages/in_app_purchase/in_app_purchase_storekit/ios/Classes/FIATransactionCache.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ - (NSArray *)getObjectsForKey:(TransactionCacheKey)key {
3333
return self.cache[@(key)];
3434
}
3535

36-
- (void)removeObjectsForKey:(TransactionCacheKey)key {
37-
[self.cache removeObjectForKey:@(key)];
36+
- (void)clear {
37+
[self.cache removeAllObjects];
3838
}
3939

4040
@end

0 commit comments

Comments
 (0)