146 lines
4.5 KiB
Swift
146 lines
4.5 KiB
Swift
//
|
|
// DevicesView.swift - The view with the list of the user's devices.
|
|
//
|
|
// Created by Marcus Handte on 21.07.22.
|
|
//
|
|
|
|
import SwiftUI
|
|
import class MoWeStaApi.Device
|
|
import class MoWeStaApi.DefaultAPI
|
|
|
|
struct DevicesView: View {
|
|
|
|
@State var devices: [Device]? = nil
|
|
@State var loading = false
|
|
@State var failure = false
|
|
@ObservedObject var state = AppState.shared
|
|
|
|
|
|
var body: some View {
|
|
VStack(spacing: 0) {
|
|
if let devs = devices {
|
|
ZStack {
|
|
List(devs, id: \Device.id) { device in
|
|
NavigationLink(destination: {
|
|
DeviceView(device: device)
|
|
}) {
|
|
DeviceEntryView(device: device)
|
|
}
|
|
}.listStyle(PlainListStyle())
|
|
VStack {
|
|
Spacer()
|
|
HStack {
|
|
Spacer()
|
|
NavigationLink(destination: {
|
|
SetupView()
|
|
}, label: {
|
|
Image(systemName: "plus")
|
|
.font(.title)
|
|
.frame(width: 60, height: 60)
|
|
.foregroundColor(Color.black)
|
|
.background(
|
|
Circle()
|
|
.fill(Color.accentColor)
|
|
.shadow(color: .gray, radius: 5)
|
|
)
|
|
|
|
})
|
|
.padding()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if loading {
|
|
Spacer()
|
|
ProgressBarView(animating: $loading)
|
|
.frame(height: 3)
|
|
}
|
|
if !loading && failure {
|
|
Spacer()
|
|
ErrorView() {
|
|
load()
|
|
}
|
|
}
|
|
}.onAppear() {
|
|
if devices == nil {
|
|
load()
|
|
}
|
|
}.navigationTitle("navigation_devices")
|
|
}
|
|
|
|
func load() {
|
|
if loading {
|
|
return
|
|
}
|
|
if let credential = state.credentials {
|
|
loading = true
|
|
failure = false
|
|
devices = nil
|
|
next(credential: credential, offset: 0)
|
|
}
|
|
}
|
|
|
|
func next(credential: UserCredentials, offset: Int) {
|
|
let rb = DefaultAPI.getCurrentUserDevicesWithRequestBuilder(pageOffset: offset, pageSize: 50)
|
|
rb.addHeaders(ApiHeader.create(credential))
|
|
rb.execute() {
|
|
response in
|
|
switch response {
|
|
case let .success(result):
|
|
if devices == nil {
|
|
devices = result.body.elements
|
|
} else {
|
|
devices!.append(contentsOf: result.body.elements)
|
|
}
|
|
let current = result.body.offset + result.body.size
|
|
if current > result.body.total {
|
|
loading = false
|
|
} else {
|
|
next(credential: credential, offset: current)
|
|
}
|
|
case let .failure(.error(code, _, _, _)):
|
|
if (code == 403) {
|
|
state.credentialFailure = true
|
|
}
|
|
failure = true
|
|
loading = false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct DeviceEntryView: View {
|
|
|
|
let device: Device
|
|
|
|
var body: some View {
|
|
HStack {
|
|
Image(ImageBindings.getDeviceIcon(device.os))
|
|
.resizable()
|
|
.aspectRatio(contentMode: .fit)
|
|
.frame(width: 40, height: 36)
|
|
.padding([.leading, .trailing], 10)
|
|
.accessibilityHint(TextBindings.getDeviceOs(device.os))
|
|
VStack(alignment: .leading, spacing: 0) {
|
|
Text(device.model)
|
|
.font(.body)
|
|
.multilineTextAlignment(.leading)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
Text("format_title \(device.manufacturer) \(device.device)")
|
|
.font(.caption)
|
|
.multilineTextAlignment(.leading)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
struct DevicesView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
DevicesView()
|
|
}
|
|
}
|