Skip to content

Commit

Permalink
add inputHint field to widget for input type
Browse files Browse the repository at this point in the history
Signed-off-by: Tassilo Karge <[email protected]>
  • Loading branch information
TAKeanice committed Nov 11, 2024
1 parent f6067f8 commit 56fa379
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
21 changes: 12 additions & 9 deletions OpenHABCore/Sources/OpenHABCore/Model/OpenHABWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ protocol Widget: AnyObject {
}

public class OpenHABWidget: NSObject, MKAnnotation, Identifiable {
public enum WidgetType: String {
public enum WidgetType: String, Decodable, UnknownCaseRepresentable {
static var unknownCase: OpenHABWidget.WidgetType = .unknown
case chart = "Chart"
case colorpicker = "Colorpicker"
case defaultWidget = "Default"
Expand All @@ -67,6 +68,11 @@ public class OpenHABWidget: NSObject, MKAnnotation, Identifiable {
case unknown = "Unknown"
}

public enum InputHint: String, Decodable, UnknownCaseRepresentable {
static var unknownCase: OpenHABWidget.InputHint = .text
case text, number, date, time, datetime
}

public var id: String = ""

public var sendCommand: ((_ item: OpenHABItem, _ command: String?) -> Void)?
Expand All @@ -89,6 +95,7 @@ public class OpenHABWidget: NSObject, MKAnnotation, Identifiable {
public var state = ""
public var text = ""
public var legend: Bool?
public var inputHint = InputHint.unknownCase
public var encoding = ""
public var forceAsItem: Bool?
public var item: OpenHABItem?
Expand Down Expand Up @@ -204,15 +211,9 @@ public class OpenHABWidget: NSObject, MKAnnotation, Identifiable {
}
}

extension OpenHABWidget.WidgetType: Decodable {}

extension OpenHABWidget.WidgetType: UnknownCaseRepresentable {
static var unknownCase: OpenHABWidget.WidgetType = .unknown
}

extension OpenHABWidget {
// This is an ugly initializer
convenience init(widgetId: String, label: String, icon: String, type: WidgetType, 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: OpenHABSitemapPage?, mappings: [OpenHABWidgetMapping], widgets: [OpenHABWidget], visibility: Bool?, switchSupport: Bool?, forceAsItem: Bool?) {
convenience init(widgetId: String, label: String, icon: String, type: WidgetType, 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?, inputHint: InputHint, encoding: String?, item: OpenHABItem?, linkedPage: OpenHABSitemapPage?, mappings: [OpenHABWidgetMapping], widgets: [OpenHABWidget], visibility: Bool?, switchSupport: Bool?, forceAsItem: Bool?) {
self.init()
id = widgetId
self.widgetId = widgetId
Expand All @@ -239,6 +240,7 @@ extension OpenHABWidget {
self.state = state ?? ""
self.text = text ?? ""
self.legend = legend
self.inputHint = inputHint
self.encoding = encoding ?? ""
self.item = item
self.linkedPage = linkedPage
Expand Down Expand Up @@ -276,6 +278,7 @@ public extension OpenHABWidget {
let state: String?
let text: String?
let legend: Bool?
let inputHint: InputHint
let encoding: String?
let groupType: String?
let item: OpenHABItem.CodingData?
Expand All @@ -292,7 +295,7 @@ extension OpenHABWidget.CodingData {
var openHABWidget: OpenHABWidget {
let mappedWidgets = widgets.map(\.openHABWidget)
// swiftlint:disable:next line_length
return OpenHABWidget(widgetId: widgetId, label: label, icon: icon, type: type, url: url, period: period, minValue: minValue, maxValue: maxValue, step: step, refresh: refresh, height: height, isLeaf: isLeaf, iconColor: iconcolor, labelColor: labelcolor, valueColor: valuecolor, service: service, state: state, text: text, legend: legend, encoding: encoding, item: item?.openHABItem, linkedPage: linkedPage?.openHABSitemapPage, mappings: mappings, widgets: mappedWidgets, visibility: visibility, switchSupport: switchSupport, forceAsItem: forceAsItem)
return OpenHABWidget(widgetId: widgetId, label: label, icon: icon, type: type, url: url, period: period, minValue: minValue, maxValue: maxValue, step: step, refresh: refresh, height: height, isLeaf: isLeaf, iconColor: iconcolor, labelColor: labelcolor, valueColor: valuecolor, service: service, state: state, text: text, legend: legend, inputHint: inputHint, encoding: encoding, item: item?.openHABItem, linkedPage: linkedPage?.openHABSitemapPage, mappings: mappings, widgets: mappedWidgets, visibility: visibility, switchSupport: switchSupport, forceAsItem: forceAsItem)
}
}

Expand Down
15 changes: 13 additions & 2 deletions openHAB/OpenHABSitemapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -803,10 +803,21 @@ extension OpenHABSitemapViewController: UITableViewDelegate, UITableViewDataSour
hostingController.title = widget.labelText
navigationController?.pushViewController(hostingController, animated: true)
} else if widget.type == .input {
let hint = widget.inputHint
// TODO: proper texts instead of hardcoded values
let alert = UIAlertController(title: "Enter new value", message: "Current value for \(widget.label) is \(widget.state)", preferredStyle: .alert)
alert.addTextField { _ in
let alert = UIAlertController(
title: "Enter new value",
message: "Current value for \(widget.label) is \(widget.state)",
preferredStyle: .alert
)
alert.addTextField { textField in
// TODO: configure (set current value, validation, allow clearing, set delegate...)
textField.clearButtonMode = .always
// TODO: change text field propoerties according to hint
switch hint {
default:
textField.keyboardType = .alphabet
}
}
let sendAction = UIAlertAction(title: "Set value", style: .destructive, handler: { [weak self] _ in
self?.sendCommand(widget.item, commandToSend: alert.textFields?[0].text) // TODO: sanitize / convert text?
Expand Down
11 changes: 9 additions & 2 deletions openHABWatch/Model/ObservableOpenHABWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ enum WidgetTypeEnum {
}
}

enum InputHint: String, Decodable, CaseIterable {
case text, number, date, time, datetime
}

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
class ObservableOpenHABWidget: NSObject, MKAnnotation, Identifiable, ObservableObject {
var id: String = ""
Expand All @@ -65,6 +69,7 @@ class ObservableOpenHABWidget: NSObject, MKAnnotation, Identifiable, ObservableO
@Published var state = ""
var text = ""
var legend: Bool?
var inputHint: InputHint = .text
var encoding = ""
@Published var item: OpenHABItem?
var linkedPage: OpenHABSitemapPage?
Expand Down Expand Up @@ -213,7 +218,7 @@ class ObservableOpenHABWidget: NSObject, MKAnnotation, Identifiable, ObservableO

extension ObservableOpenHABWidget {
// This is an ugly initializer
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: OpenHABSitemapPage?, mappings: [OpenHABWidgetMapping], widgets: [ObservableOpenHABWidget], forceAsItem: Bool?) {
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?, inputHint: InputHint, encoding: String?, item: OpenHABItem?, linkedPage: OpenHABSitemapPage?, mappings: [OpenHABWidgetMapping], widgets: [ObservableOpenHABWidget], forceAsItem: Bool?) {
self.init()

id = widgetId
Expand Down Expand Up @@ -242,6 +247,7 @@ extension ObservableOpenHABWidget {
self.state = state ?? ""
self.text = text ?? ""
self.legend = legend
self.inputHint = inputHint
self.encoding = encoding ?? ""
self.item = item
self.linkedPage = linkedPage
Expand Down Expand Up @@ -279,6 +285,7 @@ extension ObservableOpenHABWidget {
let state: String?
let text: String?
let legend: Bool?
let inputHint: InputHint
let encoding: String?
let groupType: String?
let item: OpenHABItem.CodingData?
Expand All @@ -293,7 +300,7 @@ extension ObservableOpenHABWidget.CodingData {
var openHABWidget: ObservableOpenHABWidget {
let mappedWidgets = widgets.map(\.openHABWidget)
// swiftlint:disable:next line_length
return ObservableOpenHABWidget(widgetId: widgetId, label: label, icon: icon, type: type, url: url, period: period, minValue: minValue, maxValue: maxValue, step: step, refresh: refresh, height: height, isLeaf: isLeaf, iconColor: iconColor, labelColor: labelcolor, valueColor: valuecolor, service: service, state: state, text: text, legend: legend, encoding: encoding, item: item?.openHABItem, linkedPage: linkedPage?.openHABSitemapPage, mappings: mappings, widgets: mappedWidgets, forceAsItem: forceAsItem)
return ObservableOpenHABWidget(widgetId: widgetId, label: label, icon: icon, type: type, url: url, period: period, minValue: minValue, maxValue: maxValue, step: step, refresh: refresh, height: height, isLeaf: isLeaf, iconColor: iconColor, labelColor: labelcolor, valueColor: valuecolor, service: service, state: state, text: text, legend: legend, inputHint: inputHint, encoding: encoding, item: item?.openHABItem, linkedPage: linkedPage?.openHABSitemapPage, mappings: mappings, widgets: mappedWidgets, forceAsItem: forceAsItem)
}
}

Expand Down

0 comments on commit 56fa379

Please sign in to comment.