mirror of
https://github.com/meshtastic/Meshtastic-Apple.git
synced 2026-04-20 22:13:56 +00:00
Initial Project Commit
This commit is contained in:
parent
ff017aab5e
commit
8be3ac2bba
23 changed files with 898 additions and 166 deletions
37
MeshtasticClient/Model/ModelData.swift
Normal file
37
MeshtasticClient/Model/ModelData.swift
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
Abstract:
|
||||
Storage for model data.
|
||||
*/
|
||||
|
||||
import Foundation
|
||||
import Combine
|
||||
|
||||
final class ModelData: ObservableObject {
|
||||
@Published var devices: [Device] = load("deviceData.json")
|
||||
|
||||
var nearby: [Device] {
|
||||
devices
|
||||
}
|
||||
}
|
||||
|
||||
func load<T: Decodable>(_ filename: String) -> T {
|
||||
let data: Data
|
||||
|
||||
guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
|
||||
else {
|
||||
fatalError("Couldn't find \(filename) in main bundle.")
|
||||
}
|
||||
|
||||
do {
|
||||
data = try Data(contentsOf: file)
|
||||
} catch {
|
||||
fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
|
||||
}
|
||||
|
||||
do {
|
||||
let decoder = JSONDecoder()
|
||||
return try decoder.decode(T.self, from: data)
|
||||
} catch {
|
||||
fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue