Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
b126a15
pigeon stuff
LouiseHsu Sep 3, 2024
ae9095a
refactored AND working heh
LouiseHsu Sep 4, 2024
c38b66f
format
LouiseHsu Sep 4, 2024
268a50e
pre refactor for cache
LouiseHsu Sep 4, 2024
f6ec07a
.
LouiseHsu Sep 4, 2024
aa36dfd
fix configuration file
LouiseHsu Sep 5, 2024
8724d40
more docs
LouiseHsu Sep 5, 2024
a6cbd98
more docs and clean up
LouiseHsu Sep 5, 2024
b75eec7
.
LouiseHsu Sep 13, 2024
4d2fd59
FIXED TESTING ISSUE
LouiseHsu Sep 24, 2024
aeef979
native tests are donesies
LouiseHsu Sep 25, 2024
acf499f
moreeee clean up and tests,
LouiseHsu Sep 26, 2024
c3c7204
format, comments, remove prints
LouiseHsu Sep 26, 2024
9021b6f
versioning, analyze
LouiseHsu Sep 26, 2024
666df56
symlink the test files
LouiseHsu Sep 26, 2024
97b746f
fix mac cant build
LouiseHsu Sep 26, 2024
add5bf5
podspec, licensing
LouiseHsu Sep 26, 2024
06b42d0
.
LouiseHsu Sep 27, 2024
0701d2a
remove any to get around old linter
LouiseHsu Sep 27, 2024
8f225c0
remove more anys to appease format gods
LouiseHsu Sep 27, 2024
2c6efdb
Merge branch 'main' into iap_add_purchase
LouiseHsu Sep 27, 2024
72ce51c
a lil bit of refactioring
LouiseHsu Oct 1, 2024
2e940fe
Merge branch 'iap_add_purchase' of github.com:LouiseHsu/packages into…
LouiseHsu Oct 1, 2024
442c158
better error messages
LouiseHsu Oct 1, 2024
f48510b
some refactoring
LouiseHsu Oct 1, 2024
6076bf1
.
LouiseHsu Oct 1, 2024
d8dcad2
remove uneeded filed
LouiseHsu Oct 2, 2024
9c2893b
oops pathing
LouiseHsu Oct 2, 2024
1a74fb7
.
LouiseHsu Oct 2, 2024
1b688ae
.
LouiseHsu Oct 2, 2024
74c2de3
.
LouiseHsu Oct 4, 2024
71c9a1b
.
LouiseHsu Oct 4, 2024
582156b
no message
LouiseHsu Oct 4, 2024
e6bec70
.
LouiseHsu Oct 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
some refactoring
  • Loading branch information
