Browse Source

Update VersionUpdateHandler.swift

master
Abhishek Banthia 9 months ago
parent
commit
c27007fd4b
  1. 135
      Clocker/Overall App/VersionUpdateHandler.swift

135
Clocker/Overall App/VersionUpdateHandler.swift

@ -19,6 +19,8 @@ class VersionUpdateHandler: NSObject {
static let kMacAppStoreIDKey = "VersionCheckAppStoreIDKey" static let kMacAppStoreIDKey = "VersionCheckAppStoreIDKey"
static let kVersionLastCheckedKey = "VersionLastCheckedKey" static let kVersionLastCheckedKey = "VersionLastCheckedKey"
static let kVersionLastRemindedKey = "VersionLastRemindedKey" static let kVersionLastRemindedKey = "VersionLastRemindedKey"
static let kVersionMacAppStoreBundleID = "com.apple.AppStore";
static let kVersionMacAppStoreAppID = 1056643111
static let sharedInstance = VersionUpdateHandler() static let sharedInstance = VersionUpdateHandler()
@ -39,9 +41,11 @@ class VersionUpdateHandler: NSObject {
private var dataTask: URLSessionDataTask? = .none private var dataTask: URLSessionDataTask? = .none
private var visibleLocalAlert: NSAlert? private var visibleLocalAlert: NSAlert?
private var visibleRemoteAlert: NSAlert? private var visibleRemoteAlert: NSAlert?
private var remoteRepeater: Repeater?
private var localRepeater: Repeater?
private var showOnFirstLaunch: Bool = false private var showOnFirstLaunch: Bool = false
public var previewMode: Bool = false public var previewMode: Bool = true
private var versionDetails: String? private var versionDetails: String?
override init() { override init() {
@ -75,6 +79,7 @@ class VersionUpdateHandler: NSObject {
super.init() super.init()
setAppStoreID(0)
applicationLaunched() applicationLaunched()
} }
@ -107,16 +112,17 @@ class VersionUpdateHandler: NSObject {
} }
private func updatedURL() -> URL { private func updatedURL() -> URL {
if updateURL.absoluteString.isEmpty == false { if let url = updateURL, url.absoluteString.isEmpty == false {
return updateURL return updateURL
} }
guard let appStoreId = appStoreID() else { if let appStoreId = appStoreID() {
Logger.info("No App Store ID was found for Clocker") Logger.info("No App Store ID was found for Clocker")
return URL(string: "")! return URL(string: "macappstore://itunes.apple.com/app/idid\(appStoreId)")!
} }
return URL(string: "macappstore://itunes.apple.com/app/id\(appStoreId)")! // Last resort
return URL(string: "macappstore://itunes.apple.com/app/id\(VersionUpdateHandler.kVersionMacAppStoreAppID)")!
} }
private func appStoreID() -> Int? { private func appStoreID() -> Int? {
@ -261,7 +267,6 @@ class VersionUpdateHandler: NSObject {
let results = unwrapped["results"] as? Array<Any>, let results = unwrapped["results"] as? Array<Any>,
let firstResult = results.first as? [String: Any], let firstResult = results.first as? [String: Any],
let bundleID = firstResult["bundleId"] as? String { let bundleID = firstResult["bundleId"] as? String {
if (bundleID == self.applicationBundleID) { if (bundleID == self.applicationBundleID) {
guard let minimumSupportedOSVersion = firstResult["minimumOsVersion"] as? String else { return } guard let minimumSupportedOSVersion = firstResult["minimumOsVersion"] as? String else { return }
let version = ProcessInfo.processInfo.operatingSystemVersion let version = ProcessInfo.processInfo.operatingSystemVersion
@ -279,7 +284,8 @@ class VersionUpdateHandler: NSObject {
} }
// Get app ID // Get app ID
if (appStoreID() == nil) { let appStoreIdentifier = appStoreID()
if (appStoreIdentifier == nil || appStoreIdentifier == 0) {
let appStoreIDString = firstResult["trackId"] let appStoreIDString = firstResult["trackId"]
performSelector(onMainThread: #selector(setAppStoreID(_:)), performSelector(onMainThread: #selector(setAppStoreID(_:)),
with: appStoreIDString, with: appStoreIDString,
@ -307,10 +313,16 @@ class VersionUpdateHandler: NSObject {
} }
//TODO: Set download error //TODO: Set download error
Logger.info("Versions downloaded \(versions ?? [:])")
performSelector(onMainThread: #selector(setRemoteVersionsDict(_:)), with: versions, waitUntilDone: true) performSelector(onMainThread: #selector(setRemoteVersionsDict(_:)),
performSelector(onMainThread: #selector(setLastChecked(_:)), with: Date(), waitUntilDone: true) with: versions,
performSelector(onMainThread: #selector(Self.downloadVersionsData), with: nil, waitUntilDone: true) waitUntilDone: true)
performSelector(onMainThread: #selector(setLastChecked(_:)),
with: Date(),
waitUntilDone: true)
performSelector(onMainThread: #selector(Self.downloadVersionsData),
with: nil,
waitUntilDone: true)
} }
dataTask?.resume() dataTask?.resume()
@ -365,6 +377,7 @@ class VersionUpdateHandler: NSObject {
_ defaultButton: String, _ defaultButton: String,
_ ignoreButton: String?, _ ignoreButton: String?,
_ remindButton: String?) -> NSAlert { _ remindButton: String?) -> NSAlert {
Logger.info("Showing alert")
let floatMax = CGFloat.greatestFiniteMagnitude let floatMax = CGFloat.greatestFiniteMagnitude
let alert = NSAlert() let alert = NSAlert()
@ -431,17 +444,70 @@ class VersionUpdateHandler: NSObject {
return remindButtonLabel().isEmpty == false && updatePriority < VersionUpdateHandlerPriority.high return remindButtonLabel().isEmpty == false && updatePriority < VersionUpdateHandlerPriority.high
} }
private func didDismissAlert(_: NSAlert, _: Int) { private func didDismissAlert(_ alert: NSAlert, _ buttonIndex: Int) {
// Get Button Indice // Get Button Indice
let downloadButtonIndex = 0
let ignoreButtonIndex = showIgnoreButton() ? 1 : 0
let remindButtonIndex = showRemindButtton() ? ignoreButtonIndex + 1 : 0
let latestVersion = mostRecentVersionInDict(self.remoteVersionsDict)
if (self.visibleLocalAlert == alert) {
setViewedVersionDetails(true)
visibleLocalAlert = nil
return
}
if (buttonIndex == downloadButtonIndex) {
setLastReminded(nil)
showAppPageInAppStore()
} else if (buttonIndex == ignoreButtonIndex) {
// ignore this version
setIgnoredVersion(latestVersion)
setLastReminded(nil)
} else if (buttonIndex == remindButtonIndex) {
setLastReminded(Date())
}
self.visibleRemoteAlert = nil
} }
@objc private func downloadVersionsData() { private func showAppPageInAppStore() {
if onlyPromptIfMainWindowIsAvailable { if (updateURL == nil && appStoreID() == nil) {
guard NSApplication.shared.mainWindow != nil else { if (self.verboseLogging) {
Logger.info("iVersion was unable to open App Store because app store ID isn't set")
}
}
if (self.verboseLogging) {
Logger.info("iVersion will open App Store using the following URL \(updatedURL())")
}
NSWorkspace.shared.open(updatedURL())
if (updateURL == nil) {
openAppPageWhenAppStoreLaunched()
}
}
private func openAppPageWhenAppStoreLaunched() {
// Check if App Store is running
for app in NSWorkspace.shared.runningApplications {
if (app.bundleIdentifier == Self.kVersionMacAppStoreBundleID) {
// Open App Page
Logger.info("About to open App Store with our app")
perform(#selector(NSWorkspace.shared.open(_:)), with: self.updateURL, afterDelay: 5.0)
return return
} }
}
_ = Repeater(interval: .seconds(0.5), mode: .infinite) { _ in // Try Again
openAppPageWhenAppStoreLaunched()
}
@objc private func downloadVersionsData() {
if onlyPromptIfMainWindowIsAvailable, NSApplication.shared.mainWindow == nil {
Logger.info("Main window not available in downloadVersionsData")
remoteRepeater = Repeater(interval: .seconds(0.5), mode: .once) { _ in
OperationQueue.main.addOperation { [weak self] in OperationQueue.main.addOperation { [weak self] in
guard let self = self else { guard let self = self else {
return return
@ -449,6 +515,8 @@ class VersionUpdateHandler: NSObject {
self.downloadVersionsData() self.downloadVersionsData()
} }
} }
remoteRepeater?.start()
return
} }
if checkingForNewVersion { if checkingForNewVersion {
@ -461,6 +529,8 @@ class VersionUpdateHandler: NSObject {
Logger.info("Version Update Check because an unknown error occurred") Logger.info("Version Update Check because an unknown error occurred")
} }
} }
Logger.info("Returning early because remoteVersionsDict is empty")
remoteRepeater = nil
return return
} }
@ -468,20 +538,29 @@ class VersionUpdateHandler: NSObject {
let mostRecentVersion = mostRecentVersionInDict(remoteVersionsDict) let mostRecentVersion = mostRecentVersionInDict(remoteVersionsDict)
if details != nil { if details != nil {
Logger.info("About to show visible remote alert")
// Check if ignored // Check if ignored
let showDetails = ignoredVersion() == mostRecentVersion || previewMode let showDetails = ignoredVersion() != mostRecentVersion || previewMode
// show details
if showDetails && self.visibleRemoteAlert == nil {
var title = updateAvailableTitle()
title = title.appending(" (\(mostRecentVersion))")
self.visibleRemoteAlert = showAlertWithTitle(title,
details ?? "N/A",
self.downloadButtonLabel(),
showIgnoreButton() ? self.ignoreButtonLabel() : nil,
showRemindButtton() ? self.remindButtonLabel() : nil)
}
if showDetails {} remoteRepeater = nil
} }
} }
private func checkIfNewVersion() { private func checkIfNewVersion() {
if onlyPromptIfMainWindowIsAvailable { if onlyPromptIfMainWindowIsAvailable, NSApplication.shared.mainWindow == nil {
guard NSApplication.shared.mainWindow != nil else { Logger.info("Main window not available in checkIfNewVersion")
return localRepeater = Repeater(interval: .seconds(0.5), mode: .once) { _ in
}
_ = Repeater(interval: .seconds(0.5), mode: .infinite) { _ in
OperationQueue.main.addOperation { [weak self] in OperationQueue.main.addOperation { [weak self] in
guard let self = self else { guard let self = self else {
return return
@ -489,6 +568,8 @@ class VersionUpdateHandler: NSObject {
self.checkIfNewVersion() self.checkIfNewVersion()
} }
} }
localRepeater?.start()
return
} }
let lastVersionString = lastVersion() let lastVersionString = lastVersion()
@ -498,13 +579,19 @@ class VersionUpdateHandler: NSObject {
setLastReminded(nil) setLastReminded(nil)
if (self.versionDetails != nil && visibleLocalAlert == nil && visibleRemoteAlert == nil) { if (self.versionDetails != nil && visibleLocalAlert == nil && visibleRemoteAlert == nil) {
Logger.info("Visible Local Alert about to be display")
visibleLocalAlert = showAlertWithTitle(inThisVersionTitle(), self.versionDetailsString(), okayButtonLabel(), nil, nil) visibleLocalAlert = showAlertWithTitle(inThisVersionTitle(), self.versionDetailsString(), okayButtonLabel(), nil, nil)
} else {
Logger.info("Skipping to show local alert because version details is \(self.versionDetails ?? "nil")")
} }
} }
} else { } else {
//record this as last viewed release //record this as last viewed release
Logger.info("Set Viewed Version Details")
setViewedVersionDetails(true) setViewedVersionDetails(true)
} }
localRepeater = nil
} }
} }

Loading…
Cancel
Save