Skip to content

Commit

Permalink
Merge pull request #109 from Infomaniak/fix-change-number-opening-bef…
Browse files Browse the repository at this point in the history
…ore-review

feat: Update review manager and refactor shouldRequestReview function
  • Loading branch information
PhilippeWeidmann committed Mar 6, 2024
2 parents 6db13eb + 117552d commit 7647060
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions Sources/InfomaniakCore/Account/ReviewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,39 +52,46 @@ public enum ReviewType: String {
}

public protocol ReviewManageable {
func decreaseOpeningUntilReview()
func shouldRequestReview() -> Bool
func requestReview()
}

public class ReviewManager: ReviewManageable {
let userDefaults: UserDefaults
let openingBeforeReview: Int
let openingBeforeNextReviews: Int

public init(userDefaults: UserDefaults, openingBeforeReview: Int = 50) {
private var userHasAlreadyAnsweredYes: Bool {
return userDefaults.appReview == .readyForReview
}

public init(userDefaults: UserDefaults, openingBeforeFirstReview: Int = 50, openingBeforeNextReviews: Int = 500) {
self.userDefaults = userDefaults
self.openingBeforeReview = openingBeforeReview
self.openingBeforeNextReviews = openingBeforeNextReviews
if userDefaults.object(forKey: userDefaults.key(.openingUntilReview)) == nil {
userDefaults.set(openingBeforeReview, forKey: userDefaults.key(.openingUntilReview))
userDefaults.set(openingBeforeFirstReview, forKey: userDefaults.key(.openingUntilReview))
}
}

public func decreaseOpeningUntilReview() {
userDefaults.openingUntilReview -= 1
}

public func shouldRequestReview() -> Bool {
switch userDefaults.appReview {
case .none, .feedback:
let request = userDefaults.openingUntilReview <= 0
if request {
userDefaults.openingUntilReview = openingBeforeReview
return true
guard userDefaults.openingUntilReview <= 0 else {
return false
}
return false
case .readyForReview:
if userDefaults.openingUntilReview <= 0 {
userDefaults.openingUntilReview = openingBeforeReview

userDefaults.openingUntilReview = openingBeforeNextReviews

if userHasAlreadyAnsweredYes {
// If the user has already answered yes, we will directly present the SKStoreReviewController
requestReview()
return false
} else {
return true
}
return false
}
}

public func requestReview() {
DispatchQueue.main.async {
Expand Down

0 comments on commit 7647060

Please sign in to comment.