Skip to content

Commit f2833fe

Browse files
committed
Fix ga issue
1 parent 64ff934 commit f2833fe

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

Sources/UpgradeAlert/UpgradeAlert+Variables.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extension UpgradeAlert {
2222
}
2323
// The App Store lookup URL may fail if the app is not available in all countries.
2424
// prefer this
25-
internal static var getRequestURL(countryCode = Locale.current.regionCode ?? "US") -> URL? {
25+
internal static func getRequestURL(countryCode: String = Locale.current.region?.identifier ?? "US") -> URL? {
2626
guard let bundleId = Bundle.identifier else { return nil }
2727
let requestURLStr = "https://itunes.apple.com/lookup?bundleId=\(bundleId)&country=\(countryCode)"
2828
return URL(string: requestURLStr)

Sources/UpgradeAlert/UpgradeAlert.swift

+9-9
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,14 @@ extension UpgradeAlert {
6868
}
6969
}
7070
}
71-
7271
/**
7372
* Network
7473
*/
7574
extension UpgradeAlert {
75+
/**
76+
* fixme: add doc
77+
*/
78+
typealias Completion = (AppInfo?, UAError?) -> Void
7679
/**
7780
* - Description: Retrieves the app information from the App Store using a network request. This function fetches the JSON data from the App Store API and decodes it to `AppInfo` format, handling errors appropriately.
7881
* let url = URL(string: "http://www.")
@@ -82,11 +85,11 @@ extension UpgradeAlert {
8285
* - Note: Discussing this solution: https://stackoverflow.com/questions/6256748/check-if-my-app-has-a-new-version-on-appstore
8386
* - Fixme: ⚠️️ Add timeout interval and local cache pollacy: https://github.com/amebalabs/AppVersion/blob/master/AppVersion/Source/AppStoreVersion.swift
8487
* - Parameter completion: A closure that is called when the app information retrieval is complete. It returns an optional AppInfo object and an optional UAError object. If an error occurs during the retrieval, the UAError object describes the error. If the retrieval is successful, the AppInfo object contains information about the app.
85-
- Fixme: ⚠️ make alias for type
88+
* - Fixme: ⚠️ make alias for type
8689
*/
87-
private static func getAppInfo(completion: ((AppInfo?, UAError?) -> Void)?) { /*urlStr: String, */
90+
private static func getAppInfo(completion: Completion?) { /*urlStr: String, */
8891
// Check if the request URL is valid
89-
guard let url: URL = requestURL else { completion?(nil, UAError.invalideURL); return }
92+
guard let url: URL = getRequestURL() else { completion?(nil, UAError.invalideURL); return }
9093
// Create a data task to fetch app information
9194
let task = URLSession.shared.dataTask(with: url) { data, _, error in
9295
do {
@@ -97,22 +100,19 @@ extension UpgradeAlert {
97100
}
98101
let result = try JSONDecoder().decode(LookupResult.self, from: data)
99102
guard let info: AppInfo = result.results.first else { // Get the first app info from the result
100-
101103
throw NSError(domain: "no app info", code: 0) // If there is no app info, throw an NSError with the description "no app info"
102104
}
103-
104105
// ⚠️️ new
105106
// The App Store metadata may be updated before the app binary is available, causing users to see an update prompt before they can download the update.
106107
// Check the currentVersionReleaseDate from the App Store response and delay prompting users if the update is very recent.
107108
let dateFormatter = ISO8601DateFormatter()
108-
if let releaseDate = dateFormatter.date(from: appInfo.currentVersionReleaseDate) {
109+
if let releaseDate = dateFormatter.date(from: info.currentVersionReleaseDate) {
109110
let daysSinceRelease = Calendar.current.dateComponents([.day], from: releaseDate, to: Date()).day ?? 0
110111
if daysSinceRelease < 1 {
111-
completion(.success(())) // Do not prompt the user yet
112+
completion?(info, nil) // Do not prompt the user yet
112113
return
113114
}
114115
}
115-
116116
completion?(info, nil)
117117
} catch {
118118
// Handle potential errors during data fetching and decoding

Sources/UpgradeAlert/helper/AppInfo.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public struct AppInfo: Decodable {
3131
*/
3232
public let trackViewUrl: String
3333
// ⚠️️ new
34+
// fixme: add doc
3435
public let currentVersionReleaseDate: String
3536
/**
3637
* Initializes a new instance of `AppInfo`.
@@ -40,8 +41,9 @@ public struct AppInfo: Decodable {
4041
* - version: The current version of the application.
4142
* - trackViewUrl: The URL to the application's page on the App Store.
4243
*/
43-
public init(version: String, trackViewUrl: String) {
44+
public init(version: String, trackViewUrl: String, currentVersionReleaseDate: String) {
4445
self.version = version
4546
self.trackViewUrl = trackViewUrl
47+
self.currentVersionReleaseDate = currentVersionReleaseDate
4648
}
4749
}

0 commit comments

Comments
 (0)