Skip to content

Commit

Permalink
Display paywall on 10th save
Browse files Browse the repository at this point in the history
  • Loading branch information
Arclite committed Dec 2, 2024
1 parent 6fd3067 commit 561a83f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
37 changes: 26 additions & 11 deletions Modules/Capabilities/AppRatings/Sources/AppRatingsPrompter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,53 @@

import Defaults
import ErrorHandling
import Logging
import Foundation
import Logging
import PurchaseMarketing
import Purchasing
import StoreKit

public struct AppRatingsPrompter {
public init() {
self.init(logger: TelemetryLogger(), requestMethod: SKStoreReviewController.requestReview(in:))
self.init(logger: TelemetryLogger(), ratingRequestMethod: SKStoreReviewController.requestReview(in:))
}

init(logger: Logger, requestMethod: @escaping ((UIWindowScene) -> Void) = SKStoreReviewController.requestReview(in:)) {
init(
logger: Logger,
ratingRequestMethod: @escaping ((UIWindowScene) -> Void) = SKStoreReviewController.requestReview(in:),
repository: any PurchaseRepository = Purchasing.repository
) {
self.logger = logger
self.requestMethod = requestMethod
self.ratingRequestMethod = ratingRequestMethod
self.repository = repository
}

public func displayRatingsPrompt(in windowScene: UIWindowScene?) {
@MainActor
public func displayRatingsPrompt(in windowScene: UIWindowScene?) async {
guard let windowScene else {
ErrorHandler(logger: logger).log(AppRatingsError.missingWindowScene)
return
}

guard Defaults.numberOfSaves >= Self.minNumberOfSaves
else { return }
if (Defaults.numberOfSaves > 0) && (Defaults.numberOfSaves % Self.ratingNumberOfSavesCadence == 0) {
ratingRequestMethod(windowScene)
logger.log(Event(name: .requestedRating, info: [:]))
} else if Defaults.numberOfSaves == Self.paywallNumberOfSaves, #available(iOS 16.0, *) {
guard let topViewController = windowScene.windows.first?.rootViewController,
await repository.noOnions != .purchased
else { return }

requestMethod(windowScene)
logger.log(Event(name: .requestedRating, info: [:]))
topViewController.present(PurchaseMarketingHostingController(), animated: true)
}
}

// MARK: Boilerplate

private static let minNumberOfSaves = 3
private static let ratingNumberOfSavesCadence = 3
private static let paywallNumberOfSaves = 10
private let logger: Logger
private let requestMethod: (UIWindowScene) -> Void
private let ratingRequestMethod: (UIWindowScene) -> Void
private let repository: any PurchaseRepository
}

private enum AppRatingsError: Error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ class AppViewController: UIViewController, PhotoEditorPresenting, DocumentScanni

@objc func displayAppRatingsPrompt(_ sender: WindowSceneProvider) {
let windowScene = sender.windowScene ?? self.windowScene ?? UIApplication.shared.windowScene
AppRatingsPrompter().displayRatingsPrompt(in: windowScene)
Task {
await AppRatingsPrompter().displayRatingsPrompt(in: windowScene)
}
}

// MARK: Settings View Controller
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ public enum AppRatings {
name: "AppRatings",
dependencies: [
.target(ErrorHandling.target(sdk: .catalyst)),
.target(Purchasing.target),
.target(PurchaseMarketing.target),
]
)

Expand Down

0 comments on commit 561a83f

Please sign in to comment.