Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a minimum time criterion #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
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