OTRS Singleton

This commit is contained in:
Gerrit Linnemann 2016-12-15 13:50:31 +01:00
parent 84138a8b49
commit 1adbfd973a
7 changed files with 118 additions and 46 deletions

View File

@ -10,12 +10,12 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "OTRS-Watch/OTRS.swift"
timestampString = "503411749.778027"
timestampString = "503498994.361076"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "48"
endingLineNumber = "48"
landmarkName = "get(forQue:)"
startingLineNumber = "58"
endingLineNumber = "58"
landmarkName = "reload(que:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
@ -26,12 +26,76 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "OTRS-Watch/OTRS.swift"
timestampString = "503411749.778027"
timestampString = "503498994.361076"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "56"
endingLineNumber = "56"
landmarkName = "get(forQue:)"
startingLineNumber = "66"
endingLineNumber = "66"
landmarkName = "reload(que:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "OTRS-Watch/OTRS.swift"
timestampString = "503498994.361076"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "43"
endingLineNumber = "43"
landmarkName = "reload(que:)"
landmarkType = "7">
<Locations>
<Location
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "OTRS_Watch.OTRS.reload (que : Swift.Int) -&gt; ()"
moduleName = "OTRS-Watch"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/linnemann/Documents/dev/xcode/OTRS-Watch/OTRS-Watch/OTRS.swift"
timestampString = "503498802.79459"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "45"
endingLineNumber = "45"
offsetFromSymbolStart = "387">
</Location>
<Location
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "OTRS_Watch.OTRS.(reload (que : Swift.Int) -&gt; ()).(closure #1)"
moduleName = "OTRS-Watch"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/linnemann/Documents/dev/xcode/OTRS-Watch/OTRS-Watch/OTRS.swift"
timestampString = "503498802.797104"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "48"
endingLineNumber = "48"
offsetFromSymbolStart = "53">
</Location>
</Locations>
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "OTRS-Watch/OTRS.swift"
timestampString = "503498201.457531"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "30"
endingLineNumber = "30"
landmarkName = "configure(username_:password_:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>

View File

@ -11,16 +11,26 @@ import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
OTRS.sharedInstance.configure(username_: UserDefaults.standard.string(forKey: Constants.USER_SETTINGS.CREDENTIALS.USER)!, password_: UserDefaults.standard.string(forKey: Constants.USER_SETTINGS.CREDENTIALS.PASSWORD)!)
getTicketsFromOTRS()
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
// MARK: OTRS
private func getTicketsFromOTRS() {
if let que:Int = Int(UserDefaults.standard.string(forKey: Constants.USER_SETTINGS.OTRS.QUEUE)!) {
OTRS.sharedInstance.reload(que: que)
}
}
}

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="11542" systemVersion="15G1108" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="15G1108" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11542"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11762"/>
<capability name="box content view" minToolsVersion="7.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
@ -795,7 +795,7 @@
</subviews>
</clipView>
<scroller key="horizontalScroller" hidden="YES" verticalHuggingPriority="750" horizontal="YES" id="yAG-0l-MrF">
<rect key="frame" x="1" y="119" width="223" height="15"/>
<rect key="frame" x="1" y="-16" width="0.0" height="16"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
<scroller key="verticalScroller" hidden="YES" verticalHuggingPriority="750" horizontal="NO" id="0Mc-1B-cqp">

View File

