pushdeer/ios/PushDeer-iOS/PushDeer/View/MessageListView.swift
hext d3050e154f 添加自建服务配置, 优化交互, 修复bug.
修改配置为pushdeer官方配置(包名,团队标识,api域名);
补充新添加的文字的英文翻译 (国际化);
设备和key是空列表时,加一个提示,让用户知道可以新增;
尝试解决列表删除项后UI偶尔没刷新的bug;
首次打开提示注册设备;
首次自动生成一个 key;
消息列表放到第一个位置;
推送测试时自动生成一个key;
后台进入前台后, 刷新本地消息列表;
键盘上方添加完成按钮, 用于收键盘;
发送推送测试后 自动收键盘;
及时清角标, 拯救强迫症;
为了保持一致, 设置页也改为可滑动;
添加SelfHosted配置, 可以直接一套代码跑出两种版本;
支持服务自建;
添加Env统一管理环境变量.
2022-02-14 01:13:26 +08:00

123 lines
3.7 KiB
Swift

//
// 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
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \MessageModel.created_at, ascending: false)], animation: .default)
private var messages: FetchedResults<MessageModel>
var body: some View {
BaseNavigationView(title: "消息") {
ScrollView {
LazyVStack(alignment: .leading) {
if store.isShowTestPush {
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 {
}
}
}
}
Spacer(minLength: 30)
}
}
.navigationBarItems(trailing: Button(action: {
withAnimation(.easeOut) {
store.isShowTestPush = !store.isShowTestPush
}
}, label: {
Image(systemName: store.isShowTestPush ? "chevron.up" : "chevron.down")
.foregroundColor(Color(UIColor.lightGray))
}))
}
.onAppear {
Task {
let messageItems = try await HttpRequest.getMessages().messages
try MessageModel.saveAndUpdate(messageItems: messageItems)
}
}
}
}
struct TestPushView: View {
@EnvironmentObject private var store: AppState
@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: ""))
}
}
}
.font(.system(size: 20))
.frame(width: 104, height: 42)
.foregroundColor(Color.white)
.background(Color("BtnBgColor"))
.cornerRadius(8)
.padding(EdgeInsets(top: 12, leading: 26, bottom: 0, trailing: 24))
}
}
struct MessageView_Previews: PreviewProvider {
static var previews: some View {
MessageListView()
}
}