passepartout-apple/Passepartout/App/Reusable/Reviewer.swift

98 lines
3.1 KiB
Swift
Raw Normal View History

2022-04-12 13:09:14 +00:00
//
// Reviewer.swift
// Passepartout
//
// Created by Davide De Rosa on 9/9/19.
2023-03-17 15:56:19 +00:00
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
2022-04-12 13:09:14 +00:00
//
// https://github.com/passepartoutvpn
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//
import StoreKit
import UIKit
2022-04-12 13:09:14 +00:00
2023-05-24 16:19:47 +00:00
public final class Reviewer: ObservableObject {
2022-04-12 13:09:14 +00:00
private struct Keys {
static let eventCount = "Reviewer.EventCount"
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
static let lastVersion = "Reviewer.LastVersion"
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
private let defaults: UserDefaults
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
public var eventCountBeforeRating: Int = .max
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
public init() {
defaults = .standard
}
@discardableResult
public func reportEvent() -> Bool {
2022-09-04 18:09:31 +00:00
reportEvents(1)
2022-04-12 13:09:14 +00:00
}
@discardableResult
public func reportEvents(_ eventCount: Int, appStoreId: String? = nil) -> Bool {
guard let currentVersionString = Bundle.main.infoDictionary?["CFBundleVersion"] as? String, let currentVersion = Int(currentVersionString) else {
return false
}
let lastVersion = defaults.integer(forKey: Keys.lastVersion)
if lastVersion > 0 {
print("Reviewer: App last reviewed for version \(lastVersion)")
} else {
print("Reviewer: App was never reviewed")
}
guard currentVersion != lastVersion else {
print("Reviewer: App already reviewed for version \(currentVersion)")
return false
}
var count = defaults.integer(forKey: Keys.eventCount)
count += eventCount
defaults.set(count, forKey: Keys.eventCount)
print("Reviewer: Event reported for version \(currentVersion) (count: \(count), prompt: \(eventCountBeforeRating))")
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
guard count >= eventCountBeforeRating else {
return false
}
print("Reviewer: Prompting for review...")
defaults.removeObject(forKey: Keys.eventCount)
defaults.set(currentVersion, forKey: Keys.lastVersion)
2023-03-17 20:55:47 +00:00
2023-12-16 19:58:54 +00:00
#if !os(tvOS)
2022-04-12 13:09:14 +00:00
requestReview()
2023-12-16 19:58:54 +00:00
#endif
2022-04-12 13:09:14 +00:00
return true
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
// may or may not appear
2023-12-16 19:58:54 +00:00
#if !os(tvOS)
2022-04-12 13:09:14 +00:00
private func requestReview() {
guard let scene = UIApplication.shared.connectedScenes.first(where: { $0 is UIWindowScene }) as? UIWindowScene else {
2022-04-12 13:09:14 +00:00
return
}
SKStoreReviewController.requestReview(in: scene)
}
2023-12-16 19:58:54 +00:00
#endif
2022-04-12 13:09:14 +00:00
public static func urlForReview(withAppId appId: String) -> URL {
2022-09-04 18:09:31 +00:00
URL(string: "https://apps.apple.com/app/id\(appId)?action=write-review")!
2022-04-12 13:09:14 +00:00
}
}