Commit 141e4af0 authored by Aral Balkan's avatar Aral Balkan
Browse files

Refactored the message editor / split view so that they communicate via...

Refactored the message editor / split view so that they communicate via notifications now (loosely coupled).
parent 8ff99627
......@@ -8,17 +8,29 @@
import Cocoa
public struct Message
// Had to make this a class as I need to pass it as a userInfo object in a notification
public class Message: NSObject
{
public var id:String
public var assetURLPrefix:String
public var folderURL:NSURL
public var html:String
public var synced:Bool = false
public var synced:Bool
init(id:String, assetURLPrefix:String, folderURL:NSURL, html:String, synced:Bool=false)
{
self.id = id
self.assetURLPrefix = assetURLPrefix
self.folderURL = folderURL
self.html = html
self.synced = synced
}
func JSONString() -> String
{
let dict = ["id": self.id, "assetURLPrefix": self.assetURLPrefix, "folderURL": self.folderURL.absoluteString!, "html": self.html, "synced":self.synced]
let dict = ["id": id, "assetURLPrefix": assetURLPrefix, "folderURL": folderURL.absoluteString!, "html": html, "synced":self.synced]
let jsonString = JSON(dict).rawString()
if let jsonString = jsonString
......
......@@ -160,10 +160,11 @@ class MessageSplitViewController: NSSplitViewController
println(messageDirectoryWrapper)
//
// Create the message object and show it in the timeline.
// Create the message object and broadcast it.
//
let message = Message(id: messageFileName, assetURLPrefix: assetURLPrefix, folderURL: fileURL, html: indexHTML as String, synced: false)
timelineViewController.showMessage(message)
post(MessageNotification.named(.ShowMessage), from: self, with: ["message": message])
break
}
......
......@@ -18,6 +18,7 @@ let MainSectionNavigationRequestNotification:String = "MainSectionNavigationRequ
// var path:String
//}
// MARK: - Enum: MainSection
enum MainSection:String
{
case Everyone = "Everyone"
......@@ -27,6 +28,7 @@ enum MainSection:String
case Private = "Private"
}
// MARK: - Class
class TimelineViewController: NSViewController
{
@IBOutlet weak var topLeftCornerIndicatorImageView: NSImageView!
......@@ -53,14 +55,9 @@ class TimelineViewController: NSViewController
//
// Notification observers
//
var mainSectionNavigationRequestObserver:NotificationHandler!
var pulseRemoteIndexUpdateObserver:NotificationHandler!
//
// MARK: - View lifecycle
//
var mainSectionNavigationRequestHandler:NotificationHandler?
var pulseRemoteIndexUpdateHandler:NotificationHandler?
var showMessageHandler:NotificationHandler?
override func viewDidLoad()
{
......@@ -104,18 +101,61 @@ class TimelineViewController: NSViewController
bottomLeftCornerIndicatorImageView.hidden = true
bottomRightCornerIndicatorImageView.hidden = true
}
override func viewWillAppear()
{
createConstraints()
addEventHandlers()
}
override func viewWillDisappear()
{
removeEventHandlers()
}
//
// MARK: - Notification handlers
//
func addEventHandlers()
{
//
// KVO handlers for web kit view load progress.
//
webkit.addObserver(self, forKeyPath: "loading", options: .New, context: nil)
webkit.addObserver(self, forKeyPath: "estimatedProgress", options: .New, context: nil)
//
// Handle show message notification
//
// MARK: * showMessageHandler
showMessageHandler = handle(MessageNotification.named(.ShowMessage))
{
/* as */ notification in
if let userInfo = notification.userInfo, message = userInfo["message"] as? Message
{
self.showMessage(message)
}
}
//
// Handle section change events
//
self.mainSectionNavigationRequestObserver = handle(MainSectionNavigationRequestNotification)
// MARK: * mainSectionNavigationRequestHandler
mainSectionNavigationRequestHandler = handle(MainSectionNavigationRequestNotification)
{
/* with */ notification in
/* as */ notification in
println("Received MainSectionNavigationRequestNotification: \(notification)…")
......@@ -129,42 +169,45 @@ class TimelineViewController: NSViewController
// Handle pulse remote index update events
// (Basically, messages have been updated)
//
self.pulseRemoteIndexUpdateObserver = handle(PulseNotification.named(.RemoteIndexUpdated))
// MARK: * pulseRemoteIndexUpdateHandler
pulseRemoteIndexUpdateHandler = handle(PulseNotification.named(.RemoteIndexUpdated))
{
/* with */ notification in
/* as */ notification in
println("Received RemoteIndexUpdated PulseNotification: \(notification)")
println("\(self): Received RemoteIndexUpdated PulseNotification: \(notification)")
if let userInfo = notification.userInfo, id = userInfo["id"] as? String
{
println("--> ID = \(id)")
let javascript = "messageSynced(\"\(id)\");"
self.webkit.evaluateJavaScript(javascript, completionHandler:
self.webkit.evaluateJavaScript(javascript)
{
(result, error) -> Void in
/* and handle */ (result, error) -> Void in
if error != nil
{
println("Error: \(error)")
println("\(self): Error: \(error)")
}
else
{
println("Result: \(result)")
println("\(self): Result: \(result)")
}
})
}
}
}
}
override func viewWillAppear()
func removeEventHandlers()
{
createConstraints()
webkit.removeObserver(self, forKeyPath: "loading", context: nil)
webkit.removeObserver(self, forKeyPath: "estimatedProgress", context: nil)
mainSectionNavigationRequestHandler?.remove()
pulseRemoteIndexUpdateHandler?.remove()
}
//
// MARK: - Layout
//
......@@ -178,8 +221,6 @@ class TimelineViewController: NSViewController
{
/* as */ webkit in
println("WEBKIT’S SUPERVIEW: \(webkit.superview!.width.attribute.rawValue) x \(webkit.superview!.height.attribute.rawValue)")
webkit.top == webkit.superview!.top
webkit.left == webkit.superview!.left
webkit.right == webkit.superview!.right
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment