initial commit
This commit is contained in:
commit
8de962eeb8
44 changed files with 4843 additions and 0 deletions
36
OLTListViewModel.swift
Normal file
36
OLTListViewModel.swift
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import Foundation
|
||||
import Observation
|
||||
|
||||
@MainActor
|
||||
@Observable
|
||||
final class OLTListViewModel {
|
||||
var phase: LoadPhase = .idle
|
||||
var olts: [OLTStateDoc] = []
|
||||
var search = ""
|
||||
|
||||
private let connection: MCMSConnection
|
||||
init(connection: MCMSConnection) { self.connection = connection }
|
||||
|
||||
var filtered: [OLTStateDoc] {
|
||||
let query = search.trimmingCharacters(in: .whitespaces)
|
||||
guard !query.isEmpty else { return olts }
|
||||
return olts.filter {
|
||||
$0.id.localizedCaseInsensitiveContains(query)
|
||||
|| ($0.resolvedName?.localizedCaseInsensitiveContains(query) ?? false)
|
||||
}
|
||||
}
|
||||
|
||||
func load() async {
|
||||
phase = .loading
|
||||
do {
|
||||
let fetched = try await connection.olt.allStates()
|
||||
let names = (try? await connection.olt.nameMap()) ?? [:]
|
||||
olts = fetched
|
||||
.map { olt in var olt = olt; olt.resolvedName = names[olt.id]; return olt }
|
||||
.sorted { $0.displayName.localizedCaseInsensitiveCompare($1.displayName) == .orderedAscending }
|
||||
phase = .loaded
|
||||
} catch {
|
||||
phase = .failed((error as? APIError)?.localizedDescription ?? error.localizedDescription)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue