Skip to content

Commit

Permalink
Migrate widgets from [OpenHABWidget] to OrderedDictionary<String, Ope…
Browse files Browse the repository at this point in the history
…nHABWidget> using swift-collections
  • Loading branch information
timbms committed Aug 28, 2024
1 parent f067910 commit dba903f
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 277 deletions.
6 changes: 4 additions & 2 deletions OpenHABCore/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ let package = Package(
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.0.0"),
.package(url: "https://github.com/onevcat/Kingfisher.git", from: "7.0.0")
.package(url: "https://github.com/onevcat/Kingfisher.git", from: "7.0.0"),
.package(url: "https://github.com/apple/swift-collections.git", .upToNextMajor(from: "1.1.0"))
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand All @@ -25,7 +26,8 @@ let package = Package(
name: "OpenHABCore",
dependencies: [
.product(name: "Alamofire", package: "Alamofire", condition: .when(platforms: [.iOS, .watchOS])),
.product(name: "Kingfisher", package: "Kingfisher", condition: .when(platforms: [.iOS, .watchOS]))
.product(name: "Kingfisher", package: "Kingfisher", condition: .when(platforms: [.iOS, .watchOS])),
.product(name: "Collections", package: "swift-collections")
]
),
.testTarget(
Expand Down
32 changes: 27 additions & 5 deletions OpenHABCore/Sources/OpenHABCore/Model/OpenHABSitemapPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,36 @@
//
// SPDX-License-Identifier: EPL-2.0

import Collections
import Foundation
import os.log

public class OpenHABSitemapPage: NSObject {
public class OpenHABSitemapPage: NSObject, ObservableObject {
public var sendCommand: ((_ item: OpenHABItem, _ command: String?) -> Void)?
public var widgets: [OpenHABWidget] = []
public var widgets = OrderedDictionary<String, OpenHABWidget>()
public var pageId = ""
public var title = ""
public var link = ""
public var leaf = false
public var icon = ""

public init(pageId: String, title: String, link: String, leaf: Bool, widgets: OrderedDictionary<String, OpenHABWidget>, icon: String) {
super.init()
self.pageId = pageId
self.title = title
self.link = link
self.leaf = leaf

self.widgets = widgets

for (_, widget) in self.widgets {
widget.sendCommand = { [weak self] item, command in
self?.sendCommand(item, commandToSend: command)
}
}
self.icon = icon
}

public init(pageId: String, title: String, link: String, leaf: Bool, widgets: [OpenHABWidget], icon: String) {
super.init()
self.pageId = pageId
Expand All @@ -29,8 +47,12 @@ public class OpenHABSitemapPage: NSObject {
self.leaf = leaf
var tempWidgets = [OpenHABWidget]()
tempWidgets.flatten(widgets)
self.widgets = tempWidgets
for widget in self.widgets {

self.widgets = OrderedDictionary(
uniqueKeysWithValues: tempWidgets.map { ($0.id, $0) }
)

for (_, widget) in self.widgets {
widget.sendCommand = { [weak self] item, command in
self?.sendCommand(item, commandToSend: command)
}
Expand All @@ -47,7 +69,7 @@ public class OpenHABSitemapPage: NSObject {
}

public extension OpenHABSitemapPage {
func filter(_ isIncluded: (OpenHABWidget) throws -> Bool) rethrows -> OpenHABSitemapPage {
func filter(_ isIncluded: (String, OpenHABWidget) throws -> Bool) rethrows -> OpenHABSitemapPage {
let filteredOpenHABSitemapPage = try OpenHABSitemapPage(
pageId: pageId,
title: title,
Expand Down
Loading

0 comments on commit dba903f

Please sign in to comment.