Skip to content

Commit

Permalink
Support strict concurrency checking
Browse files Browse the repository at this point in the history
  • Loading branch information
takecian committed Jul 3, 2024
1 parent 45f83b3 commit 44163ae
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 35 deletions.
8 changes: 6 additions & 2 deletions SwiftRater.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,8 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
SWIFT_STRICT_CONCURRENCY = complete;
SWIFT_VERSION = 6.0;
};
name = Debug;
};
Expand All @@ -503,7 +504,8 @@
PRODUCT_BUNDLE_IDENTIFIER = com.takecian.SwiftRater;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 5.0;
SWIFT_STRICT_CONCURRENCY = complete;
SWIFT_VERSION = 6.0;
};
name = Release;
};
Expand All @@ -517,6 +519,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.takecian.SwiftRaterTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_STRICT_CONCURRENCY = complete;
SWIFT_VERSION = 5.0;
};
name = Debug;
Expand All @@ -531,6 +534,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.takecian.SwiftRaterTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_STRICT_CONCURRENCY = complete;
SWIFT_VERSION = 5.0;
};
name = Release;
Expand Down
223 changes: 194 additions & 29 deletions SwiftRater/SwiftRater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ import StoreKit
case any
}

@objc public class SwiftRater: NSObject {

@MainActor
@objc public class SwiftRater: NSObject, @unchecked Sendable {

private static let staticLock = NSLock()
private let lock = NSLock()

enum ButtonIndex: Int {
case cancel = 0
case rate = 1
Expand All @@ -22,82 +26,241 @@ import StoreKit

@objc public static var daysUntilPrompt: Int {
get {
UsageDataManager.shared.daysUntilPrompt
staticLock.lock()
defer {
staticLock.unlock()
}
return UsageDataManager.shared.daysUntilPrompt
}
set {
staticLock.lock()
defer {
staticLock.unlock()
}
UsageDataManager.shared.daysUntilPrompt = newValue
}
}
@objc public static var usesUntilPrompt: Int {
get {
UsageDataManager.shared.usesUntilPrompt
staticLock.lock()
defer {
staticLock.unlock()
}
return UsageDataManager.shared.usesUntilPrompt
}
set {
staticLock.lock()
defer {
staticLock.unlock()
}
UsageDataManager.shared.usesUntilPrompt = newValue
}
}
@objc public static var significantUsesUntilPrompt: Int {
get {
UsageDataManager.shared.significantUsesUntilPrompt
get {
staticLock.lock()
defer {
staticLock.unlock()
}
return UsageDataManager.shared.significantUsesUntilPrompt
}
set {
staticLock.lock()
defer {
staticLock.unlock()
}
UsageDataManager.shared.significantUsesUntilPrompt = newValue
}
}

@objc public static var daysBeforeReminding: Int {
get {
UsageDataManager.shared.daysBeforeReminding
staticLock.lock()
defer {
staticLock.unlock()
}
return UsageDataManager.shared.daysBeforeReminding
}
set {
staticLock.lock()
defer {
staticLock.unlock()
}
UsageDataManager.shared.daysBeforeReminding = newValue
}
}
@objc public static var debugMode: Bool {
get {
UsageDataManager.shared.debugMode
staticLock.lock()
defer {
staticLock.unlock()
}
return UsageDataManager.shared.debugMode
}
set {
staticLock.lock()
defer {
staticLock.unlock()
}
UsageDataManager.shared.debugMode = newValue
}
}
@objc public static var conditionsMetMode: SwiftRaterConditionsMetMode {
get {
UsageDataManager.shared.conditionsMetMode
staticLock.lock()
defer {
staticLock.unlock()
}
return UsageDataManager.shared.conditionsMetMode
}
set {
staticLock.lock()
defer {
staticLock.unlock()
}
UsageDataManager.shared.conditionsMetMode = newValue
}
}

@objc public static var useStoreKitIfAvailable: Bool = true

@objc public static var showLaterButton: Bool = true

@objc public static var countryCode: String?

@objc public static var alertTitle: String?
@objc public static var alertMessage: String?
@objc public static var alertCancelTitle: String?
@objc public static var alertRateTitle: String?
@objc public static var alertRateLaterTitle: String?
@objc public static var appName: String?

@objc public static var showLog: Bool = false
@objc public static var resetWhenAppUpdated: Bool = true

@objc public static var shared = SwiftRater()
nonisolated(unsafe) private static var _useStoreKitIfAvailable = true
@objc public static var useStoreKitIfAvailable: Bool {
get {
_useStoreKitIfAvailable
}
set {
_useStoreKitIfAvailable = newValue
}
}

nonisolated(unsafe) private static var _showLaterButton = true
@objc public static var showLaterButton: Bool {
get {
_showLaterButton
}
set {
_showLaterButton = newValue
}
}

nonisolated(unsafe) private static var _countryCode: String?
@objc public static var countryCode: String? {
get {
_countryCode
}
set {
_countryCode = newValue
}
}

nonisolated(unsafe) private static var _alertTitle: String?
@objc public static var alertTitle: String? {
get {
_alertTitle
}
set {
_alertTitle = newValue
}
}

nonisolated(unsafe) private static var _alertMessage: String?
@objc public static var alertMessage: String? {
get {
_alertMessage
}
set {
_alertMessage = newValue
}
}

nonisolated(unsafe) private static var _alertCancelTitle: String?
@objc public static var alertCancelTitle: String? {
get {
_alertCancelTitle
}
set {
_alertCancelTitle = newValue
}
}

nonisolated(unsafe) private static var _alertRateTitle: String?
@objc public static var alertRateTitle: String? {
get {
_alertRateTitle
}
set {
_alertRateTitle = newValue
}
}

nonisolated(unsafe) private static var _alertRateLaterTitle: String?
@objc public static var alertRateLaterTitle: String? {
get {
_alertRateLaterTitle
}
set {
_alertRateLaterTitle = newValue
}
}

nonisolated(unsafe) private static var _appName: String?
@objc public static var appName: String? {
get {
_appName
}
set {
_countryCode = newValue
}
}

nonisolated(unsafe) private static var _showLog: Bool = false
@objc public static var showLog: Bool {
get {
_showLog
}
set {
_showLog = newValue
}
}

nonisolated(unsafe) private static var _resetWhenAppUpdated: Bool = false
@objc public static var resetWhenAppUpdated: Bool {
get {
_resetWhenAppUpdated
}
set {
_resetWhenAppUpdated = newValue
}
}

@objc public static let shared = SwiftRater()

@objc public static var isRateDone: Bool {

UsageDataManager.shared.isRateDone
}

@objc public static var isMetConditions : Bool {
UsageDataManager.shared.ratingConditionsHaveBeenMet
}

@objc public static var appID: String?

nonisolated(unsafe) private static var _appID: String?
@objc public static var appID: String? {
get {
staticLock.lock()
defer {
staticLock.unlock()
}
return _appID
}
set {
staticLock.lock()
defer {
staticLock.unlock()
}
_appID = newValue
}
}

private static var appVersion: String {
get {
Bundle.main.infoDictionary!["CFBundleShortVersionString"] as? String ?? "0.0.0"
Expand Down Expand Up @@ -209,7 +372,9 @@ import StoreKit
let url = try iTunesURLFromString()
let request = URLRequest(url: url, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 30)
URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
self.processResults(withData: data, response: response, error: error)
Task { @MainActor in
self.processResults(withData: data, response: response, error: error)
}
}).resume()
} catch let error {
postError(.malformedURL, underlyingError: error)
Expand Down
11 changes: 7 additions & 4 deletions SwiftRater/UsageDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import AppKit

let SwiftRaterInvalid = -1

class UsageDataManager {
class UsageDataManager: @unchecked Sendable {

var daysUntilPrompt: Int = SwiftRaterInvalid
var usesUntilPrompt: Int = SwiftRaterInvalid
Expand All @@ -24,7 +24,7 @@ class UsageDataManager {
static private let keySwiftRaterTrackingVersion = "keySwiftRaterTrackingVersion"
static private let keySwiftRaterReminderRequestDate = "keySwiftRaterReminderRequestDate"

static var shared = UsageDataManager()
static let shared = UsageDataManager()

let userDefaults = UserDefaults.standard

Expand Down Expand Up @@ -186,8 +186,11 @@ class UsageDataManager {
}

private func printMessage(message: String) {
if SwiftRater.showLog {
print("[SwiftRater] \(message)")
Task {
let showLog = await SwiftRater.showLog
if showLog {
print("[SwiftRater] \(message)")
}
}
}
}

0 comments on commit 44163ae

Please sign in to comment.