2021-12-27 22:33:27 +08:00
|
|
|
//
|
|
|
|
// DeviceListView.swift
|
|
|
|
// PushDeer
|
|
|
|
//
|
|
|
|
// Created by HEXT on 2021/12/25.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
/// 设备界面
|
|
|
|
struct DeviceListView: View {
|
2022-01-04 22:28:18 +08:00
|
|
|
@EnvironmentObject private var store: AppState
|
2021-12-27 22:33:27 +08:00
|
|
|
var body: some View {
|
|
|
|
BaseNavigationView(title: "设备") {
|
|
|
|
ScrollView {
|
|
|
|
LazyVStack(alignment: .center) {
|
2022-01-10 00:26:29 +08:00
|
|
|
ForEach(store.devices.reversed()) { deviceItem in
|
2021-12-27 22:33:27 +08:00
|
|
|
DeletableView(contentView: {
|
2022-01-10 00:26:29 +08:00
|
|
|
DeviceItemView(name: getName(deviceItem: deviceItem))
|
2021-12-27 22:33:27 +08:00
|
|
|
}, deleteAction: {
|
2022-01-04 22:28:18 +08:00
|
|
|
store.devices.removeAll { _deviceItem in
|
|
|
|
_deviceItem.id == deviceItem.id
|
2021-12-27 22:33:27 +08:00
|
|
|
}
|
2022-01-10 00:26:29 +08:00
|
|
|
HToast.showSuccess("已删除")
|
|
|
|
Task {
|
|
|
|
do {
|
|
|
|
_ = try await HttpRequest.rmDevice(id: deviceItem.id)
|
|
|
|
} catch {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2021-12-27 22:33:27 +08:00
|
|
|
})
|
|
|
|
.padding(EdgeInsets(top: 18, leading: 26, bottom: 0, trailing: 24))
|
|
|
|
}
|
2022-01-04 22:28:18 +08:00
|
|
|
Spacer(minLength: 30)
|
2021-12-27 22:33:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.navigationBarItems(trailing: Button(action: {
|
2022-01-10 00:26:29 +08:00
|
|
|
Task {
|
|
|
|
let devices = try await HttpRequest.regDevice().devices
|
|
|
|
withAnimation(.easeOut) {
|
|
|
|
store.devices = devices
|
|
|
|
}
|
|
|
|
HToast.showSuccess("已添加当前设备")
|
2021-12-27 22:33:27 +08:00
|
|
|
}
|
|
|
|
}, label: {
|
|
|
|
Image(systemName: "plus")
|
|
|
|
.foregroundColor(Color(UIColor.lightGray))
|
|
|
|
}))
|
|
|
|
}
|
2022-01-04 22:28:18 +08:00
|
|
|
.onAppear {
|
2022-01-10 00:26:29 +08:00
|
|
|
HttpRequest.loadDevices()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getName(deviceItem: DeviceItem) -> String {
|
|
|
|
var name = deviceItem.name
|
|
|
|
if deviceItem.is_clip == 1 {
|
|
|
|
name += " [Clip]"
|
|
|
|
}
|
|
|
|
if deviceItem.device_id == store.deviceToken {
|
|
|
|
name += " (当前设备)"
|
2022-01-04 22:28:18 +08:00
|
|
|
}
|
2022-01-10 00:26:29 +08:00
|
|
|
return name
|
2021-12-27 22:33:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DeviceView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2022-01-10 00:26:29 +08:00
|
|
|
DeviceListView().environmentObject(AppState.shared)
|
2021-12-27 22:33:27 +08:00
|
|
|
}
|
|
|
|
}
|