diff --git a/OTRS-Watch.xcodeproj/project.pbxproj b/OTRS-Watch.xcodeproj/project.pbxproj index 6f4a859..7e4df79 100644 --- a/OTRS-Watch.xcodeproj/project.pbxproj +++ b/OTRS-Watch.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 8505EACF1E01441F002A0BFB /* ViewControllerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8505EACE1E01441F002A0BFB /* ViewControllerExtension.swift */; }; 854A87B01DF6BD9A00904B3E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 854A87AF1DF6BD9A00904B3E /* AppDelegate.swift */; }; 854A87B21DF6BD9A00904B3E /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 854A87B11DF6BD9A00904B3E /* ViewController.swift */; }; 854A87B41DF6BD9A00904B3E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 854A87B31DF6BD9A00904B3E /* Assets.xcassets */; }; @@ -37,6 +38,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 8505EACE1E01441F002A0BFB /* ViewControllerExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ViewControllerExtension.swift; path = Extension/ViewControllerExtension.swift; sourceTree = ""; }; 854A87AC1DF6BD9A00904B3E /* OTRS-Watch.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "OTRS-Watch.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 854A87AF1DF6BD9A00904B3E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 854A87B11DF6BD9A00904B3E /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; @@ -80,6 +82,14 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 8505EAD01E014486002A0BFB /* Extension */ = { + isa = PBXGroup; + children = ( + 8505EACE1E01441F002A0BFB /* ViewControllerExtension.swift */, + ); + name = Extension; + sourceTree = ""; + }; 854A87A31DF6BD9900904B3E = { isa = PBXGroup; children = ( @@ -103,6 +113,7 @@ 854A87AE1DF6BD9A00904B3E /* OTRS-Watch */ = { isa = PBXGroup; children = ( + 8505EAD01E014486002A0BFB /* Extension */, 857EB22A1DF9748B00CCA941 /* Model */, 854A87AF1DF6BD9A00904B3E /* AppDelegate.swift */, 854A87B11DF6BD9A00904B3E /* ViewController.swift */, @@ -285,6 +296,7 @@ files = ( 854A87B21DF6BD9A00904B3E /* ViewController.swift in Sources */, 857EB22F1DF9845200CCA941 /* TicketExtension.swift in Sources */, + 8505EACF1E01441F002A0BFB /* ViewControllerExtension.swift in Sources */, 854A87DD1DF6DC7500904B3E /* OTRS.swift in Sources */, 857EB22C1DF974F500CCA941 /* Ticket.swift in Sources */, 854A87B01DF6BD9A00904B3E /* AppDelegate.swift in Sources */, 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 3db95dd..2374a5b 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 55f0cb0..364a590 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 = "502885498.221741" + timestampString = "503411749.778027" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "46" - endingLineNumber = "46" + startingLineNumber = "48" + endingLineNumber = "48" landmarkName = "get(forQue:)" landmarkType = "7"> @@ -26,11 +26,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "OTRS-Watch/OTRS.swift" - timestampString = "502893913.377215" + timestampString = "503411749.778027" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "53" - endingLineNumber = "53" + startingLineNumber = "56" + endingLineNumber = "56" landmarkName = "get(forQue:)" landmarkType = "7"> diff --git a/OTRS-Watch/Base.lproj/Main.storyboard b/OTRS-Watch/Base.lproj/Main.storyboard index 8052c1d..e4840e4 100644 --- a/OTRS-Watch/Base.lproj/Main.storyboard +++ b/OTRS-Watch/Base.lproj/Main.storyboard @@ -709,6 +709,105 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -812,6 +911,7 @@ + @@ -819,7 +919,7 @@ - + diff --git a/OTRS-Watch/Constants.swift b/OTRS-Watch/Constants.swift index ca72669..9c5b77b 100644 --- a/OTRS-Watch/Constants.swift +++ b/OTRS-Watch/Constants.swift @@ -25,4 +25,16 @@ struct Constants { static let QUEUE = "user::setting::otrs::queue" } } + + struct NOTIFICATION { + + struct TICKET { + + struct LIST { + static let UPDATED = "nf::ticket::list::updated" + } + + static let IN = "nf::ticket::in" + } + } } diff --git a/OTRS-Watch/Extension/ViewControllerExtension.swift b/OTRS-Watch/Extension/ViewControllerExtension.swift new file mode 100644 index 0000000..6ead55f --- /dev/null +++ b/OTRS-Watch/Extension/ViewControllerExtension.swift @@ -0,0 +1,52 @@ +// +// ViewControllerExtension.swift +// OTRS-Watch +// +// Created by Gerrit Linnemann on 14.12.16. +// Copyright © 2016 Adawim UG (haftungsbeschränkt). All rights reserved. +// + +import Cocoa + +extension ViewController: NSTableViewDataSource, NSTableViewDelegate { + + fileprivate enum CellIdentifiers { + static let OTRSTicketNumber = "OTRSTicketNumber" + static let OTRSTicketDetails = "OTRSTicketDetails" + } + + func numberOfRows(in tableView: NSTableView) -> Int { + return self.ticketDictionary.count + } + + func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { + + var image: NSImage? + var text: String = "" + var cellIdentifier: String = "" + + print("row: \(row)") + + guard let item = ticketDictionary.first else { + return nil + } + + if tableColumn == self.ticketTableView.tableColumns[0] { + cellIdentifier = CellIdentifiers.OTRSTicketNumber + image = nil + text = item.value.ticketNumber + } else if tableColumn == self.ticketTableView.tableColumns[1] { + cellIdentifier = CellIdentifiers.OTRSTicketDetails + image = nil + text = item.value.title + } + + if let cell = tableView.make(withIdentifier: cellIdentifier, owner: nil) as? NSTableCellView { + cell.textField?.stringValue = text + cell.imageView?.image = image ?? nil + return cell + } + + return nil + } +} diff --git a/OTRS-Watch/OTRS.swift b/OTRS-Watch/OTRS.swift index a7a1f46..552095b 100644 --- a/OTRS-Watch/OTRS.swift +++ b/OTRS-Watch/OTRS.swift @@ -43,16 +43,24 @@ class OTRS { // work with json ... //print(json) + var cnt = 0; + for object in json { let ticketIDs = object.value as! Array //print(ticketIDs) for ticketID in ticketIDs { if let tID:Int = Int(ticketID) { - self.fetch(ticket: tID) + self.fetch(ticket: tID, session: session) + cnt = cnt+1; } } } + + let nc = NotificationCenter.default + nc.post(name:Notification.Name(rawValue:Constants.NOTIFICATION.TICKET.LIST.UPDATED), + object: nil, + userInfo: ["count":cnt]) } } catch { print("Error in JSONSerialization") @@ -63,9 +71,7 @@ class OTRS { task.resume() } - func fetch(ticket:Int) { - let config = URLSessionConfiguration.default // Session configuration - let session = URLSession(configuration: config) // Load configuration into session + func fetch(ticket:Int, session:URLSession) { let url = URL(string: buildURLForGettingTicketInfo(ticketID: ticket)) let task = session.dataTask(with: url!, completionHandler: { // see: https://developer.apple.com/swift/blog/?id=37 @@ -78,7 +84,13 @@ class OTRS { 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) + //print(ticket) + + let nc = NotificationCenter.default + nc.post(name:Notification.Name(rawValue:Constants.NOTIFICATION.TICKET.IN), + object: nil, + userInfo: ["message":ticket.id, "ticket":ticket]) + } catch SerializationError.missing(let marker) { print("Error in JSONSerialization for ticket #\(ticket) at \"\(marker)\"") } catch { diff --git a/OTRS-Watch/ViewController.swift b/OTRS-Watch/ViewController.swift index 76a972b..f75de83 100644 --- a/OTRS-Watch/ViewController.swift +++ b/OTRS-Watch/ViewController.swift @@ -13,9 +13,15 @@ class ViewController: NSViewController { @IBOutlet weak var txtUsername: NSTextField! @IBOutlet weak var txtPassword: NSSecureTextField! @IBOutlet weak var txtQueue: NSTextField! + @IBOutlet weak var ticketTableView: NSTableView! + + var ticketDictionary: Dictionary = [:] override func viewDidLoad() { super.viewDidLoad() + + ticketTableView.delegate = self + ticketTableView.dataSource = self // Do any additional setup after loading the view. @@ -31,6 +37,15 @@ class ViewController: NSViewController { txtQueue.stringValue = UserDefaults.standard.string(forKey: Constants.USER_SETTINGS.OTRS.QUEUE)! } + let nc = NotificationCenter.default + nc.addObserver(forName:Notification.Name(rawValue:Constants.NOTIFICATION.TICKET.IN), + object:nil, queue:nil, + using:catchNewIncomingTicket) + nc.addObserver(forName:Notification.Name(rawValue:Constants.NOTIFICATION.TICKET.LIST.UPDATED), + object:nil, queue:nil, + using:catchTicketsUpdated) + + reload() } @@ -56,5 +71,24 @@ class ViewController: NSViewController { otrs.get(forQue: que) } } + + // MARK: Notification + + func catchNewIncomingTicket(notification:Notification) -> Void { + + guard let userInfo = notification.userInfo, + let message:String = userInfo["message"] as? String, + let ticket:Ticket = userInfo["ticket"] as? Ticket else { + return + } + + self.ticketDictionary.updateValue(ticket, forKey: message) + + reload() + } + + func catchTicketsUpdated(notification:Notification) -> Void { + + } }