diff --git a/src/macosx/UnrealIRCd.xcodeproj/project.pbxproj b/src/macosx/UnrealIRCd.xcodeproj/project.pbxproj index 757fc097e..f93220f7c 100644 --- a/src/macosx/UnrealIRCd.xcodeproj/project.pbxproj +++ b/src/macosx/UnrealIRCd.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ 1F1E10F61B5A43CD00D69F41 /* DaemonModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F1E10F51B5A43CD00D69F41 /* DaemonModel.swift */; }; 1F1E10F81B5A43D800D69F41 /* ConfigurationModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F1E10F71B5A43D800D69F41 /* ConfigurationModel.swift */; }; 1F1E10FA1B5A43EE00D69F41 /* AppModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F1E10F91B5A43EE00D69F41 /* AppModel.swift */; }; + 1F67373F1B5B93CC00C213D8 /* ChangeNotifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F67373E1B5B93CC00C213D8 /* ChangeNotifier.swift */; }; 1FE784F51B3DF0DD006CB6DD /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FE784F41B3DF0DD006CB6DD /* AppDelegate.swift */; }; 1FE784F71B3DF0DD006CB6DD /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FE784F61B3DF0DD006CB6DD /* ViewController.swift */; }; 1FE784F91B3DF0DD006CB6DD /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1FE784F81B3DF0DD006CB6DD /* Images.xcassets */; }; @@ -35,6 +36,7 @@ 1F1E10F51B5A43CD00D69F41 /* DaemonModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DaemonModel.swift; sourceTree = ""; }; 1F1E10F71B5A43D800D69F41 /* ConfigurationModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConfigurationModel.swift; sourceTree = ""; }; 1F1E10F91B5A43EE00D69F41 /* AppModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppModel.swift; sourceTree = ""; }; + 1F67373E1B5B93CC00C213D8 /* ChangeNotifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChangeNotifier.swift; sourceTree = ""; }; 1FE784EF1B3DF0DD006CB6DD /* UnrealIRCd.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = UnrealIRCd.app; sourceTree = BUILT_PRODUCTS_DIR; }; 1FE784F31B3DF0DD006CB6DD /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 1FE784F41B3DF0DD006CB6DD /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -93,6 +95,7 @@ 1F1E10F51B5A43CD00D69F41 /* DaemonModel.swift */, 1F1E10F71B5A43D800D69F41 /* ConfigurationModel.swift */, 1F1E10F91B5A43EE00D69F41 /* AppModel.swift */, + 1F67373E1B5B93CC00C213D8 /* ChangeNotifier.swift */, ); path = UnrealIRCd; sourceTree = ""; @@ -248,6 +251,7 @@ 1FE784F51B3DF0DD006CB6DD /* AppDelegate.swift in Sources */, 1F1E10FA1B5A43EE00D69F41 /* AppModel.swift in Sources */, 1F1E10F61B5A43CD00D69F41 /* DaemonModel.swift in Sources */, + 1F67373F1B5B93CC00C213D8 /* ChangeNotifier.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/src/macosx/UnrealIRCd/AppDelegate.swift b/src/macosx/UnrealIRCd/AppDelegate.swift index ea49f11d1..f9ff19d19 100644 --- a/src/macosx/UnrealIRCd/AppDelegate.swift +++ b/src/macosx/UnrealIRCd/AppDelegate.swift @@ -16,16 +16,14 @@ class AppDelegate: NSObject, NSApplicationDelegate { var appModel : AppModel override init() { - appModel = AppModel() + assert(mainMenu != nil, "Unable to load Menu from XIB") + appModel = AppModel(menu: mainMenu!) super.init() } func applicationDidFinishLaunching(aNotification: NSNotification) { - assert(mainMenu != nil, "Unable to load Menu from XIB") - - appModel.setupStatusItem(mainMenu!) - appModel.startupComplete() + appModel.startup() } diff --git a/src/macosx/UnrealIRCd/AppModel.swift b/src/macosx/UnrealIRCd/AppModel.swift index 8a46d51c8..f56e0f2cc 100644 --- a/src/macosx/UnrealIRCd/AppModel.swift +++ b/src/macosx/UnrealIRCd/AppModel.swift @@ -9,7 +9,7 @@ import Foundation import AppKit -class AppModel +class AppModel : ChangeNotifierDelegate { var menuItem : NSStatusItem static let logoName = "logo.png" @@ -19,19 +19,17 @@ class AppModel var windowController : NSWindowController? var mainMenu : NSMenu - init() + init(menu: NSMenu) { - - daemonModel = DaemonModel() - configurationModel = ConfigurationModel() menuItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1/*NSVariableStatusItemLength*/) - } - - func setupStatusItem(menu: NSMenu) - { mainMenu = menu menuItem.image = NSImage(named: AppModel.logoName) menuItem.menu = menu + + daemonModel = DaemonModel() + configurationModel = ConfigurationModel() + + daemonModel.attachChangeDelegate(self) } func launchHelp() @@ -47,12 +45,11 @@ class AppModel windowController!.showWindow(self) } - func startupComplete() + func startup() { if configurationModel.shouldAutoStartDaemon { daemonModel.start() - updateUIFromDaemon() } @@ -71,16 +68,14 @@ class AppModel func startDaemon() { daemonModel.stop() - updateUIFromDaemon() } func stopDaemon() { daemonModel.start() - updateUIFromDaemon() } - func updateUIFromDaemon() + func modelChanged(model: ChangeNotifier) { let daemonStatus = daemonModel.isRunning mainMenu.itemWithTitle("Start UnrealIRCd")?.enabled = !daemonStatus diff --git a/src/macosx/UnrealIRCd/Base.lproj/Main.storyboard b/src/macosx/UnrealIRCd/Base.lproj/Main.storyboard index aa91b02e3..d7bc020c1 100644 --- a/src/macosx/UnrealIRCd/Base.lproj/Main.storyboard +++ b/src/macosx/UnrealIRCd/Base.lproj/Main.storyboard @@ -61,6 +61,7 @@ + @@ -80,6 +81,11 @@ + + + + + @@ -139,6 +145,11 @@ + + + + + diff --git a/src/macosx/UnrealIRCd/ChangeNotifier.swift b/src/macosx/UnrealIRCd/ChangeNotifier.swift new file mode 100644 index 000000000..e2872276a --- /dev/null +++ b/src/macosx/UnrealIRCd/ChangeNotifier.swift @@ -0,0 +1,48 @@ +// +// ChangeNotifier.swift +// UnrealIRCd +// +// Created by Travis McArthur on 7/19/15. +// Copyright (c) 2015 UnrealIRCd Team. All rights reserved. +// + +import Foundation +class ChangeNotifier { + + var changeDelegates : Array = [] + + init() + { + + } + + func attachChangeDelegate(delegate: ChangeNotifierDelegate) + { + changeDelegates.append(delegate) + } + + func dettachChangeDelegate(delegate: ChangeNotifierDelegate) + { + + + for i in 0 ... changeDelegates.count + { + if changeDelegates[i] === delegate + { + changeDelegates.removeAtIndex(index) + } + } + } + func notifyListeners() + { + for listener in changeDelegates + { + listener.modelChanged(self) + } + } +} + +protocol ChangeNotifierDelegate +{ + func modelChanged(model: ChangeNotifier); +} \ No newline at end of file diff --git a/src/macosx/UnrealIRCd/ConfigurationModel.swift b/src/macosx/UnrealIRCd/ConfigurationModel.swift index 8b67ed59b..173386570 100644 --- a/src/macosx/UnrealIRCd/ConfigurationModel.swift +++ b/src/macosx/UnrealIRCd/ConfigurationModel.swift @@ -7,28 +7,12 @@ // import Foundation -class ConfigurationModel { +class ConfigurationModel : ChangeNotifier { let defaults = NSUserDefaults.standardUserDefaults() - var changeDelegates : Set = [] static let autoStartDaemonKey = "IRCD_AUTOSTART" static let autoStartAgentKey = "AGENT_AUTOSTART" - init() - { - - } - - func attachChangeDelegate(delegate: ConfigurationModelChangeDelegate) - { - changeDelegates.insert(delegate) - } - - func dettachChangeDelegate(delegate: ConfigurationModelChangeDelegate) - { - changeDelegates.remove(delegate) - } - var shouldAutoStartAgent : Bool { set(value) { @@ -52,17 +36,4 @@ class ConfigurationModel { return defaults.boolForKey(ConfigurationModel.autoStartDaemonKey) } } - - func notifyListeners() - { - for listener in changeDelegates - { - listener.configurationModelChanged(self) - } - } -} - -protocol ConfigurationModelChangeDelegate : Hashable -{ - func configurationModelChanged(model: ConfigurationModel); } \ No newline at end of file diff --git a/src/macosx/UnrealIRCd/DaemonModel.swift b/src/macosx/UnrealIRCd/DaemonModel.swift index fe7abcd6d..5c763f387 100644 --- a/src/macosx/UnrealIRCd/DaemonModel.swift +++ b/src/macosx/UnrealIRCd/DaemonModel.swift @@ -8,22 +8,32 @@ import Foundation -class DaemonModel +class DaemonModel : ChangeNotifier { - func start() -> Bool + + override init() { - return false + isRunning = false + super.init() + } + + + func start() + { + isRunning = true } - func stop() -> Bool + func stop() { - return false + isRunning = false } var isRunning : Bool { - return false; + didSet { + notifyListeners() + } } diff --git a/src/macosx/UnrealIRCd/ViewController.swift b/src/macosx/UnrealIRCd/ViewController.swift index 26273257a..34ce4e0d0 100644 --- a/src/macosx/UnrealIRCd/ViewController.swift +++ b/src/macosx/UnrealIRCd/ViewController.swift @@ -9,7 +9,7 @@ import Cocoa import AppKit -class ViewController: NSViewController { +class ViewController: NSViewController, ChangeNotifierDelegate { @IBOutlet weak var autoStartAgentCheckbox : NSButton? @IBOutlet weak var autoStartDaemonCheckbox : NSButton?