1
0
mirror of https://github.com/unrealircd/unrealircd.git synced 2026-07-04 02:03:13 +02:00

generalize observer pattern

This commit is contained in:
Travis McArthur
2015-07-19 01:41:52 -07:00
parent c25c9d8529
commit 32557d44e5
8 changed files with 93 additions and 56 deletions
@@ -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 = "<group>"; };
1F1E10F71B5A43D800D69F41 /* ConfigurationModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConfigurationModel.swift; sourceTree = "<group>"; };
1F1E10F91B5A43EE00D69F41 /* AppModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppModel.swift; sourceTree = "<group>"; };
1F67373E1B5B93CC00C213D8 /* ChangeNotifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChangeNotifier.swift; sourceTree = "<group>"; };
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 = "<group>"; };
1FE784F41B3DF0DD006CB6DD /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -93,6 +95,7 @@
1F1E10F51B5A43CD00D69F41 /* DaemonModel.swift */,
1F1E10F71B5A43D800D69F41 /* ConfigurationModel.swift */,
1F1E10F91B5A43EE00D69F41 /* AppModel.swift */,
1F67373E1B5B93CC00C213D8 /* ChangeNotifier.swift */,
);
path = UnrealIRCd;
sourceTree = "<group>";
@@ -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;
};
+3 -5
View File
@@ -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()
}
+9 -14
View File
@@ -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
@@ -61,6 +61,7 @@
<outlet property="mainMenu" destination="nGf-dN-dCb" id="k5P-Yl-n8g"/>
</connections>
</customObject>
<customObject id="vdW-9I-abO"/>
<customObject id="Ady-hI-5gd" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="16.5" y="-49"/>
@@ -80,6 +81,11 @@
</connections>
</windowController>
<customObject id="Oky-zY-oP4" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
<customObject id="Vyg-9E-Gh9" customClass="AppDelegate" customModule="UnrealIRCd" customModuleProvider="target">
<connections>
<outlet property="mainMenu" destination="nGf-dN-dCb" id="EwD-6y-sm5"/>
</connections>
</customObject>
</objects>
<point key="canvasLocation" x="75" y="250"/>
</scene>
@@ -139,6 +145,11 @@
</view>
</viewController>
<customObject id="rPt-NT-nkU" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
<customObject id="oyo-qn-tpc" customClass="AppDelegate" customModule="UnrealIRCd" customModuleProvider="target">
<connections>
<outlet property="mainMenu" destination="nGf-dN-dCb" id="Qwx-H9-9Nj"/>
</connections>
</customObject>
</objects>
<point key="canvasLocation" x="75" y="612.5"/>
</scene>
@@ -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<ChangeNotifierDelegate> = []
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);
}
+1 -30
View File
@@ -7,28 +7,12 @@
//
import Foundation
class ConfigurationModel {
class ConfigurationModel : ChangeNotifier {
let defaults = NSUserDefaults.standardUserDefaults()
var changeDelegates : Set<ConfigurationModelChangeDelegate> = []
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);
}
+16 -6
View File
@@ -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()
}
}
+1 -1
View File
@@ -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?