Skip to content

Commit

Permalink
Add more flexibility with requestReviewIfCriteriaMet() & enabledInDeb…
Browse files Browse the repository at this point in the history
…ugBuilds = false
  • Loading branch information
Jeehut committed Jan 1, 2024
1 parent cfec92d commit 6160d53
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ Read the [Introduction Article](https://www.fline.dev/introducing-reviewkit/?ref

That's it – you have configured App Review requests for your app!

Note that ReviewKit is using Apple's `SKStoreReviewController` API internally. That API already encapsulates some logic to make sure not to ask users too often.

### Notes

* ReviewKit is using Apple's `SKStoreReviewController` API internally. That API already encapsulates some logic to make sure not to ask users too often.
* That logic is turned off in `DEBUG` mode, so you might want to set `ReviewKit.enabledInDebugBuilds = false` once you've tested that everything works.
* There's also a function `requestReviewIfCriteriaMet()` that does not record a positive event in case you need that.


## Showcases
Expand Down
30 changes: 21 additions & 9 deletions Sources/ReviewKit/ReviewKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,31 @@ public enum ReviewKit {
/// The minimum criteria to be met to request a review from a user.
public static var criteria: ReviewCriteria = ReviewCriteria(minPositiveEventsWeight: 3, eventsExpireAfterDays: 14)

/// Turns off the review request inside an `#if DEBUG`. On by default (for testing purposes).
public static var enabledInDebugBuilds: Bool = true

/// Records a positive event and requests a review if the criteria are met. Use when a user has completed a workflow and is less likely to be annoyed.
/// - Parameter weight: The weight of the positive event. Defaults to 1.
public static func recordPositiveEventAndRequestReviewIfCriteriaMet(weight: Int = 1) {
self.recordPositiveEvent(weight: weight)
self.requestReviewIfCriteriaMet()
}

/// Records a positive event without requesting a review. Use when the user is in the middle of a workflow to track the event without annoying the user.
/// - Parameter weight: The weight of the positive event. Defaults to 1.
public static func recordPositiveEvent(weight: Int = 1) {
self.positiveEvents.append(PositiveEvent(date: Date(), weight: weight))
self.positiveEvents.removeAll { $0.date < Date().addingTimeInterval(.days(-self.criteria.eventsExpireAfterDays)) }

UserDefaults.standard.set(self.positiveEvents.map(\.rawValue), forKey: "ReviewKit.positiveEvents")
}

/// Requests a review if the criteria are met. Use when a user has completed a workflow and is less likely to be annoyed.
/// - Parameter weight: The weight of the positive event. Defaults to 1.
public static func requestReviewIfCriteriaMet(weight: Int = 1) {
#if DEBUG
guard self.enabledInDebugBuilds else { return }
#endif

let totalPositiveEventsWeight = self.positiveEvents.reduce(into: 0, { $0 += $1.weight })
if totalPositiveEventsWeight >= self.criteria.minPositiveEventsWeight {
Expand All @@ -28,15 +49,6 @@ public enum ReviewKit {
}
}

/// Records a positive event without requesting a review. Use when the user is in the middle of a workflow to track the event without annoying the user.
/// - Parameter weight: The weight of the positive event. Defaults to 1.
public static func recordPositiveEvent(weight: Int = 1) {
self.positiveEvents.append(PositiveEvent(date: Date(), weight: weight))
self.positiveEvents.removeAll { $0.date < Date().addingTimeInterval(.days(-self.criteria.eventsExpireAfterDays)) }

UserDefaults.standard.set(self.positiveEvents.map(\.rawValue), forKey: "ReviewKit.positiveEvents")
}

static var positiveEvents: [PositiveEvent] = UserDefaults.standard
.array(forKey: "ReviewKit.positiveEvents")?
.compactMap { $0 as? String }
Expand Down

0 comments on commit 6160d53

Please sign in to comment.