2021-12-27 22:33:27 +08:00
|
|
|
//
|
|
|
|
// MessageListView.swift
|
|
|
|
// PushDeer
|
|
|
|
//
|
|
|
|
// Created by HEXT on 2021/12/25.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
/// 消息界面
|
|
|
|
struct MessageListView: View {
|
2022-01-10 00:26:29 +08:00
|
|
|
@EnvironmentObject private var store: AppState
|
2022-01-15 22:13:23 +08:00
|
|
|
@Environment(\.managedObjectContext) private var viewContext
|
2022-01-04 22:28:18 +08:00
|
|
|
|
2022-01-15 22:13:23 +08:00
|
|
|
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \MessageModel.created_at, ascending: false)], animation: .default)
|
|
|
|
private var messages: FetchedResults<MessageModel>
|
2022-01-30 01:02:44 +08:00
|
|
|
|
2022-04-19 00:35:56 +08:00
|
|
|
@State private var showRemoveAllMessageView: Bool = false
|
|
|
|
@State private var lastDeleteTime: TimeInterval = 0
|
|
|
|
|
|
|
|
func recordDeleteTime() -> Void {
|
|
|
|
let currentDeleteTime = Date.timeIntervalSinceReferenceDate
|
|
|
|
if currentDeleteTime - lastDeleteTime < 60 {
|
|
|
|
showRemoveAllMessageView = true
|
|
|
|
}
|
|
|
|
lastDeleteTime = currentDeleteTime
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-01-10 00:26:29 +08:00
|
|
|
if store.isShowTestPush {
|
2022-01-04 22:28:18 +08:00
|
|
|
TestPushView()
|
|
|
|
}
|
2022-01-15 22:13:23 +08:00
|
|
|
ForEach(messages) { messageItem in
|
2022-01-10 00:26:29 +08:00
|
|
|
MessageItemView(messageItem: messageItem) {
|
2022-01-15 22:13:23 +08:00
|
|
|
let id = messageItem.id
|
|
|
|
viewContext.delete(messageItem)
|
|
|
|
try? viewContext.save()
|
|
|
|
HToast.showSuccess(NSLocalizedString("已删除", comment: "删除设备/Key/消息时提示"))
|
2022-04-19 00:35:56 +08:00
|
|
|
recordDeleteTime()
|
2022-01-10 00:26:29 +08:00
|
|
|
Task {
|
|
|
|
do {
|
2022-01-15 22:13:23 +08:00
|
|
|
_ = try await HttpRequest.rmMessage(id: Int(id))
|
2022-01-10 00:26:29 +08:00
|
|
|
} catch {
|
|
|
|
|
|
|
|
}
|
2022-01-04 22:28:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Spacer(minLength: 30)
|
|
|
|
}
|
|
|
|
}
|
2022-04-19 00:35:56 +08:00
|
|
|
.overlay(
|
|
|
|
Group {
|
|
|
|
if (showRemoveAllMessageView) {
|
|
|
|
RemoveAllMessageView{showRemoveAllMessageView = false}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
alignment: .bottom
|
|
|
|
)
|
2022-01-04 22:28:18 +08:00
|
|
|
.navigationBarItems(trailing: Button(action: {
|
|
|
|
withAnimation(.easeOut) {
|
2022-01-10 00:26:29 +08:00
|
|
|
store.isShowTestPush = !store.isShowTestPush
|
2022-01-04 22:28:18 +08:00
|
|
|
}
|
|
|
|
}, label: {
|
2022-01-10 00:26:29 +08:00
|
|
|
Image(systemName: store.isShowTestPush ? "chevron.up" : "chevron.down")
|
2022-01-04 22:28:18 +08:00
|
|
|
.foregroundColor(Color(UIColor.lightGray))
|
|
|
|
}))
|
|
|
|
}
|
2022-01-10 00:26:29 +08:00
|
|
|
.onAppear {
|
|
|
|
Task {
|
2022-01-15 22:13:23 +08:00
|
|
|
let messageItems = try await HttpRequest.getMessages().messages
|
|
|
|
try MessageModel.saveAndUpdate(messageItems: messageItems)
|
2022-01-10 00:26:29 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-04 22:28:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TestPushView: View {
|
2022-01-10 00:26:29 +08:00
|
|
|
@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("点击推送测试")
|
2022-01-10 00:26:29 +08:00
|
|
|
if testText.isEmpty {
|
2022-01-15 22:13:23 +08:00
|
|
|
HToast.showError(NSLocalizedString("推送失败, 请先输入推送内容", comment: ""))
|
2022-01-10 00:26:29 +08:00
|
|
|
return
|
|
|
|
}
|
2022-02-14 01:13:26 +08:00
|
|
|
// 收键盘
|
|
|
|
UIApplication.shared.sendAction(
|
|
|
|
#selector(UIResponder.resignFirstResponder),
|
|
|
|
to: nil,
|
|
|
|
from: nil,
|
|
|
|
for: nil
|
|
|
|
)
|
2022-01-10 00:26:29 +08:00
|
|
|
Task {
|
|
|
|
if store.keys.isEmpty {
|
2022-02-14 01:13:26 +08:00
|
|
|
// 查keys列表
|
2022-01-10 00:26:29 +08:00
|
|
|
store.keys = try await HttpRequest.getKeys().keys
|
|
|
|
}
|
2022-02-14 01:13:26 +08:00
|
|
|
if store.keys.isEmpty {
|
|
|
|
// 没查到就自动生成一个key
|
|
|
|
store.keys = try await HttpRequest.genKey().keys
|
|
|
|
}
|
2022-01-10 00:26:29 +08:00
|
|
|
if let keyItem = store.keys.first {
|
2022-01-30 01:02:44 +08:00
|
|
|
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)
|
2022-01-10 00:26:29 +08:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-15 22:13:23 +08:00
|
|
|
HToast.showError(NSLocalizedString("推送失败, 请先添加一个Key", comment: ""))
|
2022-01-10 00:26:29 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-04 22:28:18 +08:00
|
|
|
}
|
|
|
|
.font(.system(size: 20))
|
|
|
|
.frame(width: 104, height: 42)
|
|
|
|
.foregroundColor(Color.white)
|
2022-01-16 00:10:47 +08:00
|
|
|
.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 00:35:56 +08:00
|
|
|
struct RemoveAllMessageView: View {
|
|
|
|
/// 取消按钮点击的回调
|
|
|
|
let closeAction : () -> ()
|
|
|
|
var body: some View {
|
|
|
|
VStack(alignment: .center, spacing: 0) {
|
|
|
|
HLine()
|
|
|
|
.stroke(Color("borderColor"))
|
|
|
|
.frame(height: 1)
|
|
|
|
HStack(spacing: 12) {
|
|
|
|
Button(NSLocalizedString("清除全部消息", comment: "")) {
|
|
|
|
Task {
|
|
|
|
do {
|
|
|
|
try MessageModel.deleteAll()
|
|
|
|
_ = try await HttpRequest.rmAllMessage()
|
|
|
|
HToast.showSuccess(NSLocalizedString("已清空", comment: ""))
|
|
|
|
self.closeAction()
|
|
|
|
} catch {
|
|
|
|
HToast.showError(error.localizedDescription)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.font(.system(size: 20))
|
|
|
|
.padding(.horizontal)
|
|
|
|
.frame( height: 42)
|
|
|
|
.overlay(RoundedRectangle(cornerRadius: 4).stroke())
|
|
|
|
.foregroundColor(Color.accentColor)
|
|
|
|
|
|
|
|
Button(NSLocalizedString("取消", comment: "")) {
|
|
|
|
self.closeAction()
|
|
|
|
}
|
|
|
|
.font(.system(size: 20))
|
|
|
|
.padding(.horizontal)
|
|
|
|
.frame( height: 42)
|
|
|
|
.overlay(RoundedRectangle(cornerRadius: 4).stroke())
|
|
|
|
.foregroundColor(Color.accentColor)
|
|
|
|
}
|
|
|
|
.padding()
|
|
|
|
}
|
|
|
|
.background(Color("backgroundColor").opacity(0.9))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-27 22:33:27 +08:00
|
|
|
struct MessageView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
MessageListView()
|
2022-04-19 00:35:56 +08:00
|
|
|
.environmentObject(AppState.shared)
|
2021-12-27 22:33:27 +08:00
|
|
|
}
|
|
|
|
}
|