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 for widgets #742

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/pull_requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
with:
xcode-version: latest-stable

- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install dependencies
run: |
Expand All @@ -25,6 +25,6 @@ jobs:
LANG: en_US.UTF-8
LC_ALL: en_US.UTF-8
FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT: 60
uses: maierj/fastlane-action@v2.3.0
uses: maierj/fastlane-action@v3.0.0
with:
lane: unittests
2 changes: 1 addition & 1 deletion OpenHABCore/Sources/OpenHABCore/Model/OpenHABItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public extension OpenHABItem {
public extension OpenHABItem.CodingData {
var openHABItem: OpenHABItem {
let mappedMembers = members?.map(\.openHABItem) ?? []

// swiftlint:disable:next line_length
return OpenHABItem(name: name, type: type, state: state, link: link, label: label, groupType: groupType, stateDescription: stateDescription?.openHABStateDescription, commandDescription: commandDescription?.openHABCommandDescription, members: mappedMembers, category: category, options: options)
}
}
Expand Down
73 changes: 73 additions & 0 deletions OpenHABCore/Sources/OpenHABCore/Util/OpenHABItemCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public class OpenHABItemCache {
var lastUrlConnected = URL_NONE
var lastLoad = Date().timeIntervalSince1970

@available(iOS 17.0, macOS 14.0, watchOS 10.0, *)
public func getItemNames(searchTerm: String?, types: [OpenHABItem.ItemType]?) async -> [String] {
await withCheckedContinuation { continuation in
getItemNames(searchTerm: searchTerm, types: types) { result in
continuation.resume(returning: result.map { String($0) })
}
}
}

public func getItemNames(searchTerm: String?, types: [OpenHABItem.ItemType]?, completion: @escaping ([NSString]) -> Void) {
var ret = [NSString]()

Expand All @@ -45,6 +54,24 @@ public class OpenHABItemCache {
completion(ret)
}

@available(iOS 17.0, macOS 14.0, watchOS 10.0, *)
public func getItems(types: [OpenHABItem.ItemType]?) async -> [OpenHABItem] {
guard let items else {
items = await reload(searchTerm: nil, types: types)
return []
}
return items.filter { (types == nil || ($0.type != nil && types!.contains($0.type!))) }.sorted(by: \.name)
}

@available(iOS 17.0, macOS 14.0, watchOS 10.0, *)
public func getItem(name: String) async -> OpenHABItem? {
await withCheckedContinuation { continuation in
getItem(name: name) { result in
continuation.resume(returning: result)
}
}
}

@available(iOS 12.0, *)
public func getItem(name: String, completion: @escaping (OpenHABItem?) -> Void) {
let now = Date().timeIntervalSince1970
Expand All @@ -70,6 +97,52 @@ public class OpenHABItemCache {
commandOperation?.resume()
}

@available(iOS 17.0, macOS 14.0, watchOS 10.0, *)
public func reload(searchTerm: String?, types: [OpenHABItem.ItemType]?) async -> [OpenHABItem] {
await withCheckedContinuation { continuation in
reload(searchTerm: searchTerm, types: types) { result in
continuation.resume(returning: result)
}
}
}

@available(iOS 12.0, *)
public func reload(searchTerm: String?, types: [OpenHABItem.ItemType]?, completion: @escaping ([OpenHABItem]) -> Void) {
lastLoad = Date().timeIntervalSince1970

guard let uurl = getURL() else { return }

os_log("Loading items from %{PUBLIC}@", log: .default, type: .info, url)

if NetworkConnection.shared == nil {
NetworkConnection.initialize(ignoreSSL: Preferences.ignoreSSL, interceptor: nil)
}

NetworkConnection.load(from: uurl, timeout: timeout) { response in
switch response.result {
case let .success(data):
do {
try self.decodeItemsData(data)

let ret = self.items?.filter { (searchTerm == nil || $0.name.contains(searchTerm.orEmpty)) && (types == nil || ($0.type != nil && types!.contains($0.type!))) }.sorted(by: \.name) ?? []

completion(ret)
} catch {
os_log("%{PUBLIC}@ ", log: .default, type: .error, error.localizedDescription)
}
case let .failure(error):
if self.lastUrlConnected == OpenHABItemCache.URL_LOCAL {
self.localUrlFailed = true
os_log("%{PUBLIC}@ ", log: .default, type: .info, error.localizedDescription)
self.reload(searchTerm: searchTerm, types: types, completion: completion) // try remote

} else {
os_log("%{PUBLIC}@ ", log: .default, type: .error, error.localizedDescription)
}
}
}
}

@available(iOS 12.0, *)
public func reload(searchTerm: String?, types: [OpenHABItem.ItemType]?, completion: @escaping ([NSString]) -> Void) {
lastLoad = Date().timeIntervalSince1970
Expand Down
8 changes: 4 additions & 4 deletions OpenHABCore/Tests/OpenHABCoreTests/RESTAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ final class RESTAPITests: XCTestCase {
registrationOperation.resume()

// then
wait(for: [expectation], timeout: 3)
wait(for: [expectation], timeout: 6)
}

func testRegisterApp() {
Expand All @@ -77,7 +77,7 @@ final class RESTAPITests: XCTestCase {
expectation.fulfill()
}
// then
wait(for: [expectation], timeout: 3)
wait(for: [expectation], timeout: 6)
}

func testSitemap() {
Expand All @@ -92,7 +92,7 @@ final class RESTAPITests: XCTestCase {
expectation.fulfill()
}
// then
wait(for: [expectation], timeout: 3)
wait(for: [expectation], timeout: 6)
}

func testTracker() {
Expand All @@ -107,6 +107,6 @@ final class RESTAPITests: XCTestCase {
expectation.fulfill()
}
// then
wait(for: [expectation], timeout: 3)
wait(for: [expectation], timeout: 6)
}
}
Loading
Loading