Skip to content

Commit

Permalink
changes to fetchAppID
Browse files Browse the repository at this point in the history
  • Loading branch information
Catta1997 committed Jul 17, 2024
1 parent 8b5e3e4 commit d4a7a8e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 39 deletions.
58 changes: 19 additions & 39 deletions PlayCover/Model/PlayApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,53 +33,35 @@ class PlayApp: BaseApp {
}
var sessionDisableKeychain: Bool = false

func fetchAppID(bundleID: String, completion: @escaping (String?) -> Void) {
func fetchAppID(bundleID: String) async -> Int {
let urlString = "https://itunes.apple.com/lookup?bundleId=\(bundleID)"
guard let url = URL(string: urlString) else {
completion(nil)
return
}
let session = URLSession.shared
let task = session.dataTask(with: url) { data, _, error in
guard error == nil, let data = data else {
completion(nil)
return
}
do {
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let results = json["results"] as? [[String: Any]],
let appInfo = results.first,
let trackId = appInfo["trackId"] as? Int {
completion(String(trackId))
} else {
completion(nil)
}
} catch {
completion(nil)
}
let itunes: ITunesResponse? = await getITunesData(urlString)
if let appID = itunes?.results.first?.trackId {
return appID
} else {
return 0
}
task.resume()
}

@MainActor
private func runmacOSWarning() async -> Bool {
let alert = await NSAlert()
let alert = NSAlert()
alert.messageText = NSLocalizedString("alert.error", comment: "")
alert.informativeText = String(
format: NSLocalizedString("macos.version", comment: "")
)
alert.alertStyle = .warning
await alert.addButton(withTitle: NSLocalizedString("alert.start.anyway", comment: ""))
await alert.addButton(withTitle: NSLocalizedString("alert.open.appstore", comment: ""))
await alert.addButton(withTitle: NSLocalizedString("alert.quit", comment: ""))
let result = await alert.runModal()
alert.addButton(withTitle: NSLocalizedString("alert.start.anyway", comment: ""))
alert.addButton(withTitle: NSLocalizedString("alert.open.appstore", comment: ""))
alert.addButton(withTitle: NSLocalizedString("alert.quit", comment: ""))
let result = alert.runModal()
switch result {
case .alertFirstButtonReturn:
return true
case .alertSecondButtonReturn:
self.fetchAppID(bundleID: info.bundleIdentifier) { appID in
if let appID = appID, let appStoreURL = URL(string: "itms-apps://apps.apple.com/app/id\(appID)") {
NSWorkspace.shared.open(appStoreURL)
}
let appID = await self.fetchAppID(bundleID: info.bundleIdentifier)
if let appLink: URL = URL(string: "itms-apps://apps.apple.com/app/id\(appID)") {
NSWorkspace.shared.open(appLink)
}
return false
default:
Expand All @@ -93,12 +75,10 @@ class PlayApp: BaseApp {
if prohibitedToPlay {
await clearAllCache()
throw PlayCoverError.appProhibited
} else if hasMacVersion {
if await !runmacOSWarning() {
await clearAllCache()
isStarting = false
return
}
} else if hasMacVersion, await !runmacOSWarning() {
await clearAllCache()
isStarting = false
return
} else if maliciousProhibited {
await clearAllCache()
deleteApp()
Expand Down
5 changes: 5 additions & 0 deletions PlayCover/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,8 @@

"keycover.alert.title" = "Keychain was not unlocked";
"keycover.alert.content" = "Keychain access has been disabled for the current session";

"macos.version" = "This app has a native MacOS version on AppStre";
"alert.start.anyway" = "Start Anyway";
"alert.quit" = "Quit";
"alert.open.appstore" = "Open AppStore";

0 comments on commit d4a7a8e

Please sign in to comment.