Skip to content

Commit

Permalink
Move extensions for models from APIActor.swift to respective files
Browse files Browse the repository at this point in the history
Port api load to DrawerView
  • Loading branch information
timbms committed Sep 15, 2024
1 parent b7f6397 commit c07b57e
Show file tree
Hide file tree
Showing 15 changed files with 353 additions and 422 deletions.
24 changes: 24 additions & 0 deletions OpenHABCore/Sources/OpenHABCore/Model/CGFloatExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 Foundation

extension CGFloat {
init(state string: String, divisor: Float) {
let numberFormatter = NumberFormatter()
numberFormatter.locale = Locale(identifier: "US")
if let number = numberFormatter.number(from: string) {
self.init(number.floatValue / divisor)
} else {
self.init(0)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,13 @@ extension OpenHABCommandDescription.CodingData {
OpenHABCommandDescription(commandOptions: commandOptions)
}
}

extension OpenHABCommandDescription {
convenience init?(_ commands: Components.Schemas.CommandDescription?) {
if let commands {
self.init(commandOptions: commands.commandOptions?.compactMap { OpenHABCommandOptions($0) })
} else {
return nil
}
}
}
10 changes: 10 additions & 0 deletions OpenHABCore/Sources/OpenHABCore/Model/OpenHABCommandOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ public class OpenHABCommandOptions: Decodable {
self.label = label
}
}

extension OpenHABCommandOptions {
convenience init?(_ options: Components.Schemas.CommandOption?) {
if let options {
self.init(command: options.command.orEmpty, label: options.label.orEmpty)
} else {
return nil
}
}
}
13 changes: 6 additions & 7 deletions OpenHABCore/Sources/OpenHABCore/Model/OpenHABItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,13 @@ public extension OpenHABItem.CodingData {
}
}

extension CGFloat {
init(state string: String, divisor: Float) {
let numberFormatter = NumberFormatter()
numberFormatter.locale = Locale(identifier: "US")
if let number = numberFormatter.number(from: string) {
self.init(number.floatValue / divisor)
extension OpenHABItem {
convenience init?(_ item: Components.Schemas.EnrichedItemDTO?) {
if let item {
// swiftlint:disable:next line_length
self.init(name: item.name.orEmpty, type: item._type.orEmpty, state: item.state.orEmpty, link: item.link.orEmpty, label: item.label.orEmpty, groupType: nil, stateDescription: OpenHABStateDescription(item.stateDescription), commandDescription: OpenHABCommandDescription(item.commandDescription), members: [], category: item.category, options: [])
} else {
self.init(0)
return nil
}
}
}
10 changes: 10 additions & 0 deletions OpenHABCore/Sources/OpenHABCore/Model/OpenHABOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ public class OpenHABOptions: Decodable {
self.label = label
}
}

extension OpenHABOptions {
convenience init?(_ options: Components.Schemas.StateOption?) {
if let options {
self.init(value: options.value.orEmpty, label: options.label.orEmpty)
} else {
return nil
}
}
}
17 changes: 17 additions & 0 deletions OpenHABCore/Sources/OpenHABCore/Model/OpenHABPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,20 @@ public extension OpenHABPage.CodingData {
return OpenHABPage(pageId: pageId.orEmpty, title: title.orEmpty, link: link.orEmpty, leaf: leaf ?? false, widgets: mappedWidgets, icon: icon.orEmpty)
}
}

extension OpenHABPage {
convenience init?(_ page: Components.Schemas.PageDTO?) {
if let page {
self.init(
pageId: page.id.orEmpty,
title: page.title.orEmpty,
link: page.link.orEmpty,
leaf: page.leaf ?? false,
widgets: page.widgets?.compactMap { OpenHABWidget($0) } ?? [],
icon: page.icon.orEmpty
)
} else {
return nil
}
}
}
44 changes: 8 additions & 36 deletions OpenHABCore/Sources/OpenHABCore/Model/OpenHABSitemap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,42 +51,14 @@ public final class OpenHABSitemap: NSObject {
}
}

