@ -34,7 +34,6 @@ class VersionUpdateHandler: NSObject {
private var checkPeriod : Double = 0.0
private var remindPeriod : Double = 1.0
private var verboseLogging : Bool = true
private var updateURL : URL !
private var checkingForNewVersion : Bool = false
private var remoteVersionsDict : [ String : Any ] = [ : ]
private var downloadError : Error ?
@ -45,7 +44,7 @@ class VersionUpdateHandler: NSObject {
private var localRepeater : Repeater ?
private var showOnFirstLaunch : Bool = false
public var previewMode : Bool = tru e
public var previewMode : Bool = fals e
private var versionDetails : String ?
override init ( ) {
@ -79,7 +78,6 @@ class VersionUpdateHandler: NSObject {
super . init ( )
setAppStoreID ( 0 )
applicationLaunched ( )
}
@ -112,25 +110,8 @@ class VersionUpdateHandler: NSObject {
}
private func updatedURL ( ) -> URL {
if let url = updateURL , url . absoluteString . isEmpty = = false {
return updateURL
}
if let appStoreId = appStoreID ( ) {
Logger . info ( " No App Store ID was found for Clocker " )
return URL ( string : " macappstore://itunes.apple.com/app/idid \( appStoreId ) " ) !
}
// L a s t r e s o r t
return URL ( string : " macappstore://itunes.apple.com/app/id \( VersionUpdateHandler . kVersionMacAppStoreAppID ) " ) !
}
private func appStoreID ( ) -> Int ? {
return UserDefaults . standard . integer ( forKey : VersionUpdateHandler . kMacAppStoreIDKey )
}
@objc func setAppStoreID ( _ appStoreID : Int ) {
UserDefaults . standard . set ( appStoreID , forKey : VersionUpdateHandler . kMacAppStoreIDKey )
return URL ( string : " macappstore://itunes.apple.com/us/app/clocker/id1056643111 " ) !
}
@objc private func setLastChecked ( _ date : Date ) {
@ -241,19 +222,12 @@ class VersionUpdateHandler: NSObject {
var versions : [ String : String ] ? = nil
var itunesServiceURL = " http://itunes.apple.com/ \( self . appStoreCountry ? ? " us " ) /lookup "
if let appStoreID = appStoreID ( ) , appStoreID != 0 {
Logger . info ( " --- App Store ID is \( appStoreID ) " )
itunesServiceURL = itunesServiceURL . appendingFormat ( " ?id=%@ " , appStoreID )
} else {
itunesServiceURL = itunesServiceURL . appendingFormat ( " ?bundleId=%@ " , self . applicationBundleID )
}
if ( verboseLogging ) {
Logger . info ( " iVersion is checking \( itunesServiceURL ) for a new app version... " )
}
dataTask = NetworkManager . task ( with : itunesServiceURL ) { [ weak self ] response , error in
guard let self , let data = response else { return }
@ -283,18 +257,6 @@ class VersionUpdateHandler: NSObject {
versions = [ version : ( releaseNotes as ? String ) ? ? " " ]
}
// G e t a p p I D
let appStoreIdentifier = appStoreID ( )
if ( appStoreIdentifier = = nil || appStoreIdentifier = = 0 ) {
let appStoreIDString = firstResult [ " trackId " ]
performSelector ( onMainThread : #selector ( setAppStoreID ( _ : ) ) ,
with : appStoreIDString ,
waitUntilDone : true )
if ( verboseLogging ) {
Logger . info ( " iVersion found the app on iTunes. The App Store ID is \( appStoreIDString ? ? " " ) " )
}
}
newerVersionAvailable = latestVersion ? . compareVersion ( self . applicationVersion ) = = . orderedDescending
if ( verboseLogging ) {
if ( newerVersionAvailable ) {
@ -305,7 +267,7 @@ class VersionUpdateHandler: NSObject {
}
} else {
if ( verboseLogging ) {
Logger . info ( " iVersion found that the application bundle ID \( self . applicationBundleID ) does not match the bundle ID of the app found on iTunes \( bundleID ) with the specified App Store ID \( self . appStoreID ( ) ? ? 0 ) " )
Logger . info ( " iVersion found that the application bundle ID \( self . applicationBundleID ) does not match the bundle ID of the app found on iTunes \( bundleID ) with the specified App Store ID " )
}
}
} else {
@ -330,6 +292,7 @@ class VersionUpdateHandler: NSObject {
@objc private func setRemoteVersionsDict ( _ dict : [ String : Any ] ? ) {
if let unwrappedDict = dict {
Logger . info ( " Setting Remote Versions Dict to \( unwrappedDict ) " )
remoteVersionsDict = unwrappedDict
}
}
@ -473,36 +436,13 @@ class VersionUpdateHandler: NSObject {
}
private func showAppPageInAppStore ( ) {
if ( updateURL = = nil && appStoreID ( ) = = nil ) {
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 ( ) {
// C h e c k i f A p p S t o r e i s r u n n i n g
for app in NSWorkspace . shared . runningApplications {
if ( app . bundleIdentifier = = Self . kVersionMacAppStoreBundleID ) {
// O p e n A p p P a g e
Logger . info ( " About to open App Store with our app " )
perform ( #selector ( NSWorkspace . shared . open ( _ : ) ) , with : self . updateURL , afterDelay : 5.0 )
return
}
}
// T r y A g a i n
openAppPageWhenAppStoreLaunched ( )
}
@objc private func downloadVersionsData ( ) {
if onlyPromptIfMainWindowIsAvailable , NSApplication . shared . mainWindow = = nil {
@ -528,11 +468,9 @@ class VersionUpdateHandler: NSObject {
} else {
Logger . info ( " Version Update Check because an unknown error occurred " )
}
}
Logger . info ( " Returning early because remoteVersionsDict is empty " )
remoteRepeater = nil
return
}
}
let details = versionDetails ( since : applicationVersion , in : remoteVersionsDict )
let mostRecentVersion = mostRecentVersionInDict ( remoteVersionsDict )