- add option to select an application automatic checks for new version (at start/once per day/once per week/once per month/never)

- adjust select box y position
This commit is contained in:
Serhiy Mytrovtsiy
2020-07-14 17:28:46 +02:00
parent 5d64b5f743
commit 85fb2964fd
7 changed files with 209 additions and 122 deletions

101
Stats/helpers.swift Normal file
View File

@@ -0,0 +1,101 @@
//
// helpers.swift
// Stats
//
// Created by Serhiy Mytrovtsiy on 13/07/2020.
// Using Swift 5.0.
// Running on macOS 10.15.
//
// Copyright © 2020 Serhiy Mytrovtsiy. All rights reserved.
//
import Cocoa
import os.log
import StatsKit
extension AppDelegate {
internal func parseArguments() {
let args = CommandLine.arguments
if args.contains("--reset") {
os_log(.debug, log: log, "Receive --reset argument. Reseting store (UserDefaults)...")
store.reset()
}
if let disableIndex = args.firstIndex(of: "--disable") {
if args.indices.contains(disableIndex+1) {
let disableModules = args[disableIndex+1].split(separator: ",")
disableModules.forEach { (moduleName: Substring) in
if let module = modules.first(where: { $0.config.name.lowercased() == moduleName.lowercased()}) {
module.unmount()
}
}
}
}
if let mountIndex = args.firstIndex(of: "--mount-path") {
if args.indices.contains(mountIndex+1) {
let mountPath = args[mountIndex+1]
asyncShell("/usr/bin/hdiutil detach \(mountPath)")
asyncShell("/bin/rm -rf \(mountPath)")
os_log(.debug, log: log, "DMG was unmounted and mountPath deleted")
}
}
if let dmgIndex = args.firstIndex(of: "--dmg-path") {
if args.indices.contains(dmgIndex+1) {
asyncShell("/bin/rm -rf \(args[dmgIndex+1])")
os_log(.debug, log: log, "DMG was deleted")
}
}
}
internal func parseVersion() {
let key = "version"
let currentVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
if !store.exist(key: key) {
store.reset()
os_log(.debug, log: log, "Previous version not detected. Current version (%s) set", currentVersion)
} else {
let prevVersion = store.string(key: key, defaultValue: "")
if prevVersion == currentVersion {
return
}
if IsNewestVersion(currentVersion: prevVersion, latestVersion: currentVersion) {
let notification = NSUserNotification()
notification.identifier = "updated-from-\(prevVersion)-to-\(currentVersion)"
notification.title = "Successfully updated"
notification.subtitle = "Stats was updated to the v\(currentVersion)"
notification.soundName = NSUserNotificationDefaultSoundName
notification.hasActionButton = false
NSUserNotificationCenter.default.deliver(notification)
}
os_log(.debug, log: log, "Detected previous version %s. Current version (%s) set", prevVersion, currentVersion)
}
store.set(key: key, value: currentVersion)
}
internal func defaultValues() {
if !store.exist(key: "runAtLoginInitialized") {
store.set(key: "runAtLoginInitialized", value: true)
LaunchAtLogin.isEnabled = true
}
if store.exist(key: "dockIcon") {
let dockIconStatus = store.bool(key: "dockIcon", defaultValue: false) ? NSApplication.ActivationPolicy.regular : NSApplication.ActivationPolicy.accessory
NSApp.setActivationPolicy(dockIconStatus)
}
if updateIntervals(rawValue: store.string(key: "update-interval", defaultValue: updateIntervals.atStart.rawValue)) == .atStart {
self.checkForNewVersion(false)
}
}
}