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

Support input element (Issue #726) #755

Merged
merged 13 commits into from
Nov 16, 2024
Merged
24 changes: 14 additions & 10 deletions OpenHABCore/Sources/OpenHABCore/Model/OpenHABWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ 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"
case frame = "Frame"
case group = "Group"
case image = "Image"
case input = "Input"
case mapview = "Mapview"
case selection = "Selection"
case setpoint = "Setpoint"
Expand All @@ -66,13 +68,18 @@ 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)?
public var widgetId = ""
public var label = ""
public var icon = ""
public var type: WidgetType?
public var type: WidgetType = .unknownCase
public var url = ""
public var period = ""
public var minValue = 0.0
Expand All @@ -88,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 @@ -203,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 @@ -238,6 +240,7 @@ extension OpenHABWidget {
self.state = state ?? ""
self.text = text ?? ""
self.legend = legend
self.inputHint = inputHint ?? .text
self.encoding = encoding ?? ""
self.item = item
self.linkedPage = linkedPage
Expand Down Expand Up @@ -275,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 @@ -291,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
2 changes: 1 addition & 1 deletion OpenHABCore/Sources/OpenHABCore/Util/StringExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public extension String {
return OpenHABItem.ItemType(rawValue: typeString)
}

internal func toWidgetType() -> OpenHABWidget.WidgetType? {
internal func toWidgetType() -> OpenHABWidget.WidgetType {
OpenHABWidget.WidgetType(rawValue: self)
}

Expand Down
8 changes: 8 additions & 0 deletions openHAB.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

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

/* Begin PBXFileReference section */
1224F78D228A89FC00750965 /* WatchMessageService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WatchMessageService.swift; sourceTree = "<group>"; };
2F6412ED2CE494A80039FB28 /* DatePickerUITableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatePickerUITableViewCell.swift; sourceTree = "<group>"; };
2FEFD8F52BE7C5BE00E387B9 /* TextInputUITableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextInputUITableViewCell.swift; sourceTree = "<group>"; };
4D38D951256897490039DA6E /* SetNumberValueIntentHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetNumberValueIntentHandler.swift; sourceTree = "<group>"; };
4D38D959256897770039DA6E /* SetStringValueIntentHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetStringValueIntentHandler.swift; sourceTree = "<group>"; };
4D38D9612568978E0039DA6E /* SetColorValueIntentHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetColorValueIntentHandler.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -895,6 +899,7 @@
DF4B84101886DA9900F34902 /* Widgets */ = {
isa = PBXGroup;
children = (
2F6412ED2CE494A80039FB28 /* DatePickerUITableViewCell.swift */,
DAF0A28E2C56F1EE00A14A6A /* ColorPickerCell.swift */,
DF06F1FB18FEC2020011E7B9 /* ColorPickerViewController.swift */,
DF4B84121886DAC400F34902 /* FrameUITableViewCell.swift */,
Expand All @@ -909,6 +914,7 @@
DFA16EBA18883DE500EDB0BB /* SliderUITableViewCell.swift */,
DA50C7BE2B0A652F0009F716 /* SliderWithSwitchSupportUITableViewCell.swift */,
DFA13CB318872EBD006355C3 /* SwitchUITableViewCell.swift */,
2FEFD8F52BE7C5BE00E387B9 /* TextInputUITableViewCell.swift */,
DAA42BA921DC983B00244B2A /* VideoUITableViewCell.swift */,
DAA42BAB21DC984A00244B2A /* WebUITableViewCell.swift */,
DAEAA89C21E6B06300267EA3 /* ReusableView.swift */,
Expand Down Expand Up @@ -1544,6 +1550,7 @@
935B484625342B8E00E44CF0 /* URL+Static.swift in Sources */,
B7D5ECE121499E55001B0EC6 /* MapViewTableViewCell.swift in Sources */,
DA6B2EF52C89F8F200DF77CF /* ColorPickerView.swift in Sources */,
2F6412EE2CE494A80039FB28 /* DatePickerUITableViewCell.swift in Sources */,
DAA42BAA21DC983B00244B2A /* VideoUITableViewCell.swift in Sources */,
DFB2623B18830A3600D3244D /* AppDelegate.swift in Sources */,
DA6B2EF72C8B92E800DF77CF /* SelectionView.swift in Sources */,
Expand All @@ -1560,6 +1567,7 @@
938BF9D324EFD0B700E6B52F /* UIViewController+Localization.swift in Sources */,
DAA42BA821DC97E000244B2A /* NotificationTableViewCell.swift in Sources */,
DAF0A28F2C56F1EE00A14A6A /* ColorPickerCell.swift in Sources */,
2FEFD8F62BE7C5BE00E387B9 /* TextInputUITableViewCell.swift in Sources */,
938EDCE122C4FEB800661CA1 /* ScaleAspectFitImageView.swift in Sources */,
DAEAA89F21E6B16600267EA3 /* UITableView.swift in Sources */,
DFB2624418830A3600D3244D /* OpenHABSitemapViewController.swift in Sources */,
Expand Down
56 changes: 56 additions & 0 deletions openHAB/DatePickerUITableViewCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2010-2024 Contributors to the openHAB project
//
// See the NOTICE file(s) distributed with this work for additional
// information.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0
//
// SPDX-License-Identifier: EPL-2.0

import OpenHABCore
import UIKit

class DatePickerUITableViewCell: GenericUITableViewCell {
static let dateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return dateFormatter
}()

override var widget: OpenHABWidget! {
get {
super.widget
}
set(widget) {
super.widget = widget
switch widget.inputHint {
case .date:
datePicker.datePickerMode = .date
case .time:
datePicker.datePickerMode = .time
case .datetime:
datePicker.datePickerMode = .dateAndTime
default:
fatalError("Must not use this cell for input other than date and time")
}
guard let date = widget.item?.state else {
datePicker.date = Date()
return
}
datePicker.date = DateFormatter.iso8601Full.date(from: date) ?? Date.now
}
}

weak var controller: OpenHABSitemapViewController!

@IBOutlet private(set) var datePicker: UIDatePicker! {
didSet {
datePicker.addAction(UIAction { [weak self] _ in
guard let self else { return }
controller?.sendCommand(widget.item, commandToSend: DateFormatter.iso8601Full.string(from: datePicker.date))
}, for: .valueChanged)
}
}
}
Loading