Skip to content

Commit 441b29f

Browse files
authoredNov 16, 2024
Support input element (Issue #726) (#755)
* Input element support for all input type including date --------- Signed-off-by: Tassilo Karge <tassilo.karge@web.de>
1 parent dd5553e commit 441b29f

9 files changed

+387
-122
lines changed
 

‎OpenHABCore/Sources/OpenHABCore/Model/OpenHABWidget.swift

+14-10
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ protocol Widget: AnyObject {
4848
}
4949

5050
public class OpenHABWidget: NSObject, MKAnnotation, Identifiable {
51-
public enum WidgetType: String {
51+
public enum WidgetType: String, Decodable, UnknownCaseRepresentable {
52+
static var unknownCase: OpenHABWidget.WidgetType = .unknown
5253
case chart = "Chart"
5354
case colorpicker = "Colorpicker"
5455
case defaultWidget = "Default"
5556
case frame = "Frame"
5657
case group = "Group"
5758
case image = "Image"
59+
case input = "Input"
5860
case mapview = "Mapview"
5961
case selection = "Selection"
6062
case setpoint = "Setpoint"
@@ -66,13 +68,18 @@ public class OpenHABWidget: NSObject, MKAnnotation, Identifiable {
6668
case unknown = "Unknown"
6769
}
6870

71+
public enum InputHint: String, Decodable, UnknownCaseRepresentable {
72+
static var unknownCase: OpenHABWidget.InputHint = .text
73+
case text, number, date, time, datetime
74+
}
75+
6976
public var id: String = ""
7077

7178
public var sendCommand: ((_ item: OpenHABItem, _ command: String?) -> Void)?
7279
public var widgetId = ""
7380
public var label = ""
7481
public var icon = ""
75-
public var type: WidgetType?
82+
public var type: WidgetType = .unknownCase
7683
public var url = ""
7784
public var period = ""
7885
public var minValue = 0.0
@@ -88,6 +95,7 @@ public class OpenHABWidget: NSObject, MKAnnotation, Identifiable {
8895
public var state = ""
8996
public var text = ""
9097
public var legend: Bool?
98+
public var inputHint = InputHint.unknownCase
9199
public var encoding = ""
92100
public var forceAsItem: Bool?
93101
public var item: OpenHABItem?
@@ -203,15 +211,9 @@ public class OpenHABWidget: NSObject, MKAnnotation, Identifiable {
203211
}
204212
}
205213

206-
extension OpenHABWidget.WidgetType: Decodable {}
207-
208-
extension OpenHABWidget.WidgetType: UnknownCaseRepresentable {
209-
static var unknownCase: OpenHABWidget.WidgetType = .unknown
210-
}
211-
212214
extension OpenHABWidget {
213215
// This is an ugly initializer
214-
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?) {
216+
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?) {
215217
self.init()
216218
id = widgetId
217219
self.widgetId = widgetId
@@ -238,6 +240,7 @@ extension OpenHABWidget {
238240
self.state = state ?? ""
239241
self.text = text ?? ""
240242
self.legend = legend
243+
self.inputHint = inputHint ?? .text
241244
self.encoding = encoding ?? ""
242245
self.item = item
243246
self.linkedPage = linkedPage
@@ -275,6 +278,7 @@ public extension OpenHABWidget {
275278
let state: String?
276279
let text: String?
277280
let legend: Bool?
281+
let inputHint: InputHint?
278282
let encoding: String?
279283
let groupType: String?
280284
let item: OpenHABItem.CodingData?
@@ -291,7 +295,7 @@ extension OpenHABWidget.CodingData {
291295
var openHABWidget: OpenHABWidget {
292296
let mappedWidgets = widgets.map(\.openHABWidget)
293297
// swiftlint:disable:next line_length
294-
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)
298+
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)
295299
}
296300
}
297301

‎OpenHABCore/Sources/OpenHABCore/Util/StringExtension.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public extension String {
7777
return OpenHABItem.ItemType(rawValue: typeString)
7878
}
7979

80-
internal func toWidgetType() -> OpenHABWidget.WidgetType? {
80+
internal func toWidgetType() -> OpenHABWidget.WidgetType {
8181
OpenHABWidget.WidgetType(rawValue: self)
8282
}
8383

‎openHAB.xcodeproj/project.pbxproj

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
/* Begin PBXBuildFile section */
1010
1224F78F228A89FD00750965 /* WatchMessageService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1224F78D228A89FC00750965 /* WatchMessageService.swift */; };
11+
2F6412EE2CE494A80039FB28 /* DatePickerUITableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F6412ED2CE494A80039FB28 /* DatePickerUITableViewCell.swift */; };
12+
2FEFD8F62BE7C5BE00E387B9 /* TextInputUITableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2FEFD8F52BE7C5BE00E387B9 /* TextInputUITableViewCell.swift */; };
1113
4D6470DA2561F935007B03FC /* openHABIntents.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = 4D6470D32561F935007B03FC /* openHABIntents.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
1214
653B54C0285C0AC700298ECD /* OpenHABRootViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653B54BF285C0AC700298ECD /* OpenHABRootViewController.swift */; };
1315
653B54C2285E714900298ECD /* OpenHABViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653B54C1285E714900298ECD /* OpenHABViewController.swift */; };
@@ -269,6 +271,8 @@
269271

270272
/* Begin PBXFileReference section */
271273
1224F78D228A89FC00750965 /* WatchMessageService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WatchMessageService.swift; sourceTree = "<group>"; };
274+
2F6412ED2CE494A80039FB28 /* DatePickerUITableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatePickerUITableViewCell.swift; sourceTree = "<group>"; };
275+
2FEFD8F52BE7C5BE00E387B9 /* TextInputUITableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextInputUITableViewCell.swift; sourceTree = "<group>"; };
272276
4D38D951256897490039DA6E /* SetNumberValueIntentHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetNumberValueIntentHandler.swift; sourceTree = "<group>"; };
273277
4D38D959256897770039DA6E /* SetStringValueIntentHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetStringValueIntentHandler.swift; sourceTree = "<group>"; };
274278
4D38D9612568978E0039DA6E /* SetColorValueIntentHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetColorValueIntentHandler.swift; sourceTree = "<group>"; };
@@ -895,6 +899,7 @@
895899
DF4B84101886DA9900F34902 /* Widgets */ = {
896900
isa = PBXGroup;
897901
children = (
902+
2F6412ED2CE494A80039FB28 /* DatePickerUITableViewCell.swift */,
898903
DAF0A28E2C56F1EE00A14A6A /* ColorPickerCell.swift */,
899904
DF06F1FB18FEC2020011E7B9 /* ColorPickerViewController.swift */,
900905
DF4B84121886DAC400F34902 /* FrameUITableViewCell.swift */,
@@ -909,6 +914,7 @@
909914
DFA16EBA18883DE500EDB0BB /* SliderUITableViewCell.swift */,
910915
DA50C7BE2B0A652F0009F716 /* SliderWithSwitchSupportUITableViewCell.swift */,
911916
DFA13CB318872EBD006355C3 /* SwitchUITableViewCell.swift */,
917+
2FEFD8F52BE7C5BE00E387B9 /* TextInputUITableViewCell.swift */,
912918
DAA42BA921DC983B00244B2A /* VideoUITableViewCell.swift */,
913919
DAA42BAB21DC984A00244B2A /* WebUITableViewCell.swift */,
914920
DAEAA89C21E6B06300267EA3 /* ReusableView.swift */,
@@ -1544,6 +1550,7 @@
15441550
935B484625342B8E00E44CF0 /* URL+Static.swift in Sources */,
15451551
B7D5ECE121499E55001B0EC6 /* MapViewTableViewCell.swift in Sources */,
15461552
DA6B2EF52C89F8F200DF77CF /* ColorPickerView.swift in Sources */,
1553+
2F6412EE2CE494A80039FB28 /* DatePickerUITableViewCell.swift in Sources */,
15471554
DAA42BAA21DC983B00244B2A /* VideoUITableViewCell.swift in Sources */,
15481555
DFB2623B18830A3600D3244D /* AppDelegate.swift in Sources */,
15491556
DA6B2EF72C8B92E800DF77CF /* SelectionView.swift in Sources */,
@@ -1560,6 +1567,7 @@
15601567
938BF9D324EFD0B700E6B52F /* UIViewController+Localization.swift in Sources */,
15611568
DAA42BA821DC97E000244B2A /* NotificationTableViewCell.swift in Sources */,
15621569
DAF0A28F2C56F1EE00A14A6A /* ColorPickerCell.swift in Sources */,
1570+
2FEFD8F62BE7C5BE00E387B9 /* TextInputUITableViewCell.swift in Sources */,
15631571
938EDCE122C4FEB800661CA1 /* ScaleAspectFitImageView.swift in Sources */,
15641572
DAEAA89F21E6B16600267EA3 /* UITableView.swift in Sources */,
15651573
DFB2624418830A3600D3244D /* OpenHABSitemapViewController.swift in Sources */,
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) 2010-2024 Contributors to the openHAB project
2+
//
3+
// See the NOTICE file(s) distributed with this work for additional
4+
// information.
5+
//
6+
// This program and the accompanying materials are made available under the
7+
// terms of the Eclipse Public License 2.0 which is available at
8+
// http://www.eclipse.org/legal/epl-2.0
9+
//
10+
// SPDX-License-Identifier: EPL-2.0
11+
12+
import OpenHABCore
13+
import UIKit
14+
15+
class DatePickerUITableViewCell: GenericUITableViewCell {
16+
static let dateFormatter = {
17+
let dateFormatter = DateFormatter()
18+
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
19+
return dateFormatter
20+
}()
21+
22+
override var widget: OpenHABWidget! {
23+
get {
24+
super.widget
25+
}
26+
set(widget) {
27+
super.widget = widget
28+
switch widget.inputHint {
29+
case .date:
30+
datePicker.datePickerMode = .date
31+
case .time:
32+
datePicker.datePickerMode = .time
33+
case .datetime:
34+
datePicker.datePickerMode = .dateAndTime
35+
default:
36+
fatalError("Must not use this cell for input other than date and time")
37+
}
38+
guard let date = widget.item?.state else {
39+
datePicker.date = Date()
40+
return
41+
}
42+
datePicker.date = DateFormatter.iso8601Full.date(from: date) ?? Date.now
43+
}
44+
}
45+
46+
weak var controller: OpenHABSitemapViewController!
47+
48+
@IBOutlet private(set) var datePicker: UIDatePicker! {
49+
didSet {
50+
datePicker.addAction(UIAction { [weak self] _ in
51+
guard let self else { return }
52+
controller?.sendCommand(widget.item, commandToSend: DateFormatter.iso8601Full.string(from: datePicker.date))
53+
}, for: .valueChanged)
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)