Skip to content

Commit

Permalink
feat(min-time): Add a minimum time criterion
Browse files Browse the repository at this point in the history
* Add an optional minimum time criterion between the first active positive event and the current time
  • Loading branch information
joeljfischer committed Mar 15, 2024
1 parent 4a1c055 commit 3ffc597
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Read the [Introduction Article](https://www.fline.dev/introducing-reviewkit/?ref
```Swift
import ReviewKit
// ...
ReviewKit.criteria = ReviewCriteria(minPositiveEventsWeight: 5, eventsExpireAfterDays: 30)
ReviewKit.criteria = ReviewCriteria(minPositiveEventsWeight: 5, eventsExpireAfterDays: 30, minimumTimeBeforeRequest: DateComponents(day: 1))
```

3. Determine common workflows in your app and when a user completes one of them, call this:
Expand Down
5 changes: 4 additions & 1 deletion Sources/ReviewKit/ReviewCriteria.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import Foundation
public struct ReviewCriteria {
let minPositiveEventsWeight: Int
let eventsExpireAfterDays: Int
let minimumTimeBeforeRequest: DateComponents

/// - Parameters:
/// - minPositiveEventsWeight: The minimum positive events weight that needs to be surpassed to request a review. With the default event weight, this equals the number of events.
/// - eventsExpireAfterDays: The relevant time interval to consider events within when looking into the past. Events outside this time period expire and get deleted from persistent storage.
public init(minPositiveEventsWeight: Int, eventsExpireAfterDays: Int) {
/// - minimumTimeBeforeRequest: The minimum time period between the first active (un-expired) positive event and the current time to request the review. For example, setting this to 1 day will require that the first un-expired positive event be at least 1 day in the past before requesting the review. This can prevent a user who's trying out the app for the first time from receiving the review request. This is disabled by default. To manually disable it, set some component to 0, e.g. `DateComponents(day: 0)`.
public init(minPositiveEventsWeight: Int, eventsExpireAfterDays: Int, minimumTimeBeforeRequest: DateComponents = DateComponents(day: 0)) {
self.minPositiveEventsWeight = minPositiveEventsWeight
self.eventsExpireAfterDays = eventsExpireAfterDays
self.minimumTimeBeforeRequest = minimumTimeBeforeRequest
}
}
5 changes: 4 additions & 1 deletion Sources/ReviewKit/ReviewKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public enum ReviewKit {
#endif

let totalPositiveEventsWeight = self.positiveEvents.reduce(into: 0, { $0 += $1.weight })
if totalPositiveEventsWeight >= self.criteria.minPositiveEventsWeight {
let firstPositiveEventDate = self.positiveEvents.first?.date ?? .distantFuture
let requiredDateToRequestReview = Calendar.current.date(byAdding: criteria.minimumTimeBeforeRequest, to: firstPositiveEventDate)

if totalPositiveEventsWeight >= self.criteria.minPositiveEventsWeight, let requiredDateToRequestReview, requiredDateToRequestReview < Date() {
#if os(iOS)
if
#available(iOS 14.0, *),
Expand Down

0 comments on commit 3ffc597

Please sign in to comment.