pushdeer/ios/PushDeer-iOS/PushDeer/View/MessageListView.swift

123 lines
3.7 KiB
Swift
Raw Normal View History

2021-12-27 22:33:27 +08:00
//
// MessageListView.swift
// PushDeer
//
// Created by HEXT on 2021/12/25.
//
import SwiftUI
///
struct MessageListView: View {
@EnvironmentObject private var store: AppState
@Environment(\.managedObjectContext) private var viewContext
2022-01-04 22:28:18 +08:00
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \MessageModel.created_at, ascending: false)], animation: .default)
private var messages: FetchedResults<MessageModel>
2021-12-27 22:33:27 +08:00
var body: some View {
2022-01-04 22:28:18 +08:00
BaseNavigationView(title: "消息") {
ScrollView {
LazyVStack(alignment: .leading) {
if store.isShowTestPush {
2022-01-04 22:28:18 +08:00
TestPushView()
}
ForEach(messages) { messageItem in
MessageItemView(messageItem: messageItem) {
let id = messageItem.id
viewContext.delete(messageItem)
try? viewContext.save()
HToast.showSuccess(NSLocalizedString("已删除", comment: "删除设备/Key/消息时提示"))
Task {
do {
_ = try await HttpRequest.rmMessage(id: Int(id))
} catch {
}
2022-01-04 22:28:18 +08:00
}
}
}
Spacer(minLength: 30)
}
}
.navigationBarItems(trailing: Button(action: {
withAnimation(.easeOut) {
store.isShowTestPush = !store.isShowTestPush
2022-01-04 22:28:18 +08:00
}
}, label: {
Image(systemName: store.isShowTestPush ? "chevron.up" : "chevron.down")
2022-01-04 22:28:18 +08:00
.foregroundColor(Color(UIColor.lightGray))
}))
}
.onAppear {
Task {
let messageItems = try await HttpRequest.getMessages().messages
try MessageModel.saveAndUpdate(messageItems: messageItems)
}
}
2022-01-04 22:28:18 +08:00
}
}
struct TestPushView: View {
@EnvironmentObject private var store: AppState
2022-01-04 22:28:18 +08:00
@State private var testText = ""
var body: some View {
TextEditor(text: $testText)
.overlay(RoundedRectangle(cornerRadius: 4).stroke(Color.accentColor))
.frame(height: 128)
.padding(EdgeInsets(top: 18, leading: 26, bottom: 0, trailing: 24))
Button("推送测试") {
print("点击推送测试")
if testText.isEmpty {
HToast.showError(NSLocalizedString("推送失败, 请先输入推送内容", comment: ""))
return
}
//
UIApplication.shared.sendAction(
#selector(UIResponder.resignFirstResponder),
to: nil,
from: nil,
for: nil
)
Task {
if store.keys.isEmpty {
// keys
store.keys = try await HttpRequest.getKeys().keys
}
if store.keys.isEmpty {
// key
store.keys = try await HttpRequest.genKey().keys
}
if let keyItem = store.keys.first {
do {
_ = try await HttpRequest.push(pushkey: keyItem.key, text: testText, desp: "", type: "")
testText = ""
HToast.showSuccess(NSLocalizedString("推送成功", comment: ""))
let messageItems = try await HttpRequest.getMessages().messages
withAnimation(.easeOut) {
try? MessageModel.saveAndUpdate(messageItems: messageItems)
}
} catch {
HToast.showError(error.localizedDescription)
}
} else {
HToast.showError(NSLocalizedString("推送失败, 请先添加一个Key", comment: ""))
}
}
2022-01-04 22:28:18 +08:00
}
.font(.system(size: 20))
.frame(width: 104, height: 42)
.foregroundColor(Color.white)
.background(Color("BtnBgColor"))
2022-01-04 22:28:18 +08:00
.cornerRadius(8)
.padding(EdgeInsets(top: 12, leading: 26, bottom: 0, trailing: 24))
2021-12-27 22:33:27 +08:00
}
}
struct MessageView_Previews: PreviewProvider {
static var previews: some View {
MessageListView()
}
}