@ -16,7 +16,7 @@ extension ViewController: NSTableViewDataSource, NSTableViewDelegate {
}
func numberOfRows(in tableView: NSTableView) -> Int {
return self.ticketDictionary.count
return OTRS.sharedInstance.ticketDictionary.count
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
@ -25,20 +25,18 @@ extension ViewController: NSTableViewDataSource, NSTableViewDelegate {
var text: String = ""
var cellIdentifier: String = ""
print("row: \(row)")
guard let item = ticketDictionary.first else {
guard let item:Ticket = OTRS.sharedInstance.ticketDictionary[row] else {
return nil
}
if tableColumn == self.ticketTableView.tableColumns[0] {
cellIdentifier = CellIdentifiers.OTRSTicketNumber
image = nil
text = item.value.ticketNumber
text = item.ticketNumber
} else if tableColumn == self.ticketTableView.tableColumns[1] {
cellIdentifier = CellIdentifiers.OTRSTicketDetails
image = nil
text = item.value.title
text = item.title
}
if let cell = tableView.make(withIdentifier: cellIdentifier, owner: nil) as? NSTableCellView {

View File

@ -10,15 +10,23 @@ import Foundation
class OTRS {
var url : String;
var username : String;
var password : String;
class var sharedInstance: OTRS {
struct Singleton {
static let instance = OTRS()
}
return Singleton.instance
}
var username : String = "";
var password : String = "";
var ticketDictionary: Array<Ticket> = []
//MARK: init
init(url_:String, username_:String, password_:String) {
self.url = url_
func configure(username_:String, password_:String) {
self.username = username_
self.password = password_
}
@ -26,11 +34,11 @@ class OTRS {
//MARK: REST
func get(forQue:Int) {
func reload(que:Int) {
let config = URLSessionConfiguration.default // Session configuration
let session = URLSession(configuration: config) // Load configuration into session
let url = URL(string: buildURLForGettingNew(queID: forQue))
let url = URL(string: buildURLForGettingNew(queID: que))
let task = session.dataTask(with: url!, completionHandler: {
(data, response, error) in
@ -38,6 +46,8 @@ class OTRS {
if error != nil {
print(error!.localizedDescription)
} else {
self.ticketDictionary.removeAll()
do {
if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any] {
// work with json ...
@ -47,7 +57,7 @@ class OTRS {
for object in json {
let ticketIDs = object.value as! Array<String>
//print(ticketIDs)
print(ticketIDs)
for ticketID in ticketIDs {
if let tID:Int = Int(ticketID) {
@ -91,6 +101,8 @@ class OTRS {
object: nil,
userInfo: ["message":ticket.id, "ticket":ticket])
self.ticketDictionary.append(ticket)
} catch SerializationError.missing(let marker) {
print("Error in JSONSerialization for ticket #\(ticket) at \"\(marker)\"")
} catch {

View File

@ -15,8 +15,6 @@ class ViewController: NSViewController {
@IBOutlet weak var txtQueue: NSTextField!
@IBOutlet weak var ticketTableView: NSTableView!
var ticketDictionary: Dictionary<String, Ticket> = [:]
override func viewDidLoad() {
super.viewDidLoad()
@ -44,9 +42,6 @@ class ViewController: NSViewController {
nc.addObserver(forName:Notification.Name(rawValue:Constants.NOTIFICATION.TICKET.LIST.UPDATED),
object:nil, queue:nil,
using:catchTicketsUpdated)
reload()
}
override var representedObject: Any? {
@ -55,36 +50,29 @@ class ViewController: NSViewController {
}
}
// MARK: Actions
@IBAction func saveSettingsAction(_ sender: NSButton) {
UserDefaults.standard.setValue(txtUsername.stringValue, forKey: Constants.USER_SETTINGS.CREDENTIALS.USER)
UserDefaults.standard.setValue(txtPassword.stringValue, forKey: Constants.USER_SETTINGS.CREDENTIALS.PASSWORD)
UserDefaults.standard.setValue(txtQueue.stringValue, forKey: Constants.USER_SETTINGS.OTRS.QUEUE)
reload()
}
private func reload() {
let otrs = OTRS(url_: "", username_: txtUsername.stringValue, password_: txtPassword.stringValue)
if let que:Int = Int(txtQueue.stringValue) {
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 {
let _:String = userInfo["message"] as? String,
let _:Ticket = userInfo["ticket"] as? Ticket else {
return
}
self.ticketDictionary.updateValue(ticket, forKey: message)
reload()
self.ticketTableView.reloadData()
}
func catchTicketsUpdated(notification:Notification) -> Void {