Skip to content

Commit 660d36c

Browse files
committed
fix: Compilation issues on different platforms
1 parent 2bc6957 commit 660d36c

File tree

4 files changed

+124
-118
lines changed

4 files changed

+124
-118
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import PackageDescription
55
let package = Package(
66
name: "Mercato",
77
platforms: [
8-
.iOS(.v15), .tvOS(.v15), .watchOS("8.0"),
8+
.iOS("15.4"), .tvOS("17.0"), .watchOS("10.0"), .macOS("12.3"), .visionOS(.v1)
99
],
1010
products: [
1111
.library(

[email protected]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import PackageDescription
55
let package = Package(
66
name: "Mercato",
77
platforms: [
8-
.iOS(.v15), .tvOS(.v15), .watchOS("8.0"),
8+
.iOS("15.4"), .tvOS("17.0"), .watchOS("10.0"), .macOS("12.3"), .visionOS(.v1)
99
],
1010
products: [
1111
.library(

Sources/Mercato/Mercato.swift

Lines changed: 118 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import StoreKit
22

3+
#if canImport(AppKit)
4+
import AppKit
5+
public typealias DisplayContext = NSViewController
6+
#elseif os(iOS)
7+
import class UIKit.UIWindowScene
8+
public typealias DisplayContext = UIWindowScene
9+
#endif
10+
311
extension Mercato {
412

513
/// Promotional offer for a purchase.
@@ -173,53 +181,6 @@ public final class Mercato: Sendable {
173181
return await product.isEligibleForIntroOffer
174182
}
175183

176-
/// Initiates the refund process for a specified product within a given `UIWindowScene`.
177-
///
178-
/// This function attempts to begin a refund request for a transaction associated with the given product ID.
179-
/// It handles different refund request outcomes, including user cancellation, success, and unknown errors.
180-
/// The function is asynchronous and can throw errors if verification or the refund process fails.
181-
///
182-
/// - Parameters:
183-
/// - productID: The identifier of the product for which the refund process is being requested.
184-
/// - scene: The `UIWindowScene` within which the refund request will be displayed to the user.
185-
///
186-
/// - Throws:
187-
/// - `MercatoError.failedVerification`: If the transaction cannot be verified.
188-
/// - `MercatoError.canceledByUser`: If the user cancels the refund request.
189-
/// - `MercatoError.unknown(error:)`: If an unknown error occurs during the refund process.
190-
/// - `MercatoError.refund(error:)`: If a `Transaction.RefundRequestError` occurs during the refund process.
191-
/// - `MercatoError.storeKit(error:)`: If a `StoreKitError` occurs during the refund process.
192-
///
193-
/// - Availability:
194-
/// - Not available on watchOS or tvOS.
195-
///
196-
/// - Returns:
197-
/// No return value. The function will either complete successfully or throw an error.
198-
@available(watchOS, unavailable)
199-
@available(watchOS, unavailable)
200-
@available(tvOS, unavailable)
201-
func beginRefundProcess(for productID: String, in scene: UIWindowScene) async throws {
202-
guard case .verified(let transaction) = await Transaction.latest(for: productID) else { throw MercatoError.failedVerification }
203-
204-
do {
205-
let status = try await transaction.beginRefundRequest(in: scene)
206-
207-
switch status {
208-
case .userCancelled:
209-
throw MercatoError.canceledByUser
210-
case .success:
211-
break
212-
@unknown default:
213-
throw MercatoError.unknown(error: nil)
214-
}
215-
} catch let error as Transaction.RefundRequestError {
216-
throw MercatoError.refund(error: error)
217-
} catch let error as StoreKitError {
218-
throw MercatoError.storeKit(error: error)
219-
} catch {
220-
throw MercatoError.unknown(error: error)
221-
}
222-
}
223184
}
224185

225186
// MARK: - Mercato: Purchase
@@ -404,31 +365,6 @@ public extension Mercato {
404365
try await AppStore.sync()
405366
}
406367

407-
/// Displays the subscription management interface for the user within the specified `UIWindowScene`.
408-
///
409-
/// This function presents the App Store's manage subscriptions screen, allowing the user to view and manage their active subscriptions.
410-
/// It is asynchronous and throws errors if there are issues invoking the subscription management screen.
411-
///
412-
/// - Parameters:
413-
/// - scene: The `UIWindowScene` within which the subscription management interface will be presented.
414-
///
415-
/// - Throws:
416-
/// - An error if the subscription management interface could not be presented.
417-
///
418-
/// - Availability:
419-
/// - Available on iOS 15.0 and later.
420-
/// - Not available on macOS, watchOS, or tvOS.
421-
///
422-
/// - Returns:
423-
/// No return value. The function will either complete successfully or throw an error.
424-
@available(iOS 15.0, *)
425-
@available(macOS, unavailable)
426-
@available(watchOS, unavailable)
427-
@available(tvOS, unavailable)
428-
static func showManageSubscriptions(in scene: UIWindowScene) async throws {
429-
try await AppStore.showManageSubscriptions(in: scene)
430-
}
431-
432368
/// Requests product data from the App Store.
433369
/// - Parameter identifiers: A set of product identifiers to load from the App Store. If any
434370
/// identifiers are not found, they will be excluded from the return
@@ -453,35 +389,6 @@ public extension Mercato {
453389
try await shared.isEligibleForIntroOffer(for: productId)
454390
}
455391

456-
/// Initiates the refund process for a specified product within a given `UIWindowScene`.
457-
///
458-
/// This function attempts to begin a refund request for a transaction associated with the given product ID.
459-
/// It handles different refund request outcomes, including user cancellation, success, and unknown errors.
460-
/// The function is asynchronous and can throw errors if verification or the refund process fails.
461-
///
462-
/// - Parameters:
463-
/// - productID: The identifier of the product for which the refund process is being requested.
464-
/// - scene: The `UIWindowScene` within which the refund request will be displayed to the user.
465-
///
466-
/// - Throws:
467-
/// - `MercatoError.failedVerification`: If the transaction cannot be verified.
468-
/// - `MercatoError.canceledByUser`: If the user cancels the refund request.
469-
/// - `MercatoError.unknown(error:)`: If an unknown error occurs during the refund process.
470-
/// - `MercatoError.refund(error:)`: If a `Transaction.RefundRequestError` occurs during the refund process.
471-
/// - `MercatoError.storeKit(error:)`: If a `StoreKitError` occurs during the refund process.
472-
///
473-
/// - Availability:
474-
/// - Not available on watchOS or tvOS.
475-
///
476-
/// - Returns:
477-
/// No return value. The function will either complete successfully or throw an error.
478-
@available(watchOS, unavailable)
479-
@available(watchOS, unavailable)
480-
@available(tvOS, unavailable)
481-
static func beginRefundProcess(for productID: String, in scene: UIWindowScene) async throws {
482-
try await shared.beginRefundProcess(for: productID, in: scene)
483-
}
484-
485392
/// Initiates a purchase for a specified productId with optional parameters.
486393
///
487394
/// - Parameters:
@@ -555,3 +462,113 @@ public extension Mercato {
555462
try await shared.purchase(product: product, options: options, finishAutomatically: finishAutomatically)
556463
}
557464
}
465+
466+
#if os(macOS) || os(iOS)
467+
@available(macOS 12.0, *)
468+
@available(iOS 15.0, visionOS 1.0, *)
469+
@available(watchOS, unavailable)
470+
@available(tvOS, unavailable)
471+
public extension Mercato {
472+
/// Initiates the refund process for a specified product within a given `UIWindowScene`.
473+
///
474+
/// This function attempts to begin a refund request for a transaction associated with the given product ID.
475+
/// It handles different refund request outcomes, including user cancellation, success, and unknown errors.
476+
/// The function is asynchronous and can throw errors if verification or the refund process fails.
477+
///
478+
/// - Parameters:
479+
/// - productID: The identifier of the product for which the refund process is being requested.
480+
/// - scene: The `UIWindowScene` within which the refund request will be displayed to the user.
481+
///
482+
/// - Throws:
483+
/// - `MercatoError.failedVerification`: If the transaction cannot be verified.
484+
/// - `MercatoError.canceledByUser`: If the user cancels the refund request.
485+
/// - `MercatoError.unknown(error:)`: If an unknown error occurs during the refund process.
486+
/// - `MercatoError.refund(error:)`: If a `Transaction.RefundRequestError` occurs during the refund process.
487+
/// - `MercatoError.storeKit(error:)`: If a `StoreKitError` occurs during the refund process.
488+
///
489+
/// - Availability:
490+
/// - Not available on watchOS or tvOS.
491+
///
492+
/// - Returns:
493+
/// No return value. The function will either complete successfully or throw an error.
494+
@available(macOS 12.0, *)
495+
@available(iOS 15.0, visionOS 1.0, *)
496+
@available(watchOS, unavailable)
497+
@available(tvOS, unavailable)
498+
func beginRefundProcess(for productID: String, in displayContext: DisplayContext) async throws {
499+
guard case .verified(let transaction) = await Transaction.latest(for: productID) else { throw MercatoError.failedVerification }
500+
501+
do {
502+
let status = try await transaction.beginRefundRequest(in: displayContext)
503+
504+
switch status {
505+
case .userCancelled:
506+
throw MercatoError.canceledByUser
507+
case .success:
508+
break
509+
@unknown default:
510+
throw MercatoError.unknown(error: nil)
511+
}
512+
} catch let error as Transaction.RefundRequestError {
513+
throw MercatoError.refund(error: error)
514+
} catch let error as StoreKitError {
515+
throw MercatoError.storeKit(error: error)
516+
} catch {
517+
throw MercatoError.unknown(error: error)
518+
}
519+
}
520+
521+
@available(macOS 12.0, *)
522+
@available(iOS 15.0, visionOS 1.0, *)
523+
@available(watchOS, unavailable)
524+
@available(tvOS, unavailable)
525+
static func beginRefundProcess(for productID: String, in displayContext: DisplayContext) async throws {
526+
try await shared.beginRefundProcess(for: productID, in: displayContext)
527+
}
528+
}
529+
#endif
530+
531+
// MARK: Manage Subscription
532+
533+
#if os(iOS)
534+
@available(iOS 15.0, visionOS 1.0, *)
535+
@available(macOS, unavailable)
536+
@available(watchOS, unavailable)
537+
@available(tvOS, unavailable)
538+
public extension Mercato {
539+
/// Displays the subscription management interface for the user within the specified `UIWindowScene`.
540+
///
541+
/// This function presents the App Store's manage subscriptions screen, allowing the user to view and manage their active subscriptions.
542+
/// It is asynchronous and throws errors if there are issues invoking the subscription management screen.
543+
///
544+
/// - Parameters:
545+
/// - scene: The `UIWindowScene` within which the subscription management interface will be presented.
546+
///
547+
/// - Throws:
548+
/// - An error if the subscription management interface could not be presented.
549+
///
550+
/// - Availability:
551+
/// - Available on iOS 15.0 and later.
552+
/// - Not available on macOS, watchOS, or tvOS.
553+
///
554+
/// - Returns:
555+
/// No return value. The function will either complete successfully or throw an error.
556+
@available(iOS 15.0, visionOS 1.0, *)
557+
@available(macOS, unavailable)
558+
@available(watchOS, unavailable)
559+
@available(tvOS, unavailable)
560+
@MainActor
561+
static func showManageSubscriptions(in scene: UIWindowScene) async throws {
562+
try await AppStore.showManageSubscriptions(in: scene)
563+
}
564+
565+
@available(iOS 17.0, visionOS 1.0, *)
566+
@available(macOS, unavailable)
567+
@available(watchOS, unavailable)
568+
@available(tvOS, unavailable)
569+
@MainActor
570+
static func showManageSubscriptions(in scene: UIWindowScene, subscriptionGroupID: String) async throws {
571+
try await AppStore.showManageSubscriptions(in: scene, subscriptionGroupID: subscriptionGroupID)
572+
}
573+
}
574+
#endif

Sources/Mercato/MercatoError.swift

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,11 @@ extension MercatoError: LocalizedError {
3030
public var errorDescription: String? {
3131
switch self {
3232
case .storeKit(error: let error):
33-
if #available(iOS 15.4, *) {
34-
return error.errorDescription
35-
} else {
36-
return error.localizedDescription
37-
}
33+
return error.errorDescription
3834
case .purchase(error: let error):
39-
if #available(iOS 15.4, *) {
40-
return error.errorDescription
41-
} else {
42-
return error.localizedDescription
43-
}
35+
return error.errorDescription
4436
case .refund(error: let error):
45-
if #available(iOS 15.4, *) {
46-
return error.errorDescription
47-
} else {
48-
return error.localizedDescription
49-
}
37+
return error.errorDescription
5038
case .canceledByUser:
5139
return "This error happens when user cancels purchase flow"
5240
case .purchaseIsPending:
@@ -58,3 +46,4 @@ extension MercatoError: LocalizedError {
5846
}
5947
}
6048
}
49+

0 commit comments

Comments
 (0)