LouiseHsu committed Oct 1, 2024
commit f48510bf937bbafa0f1b7e278e8c98174a646a2e
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public class InAppPurchasePlugin: NSObject, FlutterPlugin, InAppPurchaseAPI {
// This property is optional, as it requires self to exist to be initialized.
public var paymentQueueHandler: FLTPaymentQueueHandlerProtocol?

// This should be an Task, but Task is on available >= iOS 13
var updateListenerTask: Any?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you try out that getter trick discussed yesterday?

var transactionListenerAPI: TransactionCallbacks? = nil
var transactionDelegate: InAppPurchase2CallbackAPI? = nil

public static func register(with registrar: FlutterPluginRegistrar) {
#if os(iOS)
Expand Down Expand Up @@ -96,7 +97,7 @@ public class InAppPurchasePlugin: NSObject, FlutterPlugin, InAppPurchaseAPI {
let messenger = registrar.messenger
#endif
setupTransactionObserverChannelIfNeeded(withMessenger: messenger)
self.transactionListenerAPI = TransactionCallbacks.init(binaryMessenger: messenger)
self.transactionDelegate = InAppPurchase2CallbackAPI(binaryMessenger: messenger)
}

// MARK: - Pigeon Functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// found in the LICENSE file.

@available(iOS 15.0, macOS 12.0, *)
extension InAppPurchasePlugin: @preconcurrency InAppPurchase2API {
extension InAppPurchasePlugin: InAppPurchase2API {
// MARK: - Pigeon Functions

/// Wrapper method around StoreKit2's canMakePayments() method
Expand Down Expand Up @@ -37,12 +37,12 @@ extension InAppPurchasePlugin: @preconcurrency InAppPurchase2API {

/// Gets the appropriate product, then calls purchase on it.
/// https://developer.apple.com/documentation/storekit/product/3791971-purchase
@MainActor
func purchase(
id: String, options: SK2ProductPurchaseOptionsMessage?,
completion: @escaping (Result<SK2ProductPurchaseResultMessage, Error>) -> Void
) {
Task {
@MainActor in
do {
let product = try await Product.products(for: [id]).first
guard let product = product else {
Expand All @@ -59,7 +59,7 @@ extension InAppPurchasePlugin: @preconcurrency InAppPurchase2API {
case .success(let verification):
switch verification {
case .verified(let transaction):
self.transactionListenerAPI?.transactionUpdated(updatedTransactions: transaction)
self.sendTransactionUpdate(transaction: transaction)
completion(.success(result.convertToPigeon()))
case .unverified(_, let error):
completion(.failure(error))
Expand Down Expand Up @@ -94,6 +94,7 @@ extension InAppPurchasePlugin: @preconcurrency InAppPurchase2API {
completion: @escaping (Result<[SK2TransactionMessage], Error>) -> Void
) {
Task {
@MainActor in
do {
let transactionsMsgs = await rawTransactions().map {
$0.convertToPigeon()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use keypath

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Keypath can’t be used on instance methods"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh you are right

Expand All @@ -117,12 +118,12 @@ extension InAppPurchasePlugin: @preconcurrency InAppPurchase2API {
/// https://developer.apple.com/documentation/storekit/transaction/3851206-updates
/// This function should be called as soon as the app starts to avoid missing any Transactions done outside of the app.
func startListeningToTransactions() throws {
self.updateListenerTask = Task {
self.updateListenerTask = Task { [weak self] in
for await verificationResult in Transaction.updates {
switch verificationResult {
case .verified(let transaction):
self.transactionListenerAPI?.transactionUpdated(updatedTransactions: transaction)
case .unverified(_, _):
self?.sendTransactionUpdate(transaction: transaction)
case .unverified:
break
}
}
Expand All @@ -131,18 +132,29 @@ extension InAppPurchasePlugin: @preconcurrency InAppPurchase2API {

/// Stop subscribing to Transaction.updates
func stopListeningToTransactions() throws {
self.updateListenerTask = nil
getUpdateListenerTask().cancel()
}

/// Sends an transaction back to Dart. Access these transactions with `purchaseStream`
func sendTransactionUpdate(transaction: Transaction) {
let transactionMsg = transaction.convertToPigeon()
transactionDelegate?.onTransactionsUpdated(newTransaction: transactionMsg) { result in
switch result {
case .success: break
case .failure(let error):
print("Failed to send transaction updates: \(error)")
}
}
}

/// Helper function that fetches and unwraps all verified transactions
private func rawTransactions() async -> [Transaction] {
var transactions: [Transaction] = []

for await verificationResult in Transaction.all {
switch verificationResult {
case .verified(let transaction):
transactions.append(transaction)
case .unverified(_, _):
case .unverified:
break
}
}
Expand All @@ -157,10 +169,15 @@ extension InAppPurchasePlugin: @preconcurrency InAppPurchase2API {
if transaction.id == id {
return transaction
}
case .unverified(_, _):
case .unverified:
continue
}
}
return nil
}

/// Helper function to cast updateListenerTask to a task
func getUpdateListenerTask() -> Task<(), Never> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use a getter (rather than a function) in the class definition (rather than extension). And make updateListenerTask private.

return self.updateListenerTask as! Task<(), Never>
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

final class TransactionCallbacks: InAppPurchase2CallbackAPI {
let callbackAPI: InAppPurchase2CallbackAPI

init(binaryMessenger: FlutterBinaryMessenger) {
callbackAPI = InAppPurchase2CallbackAPI(binaryMessenger: binaryMessenger)
super.init(binaryMessenger: binaryMessenger)
}

@available(iOS 15.0, macOS 12.0, *)
func transactionUpdated(updatedTransactions: Transaction, restoring: Bool = false) {
let transactionMsg = updatedTransactions.convertToPigeon(
restoring: restoring)
callbackAPI.onTransactionsUpdated(newTransaction: transactionMsg) { result in
switch result {
case .success: break
case .failure(let error):
print("Failed to send transaction updates: \(error)")
}
}
}

}
//// Copyright 2013 The Flutter Authors. All rights reserved.
//// Use of this source code is governed by a BSD-style license that can be
//// found in the LICENSE file.
//
///// TODO Move to main plugin
//final class TransactionCallbacks: InAppPurchase2CallbackAPI {
// let callbackAPI: InAppPurchase2CallbackAPI
//
// init(binaryMessenger: FlutterBinaryMessenger) {
// callbackAPI = InAppPurchase2CallbackAPI(binaryMessenger: binaryMessenger)
// super.init(binaryMessenger: binaryMessenger)
// }
//
// @available(iOS 15.0, macOS 12.0, *)
// func transactionUpdated(updatedTransactions: Transaction, restoring: Bool = false) {
// let transactionMsg = updatedTransactions.convertToPigeon(
// restoring: restoring)
// callbackAPI.onTransactionsUpdated(newTransaction: transactionMsg) { result in
// switch result {
// case .success: break
// case .failure(let error):
// print("Failed to send transaction updates: \(error)")
// }
// }
// }
//
//}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class InAppPurchaseStoreKitPlatform extends InAppPurchasePlatform {
InAppPurchaseStoreKitPlatform();

/// Experimental flag for StoreKit2.
static bool _useStoreKit2 = true;
static bool _useStoreKit2 = false;

/// StoreKit1
static late SKPaymentQueueWrapper _skPaymentQueueWrapper;
Expand Down