Skip to content

Commit

Permalink
Refactor Core Data usage in RemoteMessagingStore (#3425)
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 1cf43fa commit 698f13f
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 64 deletions.
2 changes: 1 addition & 1 deletion DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10947,7 +10947,7 @@
repositoryURL = "https://github.com/DuckDuckGo/BrowserServicesKit";
requirement = {
kind = exactVersion;
version = 198.4.0;
version = 199.0.0;
};
};
9F8FE9472BAE50E50071E372 /* 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
12 changes: 8 additions & 4 deletions DuckDuckGo/HomeMessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ struct HomeMessageView: View {

private var closeButton: some View {
Button {
viewModel.onDidClose(.close)
Task {
await viewModel.onDidClose(.close)
}
} label: {
Image("Close-24")
.foregroundColor(.primary)
Expand Down Expand Up @@ -143,9 +145,11 @@ struct HomeMessageView: View {
private var buttons: some View {
ForEach(viewModel.buttons, id: \.title) { buttonModel in
Button {
buttonModel.action()
if case .share(let value, let title) = buttonModel.actionStyle {
activityItem = ShareItem(value: value, title: title)
Task { @MainActor in
await buttonModel.action()
if case .share(let value, let title) = buttonModel.actionStyle {
activityItem = ShareItem(value: value, title: title)
}
}
} label: {
HStack {
Expand Down
26 changes: 13 additions & 13 deletions DuckDuckGo/HomeMessageViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,40 +118,40 @@ struct HomeMessageViewModel {
}
}

let onDidClose: (ButtonAction?) -> Void
let onDidClose: (ButtonAction?) async -> Void
let onDidAppear: () -> Void
let onAttachAdditionalParameters: ((_ useCase: PrivacyProDataReportingUseCase, _ params: [String: String]) -> [String: String])?

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

switch remoteAction {
case .share:
return {
onDidClose(buttonAction)
return { @MainActor in
await onDidClose(buttonAction)
}
case .url(let value):
return {
return { @MainActor in
LaunchTabNotification.postLaunchTabNotification(urlString: value)
onDidClose(buttonAction)
await onDidClose(buttonAction)
}
case .survey(let value):
return {
return { @MainActor in
LaunchTabNotification.postLaunchTabNotification(urlString: value)
onDidClose(buttonAction)
await onDidClose(buttonAction)
}
case .appStore:
return {
return { @MainActor in
let url = URL.appStore
if UIApplication.shared.canOpenURL(url as URL) {
UIApplication.shared.open(url)
}
onDidClose(buttonAction)
await onDidClose(buttonAction)
}
case .dismiss:
return {
onDidClose(buttonAction)
return { @MainActor in
await onDidClose(buttonAction)
}
}
}
Expand All @@ -166,6 +166,6 @@ struct HomeMessageButtonViewModel {

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

}
2 changes: 1 addition & 1 deletion DuckDuckGo/HomeMessageViewModelBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct HomeMessageViewModelBuilder {

static func build(for remoteMessage: RemoteMessageModel,
with privacyProDataReporter: PrivacyProDataReporting?,
onDidClose: @escaping (HomeMessageViewModel.ButtonAction?) -> Void,
onDidClose: @escaping (HomeMessageViewModel.ButtonAction?) async -> Void,
onDidAppear: @escaping () -> Void) -> HomeMessageViewModel? {
guard let content = remoteMessage.content else { return nil }

Expand Down
8 changes: 5 additions & 3 deletions DuckDuckGo/HomeMessageViewSectionRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,11 @@ class HomeMessageViewSectionRenderer: NSObject, HomeViewSectionRenderer {
private func dismissHomeMessage(_ message: HomeMessage,
at indexPath: IndexPath,
in collectionView: UICollectionView) {
homePageConfiguration.dismissHomeMessage(message)
animateCellDismissal(at: indexPath, in: collectionView) {
self.controller?.homeMessageRenderer(self, didDismissHomeMessage: message)
Task { @MainActor in
await homePageConfiguration.dismissHomeMessage(message)
animateCellDismissal(at: indexPath, in: collectionView) {
self.controller?.homeMessageRenderer(self, didDismissHomeMessage: message)
}
}
}

Expand Down
9 changes: 6 additions & 3 deletions DuckDuckGo/HomePageConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ final class HomePageConfiguration: HomePageMessagesConfiguration {
return .remoteMessage(remoteMessage: remoteMessageToPresent)
}

func dismissHomeMessage(_ homeMessage: HomeMessage) {
@MainActor
func dismissHomeMessage(_ homeMessage: HomeMessage) async {
switch homeMessage {
case .remoteMessage(let remoteMessage):
Logger.remoteMessaging.info("Home message dismissed: \(remoteMessage.id)")
remoteMessagingClient.store.dismissRemoteMessage(withID: remoteMessage.id)
await remoteMessagingClient.store.dismissRemoteMessage(withID: remoteMessage.id)

if let index = homeMessages.firstIndex(of: homeMessage) {
homeMessages.remove(at: index)
Expand All @@ -113,7 +114,9 @@ final class HomePageConfiguration: HomePageMessagesConfiguration {
Pixel.fire(pixel: .remoteMessageShownUnique,
withAdditionalParameters: additionalParameters(for: remoteMessage.id))
}
remoteMessagingClient.store.updateRemoteMessage(withID: remoteMessage.id, asShown: true)
Task {
await remoteMessagingClient.store.updateRemoteMessage(withID: remoteMessage.id, asShown: true)
}
}

default:
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/HomePageMessagesConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ protocol HomePageMessagesConfiguration {

func refresh()

func dismissHomeMessage(_ homeMessage: HomeMessage)
func dismissHomeMessage(_ homeMessage: HomeMessage) async
func didAppear(_ homeMessage: HomeMessage)
}
17 changes: 9 additions & 8 deletions DuckDuckGo/NewTabPageMessagesModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ final class NewTabPageMessagesModel: ObservableObject {
refresh()
}

func dismissHomeMessage(_ homeMessage: HomeMessage) {
homePageMessagesConfiguration.dismissHomeMessage(homeMessage)
@MainActor
func dismissHomeMessage(_ homeMessage: HomeMessage) async {
await homePageMessagesConfiguration.dismissHomeMessage(homeMessage)
updateHomeMessageViewModel()
}

Expand All @@ -77,7 +78,7 @@ final class NewTabPageMessagesModel: ObservableObject {
switch message {
case .placeholder:
return HomeMessageViewModel(messageId: "", sendPixels: false, modelType: .small(titleText: "", descriptionText: "")) { [weak self] _ in
self?.dismissHomeMessage(message)
await self?.dismissHomeMessage(message)
} onDidAppear: {
// no-op
} onAttachAdditionalParameters: { _, params in
Expand All @@ -89,15 +90,15 @@ final class NewTabPageMessagesModel: ObservableObject {
// as a result of refreshing a config while the user was on a new tab page already.
didAppear(message)

return HomeMessageViewModelBuilder.build(for: remoteMessage, with: privacyProDataReporter) { [weak self] action in
return HomeMessageViewModelBuilder.build(for: remoteMessage, with: privacyProDataReporter) { @MainActor [weak self] action in
guard let action,
let self else { return }

switch action {

case .action(let isSharing):
if !isSharing {
self.dismissHomeMessage(message)
await self.dismissHomeMessage(message)
}
if remoteMessage.isMetricsEnabled {
pixelFiring.fire(.remoteMessageActionClicked,
Expand All @@ -106,7 +107,7 @@ final class NewTabPageMessagesModel: ObservableObject {

case .primaryAction(let isSharing):
if !isSharing {
self.dismissHomeMessage(message)
await self.dismissHomeMessage(message)
}
if remoteMessage.isMetricsEnabled {
pixelFiring.fire(.remoteMessagePrimaryActionClicked,
Expand All @@ -115,15 +116,15 @@ final class NewTabPageMessagesModel: ObservableObject {

case .secondaryAction(let isSharing):
if !isSharing {
self.dismissHomeMessage(message)
await self.dismissHomeMessage(message)
}
if remoteMessage.isMetricsEnabled {
pixelFiring.fire(.remoteMessageSecondaryActionClicked,
withAdditionalParameters: self.additionalParameters(for: remoteMessage.id))
}

case .close:
self.dismissHomeMessage(message)
await self.dismissHomeMessage(message)
if remoteMessage.isMetricsEnabled {
pixelFiring.fire(.remoteMessageDismissed,
withAdditionalParameters: self.additionalParameters(for: remoteMessage.id))
Expand Down
Loading

0 comments on commit 698f13f

Please sign in to comment.