LocationBasedServices/mowesta/pipeline/head There was a failure building this commit
217 lines
7.6 KiB
Swift
217 lines
7.6 KiB
Swift
//
|
|
// DeviceView.swift - The view to display the data of a single device.
|
|
//
|
|
// Created by Marcus Handte on 21.07.22.
|
|
//
|
|
|
|
import SwiftUI
|
|
import class MoWeStaApi.Device
|
|
import class MoWeStaApi.DefaultAPI
|
|
import class MoWeStaApi.Measurement
|
|
import LocosLabGeometry
|
|
|
|
struct DeviceView: View {
|
|
|
|
let device: Device
|
|
|
|
@ObservedObject var state = AppState.shared
|
|
@State var time = Int64(Date().timeIntervalSince1970 * 1000)
|
|
@State var measurements: [DeviceMeasurement]? = nil
|
|
@State var offset = 0
|
|
@State var total = 0
|
|
@State var loading = false
|
|
@State var failure = false
|
|
@State var measurement: DeviceMeasurement?
|
|
@State var showMeasurement = false
|
|
@State var showCustomize = false
|
|
@State var customizeTemperature = false
|
|
|
|
|
|
@ViewBuilder
|
|
func createTrailing() -> some View {
|
|
Button(action: {
|
|
loadNext()
|
|
}) {
|
|
Text("action_load_more")
|
|
.font(.body)
|
|
.rotationEffect(.degrees(-90))
|
|
.fixedSize()
|
|
.frame(width: 44, height: 100)
|
|
.foregroundColor(.accentColor)
|
|
}
|
|
}
|
|
|
|
var body: some View {
|
|
GeometryReader { proxy in
|
|
VStack(spacing: 0) {
|
|
if let ms = measurements {
|
|
ScrollView(showsIndicators: false) {
|
|
MeasurementView(measurements: ms, action: { measurement in
|
|
self.measurement = measurement
|
|
showMeasurement = true
|
|
}, trailing: (!loading && offset < total) ? createTrailing : nil )
|
|
.frame(maxWidth: .infinity, minHeight: 300, idealHeight: max(300, proxy.size.height))
|
|
}
|
|
}
|
|
if loading {
|
|
if measurements == nil {
|
|
ProgressSpinnerView()
|
|
.frame(maxWidth: .infinity)
|
|
} else {
|
|
Spacer()
|
|
ProgressBarView(animating: $loading)
|
|
.frame(maxHeight: 3)
|
|
}
|
|
}
|
|
if !loading && failure {
|
|
Spacer()
|
|
ErrorView() {
|
|
loadNext()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.navigationTitle(device.model)
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.onAppear {
|
|
if measurements == nil && !loading {
|
|
loadNext()
|
|
}
|
|
}
|
|
.sheet(isPresented: $showMeasurement) {
|
|
NavigationView() {
|
|
MeasurementDetailSheet(measurement: $measurement)
|
|
.navigationBarItems(trailing:
|
|
Button(action: {
|
|
showMeasurement.toggle()
|
|
}) {
|
|
Text("button_done")
|
|
}
|
|
)
|
|
}
|
|
}
|
|
.toolbar {
|
|
ToolbarItemGroup(placement: .navigationBarTrailing) {
|
|
Button(action: {
|
|
customizeTemperature = true
|
|
showCustomize = true
|
|
}) {
|
|
Image("icon_thermometer")
|
|
.accessibilityHint("action_temperature")
|
|
}
|
|
|
|
Button(action: {
|
|
customizeTemperature = false
|
|
showCustomize = true
|
|
}) {
|
|
Image("icon_barometer")
|
|
.accessibilityHint("action_system")
|
|
}
|
|
}
|
|
}
|
|
.actionSheet(isPresented: $showCustomize) {
|
|
if customizeTemperature {
|
|
return ActionSheet(title: Text("action_temperature"), message: nil, buttons: [
|
|
.default(Text("customize_temperature_celsius").foregroundColor(.accentColor)) {
|
|
state.temperatureUnit = .celsius
|
|
},
|
|
.default(Text("customize_temperature_fahrenheit")) {
|
|
state.temperatureUnit = .fahrenheit
|
|
},
|
|
.default(Text("customize_temperature_both")) {
|
|
state.temperatureUnit = .both
|
|
}
|
|
])
|
|
} else {
|
|
return ActionSheet(title: Text("action_system"), message: nil, buttons: [
|
|
.default(Text("customize_system_standard").foregroundColor(.accentColor)) {
|
|
state.unitSystem = .si
|
|
},
|
|
.default(Text("customize_system_imperial")) {
|
|
state.unitSystem = .imperial
|
|
},
|
|
.default(Text("customize_system_both")) {
|
|
state.unitSystem = .both
|
|
}
|
|
])
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public func loadNext() {
|
|
if loading {
|
|
return
|
|
}
|
|
if let credentials = state.credentials {
|
|
failure = false
|
|
loading = true
|
|
let rb = DefaultAPI.getDeviceMeasurementsWithRequestBuilder(id: device.id!, start: 0, duration: time, pageOffset: offset, pageSize: 150)
|
|
rb.addHeaders(ApiHeader.create(credentials))
|
|
rb.execute() {
|
|
result in
|
|
switch result {
|
|
case let .success(response):
|
|
if measurements == nil {
|
|
measurements = wrapMeasurements(response.body.elements);
|
|
offset = response.body.size
|
|
} else {
|
|
measurements!.append(contentsOf: wrapMeasurements(response.body.elements))
|
|
offset += response.body.size
|
|
}
|
|
total = response.body.total
|
|
case let .failure(.error(code, _, _, _)):
|
|
if code == 403 {
|
|
state.credentialFailure = true
|
|
}
|
|
failure = true
|
|
}
|
|
loading = false
|
|
}
|
|
} else {
|
|
state.credentialFailure = true
|
|
}
|
|
}
|
|
|
|
func wrapMeasurements(_ reports: [Measurement]) -> [DeviceMeasurement] {
|
|
var result: [DeviceMeasurement] = []
|
|
reports.forEach() { r in
|
|
result.append(DeviceMeasurement(measurement: r))
|
|
}
|
|
return result
|
|
}}
|
|
|
|
struct DeviceMeasurement: Measured {
|
|
|
|
let measurement: Measurement
|
|
var id: Int64 { get { return measurement.id! } }
|
|
var time: Int64 { get { return measurement.time } }
|
|
var humidity: Double? { get { return measurement.humidity }}
|
|
var pressure: Double? { get { return measurement.pressure }}
|
|
var temperature: Double? { get { return measurement.temperature }}
|
|
|
|
}
|
|
|
|
struct MeasurementDetailSheet: View {
|
|
|
|
let measurement: Binding<DeviceMeasurement?>
|
|
|
|
var body: some View {
|
|
ScrollView(showsIndicators: false) {
|
|
VStack {
|
|
MeasurementDetailView(measurement: measurement, action: nil)
|
|
.padding([.leading, .trailing, .top])
|
|
if let m = measurement.wrappedValue, let c = m.measurement.coordinate {
|
|
PlaceMapView(center: Coordinate(longitude: c.longitude, latitude: c.latitude) , zoom: 14, boundary: nil)
|
|
.frame(maxWidth: .infinity)
|
|
.frame(height: 200)
|
|
.clipShape(RoundedRectangle(cornerRadius: 6))
|
|
.padding([.leading, .trailing, .bottom])
|
|
}
|
|
}
|
|
}
|
|
.navigationBarTitle("label_measurement")
|
|
}
|
|
}
|