mirror of
https://github.com/home-assistant/iOS.git
synced 2026-02-19 17:31:56 -06:00
<!-- Thank you for submitting a Pull Request and helping to improve Home Assistant. Please complete the following sections to help the processing and review of your changes. Please do not delete anything from this template. --> ## Summary <!-- Provide a brief summary of the changes you have made and most importantly what they aim to achieve --> ## Screenshots <!-- If this is a user-facing change not in the frontend, please include screenshots in light and dark mode. --> ## Link to pull request in Documentation repository <!-- Pull requests that add, change or remove functionality must have a corresponding pull request in the Companion App Documentation repository (https://github.com/home-assistant/companion.home-assistant). Please add the number of this pull request after the "#" --> Documentation: home-assistant/companion.home-assistant# ## Any other notes <!-- If there is any other information of note, like if this Pull Request is part of a bigger change, please include it here. -->
31 lines
1.0 KiB
Swift
31 lines
1.0 KiB
Swift
import Foundation
|
|
import PromiseKit
|
|
import RealmSwift
|
|
|
|
public struct ClientEventStore {
|
|
public var addEvent: (ClientEvent) -> Promise<Void> = { event in
|
|
let realm = Current.realm()
|
|
Current.Log.info("ClientEventStore - \(event.type): \(event.text) \(event.jsonPayload ?? [:])")
|
|
return realm.reentrantWrite {
|
|
realm.add(event)
|
|
}
|
|
}
|
|
|
|
public func getEvents(filter: String? = nil) -> AnyRealmCollection<ClientEvent> {
|
|
let realm = Current.realm()
|
|
let objects = realm.objects(ClientEvent.self).sorted(byKeyPath: "date", ascending: false)
|
|
if let filter, filter.isEmpty == false {
|
|
return AnyRealmCollection(objects.filter(NSPredicate(format: "text contains[c] %@", filter)))
|
|
} else {
|
|
return AnyRealmCollection(objects)
|
|
}
|
|
}
|
|
|
|
public var clearAllEvents: () -> Promise<Void> = {
|
|
let realm = Current.realm()
|
|
return realm.reentrantWrite {
|
|
realm.delete(realm.objects(ClientEvent.self))
|
|
}
|
|
}
|
|
}
|