Skip to content

Commit

Permalink
Fix for #409 (#411)
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Müller-Seydlitz <[email protected]>
  • Loading branch information
timbms authored Sep 20, 2019
1 parent 3044966 commit d6a0c3b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 43 deletions.
76 changes: 42 additions & 34 deletions openHAB/OpenHABWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,59 @@ class OpenHABWidget: NSObject, MKAnnotation {
return array[0].trimmingCharacters(in: .whitespaces)
}

// Text after "["
// Text between square brackets
var labelValue: String? {
let array = label.components(separatedBy: "[")
if array.count > 1 {
var characterSet = CharacterSet.whitespaces
characterSet.insert(charactersIn: "]")
return array[1].trimmingCharacters(in: characterSet)
}
return nil

// Swift 5 raw strings
let pattern = #"\[(.*?)\]"#
let string = label as NSString

let regex = try? NSRegularExpression(pattern: pattern, options: [])
guard let match = regex?.firstMatch(in: label, options: [], range: NSRange(location: 0, length: string.length)) else { return nil }
guard let range = Range(match.range(at: 1), in: label) else { return nil }
return String(label[range])

}

var coordinate: CLLocationCoordinate2D {
return item?.stateAsLocation()?.coordinate ?? kCLLocationCoordinate2DInvalid
}

func sendCommandDouble(_ command: Double) {
sendCommand(String(command))
}

func sendCommand(_ command: String?) {
guard let item = item else {
os_log("Command for Item = nil", log: .default, type: .info)
return
}
guard let sendCommand = sendCommand else {
os_log("sendCommand closure not set", log: .default, type: .info)
return
}
sendCommand(item, command)
}

func mappingIndex(byCommand command: String?) -> Int? {
for mapping in mappings where mapping.command == command {
return (mappings as NSArray).index(of: mapping)
}
return nil
}

}

extension OpenHABWidget {

// This is an ugly initializer
init(widgetId: String, label: String, icon: String, type: String, url: String?, period: String?, minValue: Double?, maxValue: Double?, step: Double?, refresh: Int?, height: Double?, isLeaf: Bool?, iconColor: String?, labelColor: String?, valueColor: String?, service: String?, state: String?, text: String?, legend: Bool?, encoding: String?, item: OpenHABItem?, linkedPage: OpenHABLinkedPage?, mappings: [OpenHABWidgetMapping], widgets: [OpenHABWidget] ) {
convenience init(widgetId: String, label: String, icon: String, type: String, url: String?, period: String?, minValue: Double?, maxValue: Double?, step: Double?, refresh: Int?, height: Double?, isLeaf: Bool?, iconColor: String?, labelColor: String?, valueColor: String?, service: String?, state: String?, text: String?, legend: Bool?, encoding: String?, item: OpenHABItem?, linkedPage: OpenHABLinkedPage?, mappings: [OpenHABWidgetMapping], widgets: [OpenHABWidget] ) {

func toString (_ with: Double?) -> String {
guard let double = with else { return "" }
return String(format: "%.1f", double)
}
self.init()
self.widgetId = widgetId
self.label = label
self.type = type
Expand Down Expand Up @@ -105,8 +136,8 @@ class OpenHABWidget: NSObject, MKAnnotation {
self.step = abs(self.step)
}

init(xml xmlElement: XMLElement) {
super.init()
convenience init(xml xmlElement: XMLElement) {
self.init()
for child in xmlElement.children {
switch child.tag {
case "widgetId": widgetId = child.stringValue
Expand Down Expand Up @@ -142,29 +173,6 @@ class OpenHABWidget: NSObject, MKAnnotation {
}
}
}

func sendCommandDouble(_ command: Double) {
sendCommand(String(command))
}

func sendCommand(_ command: String?) {
guard let item = item else {
os_log("Command for Item = nil", log: .default, type: .info)
return
}
guard let sendCommand = sendCommand else {
os_log("sendCommand closure not set", log: .default, type: .info)
return
}
sendCommand(item, command)
}

func mappingIndex(byCommand command: String?) -> Int? {
for mapping in mappings where mapping.command == command {
return (mappings as NSArray).index(of: mapping)
}
return nil
}
}

extension OpenHABWidget {
Expand Down
6 changes: 0 additions & 6 deletions openHABTestsSwift/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,8 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<<<<<<< HEAD
<string>2.1.10</string>
<key>CFBundleVersion</key>
<string>45</string>
=======
<string>2.1.32</string>
<key>CFBundleVersion</key>
<string>45</string>
>>>>>>> origin/develop
</dict>
</plist>
8 changes: 8 additions & 0 deletions openHABTestsSwift/OpenHABGeneralTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ class OpenHABGeneralTests: XCTestCase {
iconType: .svg ).url
XCTAssertEqual(urlc, URL(string: "http://192.169.2.1/icon/switch?state=OFF&format=SVG"), "Check endpoint creation")
}

func testLabelVale() {
let widget = OpenHABWidget()
widget.label = "llldl [llsl]"
XCTAssertEqual(widget.labelValue, "llsl")
widget.label = "llllsl[kkks] llls"
XCTAssertEqual(widget.labelValue, "kkks")
}
}
6 changes: 3 additions & 3 deletions openHABTestsSwift/OpenHABXMLParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class OpenHABXMLParserTests: XCTestCase {
}
}

func testFuzisingleXMLWidgetDecoder() {
func testFuziSingleXMLWidgetDecoder() {
var widget: OpenHABWidget

do {
Expand All @@ -64,7 +64,7 @@ class OpenHABXMLParserTests: XCTestCase {
}
}

func testnestedXMLWidgetDecoder() {
func testNestedXMLWidgetDecoder() {
var widget: OpenHABWidget

do {
Expand All @@ -80,7 +80,7 @@ class OpenHABXMLParserTests: XCTestCase {
}
}

func testshortUrlXMLWidgetDecoder() {
func testShortUrlXMLWidgetDecoder() {
var widget: OpenHABWidget

do {
Expand Down

0 comments on commit d6a0c3b

Please sign in to comment.