diff --git a/p2p_wallet/Scenes/Main/ReferralProgram/Details/Bridge/BridgeModels.swift b/p2p_wallet/Scenes/Main/ReferralProgram/Details/Bridge/BridgeModels.swift index f88820b65..da97f459a 100644 --- a/p2p_wallet/Scenes/Main/ReferralProgram/Details/Bridge/BridgeModels.swift +++ b/p2p_wallet/Scenes/Main/ReferralProgram/Details/Bridge/BridgeModels.swift @@ -4,6 +4,7 @@ enum ReferralBridgeMethod: String { case signMessage case getUserPublicKey case openTermsUrl + case navigateToSwap } enum ReferralBridgeError: String { diff --git a/p2p_wallet/Scenes/Main/ReferralProgram/Details/Bridge/ReferralBridge.js b/p2p_wallet/Scenes/Main/ReferralProgram/Details/Bridge/ReferralBridge.js index dd7bdee14..954077376 100644 --- a/p2p_wallet/Scenes/Main/ReferralProgram/Details/Bridge/ReferralBridge.js +++ b/p2p_wallet/Scenes/Main/ReferralProgram/Details/Bridge/ReferralBridge.js @@ -34,5 +34,8 @@ window.ReferralBridge = { }, openTermsUrl: function(link) { handleRequest({ method: "openTermsUrl", link: link }); + }, + navigateToSwap: function() { + handleRequest({ method: "navigateToSwap" }); } } diff --git a/p2p_wallet/Scenes/Main/ReferralProgram/Details/Bridge/ReferralJSBridge.swift b/p2p_wallet/Scenes/Main/ReferralProgram/Details/Bridge/ReferralJSBridge.swift index 8c2fcbf92..8776f1c07 100644 --- a/p2p_wallet/Scenes/Main/ReferralProgram/Details/Bridge/ReferralJSBridge.swift +++ b/p2p_wallet/Scenes/Main/ReferralProgram/Details/Bridge/ReferralJSBridge.swift @@ -7,13 +7,17 @@ import TweetNacl import WebKit protocol ReferralBridge { - var sharePublisher: AnyPublisher { get } - var openTermsUrl: AnyPublisher { get } + var actionPublisher: AnyPublisher { get } +} + +enum ReferralBridgeAction { + case openShare(String) + case openTerms(URL) + case openSwap } final class ReferralJSBridge: NSObject, ReferralBridge { - var sharePublisher: AnyPublisher { shareSubject.eraseToAnyPublisher() } - var openTermsUrl: AnyPublisher { openTermsUrlSubject.eraseToAnyPublisher() } + var actionPublisher: AnyPublisher { actionSubject.eraseToAnyPublisher() } // MARK: - Dependencies @@ -22,8 +26,8 @@ final class ReferralJSBridge: NSObject, ReferralBridge { // MARK: - Properties - private let shareSubject = PassthroughSubject() - private let openTermsUrlSubject = PassthroughSubject() + private let actionSubject = PassthroughSubject() + private var subscriptions: [AnyCancellable] = [] private weak var webView: WKWebView? @@ -103,7 +107,7 @@ extension ReferralJSBridge: WKScriptMessageHandlerWithReply { switch method { case .showShareDialog: if let link = dict["link"] as? String { - shareSubject.send(link) + actionSubject.send(.openShare(link)) handler(link, nil) } else { handler(nil, .emptyLink) @@ -138,11 +142,14 @@ extension ReferralJSBridge: WKScriptMessageHandlerWithReply { handler(user.account.publicKey.base58EncodedString, nil) case .openTermsUrl: if let link = dict["link"] as? String, let url = URL(string: link) { - openTermsUrlSubject.send(url) + actionSubject.send(.openTerms(url)) handler(link, nil) } else { handler(nil, .emptyLink) } + case .navigateToSwap: + actionSubject.send(.openSwap) + handler("", nil) } } } diff --git a/p2p_wallet/Scenes/Main/ReferralProgram/Details/ReferralProgramCoordinator.swift b/p2p_wallet/Scenes/Main/ReferralProgram/Details/ReferralProgramCoordinator.swift index 34e6a0390..dbd598a09 100644 --- a/p2p_wallet/Scenes/Main/ReferralProgram/Details/ReferralProgramCoordinator.swift +++ b/p2p_wallet/Scenes/Main/ReferralProgram/Details/ReferralProgramCoordinator.swift @@ -17,21 +17,29 @@ final class ReferralProgramCoordinator: Coordinator { vc.hidesBottomBarWhenPushed = true navigationController.pushViewController(vc, animated: true) - viewModel.openShare - .sink { [weak vc] link in - let activityVC = UIActivityViewController( - activityItems: [link], - applicationActivities: nil - ) - vc?.present(activityVC, animated: true) - } - .store(in: &subscriptions) - - viewModel.openTerms - .sink { [weak self] url in - guard let self else { return } - coordinate(to: TermsAndConditionsCoordinator(navigationController: self.navigationController, url: url)) + viewModel.bridge.actionPublisher + .sink { [weak self, weak vc] action in + guard let self, let vc else { return } + switch action { + case let .openShare(string): + let activityVC = UIActivityViewController( + activityItems: [string], + applicationActivities: nil + ) + vc.present(activityVC, animated: true) + case let .openTerms(url): + coordinate(to: TermsAndConditionsCoordinator( + navigationController: self.navigationController, + url: url + )) + .sink(receiveValue: {}).store(in: &self.subscriptions) + case .openSwap: + coordinate(to: JupiterSwapCoordinator( + navigationController: self.navigationController, + params: .init(dismissAfterCompletion: true, openKeyboardOnStart: true, source: .deeplink) + )) .sink(receiveValue: {}).store(in: &self.subscriptions) + } } .store(in: &subscriptions) diff --git a/p2p_wallet/Scenes/Main/ReferralProgram/Details/ReferralProgramViewModel.swift b/p2p_wallet/Scenes/Main/ReferralProgram/Details/ReferralProgramViewModel.swift index 22644c4d0..471b018c1 100644 --- a/p2p_wallet/Scenes/Main/ReferralProgram/Details/ReferralProgramViewModel.swift +++ b/p2p_wallet/Scenes/Main/ReferralProgram/Details/ReferralProgramViewModel.swift @@ -11,9 +11,6 @@ final class ReferralProgramViewModel: BaseViewModel, ObservableObject { let bridge: ReferralJSBridge let webView: WKWebView - let openShare = PassthroughSubject() - let openTerms = PassthroughSubject() - override init() { let wkWebView = ReferralProgramViewModel.buildWebView() webView = wkWebView @@ -22,18 +19,6 @@ final class ReferralProgramViewModel: BaseViewModel, ObservableObject { super.init() bridge.inject() - - bridge.sharePublisher - .sink(receiveValue: { [weak self] value in - self?.openShare.send(value) - }) - .store(in: &subscriptions) - - bridge.openTermsUrl - .sink(receiveValue: { [weak self] value in - self?.openTerms.send(value) - }) - .store(in: &subscriptions) } private static func buildWebView() -> WKWebView {