public extension OpenHABSitemap {
struct CodingData: Decodable {
public let name: String
public let label: String
public let page: OpenHABPage.CodingData?
public let link: String
public let icon: String?

private enum CodingKeys: String, CodingKey {
case page = "homepage"
case name
case label
case link
case icon
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

name = try container.decode(forKey: .name)
label = try container.decode(forKey: .label, default: name)
page = try container.decode(forKey: .page)
link = try container.decode(forKey: .link)
icon = try container.decodeIfPresent(forKey: .icon)
}
}
}

public extension OpenHABSitemap.CodingData {
var openHABSitemap: OpenHABSitemap {
OpenHABSitemap(
name: name,
icon: icon.orEmpty,
label: label,
link: link,
page: page?.openHABSitemapPage
extension OpenHABSitemap {
convenience init(_ sitemap: Components.Schemas.SitemapDTO) {
self.init(
name: sitemap.name.orEmpty,
icon: sitemap.icon.orEmpty,
label: sitemap.label.orEmpty,
link: sitemap.link.orEmpty,
page: OpenHABPage(sitemap.homepage)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// 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 Foundation

public class OpenHABSitemapWidgetEvent {
var sitemapName: String?
var pageId: String?
var widgetId: String?
var label: String?
var labelSource: String?
var icon: String?
var reloadIcon: Bool?
var labelcolor: String?
var valuecolor: String?
var iconcolor: String?
var visibility: Bool?
var state: String?
var enrichedItem: OpenHABItem?
var descriptionChanged: Bool?

init(sitemapName: String? = nil, pageId: String? = nil, widgetId: String? = nil, label: String? = nil, labelSource: String? = nil, icon: String? = nil, reloadIcon: Bool? = nil, labelcolor: String? = nil, valuecolor: String? = nil, iconcolor: String? = nil, visibility: Bool? = nil, state: String? = nil, enrichedItem: OpenHABItem? = nil, descriptionChanged: Bool? = nil) {
self.sitemapName = sitemapName
self.pageId = pageId
self.widgetId = widgetId
self.label = label
self.labelSource = labelSource
self.icon = icon
self.reloadIcon = reloadIcon
self.labelcolor = labelcolor
self.valuecolor = valuecolor
self.iconcolor = iconcolor
self.visibility = visibility
self.state = state
self.enrichedItem = enrichedItem
self.descriptionChanged = descriptionChanged
}

convenience init?(_ event: Components.Schemas.SitemapWidgetEvent?) {
guard let event else { return nil }
// swiftlint:disable:next line_length
self.init(sitemapName: event.sitemapName, pageId: event.pageId, widgetId: event.widgetId, label: event.label, labelSource: event.labelSource, icon: event.icon, reloadIcon: event.reloadIcon, labelcolor: event.labelcolor, valuecolor: event.valuecolor, iconcolor: event.iconcolor, visibility: event.visibility, state: event.state, enrichedItem: OpenHABItem(event.item), descriptionChanged: event.descriptionChanged)
}
}

extension OpenHABSitemapWidgetEvent: CustomStringConvertible {
public var description: String {
"\(widgetId ?? "") \(label ?? "") \(enrichedItem?.state ?? "")"
}
}

public extension OpenHABSitemapWidgetEvent {
struct CodingData: Decodable, Hashable, Equatable {
public static func == (lhs: OpenHABSitemapWidgetEvent.CodingData, rhs: OpenHABSitemapWidgetEvent.CodingData) -> Bool {
lhs.widgetId == rhs.widgetId
}

public func hash(into hasher: inout Hasher) {
hasher.combine(widgetId)
}

var sitemapName: String?
var pageId: String?
var widgetId: String?
var label: String?
var labelSource: String?
var icon: String?
var reloadIcon: Bool?
var labelcolor: String?
var valuecolor: String?
var iconcolor: String?
var visibility: Bool?
// var state: String?
var item: OpenHABItem.CodingData?
var descriptionChanged: Bool?
var link: String?
}
}

extension OpenHABSitemapWidgetEvent.CodingData {
var openHABSitemapWidgetEvent: OpenHABSitemapWidgetEvent {
// swiftlint:disable:next line_length
OpenHABSitemapWidgetEvent(sitemapName: sitemapName, pageId: pageId, widgetId: widgetId, label: label, labelSource: labelSource, icon: icon, reloadIcon: reloadIcon, labelcolor: labelcolor, valuecolor: valuecolor, iconcolor: iconcolor, visibility: visibility, enrichedItem: item?.openHABItem, descriptionChanged: descriptionChanged)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,13 @@ extension OpenHABStateDescription.CodingData {
OpenHABStateDescription(minimum: minimum, maximum: maximum, step: step, readOnly: readOnly, options: options, pattern: pattern)
}
}

extension OpenHABStateDescription {
convenience init?(_ state: Components.Schemas.StateDescription?) {
if let state {
self.init(minimum: state.minimum, maximum: state.maximum, step: state.step, readOnly: state.readOnly, options: state.options?.compactMap { OpenHABOptions($0) }, pattern: state.pattern)
} else {
return nil
}
}
}
10 changes: 4 additions & 6 deletions OpenHABCore/Sources/OpenHABCore/Model/OpenHABUiTile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import Foundation

public class OpenHABUiTile: Decodable {
public class OpenHABUiTile {
public var name = ""
public var url = ""
public var imageUrl = ""
Expand All @@ -23,10 +23,8 @@ public class OpenHABUiTile: Decodable {
}
}

public extension OpenHABUiTile {
struct CodingData: Decodable {
public let name: String
public let url: String
public let imageUrl: String
extension OpenHABUiTile {
convenience init(_ tile: Components.Schemas.TileDTO) {
self.init(name: tile.name.orEmpty, url: tile.url.orEmpty, imageUrl: tile.imageUrl.orEmpty)
}
}
53 changes: 53 additions & 0 deletions OpenHABCore/Sources/OpenHABCore/Model/OpenHABWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,56 @@ extension [OpenHABWidget] {
}
}
}

extension OpenHABWidget {
convenience init(_ widget: Components.Schemas.WidgetDTO) {
self.init(
widgetId: widget.widgetId.orEmpty,
label: widget.label.orEmpty,
icon: widget.icon.orEmpty,
type: OpenHABWidget.WidgetType(rawValue: widget._type!),
url: widget.url,
period: widget.period,
minValue: widget.minValue,
maxValue: widget.maxValue,
step: widget.step,
refresh: widget.refresh.map(Int.init),
height: 50, // TODO:
isLeaf: true,
iconColor: widget.iconcolor,
labelColor: widget.labelcolor,
valueColor: widget.valuecolor,
service: widget.service,
state: widget.state,
text: "",
legend: widget.legend,
encoding: widget.encoding,
item: OpenHABItem(widget.item),
linkedPage: OpenHABPage(widget.linkedPage),
mappings: widget.mappings?.compactMap(OpenHABWidgetMapping.init) ?? [],
widgets: widget.widgets?.compactMap { OpenHABWidget($0) } ?? [],
visibility: widget.visibility,
switchSupport: widget.switchSupport,
forceAsItem: widget.forceAsItem
)
}
}

extension OpenHABWidget {
func update(with event: OpenHABSitemapWidgetEvent) {
state = event.state ?? state
icon = event.icon ?? icon
label = event.label ?? label
iconColor = event.iconcolor ?? ""
labelcolor = event.labelcolor ?? ""
valuecolor = event.valuecolor ?? ""
visibility = event.visibility ?? visibility

if let enrichedItem = event.enrichedItem {
if let link = item?.link {
enrichedItem.link = link
}
item = enrichedItem
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ public class OpenHABWidgetMapping: NSObject, Decodable {
self.label = label.orEmpty
}
}

extension OpenHABWidgetMapping {
convenience init(_ mapping: Components.Schemas.MappingDTO) {
self.init(command: mapping.command, label: mapping.label)
}
}
Loading

0 comments on commit c07b57e

Please sign in to comment.