Merge pull request #42 from Hext123/main

修复iOS 14闪退的问题, 优化一些问题
This commit is contained in:
Easy 2022-01-30 06:25:02 +08:00 committed by GitHub
commit 24ee83ea49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 68 additions and 22 deletions

View File

@ -20,8 +20,12 @@ class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDele
application.registerForRemoteNotifications() application.registerForRemoteNotifications()
Task { Task {
// APP, , . () let notFirstStart = UserDefaults.standard.bool(forKey: "PushDeer_notFirstStart")
_ = try await HttpRequest.fake() if !notFirstStart {
// APP, , . ()
_ = try await HttpRequest.fake()
UserDefaults.standard.set(true, forKey: "PushDeer_notFirstStart")
}
} }
return true return true
@ -33,6 +37,10 @@ class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDele
AppState.shared.deviceToken = deviceTokenString AppState.shared.deviceToken = deviceTokenString
} }
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("didFailToRegisterForRemoteNotificationsWithError: ", error)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions { func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions {
print("willPresent:", notification.request.content.userInfo) print("willPresent:", notification.request.content.userInfo)
Task { Task {

View File

@ -23,7 +23,7 @@ struct TokenContent: Codable{
struct UserInfoContent: Codable{ struct UserInfoContent: Codable{
let id: Int let id: Int
let name: String let name: String
let email: String let email: String?
let apple_id: String let apple_id: String
let wechat_id: String? let wechat_id: String?
let level: Int let level: Int

View File

@ -20,7 +20,7 @@ class AppState: ObservableObject {
/// key /// key
@Published var keys: [KeyItem] = [] @Published var keys: [KeyItem] = []
/// ///
// @Published var messages: [MessageItem] = [] // @Published var messages: [MessageItem] = []
/// tab /// tab
@Published var tabSelectedIndex: Int { @Published var tabSelectedIndex: Int {
didSet { didSet {
@ -79,13 +79,18 @@ class AppState: ObservableObject {
} catch { } catch {
print(error) print(error)
//
throw NSError(domain: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "\n\(error.localizedDescription)", code: -4, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "(-4)\n\(error.localizedDescription)"])
} }
} else {
// Apple
throw NSError(domain: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示"), code: -3, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "(-3)"])
} }
case let .failure(error): case let .failure(error):
print(error) print(error)
// Apple
throw NSError(domain: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "\n\(error.localizedDescription)", code: -2, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "(-2)\n\(error.localizedDescription)"])
} }
//
throw NSError(domain: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示"), code: -1, userInfo: nil)
} }
} }

View File

@ -26,9 +26,9 @@ struct HttpRequest {
continuation.resume(returning: content) continuation.resume(returning: content)
} else if result.code == 80403 { } else if result.code == 80403 {
AppState.shared.token = "" AppState.shared.token = ""
continuation.resume(throwing: NSError(domain: result.error ?? NSLocalizedString("登录过期", comment: "token失效时提示"), code: result.code, userInfo: nil)) continuation.resume(throwing: NSError(domain: result.error ?? NSLocalizedString("登录过期", comment: "token失效时提示"), code: result.code, userInfo: [NSLocalizedDescriptionKey: result.error ?? NSLocalizedString("登录过期", comment: "token失效时提示")]))
} else { } else {
continuation.resume(throwing: NSError(domain: result.error ?? NSLocalizedString("接口报错", comment: "接口报错时提示"), code: result.code, userInfo: nil)) continuation.resume(throwing: NSError(domain: result.error ?? NSLocalizedString("接口报错", comment: "接口报错时提示"), code: result.code, userInfo: [NSLocalizedDescriptionKey: result.error ?? NSLocalizedString("接口报错", comment: "接口报错时提示")]))
} }
} catch { } catch {
print(error) print(error)

View File

@ -31,7 +31,9 @@ struct PersistenceController {
* The store could not be migrated to the current model version. * The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was. Check the error message to determine what the actual problem was.
*/ */
fatalError("Unresolved error \(error), \(error.userInfo)") // fatalError("Unresolved error \(error), \(error.userInfo)")
print("Unresolved error \(error), \(error.userInfo)")
HToast.showError("数据库初始化失败!\n\(error.localizedDescription)")
} }
}) })
container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.automaticallyMergesChangesFromParent = true

View File

