Skip to content

Commit

Permalink
Refactor Core Data usage in RemoteMessagingStore (#3381)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/1201048563534612/1208493869353425/f

Description:
This change updates RMF client to use new async API from BSK's RemoteMessagingStore.
  • Loading branch information
ayoy authored Oct 10, 2024
1 parent 24cf340 commit 39ae68c
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 30 deletions.
2 changes: 1 addition & 1 deletion DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14288,7 +14288,7 @@
repositoryURL = "https://github.com/duckduckgo/BrowserServicesKit";
requirement = {
kind = exactVersion;
version = 198.4.0;
version = 199.0.0;
};
};
9FF521422BAA8FF300B9819B /* XCRemoteSwiftPackageReference "lottie-spm" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/duckduckgo/BrowserServicesKit",
"state" : {
"revision" : "c642e92389688016e2dad549f6d6e7c4cd1642d3",
"version" : "198.4.0"
"revision" : "ec46d991838527dd8c7041a3673428c0e18e0fdc",
"version" : "199.0.0"
}
},
{
Expand Down
18 changes: 9 additions & 9 deletions DuckDuckGo/HomePage/Model/RemoteMessageViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,32 @@ struct RemoteMessageViewModel {
}
}

let onDidClose: (ButtonAction?) -> Void
let onDidClose: (ButtonAction?) async -> Void
let onDidAppear: () -> Void
let onDidDisappear: () -> Void
let openURLHandler: (URL) -> Void

func mapActionToViewModel(remoteAction: RemoteAction,
buttonAction: RemoteMessageViewModel.ButtonAction,
onDidClose: @escaping (RemoteMessageViewModel.ButtonAction?) -> Void) -> () -> Void {
onDidClose: @escaping (RemoteMessageViewModel.ButtonAction?) async -> Void) -> () async -> Void {

switch remoteAction {
case .url(let value), .share(let value, _), .survey(let value):
return {
return { @MainActor in
if let url = URL.makeURL(from: value) {
openURLHandler(url)
}
onDidClose(buttonAction)
await onDidClose(buttonAction)
}
case .appStore:
return {
return { @MainActor in
let url = URL(string: "https://apps.apple.com/app/duckduckgo-privacy-browser/id663592361")!
openURLHandler(url)
onDidClose(buttonAction)
await onDidClose(buttonAction)
}
case .dismiss:
return {
onDidClose(buttonAction)
return { @MainActor in
await onDidClose(buttonAction)
}
}
}
Expand All @@ -108,7 +108,7 @@ struct RemoteMessageButtonViewModel {

let title: String
var actionStyle: ActionStyle = .default
let action: () -> Void
let action: () async -> Void
}

extension RemoteAction {
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/HomePage/View/HomePageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ extension HomePage.Views {
messageId: remoteMessage.id,
modelType: modelType,
onDidClose: { action in
activeRemoteMessageModel.dismissRemoteMessage(with: action)
await activeRemoteMessageModel.dismissRemoteMessage(with: action)
},
onDidAppear: {
activeRemoteMessageModel.isViewOnScreen = true
Expand Down
10 changes: 8 additions & 2 deletions DuckDuckGo/HomePage/View/RemoteMessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ struct RemoteMessageView: View {

private var closeButton: some View {
HomePage.Views.CloseButton(icon: .close, size: 16) {
viewModel.onDidClose(.close)
Task {
await viewModel.onDidClose(.close)
}
}
.visibility(isHovering ? .visible : .invisible)
.padding(6)
Expand Down Expand Up @@ -159,7 +161,11 @@ extension RemoteMessageButtonViewModel {
}

var standardButton: some View {
Button(action: action) {
Button {
Task { @MainActor in
await action()
}
} label: {
Text(title)
}
}
Expand Down
4 changes: 3 additions & 1 deletion DuckDuckGo/Menus/MainMenuActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,9 @@ extension AppDelegate {
}

@objc func resetRemoteMessages(_ sender: Any?) {
remoteMessagingClient.store?.resetRemoteMessages()
Task {
await remoteMessagingClient.store?.resetRemoteMessages()
}
}

@objc func resetNewTabPageCustomization(_ sender: Any?) {
Expand Down
18 changes: 11 additions & 7 deletions DuckDuckGo/RemoteMessaging/ActiveRemoteMessageModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,33 @@ final class ActiveRemoteMessageModel: ObservableObject {
}
.store(in: &cancellables)

let remoteMessagePublisher = $remoteMessage.compactMap({ $0 }).asVoid()
let remoteMessagePublisher = $remoteMessage
.compactMap({ $0 })
.filter { [weak self] _ in self?.isViewOnScreen == true }
.asVoid()
let isViewOnScreenPublisher = $isViewOnScreen.removeDuplicates().filter({ $0 }).asVoid()
Publishers.Merge(remoteMessagePublisher, isViewOnScreenPublisher)
.receive(on: DispatchQueue.main)
.sink { [weak self] in
guard let self else {
return
}
if isViewOnScreen {
markRemoteMessageAsShown()
Task {
await self.markRemoteMessageAsShown()
}
}
.store(in: &cancellables)

updateRemoteMessage()
}

func dismissRemoteMessage(with action: RemoteMessageViewModel.ButtonAction?) {
@MainActor
func dismissRemoteMessage(with action: RemoteMessageViewModel.ButtonAction?) async {
guard let remoteMessage else {
return
}

store()?.dismissRemoteMessage(withID: remoteMessage.id)
await store()?.dismissRemoteMessage(withID: remoteMessage.id)
self.remoteMessage = nil

let pixel: GeneralPixel? = {
Expand All @@ -134,7 +138,7 @@ final class ActiveRemoteMessageModel: ObservableObject {
}
}

func markRemoteMessageAsShown() {
func markRemoteMessageAsShown() async {
guard let remoteMessage, let store = store() else {
return
}
Expand All @@ -147,7 +151,7 @@ final class ActiveRemoteMessageModel: ObservableObject {
if remoteMessage.isMetricsEnabled {
PixelKit.fire(GeneralPixel.remoteMessageShownUnique, withAdditionalParameters: ["message": remoteMessage.id])
}
store.updateRemoteMessage(withID: remoteMessage.id, asShown: true)
await store.updateRemoteMessage(withID: remoteMessage.id, asShown: true)
}
}

Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/DataBrokerProtection/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ let package = Package(
targets: ["DataBrokerProtection"])
],
dependencies: [
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "198.4.0"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "199.0.0"),
.package(path: "../SwiftUIExtensions"),
.package(path: "../XPCHelper"),
],
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/NetworkProtectionMac/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let package = Package(
.library(name: "VPNAppLauncher", targets: ["VPNAppLauncher"]),
],
dependencies: [
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "198.4.0"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "199.0.0"),
.package(url: "https://github.com/airbnb/lottie-spm", exact: "4.4.3"),
.package(path: "../AppLauncher"),
.package(path: "../UDSHelper"),
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/SubscriptionUI/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let package = Package(
targets: ["SubscriptionUI"]),
],
dependencies: [
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "198.4.0"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "199.0.0"),
.package(path: "../SwiftUIExtensions")
],
targets: [
Expand Down
8 changes: 4 additions & 4 deletions UnitTests/RemoteMessaging/ActiveRemoteMessageModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,26 @@ final class ActiveRemoteMessageModelTests: XCTestCase {
XCTAssertEqual(model.remoteMessage, message)
}

func testWhenMessageIsDismissedThenItIsClearedFromModel() throws {
func testWhenMessageIsDismissedThenItIsClearedFromModel() async throws {
store.scheduledRemoteMessage = message
model = ActiveRemoteMessageModel(
remoteMessagingStore: self.store,
remoteMessagingAvailabilityProvider: MockRemoteMessagingAvailabilityProvider()
)
model.dismissRemoteMessage(with: .close)
await model.dismissRemoteMessage(with: .close)

XCTAssertNil(model.remoteMessage)
}

func testWhenMessageIsMarkedAsShownThenShownFlagIsSavedInStore() throws {
func testWhenMessageIsMarkedAsShownThenShownFlagIsSavedInStore() async throws {
store.scheduledRemoteMessage = message
model = ActiveRemoteMessageModel(
remoteMessagingStore: self.store,
remoteMessagingAvailabilityProvider: MockRemoteMessagingAvailabilityProvider()
)

XCTAssertFalse(store.hasShownRemoteMessage(withID: message.id))
model.markRemoteMessageAsShown()
await model.markRemoteMessageAsShown()
XCTAssertTrue(store.hasShownRemoteMessage(withID: message.id))
}
}

0 comments on commit 39ae68c

Please sign in to comment.