Skip to content

Commit

Permalink
Improve defaults db access in notification debugging ui
Browse files Browse the repository at this point in the history
  • Loading branch information
tmolitor-stud-tu committed Jan 4, 2025
1 parent 15a2c3b commit c8e488f
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions Monal/Classes/NotificationDebugging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,32 @@ import OrderedCollections
class NotificationDebuggingDefaultsDB: ObservableObject {
@defaultsDB("lastAppexStart")
var lastAppexStart: Date?

@defaultsDB("selectedPushServer")
var selectedPushServer: String
}

struct NotificationDebugging: View {
private let applePushEnabled: Bool
private let applePushToken: String
private let xmppAccountInfo: [xmpp]

private let availablePushServers: Dictionary<String, String>

@State private var pushPermissionEnabled = false // state because we get this value through an async call
@State private var showPushToken = false

@State private var selectedPushServer: String

@ObservedObject var notificationDebuggingDefaultsDB = NotificationDebuggingDefaultsDB()

init() {
self.applePushEnabled = MLXMPPManager.sharedInstance().hasAPNSToken;
self.applePushToken = MLXMPPManager.sharedInstance().pushToken;
self.xmppAccountInfo = MLXMPPManager.sharedInstance().connectedXMPP as! [xmpp]

// push server selector
self.availablePushServers = HelperTools.getAvailablePushServers()
}

var body: some View {
Form {
Group {
Expand Down Expand Up @@ -83,14 +93,13 @@ struct NotificationDebugging: View {
}
}
Section(header: Text("Pushserver Region").font(.title3)) {
Picker(selection: $selectedPushServer, label: Text("Push Server")) {
Picker(selection: $notificationDebuggingDefaultsDB.selectedPushServer, label: Text("Push Server")) {
ForEach(self.availablePushServers.sorted(by: >), id: \.key) { pushServerFqdn, pushServerName in
Text(pushServerName).tag(pushServerFqdn)
}
}.pickerStyle(.menu)//.menuStyle(.borderlessButton)
.onChange(of: selectedPushServer) { pushServerFqdn in
DDLogDebug("Selected \(pushServerFqdn) as push server")
HelperTools.defaultsDB().setValue(pushServerFqdn, forKey: "selectedPushServer")
.onChange(of: notificationDebuggingDefaultsDB.selectedPushServer) { pushServerFqdn in
DDLogDebug("Selected \(pushServerFqdn) as push server...")
// enable push again to switch to the selected server
for account in self.xmppAccountInfo {
account.enablePush()
Expand All @@ -113,16 +122,6 @@ struct NotificationDebugging: View {
}
});
}

init() {
self.applePushEnabled = MLXMPPManager.sharedInstance().hasAPNSToken;
self.applePushToken = MLXMPPManager.sharedInstance().pushToken;
self.xmppAccountInfo = MLXMPPManager.sharedInstance().connectedXMPP as! [xmpp]

// push server selector
self.availablePushServers = HelperTools.getAvailablePushServers()
self.selectedPushServer = HelperTools.defaultsDB().object(forKey: "selectedPushServer") as! String
}
}

struct PushSettings_Previews: PreviewProvider {
Expand Down

0 comments on commit c8e488f

Please sign in to comment.