ios: test ntf server (#4394)

* ios: test ntf server

* reset on change

* fix formatting
This commit is contained in:
spaced4ndy
2024-07-03 21:31:09 +04:00
committed by GitHub
parent 1180edf80e
commit e963ef53e7
2 changed files with 76 additions and 9 deletions
@@ -14,8 +14,26 @@ struct NotificationsView: View {
@State private var notificationMode: NotificationsMode = ChatModel.shared.notificationMode
@State private var showAlert: NotificationAlert?
@State private var legacyDatabase = dbContainerGroupDefault.get() == .documents
@State private var testing = false
@State private var testedSuccess: Bool? = nil
var body: some View {
ZStack {
viewBody()
if testing {
ProgressView().scaleEffect(2)
}
}
.alert(item: $showAlert) { alert in
if let token = m.deviceToken {
return notificationAlert(alert, token)
} else {
return Alert(title: Text("No device token!"))
}
}
}
private func viewBody() -> some View {
List {
Section {
NavigationLink {
@@ -34,13 +52,6 @@ struct NotificationsView: View {
}
.navigationTitle("Send notifications")
.navigationBarTitleDisplayMode(.inline)
.alert(item: $showAlert) { alert in
if let token = m.deviceToken {
return notificationAlert(alert, token)
} else {
return Alert(title: Text("No device token!"))
}
}
} label: {
HStack {
Text("Send notifications")
@@ -79,6 +90,7 @@ struct NotificationsView: View {
if let server = m.notificationServer {
smpServers("Push server", [server])
testServerButton(server)
}
} header: {
Text("Push notifications")
@@ -109,6 +121,11 @@ struct NotificationsView: View {
notificationMode = m.notificationMode
}
)
case let .testFailure(testFailure):
return Alert(
title: Text("Server test failed!"),
message: Text(testFailure.localizedDescription)
)
case let .error(title, error):
return Alert(title: Text(title), message: Text(error))
}
@@ -133,6 +150,7 @@ struct NotificationsView: View {
notificationMode = .off
m.notificationMode = .off
m.notificationServer = nil
testedSuccess = nil
}
} catch let error {
await MainActor.run {
@@ -150,6 +168,7 @@ struct NotificationsView: View {
notificationMode = ntfMode
m.notificationMode = ntfMode
m.notificationServer = ntfServer
testedSuccess = nil
}
} catch let error {
await MainActor.run {
@@ -161,6 +180,52 @@ struct NotificationsView: View {
}
}
}
private func testServerButton(_ server: String) -> some View {
HStack {
Button("Test server") {
testing = true
Task {
await testServer(server)
await MainActor.run { testing = false }
}
}
.disabled(testing)
if !testing {
Spacer()
showTestStatus()
}
}
}
@ViewBuilder func showTestStatus() -> some View {
if testedSuccess == true {
Image(systemName: "checkmark")
.foregroundColor(.green)
} else if testedSuccess == false {
Image(systemName: "multiply")
.foregroundColor(.red)
}
}
private func testServer(_ server: String) async {
do {
let r = try await testProtoServer(server: server)
switch r {
case .success:
await MainActor.run {
testedSuccess = true
}
case let .failure(f):
await MainActor.run {
showAlert = .testFailure(testFailure: f)
testedSuccess = false
}
}
} catch let error {
logger.error("testServerConnection \(responseError(error))")
}
}
}
func ntfModeDescription(_ mode: NotificationsMode) -> LocalizedStringKey {
@@ -212,11 +277,13 @@ struct SelectionListView<Item: SelectableItem>: View {
enum NotificationAlert: Identifiable {
case setMode(mode: NotificationsMode)
case testFailure(testFailure: ProtocolTestFailure)
case error(title: LocalizedStringKey, error: String)
var id: String {
switch self {
case let .setMode(mode): return "enable \(mode.rawValue)"
case let .testFailure(testFailure): return "testFailure \(testFailure.testStep) \(testFailure.testError)"
case let .error(title, error): return "error \(title): \(error)"
}
}
+2 -2
View File
@@ -1220,8 +1220,8 @@ public enum ProtocolTestStep: String, Decodable, Equatable {
}
public struct ProtocolTestFailure: Decodable, Error, Equatable {
var testStep: ProtocolTestStep
var testError: AgentErrorType
public var testStep: ProtocolTestStep
public var testError: AgentErrorType
public static func == (l: ProtocolTestFailure, r: ProtocolTestFailure) -> Bool {
l.testStep == r.testStep