Skip to content

Commit

Permalink
Merge pull request #1706 from p2p-org/feature/pwn-979
Browse files Browse the repository at this point in the history
[ETH-979] Navigate to main if referrer deeplink is used
  • Loading branch information
lisemyon authored Feb 16, 2024
2 parents 9b2b414 + 536434d commit e21270b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/marmelroy/PhoneNumberKit.git",
"state" : {
"revision" : "7268078304df2f4e07ac864b5ba27ae4c1e6f4a6",
"version" : "3.7.7"
"revision" : "301e2c1931ab294aa7c1bbabf2ed5fe7a78f1549",
"version" : "3.7.8"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ final class DeeplinkAppDelegateService: NSObject, AppDelegateService {
}

if urlComponents.host == "r.key.app" {
setReferrerIfNeeded(r: String(urlComponents.path.dropFirst()))
GlobalAppState.shared.referrerUrl = urlComponents.url
}
}

Expand Down Expand Up @@ -127,16 +127,7 @@ final class DeeplinkAppDelegateService: NSObject, AppDelegateService {
}
// keyapp://referral
else if scheme == "keyapp", host == "referral" {
setReferrerIfNeeded(r: components.path)
}
}

private func setReferrerIfNeeded(r: String) {
Task {
let _ = await RemoteConfigWarmupProcess().start()
guard available(.referralProgramEnabled) else { return }
let referralService: ReferralProgramService = Resolver.resolve()
_ = await referralService.setReferent(from: r)
GlobalAppState.shared.referrerUrl = components.url
}
}
}
Expand Down
1 change: 1 addition & 0 deletions p2p_wallet/Common/Services/GlobalAppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class GlobalAppState: ObservableObject {
@Published var surveyID: String?
@Published var sendViaLinkUrl: URL?
@Published var swapUrl: URL?
@Published var referrerUrl: URL?

private init() {
if let forcedValue = Defaults.forcedNameServiceEndpoint {
Expand Down
30 changes: 25 additions & 5 deletions p2p_wallet/Scenes/TabBar/TabBarCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,13 @@ final class TabBarCoordinator: Coordinator<Void> {
let urlComponent = URLComponents(url: url, resolvingAgainstBaseURL: true)
let from = urlComponent?.queryItems?.first { $0.name == "from" }?.value
let to = urlComponent?.queryItems?.first { $0.name == "to" }?.value
let r = urlComponent?.queryItems?.first { $0.name == "r" }?.value

if from == nil, to == nil {
return
}

Task {
guard available(.referralProgramEnabled), let r else { return }
let referralService: ReferralProgramService = Resolver.resolve()
_ = await referralService.setReferent(from: r)
if let r = urlComponent?.queryItems?.first(where: { $0.name == "r" })?.value {
setReferrerIfNeeded(r: r)
}

self.routeToSwap(
Expand All @@ -125,6 +122,21 @@ final class TabBarCoordinator: Coordinator<Void> {
}
.store(in: &subscriptions)

tabBarViewModel.moveToMain
.filter { _ in available(.referralProgramEnabled) }
.compactMap { URLComponents(url: $0, resolvingAgainstBaseURL: true)?.path.dropFirst() }
.sink { [weak self] referrer in
guard let self else { return }

UIApplication.dismissCustomPresentedViewController {
self.navigationControllerForSelectedTab()?.popToRootViewController(animated: true)
self.tabBarController.changeItem(to: .wallet)
}

setReferrerIfNeeded(r: String(referrer))
}
.store(in: &subscriptions)

listenToWallet()
}

Expand Down Expand Up @@ -243,4 +255,12 @@ final class TabBarCoordinator: Coordinator<Void> {
})
.store(in: &subscriptions)
}

private func setReferrerIfNeeded(r: String) {
Task {
guard available(.referralProgramEnabled) else { return }
let referralService: ReferralProgramService = Resolver.resolve()
_ = await referralService.setReferent(from: r)
}
}
}
15 changes: 15 additions & 0 deletions p2p_wallet/Scenes/TabBar/TabBarViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ extension TabBarViewModel {
.eraseToAnyPublisher()
}

var moveToMain: AnyPublisher<URL, Never> {
Publishers.CombineLatest(
authenticationStatusPublisher,
becomeActiveSubject
)
.debounce(for: .milliseconds(900), scheduler: RunLoop.main)
.filter { $0.0 == nil }
.compactMap { _ in GlobalAppState.shared.referrerUrl }
.handleEvents(receiveOutput: { _ in
GlobalAppState.shared.referrerUrl = nil
})
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}

var moveToSendViaLinkClaim: AnyPublisher<URL, Never> {
Publishers.CombineLatest(
authenticationStatusPublisher,
Expand Down

0 comments on commit e21270b

Please sign in to comment.