From 2c868ec0a23b1da74baba840b69aa5ea0c97bd14 Mon Sep 17 00:00:00 2001 From: dianakarh <86302636+dianakarh@users.noreply.github.com> Date: Fri, 22 Sep 2023 15:07:03 +0300 Subject: [PATCH] MOB-4891 reduce number of calls to quotes api (#612) * reduce number of quotes api calls * fix quotes reloading when returning from the address component --- .../QuoteList/KarhooQuoteListViewModel.swift | 29 +++++++++++++------ Podfile | 2 +- Podfile.lock | 8 ++--- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/KarhooUISDK/Screens/QuoteList/KarhooQuoteListViewModel.swift b/KarhooUISDK/Screens/QuoteList/KarhooQuoteListViewModel.swift index 58fb3b2c3..52be66944 100644 --- a/KarhooUISDK/Screens/QuoteList/KarhooQuoteListViewModel.swift +++ b/KarhooUISDK/Screens/QuoteList/KarhooQuoteListViewModel.swift @@ -21,6 +21,7 @@ final class KarhooQuoteListViewModel: QuoteListViewModel { private var quotesObserver: KarhooSDK.Observer? private var quoteSearchObservable: KarhooSDK.Observable? private var quotesSearchForDetailsInProgress: JourneyDetails? + private var currentlyUsedJourneyDetails: JourneyDetails? private var selectedQuoteOrder: QuoteListSortOrder = .price private let quoteSorter: QuoteSorter private let quoteFilter: QuoteFilterHandler @@ -56,7 +57,6 @@ final class KarhooQuoteListViewModel: QuoteListViewModel { self.quoteFilter = quoteFilter self.analytics = analytics self.onQuotesUpdated = onQuotesUpdated - journeyDetailsManager.add(observer: self) if let journeyDetails = journeyDetails { journeyDetailsManager.silentReset(with: journeyDetails) @@ -64,22 +64,21 @@ final class KarhooQuoteListViewModel: QuoteListViewModel { } deinit { - journeyDetailsManager.remove(observer: self) - quoteSearchObservable?.unsubscribe(observer: quotesObserver) - unsubscribeFromBecomeAndResignActiveNotifications() + cleanup() } func viewDidLoad() { } func viewWillAppear() { + journeyDetailsManager.add(observer: self) subscribeToBecomeAndResignActiveNotifications() isViewVisible = true guard let journeyDetails = journeyDetailsManager.getJourneyDetails() else { return } analytics.quoteListOpened(journeyDetails) - if shouldReloadQuotes() { + if shouldReloadExpiringQuotes() || journeyDetailsChanged() { quotesSearchForDetailsInProgress = nil journeyDetailsChanged(details: journeyDetailsManager.getJourneyDetails()) } @@ -87,8 +86,14 @@ final class KarhooQuoteListViewModel: QuoteListViewModel { func viewWillDisappear() { isViewVisible = false - unsubscribeFromBecomeAndResignActiveNotifications() reportHowManyQuotesHasBeenShown() + cleanup() + } + + private func cleanup() { + journeyDetailsManager.remove(observer: self) + quoteSearchObservable?.unsubscribe(observer: quotesObserver) + unsubscribeFromBecomeAndResignActiveNotifications() } // MARK: - Endpoints @@ -143,17 +148,21 @@ final class KarhooQuoteListViewModel: QuoteListViewModel { NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil) } - private func shouldReloadQuotes() -> Bool { + private func shouldReloadExpiringQuotes() -> Bool { guard let fireDate = dateOfListReceiving else { return true } let intervalToFire = fireDate.timeIntervalSinceNow // NOTE: 'fireDate.timeIntervalSinceNow' is negative when list is reloading and new timer is not started yet - return intervalToFire > 0 && intervalToFire < minimumAcceptedValidityToQuoteRefresh + return intervalToFire < minimumAcceptedValidityToQuoteRefresh + } + + private func journeyDetailsChanged() -> Bool { + currentlyUsedJourneyDetails != journeyDetailsManager.getJourneyDetails() } @objc func didChangeActivityState(_ notification: Notification) { if notification.name == UIApplication.didBecomeActiveNotification { isViewVisible = true - if shouldReloadQuotes() { + if shouldReloadExpiringQuotes() { quotesSearchForDetailsInProgress = nil journeyDetailsChanged(details: journeyDetailsManager.getJourneyDetails()) } @@ -253,6 +262,7 @@ final class KarhooQuoteListViewModel: QuoteListViewModel { onStateUpdated?(.loading) fetchedQuotes = nil quotesSearchForDetailsInProgress = details + currentlyUsedJourneyDetails = details quoteSearchObservable = quoteService.quotes(quoteSearch: quoteSearch).observable(pollTime: quoteListPollTime) quoteSearchObservable?.subscribe(observer: quotesObserver) } @@ -311,6 +321,7 @@ final class KarhooQuoteListViewModel: QuoteListViewModel { onStateUpdated?(.empty(reason: .noQuotesAfterFiltering)) case (_, _, .completed): quotesSearchForDetailsInProgress = nil + quoteSearchObservable?.unsubscribe(observer: quotesObserver) onStateUpdated?(.fetched(quotes: sortedQuotes)) case (_, _, .progressing) where newQuotes.all.isEmpty: onStateUpdated?(.loading) diff --git a/Podfile b/Podfile index 0f9c5326f..051643a29 100644 --- a/Podfile +++ b/Podfile @@ -40,7 +40,7 @@ end # UISDK framework target 'KarhooUISDK' do - pod 'KarhooSDK', :git => 'https://github.com/karhoo/karhoo-ios-sdk', :tag => '1.8.1' + pod 'KarhooSDK', :git => 'https://github.com/karhoo/karhoo-ios-sdk', :branch => 'master' # pod 'KarhooSDK', '1.8.1' pod 'SwiftLint', '~> 0.47' pod 'SwiftFormat/CLI', '~> 0.49' diff --git a/Podfile.lock b/Podfile.lock index d5e2ee4e6..17f2d2b79 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -79,7 +79,7 @@ DEPENDENCIES: - Adyen (= 4.7.2) - Braintree/PaymentFlow (~> 5.20.1) - BraintreeDropIn (~> 9.8.1) - - KarhooSDK (from `https://github.com/karhoo/karhoo-ios-sdk`, tag `1.8.1`) + - KarhooSDK (from `https://github.com/karhoo/karhoo-ios-sdk`, branch `master`) - KarhooUISDK (from `./`) - KarhooUISDK/Adyen (from `./`) - KarhooUISDK/Braintree (from `./`) @@ -106,15 +106,15 @@ SPEC REPOS: EXTERNAL SOURCES: KarhooSDK: + :branch: master :git: https://github.com/karhoo/karhoo-ios-sdk - :tag: 1.8.1 KarhooUISDK: :path: "./" CHECKOUT OPTIONS: KarhooSDK: + :commit: 9b13e1ef868c9f31553ffa08fa813a89ceb4003d :git: https://github.com/karhoo/karhoo-ios-sdk - :tag: 1.8.1 SPEC CHECKSUMS: Adyen: 2b1ff574b46d7af43717869859c686b843b8d772 @@ -132,6 +132,6 @@ SPEC CHECKSUMS: SwiftFormat: b2062ab08a5c83c075e32355175c58b99e52187f SwiftLint: 1b7561918a19e23bfed960e40759086e70f4dba5 -PODFILE CHECKSUM: cf38654a5e8510ed542d9041b5d68b8d9cb61ea2 +PODFILE CHECKSUM: a7ff5d1f3f24eef19cd4bc4e247d05b3e40d44de COCOAPODS: 1.12.1