aboutsummaryrefslogtreecommitdiff
path: root/DWA140Menu/ViewController.swift
blob: 416fb1c36cf1280fb0a0d20cdd0af3c641f3bcc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
//  ViewController.swift
//  DWA140Menu
//
//  Copyright © 2018 Dylan Bolger. All rights reserved.
//

import Cocoa
import LaunchAtLogin

class ViewController: NSViewController {
    
    @IBOutlet weak var versionLabel: NSTextField!
    @IBOutlet weak var loginButton: NSButton!
    @IBOutlet weak var awesomeSubtitle: NSTextField!
    @IBOutlet weak var loginSubtitle: NSTextField!
    @IBOutlet weak var versionCheckLabel: NSButton!
    
    let defaults : UserDefaults = .standard
    let delegate = NSApplication.shared.delegate as! AppDelegate
    var loginLaunch = UserDefaults.standard.bool(forKey: "loginLaunch")
    let appVersion = (Bundle.main.infoDictionary!["CFBundleShortVersionString"] as? String)!
    let style = NSMutableParagraphStyle()
    
    
    override func viewDidAppear() {
        style.alignment = .right
        if checkForUpdate() == "Update available" {
            versionCheckLabel.stringValue = checkForUpdate()
            versionCheckLabel.attributedTitle = NSAttributedString(string: "Update available", attributes: [ NSAttributedString.Key.foregroundColor : #colorLiteral(red: 0, green: 0.4784313725, blue: 1, alpha: 1), NSAttributedString.Key.paragraphStyle : style ])
        }
        loginButton.state = loginLaunch ? .on : .off
        versionLabel.stringValue = "Version " + appVersion
        if loginLaunch {
            loginSubtitle.stringValue = "This application will launch when you login."
        }
    }
    
    func toggleLoginLaunch() {
        loginButton.state = !loginLaunch ? .on : .off
        defaults.set(!loginLaunch, forKey: "loginLaunch")
        loginLaunch = !loginLaunch
        if loginLaunch {
            LaunchAtLogin.isEnabled = true
        } else {
            LaunchAtLogin.isEnabled = false
        }
    }
    
    func checkForUpdate() -> String  {
        var Update: String? = nil
        if let url = URL(string: "https://raw.githubusercontent.com/FivePixels/dwa140shortcut/master/version.txt") {
            Update = try? String(contentsOf: url, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
        }
        let result = Update?.dropLast(1)
        if Update == nil {
            return "?"
        } else if result!.compare(appVersion, options: .numeric, range: nil, locale: .current) == .orderedDescending {
            return "Update available"
        } else {
            return "Up to date"
        }
    }
    
    @IBAction func updatePressed(_ sender: NSButton) {
        if checkForUpdate() == "Update available" {
            NSWorkspace.shared.open(URL(string: "https://github.com/fivepixels/dwa140shortcut/releases")!)
        }
    }
    @IBAction func loginButtonPressed(_ sender: NSButton) {
        toggleLoginLaunch()
        if loginLaunch {
            loginSubtitle.stringValue = "This application will launch when you login."
        } else {
            loginSubtitle.stringValue = "An even easier way to get connected, faster."
        }
    }
    @IBAction func triedAwesome(_ sender: NSButton) {
        // it's an invisible button on top of the disabled checkbox.
        awesomeSubtitle.stringValue = "You can't change the fact that you're awesome."
        DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
            self.awesomeSubtitle.stringValue = "It's just a fact."
        }
    }
    
    @IBAction func sourceButtonPressed(_ sender: NSButton) {
        NSWorkspace.shared.open((URL(string:"https://github.com/fivepixels/dwa140shortcut") ?? nil)!)
    }
    
    @IBAction func coffeePressed(_ sender: NSButton) {
        NSWorkspace.shared.open((URL(string: "https://paypal.me/fivepixels") ?? nil)!)
    }
    
    @IBAction func loveButtonPressed(_ sender: NSButton) {
        NSWorkspace.shared.open((URL(string:"https://twitter.com/o5pxels") ?? nil)!)
    }
}