From 4260c2001265c799e4cfb5e0e011fa994975514c Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Tue, 13 Feb 2024 17:58:54 +0400 Subject: [PATCH] ios: show ntf server (#3800) --- apps/ios/Shared/Model/ChatModel.swift | 1 + apps/ios/Shared/Model/SimpleXAPI.swift | 10 +++++----- .../Views/UserSettings/NotificationsView.swift | 18 ++++++++++++++---- apps/ios/SimpleXChat/APITypes.swift | 4 ++-- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/apps/ios/Shared/Model/ChatModel.swift b/apps/ios/Shared/Model/ChatModel.swift index c31ad579a..0fff53a06 100644 --- a/apps/ios/Shared/Model/ChatModel.swift +++ b/apps/ios/Shared/Model/ChatModel.swift @@ -80,6 +80,7 @@ final class ChatModel: ObservableObject { @Published var tokenRegistered = false @Published var tokenStatus: NtfTknStatus? @Published var notificationMode = NotificationsMode.off + @Published var notificationServer: String? @Published var notificationPreview: NotificationPreviewMode = ntfPreviewModeGroupDefault.get() // pending notification actions @Published var ntfContactRequest: NTFContactRequest? diff --git a/apps/ios/Shared/Model/SimpleXAPI.swift b/apps/ios/Shared/Model/SimpleXAPI.swift index d1a16f73a..0abe06842 100644 --- a/apps/ios/Shared/Model/SimpleXAPI.swift +++ b/apps/ios/Shared/Model/SimpleXAPI.swift @@ -412,14 +412,14 @@ func apiDeleteMemberChatItem(groupId: Int64, groupMemberId: Int64, itemId: Int64 throw r } -func apiGetNtfToken() -> (DeviceToken?, NtfTknStatus?, NotificationsMode) { +func apiGetNtfToken() -> (DeviceToken?, NtfTknStatus?, NotificationsMode, String?) { let r = chatSendCmdSync(.apiGetNtfToken) switch r { - case let .ntfToken(token, status, ntfMode): return (token, status, ntfMode) - case .chatCmdError(_, .errorAgent(.CMD(.PROHIBITED))): return (nil, nil, .off) + case let .ntfToken(token, status, ntfMode, ntfServer): return (token, status, ntfMode, ntfServer) + case .chatCmdError(_, .errorAgent(.CMD(.PROHIBITED))): return (nil, nil, .off, nil) default: logger.debug("apiGetNtfToken response: \(String(describing: r))") - return (nil, nil, .off) + return (nil, nil, .off, nil) } } @@ -1309,7 +1309,7 @@ func startChat(refreshInvitations: Bool = true) throws { if (refreshInvitations) { try refreshCallInvitations() } - (m.savedToken, m.tokenStatus, m.notificationMode) = apiGetNtfToken() + (m.savedToken, m.tokenStatus, m.notificationMode, m.notificationServer) = apiGetNtfToken() // deviceToken is set when AppDelegate.application(didRegisterForRemoteNotificationsWithDeviceToken:) is called, // when it is called before startChat if let token = m.deviceToken { diff --git a/apps/ios/Shared/Views/UserSettings/NotificationsView.swift b/apps/ios/Shared/Views/UserSettings/NotificationsView.swift index 04c02f0dd..4876d60ec 100644 --- a/apps/ios/Shared/Views/UserSettings/NotificationsView.swift +++ b/apps/ios/Shared/Views/UserSettings/NotificationsView.swift @@ -76,6 +76,10 @@ struct NotificationsView: View { Text(m.notificationPreview.label) } } + + if let server = m.notificationServer { + smpServers("Push server", [server]) + } } header: { Text("Push notifications") } footer: { @@ -87,6 +91,9 @@ struct NotificationsView: View { } } .disabled(legacyDatabase) + .onAppear { + (m.savedToken, m.tokenStatus, m.notificationMode, m.notificationServer) = apiGetNtfToken() + } } private func notificationAlert(_ alert: NotificationAlert, _ token: DeviceToken) -> Alert { @@ -125,6 +132,7 @@ struct NotificationsView: View { m.tokenStatus = .new notificationMode = .off m.notificationMode = .off + m.notificationServer = nil } } catch let error { await MainActor.run { @@ -135,11 +143,13 @@ struct NotificationsView: View { } default: do { - let status = try await apiRegisterToken(token: token, notificationMode: mode) + let _ = try await apiRegisterToken(token: token, notificationMode: mode) + let (_, tknStatus, ntfMode, ntfServer) = apiGetNtfToken() await MainActor.run { - m.tokenStatus = status - notificationMode = mode - m.notificationMode = mode + m.tokenStatus = tknStatus + notificationMode = ntfMode + m.notificationMode = ntfMode + m.notificationServer = ntfServer } } catch let error { await MainActor.run { diff --git a/apps/ios/SimpleXChat/APITypes.swift b/apps/ios/SimpleXChat/APITypes.swift index ae091f841..daad9d87f 100644 --- a/apps/ios/SimpleXChat/APITypes.swift +++ b/apps/ios/SimpleXChat/APITypes.swift @@ -613,7 +613,7 @@ public enum ChatResponse: Decodable, Error { case callEnded(user: UserRef, contact: Contact) case callInvitations(callInvitations: [RcvCallInvitation]) case ntfTokenStatus(status: NtfTknStatus) - case ntfToken(token: DeviceToken, status: NtfTknStatus, ntfMode: NotificationsMode) + case ntfToken(token: DeviceToken, status: NtfTknStatus, ntfMode: NotificationsMode, ntfServer: String) case ntfMessages(user_: User?, connEntity_: ConnectionEntity?, msgTs: Date?, ntfMessages: [NtfMsgInfo]) case ntfMessage(user: UserRef, connEntity: ConnectionEntity, ntfMessage: NtfMsgInfo) case contactConnectionDeleted(user: UserRef, connection: PendingContactConnection) @@ -912,7 +912,7 @@ public enum ChatResponse: Decodable, Error { case let .callEnded(u, contact): return withUser(u, "contact: \(contact.id)") case let .callInvitations(invs): return String(describing: invs) case let .ntfTokenStatus(status): return String(describing: status) - case let .ntfToken(token, status, ntfMode): return "token: \(token)\nstatus: \(status.rawValue)\nntfMode: \(ntfMode.rawValue)" + case let .ntfToken(token, status, ntfMode, ntfServer): return "token: \(token)\nstatus: \(status.rawValue)\nntfMode: \(ntfMode.rawValue)\nntfServer: \(ntfServer)" case let .ntfMessages(u, connEntity, msgTs, ntfMessages): return withUser(u, "connEntity: \(String(describing: connEntity))\nmsgTs: \(String(describing: msgTs))\nntfMessages: \(String(describing: ntfMessages))") case let .ntfMessage(u, connEntity, ntfMessage): return withUser(u, "connEntity: \(String(describing: connEntity))\nntfMessage: \(String(describing: ntfMessage))") case let .contactConnectionDeleted(u, connection): return withUser(u, String(describing: connection))