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
|
2022-02-14 01:13:26 +08:00
|
|
|
@State private var isShowAlert = false
|
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-15 22:13:23 +08:00
|
|
|
DeviceItemView(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-02-14 01:13:26 +08:00
|
|
|
// 尝试触发 @Published, 看能不能解决 UI 偶尔不更新的问题
|
|
|
|
store.devices = store.devices
|
2022-01-15 22:13:23 +08:00
|
|
|
HToast.showSuccess(NSLocalizedString("已删除", comment: "删除设备/Key/消息时提示"))
|
2022-01-10 00:26:29 +08:00
|
|
|
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-02-14 01:13:26 +08:00
|
|
|
|
|
|
|
if store.devices.isEmpty {
|
|
|
|
Text("你还未注册当前设备, 注册后才能收到推送, 你可以点击右上角 \(Image(systemName: "plus")) 添加当前设备")
|
|
|
|
.foregroundColor(Color(UIColor.lightGray))
|
|
|
|
.padding()
|
|
|
|
}
|
|
|
|
|
2022-01-04 22:28:18 +08:00
|
|
|
Spacer(minLength: 30)
|
2021-12-27 22:33:27 +08:00
|
|
|
}
|
|
|
|
}
|
2022-02-14 01:13:26 +08:00
|
|
|
.navigationBarItems(trailing: Button(action: regDevice, label: {
|
2021-12-27 22:33:27 +08:00
|
|
|
Image(systemName: "plus")
|
|
|
|
.foregroundColor(Color(UIColor.lightGray))
|
|
|
|
}))
|
|
|
|
}
|
2022-01-04 22:28:18 +08:00
|
|
|
.onAppear {
|
2022-02-14 01:13:26 +08:00
|
|
|
Task {
|
|
|
|
// 加载已注册设备列表
|
|
|
|
let result = try await HttpRequest.getDevices()
|
|
|
|
AppState.shared.devices = result.devices
|
|
|
|
|
|
|
|
// 首次提示添加设备
|
|
|
|
let hasAlertRegDevice = UserDefaults.standard.bool(forKey: "PushDeer_hasAlertRegDevice")
|
|
|
|
if !AppState.shared.deviceToken.isEmpty && !hasAlertRegDevice {
|
|
|
|
let hasContains = result.devices.contains { deviceItem in
|
|
|
|
deviceItem.device_id == AppState.shared.deviceToken
|
|
|
|
}
|
|
|
|
if !hasContains {
|
|
|
|
isShowAlert = true
|
|
|
|
UserDefaults.standard.set(true, forKey: "PushDeer_hasAlertRegDevice")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.alert(isPresented: $isShowAlert) {
|
|
|
|
Alert(
|
|
|
|
title: Text("温馨提示"),
|
|
|
|
message: Text("你还未注册当前设备, 注册后才能收到推送, 是否现在注册? (你还可以稍后点击右上角 + 符号添加当前设备)"),
|
|
|
|
primaryButton: .default(
|
|
|
|
Text("注册"),
|
|
|
|
action: regDevice
|
|
|
|
),
|
|
|
|
secondaryButton: .cancel(Text("稍后"))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func regDevice() -> Void {
|
|
|
|
Task {
|
|
|
|
let hasContains = store.devices.contains { store.deviceToken == $0.device_id }
|
|
|
|
if hasContains {
|
|
|
|
HToast.showInfo(NSLocalizedString("已添加过当前设备", comment: ""))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let devices = try await HttpRequest.regDevice().devices
|
|
|
|
withAnimation(.easeOut) {
|
|
|
|
store.devices = devices
|
|
|
|
}
|
|
|
|
HToast.showSuccess(NSLocalizedString("已添加当前设备", comment: ""))
|
2022-01-10 00:26:29 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-27 22:33:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct DeviceView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2022-01-16 00:10:47 +08:00
|
|
|
DeviceListView()
|
|
|
|
.environmentObject(AppState.shared)
|
|
|
|
.environment(\.colorScheme, .dark)
|
2021-12-27 22:33:27 +08:00
|
|
|
}
|
|
|
|
}
|