Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve on formatting issue #752 #753

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions OpenHABCore/Sources/OpenHABCore/Model/NumberState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public struct NumberState: CustomStringConvertible, Equatable {
private(set) var unit: String? = ""
private(set) var format: String? = ""

public var intValue: Int {
Int(value)
}

public var stringValue: String {
String(value)
}

// Access to default memberwise initializer not permitted outside of package
public init(value: Double, unit: String? = "", format: String? = "") {
self.value = value
Expand All @@ -28,28 +36,31 @@ public struct NumberState: CustomStringConvertible, Equatable {
}

public func toString(locale: Locale?) -> String {
if let format, format.isEmpty == false {
let actualFormat = format.replacingOccurrences(of: "%unit%", with: unit ?? "")
if format.contains("%d") == true {
return String(format: actualFormat, locale: locale, Int(value))
if let format, !format.isEmpty {
let actualFormat = format
.replacingOccurrences(of: "%unit%", with: unit ?? "")
// %s in Java is for Strings, but does not work in Swift, see
// https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html)
.replacingOccurrences(of: "%s", with: "%@")
let formatValue: CVarArg = if format.contains("%d") {
intValue
} else if format.contains("%s") {
stringValue
} else {
return String(format: actualFormat, locale: locale, value)
value
}
return String(format: actualFormat, locale: locale, formatValue)
}
if let unit, unit.isEmpty == false {
return "\(formatValue()) \(unit)"
if let unit, !unit.isEmpty {
return "\(stringValue) \(unit)"
} else {
return formatValue()
return stringValue
}
}

public func formatValue() -> String {
String(value)
}

private func getActualValue() -> NSNumber {
if format?.contains("%d") == true {
NSNumber(value: Int(value))
NSNumber(value: intValue)
} else {
NSNumber(value: value)
}
Expand Down
2 changes: 1 addition & 1 deletion OpenHABCore/Sources/OpenHABCore/Model/OpenHABWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public class OpenHABWidget: NSObject, MKAnnotation, Identifiable {
sendCommand(state.toString(locale: Locale(identifier: "US")))
} else {
// For all other items, send the plain value
sendCommand(state.formatValue())
sendCommand(state.stringValue)
}
}

Expand Down
25 changes: 13 additions & 12 deletions openHAB/SetpointUITableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,22 @@ class SetpointUITableViewCell: GenericUITableViewCell {
private func handleUpDown(down: Bool) {
var numberState = widget?.stateValueAsNumberState
let stateValue = numberState?.value ?? widget.minValue
let newValue: Double = switch down {
case true:
stateValue - widget.step
case false:
stateValue + widget.step
let newValue: Double = down ? stateValue - widget.step : stateValue + widget.step

let limitedNewValue = newValue.clamped(to: widget.minValue ... widget.maxValue)

guard limitedNewValue != stateValue else {
// nothing to update, skip sending value
return
}
if newValue >= widget.minValue, newValue <= widget.maxValue {
if numberState != nil {
numberState?.value = newValue
} else {
numberState = NumberState(value: newValue)
}

widget.sendItemUpdate(state: numberState)
if numberState != nil {
numberState?.value = newValue
} else {
numberState = NumberState(value: newValue)
}

widget.sendItemUpdate(state: numberState)
}

@objc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class ObservableOpenHABWidget: NSObject, MKAnnotation, Identifiable, ObservableO
sendCommand(state.toString(locale: Locale(identifier: "US")))
} else {
// For all other items, send the plain value
sendCommand(state.formatValue())
sendCommand(state.stringValue)
}
}

Expand Down
Loading