Skip to content

Commit

Permalink
Update RemoteMessagingDebugMenu to work on a private-queue context (#…
Browse files Browse the repository at this point in the history
…3120)

Task/Issue URL: https://app.asana.com/0/1201621708115095/1208001252080891/f

Description:
Also skip populating RMF debug menu when database is not initialized.
  • Loading branch information
ayoy authored Aug 20, 2024
1 parent 98d7746 commit 2fafe3c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
3 changes: 2 additions & 1 deletion DuckDuckGo/RemoteMessaging/RemoteMessagingClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ final class RemoteMessagingClient: RemoteMessagingProcessing {
isRemoteMessagingDatabaseLoaded = true
}

private var isRemoteMessagingDatabaseLoaded = false
// Publicly accessible for use in RemoteMessagingDebugMenu
private(set) var isRemoteMessagingDatabaseLoaded = false
private let remoteMessagingStoreProvider: RemoteMessagingStoreProviding
private var scheduledRefreshCancellable: AnyCancellable?
private var featureFlagCancellable: AnyCancellable?
Expand Down
63 changes: 44 additions & 19 deletions DuckDuckGo/RemoteMessaging/RemoteMessagingDebugMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,36 @@ import AppKitExtensions

final class RemoteMessagingDebugMenu: NSMenu {

struct MessageModel: CustomStringConvertible {
let id: String
let shown: String
let status: String

init(message: RemoteMessageManagedObject) {
self.id = message.id ?? "?"
self.shown = message.shown ? "shown" : "not shown"
self.status = Self.statusString(for: message.status)
}

var description: String {
"ID: \(id) | \(shown) | \(status)"
}

/// This should be kept in sync with `RemoteMessageStatus` private enum from BSK
private static func statusString(for status: NSNumber?) -> String {
switch status?.int16Value {
case 0:
return "scheduled"
case 1:
return "dismissed"
case 2:
return "done"
default:
return "unknown"
}
}
}

init() {
super.init(title: "")

Expand All @@ -48,24 +78,32 @@ final class RemoteMessagingDebugMenu: NSMenu {
removeItem(at: 3)
}

guard NSApplication.runType.requiresEnvironment else {
guard NSApplication.runType.requiresEnvironment, NSApp.delegateTyped.remoteMessagingClient.isRemoteMessagingDatabaseLoaded else {
return
}

let database = NSApp.delegateTyped.remoteMessagingClient.database
let context = database.makeContext(concurrencyType: .mainQueueConcurrencyType)
let context = database.makeContext(concurrencyType: .privateQueueConcurrencyType)
let fetchRequest = RemoteMessageManagedObject.fetchRequest()
fetchRequest.returnsObjectsAsFaults = false
let messages = (try? context.fetch(fetchRequest)) ?? []

let headerItem = NSMenuItem(title: "\(messages.count) Message(s) in database:")
var messageModels: [MessageModel] = []

context.performAndWait {
let messages = (try? context.fetch(fetchRequest)) ?? []
for message in messages {
messageModels.append(MessageModel(message: message))
}
}

let headerItem = NSMenuItem(title: "\(messageModels.count) Message(s) in database:")
headerItem.isEnabled = false

addItem(NSMenuItem.separator())
addItem(headerItem)

for message in messages {
let item = NSMenuItem(title: "ID: \(message.id ?? "?") | \(message.shown ? "shown" : "not shown") | \(statusString(for: message.status))")
for message in messageModels {
let item = NSMenuItem(title: message.description)
item.isEnabled = false
addItem(item)
}
Expand All @@ -81,17 +119,4 @@ final class RemoteMessagingDebugMenu: NSMenu {
}
}

/// This should be kept in sync with `RemoteMessageStatus` private enum from BSK
private func statusString(for status: NSNumber?) -> String {
switch status?.int16Value {
case 0:
return "scheduled"
case 1:
return "dismissed"
case 2:
return "done"
default:
return "unknown"
}
}
}

0 comments on commit 2fafe3c

Please sign in to comment.