Skip to content

Commit

Permalink
Merge pull request #1703 from p2p-org/feature/pwn-966
Browse files Browse the repository at this point in the history
[ETH-966] New action for referral bridge
  • Loading branch information
bigearsenal authored Feb 16, 2024
2 parents 41dc5ea + 9c412f1 commit 9b2b414
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ enum ReferralBridgeMethod: String {
case signMessage
case getUserPublicKey
case openTermsUrl
case navigateToSwap
}

enum ReferralBridgeError: String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@ window.ReferralBridge = {
},
openTermsUrl: function(link) {
handleRequest({ method: "openTermsUrl", link: link });
},
navigateToSwap: function() {
handleRequest({ method: "navigateToSwap" });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import TweetNacl
import WebKit

protocol ReferralBridge {
var sharePublisher: AnyPublisher<String, Never> { get }
var openTermsUrl: AnyPublisher<URL, Never> { get }
var actionPublisher: AnyPublisher<ReferralBridgeAction, Never> { get }
}

enum ReferralBridgeAction {
case openShare(String)
case openTerms(URL)
case openSwap
}

final class ReferralJSBridge: NSObject, ReferralBridge {
var sharePublisher: AnyPublisher<String, Never> { shareSubject.eraseToAnyPublisher() }
var openTermsUrl: AnyPublisher<URL, Never> { openTermsUrlSubject.eraseToAnyPublisher() }
var actionPublisher: AnyPublisher<ReferralBridgeAction, Never> { actionSubject.eraseToAnyPublisher() }

// MARK: - Dependencies

Expand All @@ -22,8 +26,8 @@ final class ReferralJSBridge: NSObject, ReferralBridge {

// MARK: - Properties

private let shareSubject = PassthroughSubject<String, Never>()
private let openTermsUrlSubject = PassthroughSubject<URL, Never>()
private let actionSubject = PassthroughSubject<ReferralBridgeAction, Never>()

private var subscriptions: [AnyCancellable] = []
private weak var webView: WKWebView?

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,29 @@ final class ReferralProgramCoordinator: Coordinator<Void> {
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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ final class ReferralProgramViewModel: BaseViewModel, ObservableObject {
let bridge: ReferralJSBridge
let webView: WKWebView

let openShare = PassthroughSubject<String, Never>()
let openTerms = PassthroughSubject<URL, Never>()

override init() {
let wkWebView = ReferralProgramViewModel.buildWebView()
webView = wkWebView
Expand All @@ -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 {
Expand Down

0 comments on commit 9b2b414

Please sign in to comment.