@ -42,7 +42,7 @@ struct DeletableView<Content : View> : View {
DragGesture() DragGesture()
.onChanged({ value in .onChanged({ value in
let width = value.translation.width let width = value.translation.width
print("onChanged", width) // print("onChanged", width)
let endX = isShowDelete ? offsetMaxX : 0.0 let endX = isShowDelete ? offsetMaxX : 0.0
if width < endX { if width < endX {
offsetX = width - endX offsetX = width - endX

View File

@ -25,8 +25,14 @@ struct EditableText: View {
var body: some View { var body: some View {
if #available(iOS 15.0, *) { if #available(iOS 15.0, *) {
textField() Group {
.submitLabel(.done) // https://stackoverflow.com/questions/70506330/swiftui-app-crashes-with-different-searchbar-viewmodifier-on-ios-14-15/70603710#70603710
// !!!: Group , , Xcode 13.2 bug, if , .
if #available(iOS 15.0, *) {
textField()
.submitLabel(.done)
}
}
} else { } else {
textField() textField()
} }

View File

@ -89,7 +89,10 @@ struct DeviceItemView: View {
if deviceName.contains("mac") { if deviceName.contains("mac") {
return "macwindow" return "macwindow"
} }
return "ipad.and.iphone" if #available(iOS 15.0, *) {
return "ipad.and.iphone"
}
return "laptopcomputer.and.iphone"
} }
} }

View File

@ -37,6 +37,7 @@ struct LoginView: View {
// //
} catch { } catch {
showLoading = false showLoading = false
HToast.showError(error.localizedDescription)
} }
} }
) )

View File

@ -15,7 +15,11 @@ struct MainView: View {
TabView.init(selection: $store.tabSelectedIndex) { TabView.init(selection: $store.tabSelectedIndex) {
DeviceListView() DeviceListView()
.tabItem { .tabItem {
Label("设备",systemImage: "ipad.and.iphone") if #available(iOS 15.0, *) {
Label("设备",systemImage: "ipad.and.iphone")
} else {
Label("设备",systemImage: "laptopcomputer.and.iphone")
}
} }
.tag(0) .tag(0)

View File

@ -14,7 +14,7 @@ struct MessageListView: View {
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \MessageModel.created_at, ascending: false)], animation: .default) @FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \MessageModel.created_at, ascending: false)], animation: .default)
private var messages: FetchedResults<MessageModel> private var messages: FetchedResults<MessageModel>
var body: some View { var body: some View {
BaseNavigationView(title: "消息") { BaseNavigationView(title: "消息") {
ScrollView { ScrollView {
@ -78,12 +78,16 @@ struct TestPushView: View {
store.keys = try await HttpRequest.getKeys().keys store.keys = try await HttpRequest.getKeys().keys
} }
if let keyItem = store.keys.first { if let keyItem = store.keys.first {
_ = try await HttpRequest.push(pushkey: keyItem.key, text: testText, desp: "", type: "") do {
testText = "" _ = try await HttpRequest.push(pushkey: keyItem.key, text: testText, desp: "", type: "")
HToast.showSuccess(NSLocalizedString("推送成功", comment: "")) testText = ""
let messageItems = try await HttpRequest.getMessages().messages HToast.showSuccess(NSLocalizedString("推送成功", comment: ""))
withAnimation(.easeOut) { let messageItems = try await HttpRequest.getMessages().messages
try? MessageModel.saveAndUpdate(messageItems: messageItems) withAnimation(.easeOut) {
try? MessageModel.saveAndUpdate(messageItems: messageItems)
}
} catch {
HToast.showError(error.localizedDescription)
} }
} else { } else {
HToast.showError(NSLocalizedString("推送失败, 请先添加一个Key", comment: "")) HToast.showError(NSLocalizedString("推送失败, 请先添加一个Key", comment: ""))

View File

@ -15,7 +15,7 @@ struct SettingsView: View {
var body: some View { var body: some View {
BaseNavigationView(title: "设置") { BaseNavigationView(title: "设置") {
VStack { VStack {
SettingsItemView(title: NSLocalizedString("登录为", comment: "") + " \(store.userInfo?.name ?? "--")", button: NSLocalizedString("退出", comment: "退出登录按钮上的文字")) { SettingsItemView(title: NSLocalizedString("登录为", comment: "") + " " + userName(), button: NSLocalizedString("退出", comment: "退出登录按钮上的文字")) {
store.token = "" store.token = ""
} }
.padding(EdgeInsets(top: 18, leading: 20, bottom: 0, trailing: 20)) .padding(EdgeInsets(top: 18, leading: 20, bottom: 0, trailing: 20))
@ -45,6 +45,18 @@ struct SettingsView: View {
} }
} }
} }
func userName() -> String {
if let name = store.userInfo?.name {
if name.isEmpty {
return NSLocalizedString("苹果用户", comment: "")
} else {
return name
}
} else {
return "--"
}
}
} }
struct SettingsView_Previews: PreviewProvider { struct SettingsView_Previews: PreviewProvider {

View File

@ -109,3 +109,4 @@
/* No comment provided by engineer. */ /* No comment provided by engineer. */
"自定义服务器" = "Custom server"; "自定义服务器" = "Custom server";
"苹果用户" = "Apple User";