diff --git a/OTRS-Watch.xcodeproj/project.xcworkspace/xcuserdata/linnemann.xcuserdatad/UserInterfaceState.xcuserstate b/OTRS-Watch.xcodeproj/project.xcworkspace/xcuserdata/linnemann.xcuserdatad/UserInterfaceState.xcuserstate index d25ae22..bf87149 100644 Binary files a/OTRS-Watch.xcodeproj/project.xcworkspace/xcuserdata/linnemann.xcuserdatad/UserInterfaceState.xcuserstate and b/OTRS-Watch.xcodeproj/project.xcworkspace/xcuserdata/linnemann.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/OTRS-Watch.xcodeproj/xcuserdata/linnemann.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/OTRS-Watch.xcodeproj/xcuserdata/linnemann.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 928fa50..25fc655 100644 --- a/OTRS-Watch.xcodeproj/xcuserdata/linnemann.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/OTRS-Watch.xcodeproj/xcuserdata/linnemann.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -10,11 +10,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "OTRS-Watch/OTRS.swift" - timestampString = "503499740.022029" + timestampString = "504524069.32379" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "45" - endingLineNumber = "45" + startingLineNumber = "47" + endingLineNumber = "47" landmarkName = "reload(que:)" landmarkType = "7"> @@ -58,11 +58,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "OTRS-Watch/OTRS.swift" - timestampString = "503499740.022029" + timestampString = "504524069.32379" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "32" - endingLineNumber = "32" + startingLineNumber = "34" + endingLineNumber = "34" landmarkName = "OTRS" landmarkType = "3"> diff --git a/OTRS-Watch/Base.lproj/Main.storyboard b/OTRS-Watch/Base.lproj/Main.storyboard index 72e0d35..0c77db2 100644 --- a/OTRS-Watch/Base.lproj/Main.storyboard +++ b/OTRS-Watch/Base.lproj/Main.storyboard @@ -659,7 +659,7 @@ - + @@ -685,16 +685,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/OTRS-Watch/Constants.swift b/OTRS-Watch/Constants.swift index 674055b..a91c1ef 100644 --- a/OTRS-Watch/Constants.swift +++ b/OTRS-Watch/Constants.swift @@ -36,6 +36,11 @@ struct Constants { } static let IN = "nf::ticket::in" + + struct ACTION { + static let CONNECTED = "nf::ticket::action::connected" + static let ERROR = "nf::ticket::action::error" + } } } diff --git a/OTRS-Watch/Extension/ViewControllerExtension.swift b/OTRS-Watch/Extension/ViewControllerExtension.swift index 150d58a..dba258a 100644 --- a/OTRS-Watch/Extension/ViewControllerExtension.swift +++ b/OTRS-Watch/Extension/ViewControllerExtension.swift @@ -65,7 +65,7 @@ extension ViewController: NSTableViewDataSource, NSTableViewDelegate { let myTableViewFromNotification = notification.object as! NSTableView let ticket = OTRS.sharedInstance.ticketDictionary[row] as! Ticket - print("Ticket ausgewählt: \(ticket.title)") + print("🖥 Ticket ausgewählt: \(ticket.title)") self.ticketDetailsBox.title = "Ticket#\(ticket.ticketNumber)" self.txtTicketTitle.stringValue = ticket.title diff --git a/OTRS-Watch/OTRS.swift b/OTRS-Watch/OTRS.swift index ae30f15..b4d3b1b 100644 --- a/OTRS-Watch/OTRS.swift +++ b/OTRS-Watch/OTRS.swift @@ -6,7 +6,9 @@ // Copyright © 2016 Adawim UG (haftungsbeschränkt). All rights reserved. // // +// http://saeotrs01.sae.intra/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/ // http://otrs.github.io/doc/manual/admin/stable/en/html/genericinterface.html +// https://developer.apple.com/swift/blog/?id=37 // import Foundation @@ -40,7 +42,7 @@ class OTRS { //MARK: REST func reload(que:Int) { - + let nc = NotificationCenter.default let config = URLSessionConfiguration.default // Session configuration let session = URLSession(configuration: config) // Load configuration into session let urlNew = URL(string: buildURLForGettingNew(queID: que)) @@ -48,27 +50,35 @@ class OTRS { self.ticketDictionary.removeAll() + nc.post(name:Notification.Name(rawValue:Constants.NOTIFICATION.TICKET.ACTION.CONNECTED), + object: nil, + userInfo: ["message":""]) + reloadByURL(session: session, url: urlNew!) reloadByURL(session: session, url: urlOpen!) } private func reloadByURL(session:URLSession, url:URL) { + let nc = NotificationCenter.default + let task = session.dataTask(with: url, completionHandler: { (data, response, error) in if error != nil { print(error!.localizedDescription) + nc.post(name:Notification.Name(rawValue:Constants.NOTIFICATION.TICKET.ACTION.ERROR), + object: nil, + userInfo: ["message":error!.localizedDescription]) } else { do { if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any] { // work with json ... - //print(json) var cnt = 0; for object in json { let ticketIDs = object.value as! Array - print(ticketIDs) + print("📑 Going to load: \(ticketIDs)") for ticketID in ticketIDs { cnt += 1; @@ -81,7 +91,10 @@ class OTRS { } } } catch { - print("Error in JSONSerialization") + print("📑 Error in JSONSerialization") + nc.post(name:Notification.Name(rawValue:Constants.NOTIFICATION.TICKET.ACTION.ERROR), + object: nil, + userInfo: ["message":"Fehler beim Auslesesen der übertragenen Daten."]) } } }) @@ -91,18 +104,21 @@ class OTRS { func fetch(ticket:Int, session:URLSession, isLast:Bool) { let url = URL(string: buildURLForGettingTicketInfo(ticketID: ticket)) + let nc = NotificationCenter.default let task = session.dataTask(with: url!, completionHandler: { // see: https://developer.apple.com/swift/blog/?id=37 (data, response, error) in if error != nil { print(error!.localizedDescription) + nc.post(name:Notification.Name(rawValue:Constants.NOTIFICATION.TICKET.ACTION.ERROR), + object: nil, + userInfo: ["message":error!.localizedDescription]) } else { do { let dictionary = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String : AnyObject] let ticketData : Dictionary = dictionary["Ticket"]?.firstObject as! [String : AnyObject] let ticket = try Ticket(source: ticketData) - //print(ticket) self.ticketDictionary.append(ticket) @@ -119,9 +135,15 @@ class OTRS { } } catch SerializationError.missing(let marker) { - print("Error in JSONSerialization for ticket #\(ticket) at \"\(marker)\"") + print("📑 Error in JSONSerialization for ticket #\(ticket) at \"\(marker)\"") + nc.post(name:Notification.Name(rawValue:Constants.NOTIFICATION.TICKET.ACTION.ERROR), + object: nil, + userInfo: ["message":"Fehler beim Auslesesen der übertragenen Daten zu Ticket #\(ticket)"]) } catch { - print("Error in JSONSerialization for ticket #\(ticket): \(error)") + print("📑 Error in JSONSerialization for ticket #\(ticket): \(error)") + nc.post(name:Notification.Name(rawValue:Constants.NOTIFICATION.TICKET.ACTION.ERROR), + object: nil, + userInfo: ["message":"Fehler beim Auslesesen der übertragenen Daten zu Ticket #\(ticket)"]) } } }) diff --git a/OTRS-Watch/ViewController.swift b/OTRS-Watch/ViewController.swift index 6bda580..c655382 100644 --- a/OTRS-Watch/ViewController.swift +++ b/OTRS-Watch/ViewController.swift @@ -100,7 +100,7 @@ class ViewController: NSViewController { guard let userInfo = notification.userInfo, let _:String = userInfo["message"] as? String, - let _:Ticket = userInfo["ticket"] as? Ticket else { + let t:Ticket = userInfo["ticket"] as? Ticket else { return } @@ -111,10 +111,12 @@ class ViewController: NSViewController { DispatchQueue.main.async { self.ticketTableView.reloadData() } + + print("🖥 Ticket to show: \(t.id)") } func catchTicketsUpdated(notification:Notification) -> Void { - print("\(OTRS.sharedInstance.ticketDictionary.count) tickets to be shown") + print("🖥 \(OTRS.sharedInstance.ticketDictionary.count) tickets to be shown") } } diff --git a/OTRS-Watch/WindowController.swift b/OTRS-Watch/WindowController.swift index 253902a..fe381b1 100644 --- a/OTRS-Watch/WindowController.swift +++ b/OTRS-Watch/WindowController.swift @@ -10,6 +10,10 @@ import Cocoa class WindowController: NSWindowController { + @IBOutlet weak var ticketCount: NSButton! + @IBOutlet weak var activity: NSTextField! + + var viewController: ViewController { get { return self.window!.contentViewController! as! ViewController @@ -20,6 +24,22 @@ class WindowController: NSWindowController { super.windowDidLoad() // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. + + self.activity.stringValue = "" + + let nc = NotificationCenter.default + nc.addObserver(forName:Notification.Name(rawValue:Constants.NOTIFICATION.TICKET.ACTION.CONNECTED), + object:nil, queue:nil, + using:catchConnected) + nc.addObserver(forName:Notification.Name(rawValue:Constants.NOTIFICATION.TICKET.IN), + object:nil, queue:nil, + using:catchLoading) + nc.addObserver(forName:Notification.Name(rawValue:Constants.NOTIFICATION.TICKET.LIST.UPDATED), + object:nil, queue:nil, + using:catchUpdateFinished) + nc.addObserver(forName:Notification.Name(rawValue:Constants.NOTIFICATION.TICKET.ACTION.ERROR), + object:nil, queue:nil, + using:catchError) } @IBAction func toolbarActionReload(_ sender: NSToolbarItem) { @@ -33,5 +53,81 @@ class WindowController: NSWindowController { @IBAction func toolbarActionSettings(_ sender: NSToolbarItem) { self.viewController.tabView.selectTabViewItem(at: Constants.TABS.TAB.SETTINGS) } + + // MARK: Notification + + func catchConnected(notification:Notification) -> Void { + guard let userInfo = notification.userInfo else { + return + } + + DispatchQueue.main.async(execute: { + guard let message:String = userInfo["message"] as? String else { + return + } + + self.activity.stringValue = "Verbindungsaufbau ..." + }) + + debugStatusBarContent() + } + + func catchLoading(notification:Notification) -> Void { + guard let userInfo = notification.userInfo else { + return + } + + DispatchQueue.main.async(execute: { + if let ticketID:String = userInfo["message"] as? String { + self.activity.stringValue = "Ticket #\(ticketID) geladen." + } + }) + + debugStatusBarContent() + } + + func catchUpdateFinished(notification:Notification) -> Void { + DispatchQueue.main.async(execute: { + self.activity.stringValue = "" + }) + + guard let userInfo = notification.userInfo else { + return + } + + DispatchQueue.main.async(execute: { + if let ticketCount:Int = userInfo["count"] as? Int { + self.ticketCount.title = "\(ticketCount)" + } + }) + + debugStatusBarContent() + } + + func catchError(notification:Notification) -> Void { + DispatchQueue.main.async(execute: { + self.activity.stringValue = "Fehler bei der Verarbeitung!" + }) + + guard let userInfo = notification.userInfo else { + return + } + + DispatchQueue.main.async(execute: { + if let message:String = userInfo["message"] as? String { + self.activity.stringValue = "\(message)" + } + }) + + debugStatusBarContent() + } + + // MARK: Functions + + private func debugStatusBarContent() { + if self.activity.stringValue != "" { + print("🖨 \(self.activity.stringValue)") + } + } }