2021-12-27 22:33:27 +08:00
|
|
|
//
|
|
|
|
// KeyListView.swift
|
|
|
|
// PushDeer
|
|
|
|
//
|
|
|
|
// Created by HEXT on 2021/12/25.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
/// Key 界面
|
|
|
|
struct KeyListView: View {
|
2022-01-10 00:26:29 +08:00
|
|
|
@EnvironmentObject private var store: AppState
|
2021-12-27 22:33:27 +08:00
|
|
|
var body: some View {
|
|
|
|
BaseNavigationView(title: "Key") {
|
|
|
|
ScrollView {
|
|
|
|
LazyVStack(alignment: .center) {
|
2022-01-10 00:26:29 +08:00
|
|
|
ForEach(store.keys.reversed()) { keyItem in
|
2021-12-27 22:33:27 +08:00
|
|
|
DeletableView(contentView: {
|
|
|
|
CardView {
|
|
|
|
KeyItemView(keyItem: keyItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
}, deleteAction: {
|
2022-01-10 00:26:29 +08:00
|
|
|
store.keys.removeAll { _keyItem in
|
2021-12-27 22:33:27 +08:00
|
|
|
keyItem.id == _keyItem.id
|
|
|
|
}
|
2022-02-14 01:13:26 +08:00
|
|
|
// 尝试触发 @Published, 看能不能解决 UI 偶尔不更新的问题
|
|
|
|
store.keys = store.keys
|
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.rmKey(id: keyItem.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.keys.isEmpty {
|
|
|
|
Text("你还未添加过 Key, 发送推送需要使用 Key, 你可以点击右上角 \(Image(systemName: "plus")) 生成一个 Key")
|
|
|
|
.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: genKey, label: {
|
2021-12-27 22:33:27 +08:00
|
|
|
Image(systemName: "plus")
|
|
|
|
.foregroundColor(Color(UIColor.lightGray))
|
|
|
|
}))
|
|
|
|
}
|
2022-01-10 00:26:29 +08:00
|
|
|
.onAppear {
|
2022-02-14 01:13:26 +08:00
|
|
|
Task {
|
|
|
|
let result = try await HttpRequest.getKeys()
|
|
|
|
AppState.shared.keys = result.keys
|
|
|
|
|
|
|
|
// 首次自动生成一个 key
|
|
|
|
let hasAlertGenKey = UserDefaults.standard.bool(forKey: "PushDeer_hasAlertGenKey")
|
|
|
|
if result.keys.isEmpty && !hasAlertGenKey {
|
|
|
|
genKey()
|
|
|
|
UserDefaults.standard.set(true, forKey: "PushDeer_hasAlertGenKey")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func genKey() -> Void {
|
|
|
|
Task {
|
|
|
|
let keys = try await HttpRequest.genKey().keys
|
|
|
|
withAnimation(.easeOut) {
|
|
|
|
store.keys = keys
|
|
|
|
}
|
|
|
|
HToast.showSuccess(NSLocalizedString("已添加新Key", comment: ""))
|
2022-01-10 00:26:29 +08:00
|
|
|
}
|
2021-12-27 22:33:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct KeyView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2022-01-10 00:26:29 +08:00
|
|
|
KeyListView().environmentObject(AppState.shared)
|
2021-12-27 22:33:27 +08:00
|
|
|
}
|
|
|
|
}
|