From db71ebdff2cfa7eb3b1e5d6e91a7621383d3f8ac Mon Sep 17 00:00:00 2001 From: YehorKyrylov <> Date: Fri, 23 Dec 2022 11:15:35 +0100 Subject: [PATCH] update after test review --- Sources/PandaSDK/ConfiguredPanda.swift | 13 +---- .../PandaSDK/Views/WebViewController.swift | 49 ++++++++++++++----- Sources/PandaSDK/Views/WebViewModel.swift | 4 +- 3 files changed, 41 insertions(+), 25 deletions(-) diff --git a/Sources/PandaSDK/ConfiguredPanda.swift b/Sources/PandaSDK/ConfiguredPanda.swift index 81fedcf..c0b4a2a 100644 --- a/Sources/PandaSDK/ConfiguredPanda.swift +++ b/Sources/PandaSDK/ConfiguredPanda.swift @@ -544,18 +544,7 @@ final public class Panda: PandaProtocol, ObserverSupport { pandaLog("onViewDidAppear \(String(describing: screenName)) \(String(describing: screenId))") self?.send(event: .screenShowed(screenId: screenId ?? "", screenName: screenName ?? "", source: entryPoint, course: course)) } - viewModel.onDidFinishLoading = { [weak self] screenId, screenName, course, view in - self?.appStoreClient.isNeedToHideTrialPurchasesOnPandaScreen { result in - switch result { - case let .success(isNeedToHide): - if isNeedToHide { - view.hideTrialPurchases() - } - - case let .failure(error): - pandaLog(error.localizedDescription) - } - } + viewModel.onDidFinishLoading = { [weak self] screenId, screenName, course in pandaLog("onDidFinishLoading \(String(describing: screenName)) \(String(describing: screenId))") self?.send(event: .screenLoaded(screenId: screenId ?? "", screenName: screenName ?? "", source: entryPoint)) diff --git a/Sources/PandaSDK/Views/WebViewController.swift b/Sources/PandaSDK/Views/WebViewController.swift index b98f9e1..f568c38 100644 --- a/Sources/PandaSDK/Views/WebViewController.swift +++ b/Sources/PandaSDK/Views/WebViewController.swift @@ -326,7 +326,7 @@ final class WebViewController: UIViewController, WKScriptMessageHandler { if let e = error { pandaLog("error: \(e)") } else if self.viewModel?.screenData.id.string == "89a4b8c2-cb7b-45a5-a8df-f5a8ffd32618" { - self.viewModel?.onDidFinishLoading?(self.viewModel?.screenData.id.string, "Tutors-Phone-Collection-v2-Schedule", (self.viewModel?.payload?.data?["course"] as? String), self) + self.viewModel?.onDidFinishLoading?(self.viewModel?.screenData.id.string, "Tutors-Phone-Collection-v2-Schedule", (self.viewModel?.payload?.data?["course"] as? String)) } if let res = result { @@ -352,22 +352,24 @@ final class WebViewController: UIViewController, WKScriptMessageHandler { func didFinishLoading(_ url: URL?) { guard let url = url else { - viewModel?.onDidFinishLoading?(viewModel?.screenData.id.string, viewModel?.screenData.name, (viewModel?.payload?.data?["course"] as? String), self) + viewModel?.onDidFinishLoading?(viewModel?.screenData.id.string, viewModel?.screenData.name, (viewModel?.payload?.data?["course"] as? String)) return } let urlComps = URLComponents(url: url, resolvingAgainstBaseURL: true) let screenID = urlComps?.queryItems?.first(where: { $0.name == "screen_id" })?.value ?? viewModel?.screenData.id.string let screenName = urlComps?.queryItems?.first(where: { $0.name == "screen_name" })?.value ?? viewModel?.screenData.name - viewModel?.onDidFinishLoading?(screenID, screenName, (viewModel?.payload?.data?["course"] as? String), self) + viewModel?.onDidFinishLoading?(screenID, screenName, (viewModel?.payload?.data?["course"] as? String)) } func handleScreenDidLoad() { NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(failedByTimeOut), object: nil) setPayload() - - wv.alpha = 1 - loadingIndicator.stopAnimating() + hideTrialPurchasesIfNeeded { [weak self] in + self?.wv.alpha = 1 + self?.loadingIndicator.stopAnimating() + } + pandaLog("html did load \(Date().timeIntervalSince1970) \(Date())") } @@ -654,12 +656,33 @@ extension WebViewController { } } - func hideTrialPurchases() { - DispatchQueue.main.async { - self.wv.evaluateJavaScript("removeTrialUi()") { _, error in - if let error = error { - pandaLog(error.localizedDescription) + func hideTrialPurchasesIfNeeded(actionAfter: @escaping (() -> Void)) { + guard + let panda = Panda.shared as? Panda + else { + actionAfter() + return + } + + panda.appStoreClient.isNeedToHideTrialPurchasesOnPandaScreen { result in + switch result { + case let .success(isNeedToHide): + switch isNeedToHide { + case true: + DispatchQueue.main.async { [weak self] in + self?.wv.evaluateJavaScript(Constants.hideTrialJSFunctionName) { _, error in + if let error = error { + pandaLog(error.localizedDescription) + } + actionAfter() + } + } + case false: + actionAfter() } + case let .failure(error): + pandaLog(error.localizedDescription) + actionAfter() } } } @@ -823,4 +846,8 @@ extension WebViewController { case onPolicy case onSubscriptionTerms } + + private enum Constants { + static let hideTrialJSFunctionName = "removeTrialUi" + } } diff --git a/Sources/PandaSDK/Views/WebViewModel.swift b/Sources/PandaSDK/Views/WebViewModel.swift index fb97705..629d84c 100644 --- a/Sources/PandaSDK/Views/WebViewModel.swift +++ b/Sources/PandaSDK/Views/WebViewModel.swift @@ -14,7 +14,7 @@ protocol WebViewModelProtocol { var onApplePayPurchase: ((_ pandaID: String?, _ source: String, _ screenId: String, _ screenName: String, _ viewController: WebViewController) -> Void)! { get set } var onViewWillAppear: ((_ screenId: String?, _ screenName: String?) -> Void)? { get set } var onViewDidAppear: ((_ screenId: String?, _ screenName: String?, _ course: String?) -> Void)? { get set } - var onDidFinishLoading: ((_ screenId: String?, _ screenName: String?, _ course: String?, _ viewController: WebViewController) -> Void)? { get set } + var onDidFinishLoading: ((_ screenId: String?, _ screenName: String?, _ course: String?) -> Void)? { get set } var onBillingIssue: ((_ viewController: WebViewController) -> Void)? { get set } var onRestorePurchase: ((_ viewController: WebViewController, _ screenId: String?, _ screenName: String?) -> Void)? { get set } var onTerms: (() -> Void)? { get set } @@ -41,7 +41,7 @@ final class WebViewModel: WebViewModelProtocol { @objc var onApplePayPurchase: ((String?, String, String, String, WebViewController) -> Void)! var onViewWillAppear: ((_ screenId: String?, _ screenName: String?) -> Void)? var onViewDidAppear: ((_ screenId: String?, _ screenName: String?, _ course: String?) -> Void)? - var onDidFinishLoading: ((_ screenId: String?, _ screenName: String?, _ course: String?, _ viewController: WebViewController) -> Void)? + var onDidFinishLoading: ((_ screenId: String?, _ screenName: String?, _ course: String?) -> Void)? var onBillingIssue: ((_ viewController: WebViewController) -> Void)? var onRestorePurchase: ((_ viewController: WebViewController, _ screenId: String?, _ screenName: String?) -> Void)? var onTerms: (() -> Void)?