You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.4 KiB
76 lines
2.4 KiB
6 years ago
|
// Copyright © 2015 Abhishek Banthia
|
||
|
|
||
|
import Cocoa
|
||
4 years ago
|
import StoreKit
|
||
6 years ago
|
|
||
4 years ago
|
final class ReviewController {
|
||
6 years ago
|
private static var storage = UserDefaults.standard
|
||
6 years ago
|
private static let version: String = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? "N/A"
|
||
6 years ago
|
private static var debugging = false
|
||
6 years ago
|
|
||
6 years ago
|
private enum Keys {
|
||
|
static let lastPrompt = "last-prompt"
|
||
|
static let lastVersion = "last-version"
|
||
|
static let install = "install"
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
class func applicationDidLaunch(_ defaults: UserDefaults = UserDefaults.standard) {
|
||
|
if ProcessInfo.processInfo.arguments.contains(CLUITestingLaunchArgument) {
|
||
|
debugging = true
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
storage = defaults
|
||
6 years ago
|
|
||
6 years ago
|
guard defaults.object(forKey: Keys.install) == nil else { return }
|
||
|
defaults.set(Date(), forKey: Keys.install)
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
class func setPreviewMode(_ value: Bool) {
|
||
|
debugging = value
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
class func prompted() {
|
||
|
storage.set(Date(), forKey: Keys.lastPrompt)
|
||
|
storage.set(version, forKey: Keys.lastVersion)
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
class func canPrompt() -> Bool {
|
||
|
guard debugging == false else { return true }
|
||
6 years ago
|
|
||
6 years ago
|
let day: TimeInterval = -1 * 60 * 60 * 24
|
||
|
let minInstall: TimeInterval = day * 7
|
||
6 years ago
|
|
||
6 years ago
|
// Check if the app has been installed for atleast 7 days
|
||
|
guard let install = storage.object(forKey: Keys.install) as? Date,
|
||
|
install.timeIntervalSinceNow < minInstall
|
||
6 years ago
|
else { return false }
|
||
6 years ago
|
|
||
6 years ago
|
// If we have never been prompted before, go ahead and prompt
|
||
|
guard let lastPrompt = storage.object(forKey: Keys.lastPrompt) as? Date,
|
||
|
let lastVersion = storage.object(forKey: Keys.lastVersion) as? String
|
||
6 years ago
|
else { return true }
|
||
6 years ago
|
|
||
6 years ago
|
// Minimum interval between two versions should be 45
|
||
6 years ago
|
let minInterval: TimeInterval = day * 90
|
||
6 years ago
|
|
||
|
// never prompt w/in the same version
|
||
|
return lastVersion != version
|
||
|
// limit all types of prompts to at least 1mo intervals
|
||
|
&& lastPrompt.timeIntervalSinceNow < minInterval
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
class func prompt() {
|
||
|
guard let ratingsURL = URL(string: AboutUsConstants.AppStoreLink) else {
|
||
|
return
|
||
|
}
|
||
4 years ago
|
|
||
|
if #available(OSX 10.14, *) {
|
||
|
SKStoreReviewController.requestReview()
|
||
|
} else {
|
||
|
NSWorkspace.shared.open(ratingsURL)
|
||
|
}
|
||
|
|
||
6 years ago
|
prompted()
|
||
|
}
|
||
|
}
|