Skip to content

Commit

Permalink
MOB-4891 reduce number of calls to quotes api (#612)
Browse files Browse the repository at this point in the history
* reduce number of quotes api calls

* fix quotes reloading when returning from the address component
  • Loading branch information
dianakarh authored Sep 22, 2023
1 parent 0caceae commit 2c868ec
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
29 changes: 20 additions & 9 deletions KarhooUISDK/Screens/QuoteList/KarhooQuoteListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ final class KarhooQuoteListViewModel: QuoteListViewModel {
private var quotesObserver: KarhooSDK.Observer<Quotes>?
private var quoteSearchObservable: KarhooSDK.Observable<Quotes>?
private var quotesSearchForDetailsInProgress: JourneyDetails?
private var currentlyUsedJourneyDetails: JourneyDetails?
private var selectedQuoteOrder: QuoteListSortOrder = .price
private let quoteSorter: QuoteSorter
private let quoteFilter: QuoteFilterHandler
Expand Down Expand Up @@ -56,39 +57,43 @@ 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)
}
}

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())
}
}

func viewWillDisappear() {
isViewVisible = false
unsubscribeFromBecomeAndResignActiveNotifications()
reportHowManyQuotesHasBeenShown()
cleanup()
}

private func cleanup() {
journeyDetailsManager.remove(observer: self)
quoteSearchObservable?.unsubscribe(observer: quotesObserver)
unsubscribeFromBecomeAndResignActiveNotifications()
}

// MARK: - Endpoints
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
8 changes: 4 additions & 4 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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 `./`)
Expand All @@ -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
Expand All @@ -132,6 +132,6 @@ SPEC CHECKSUMS:
SwiftFormat: b2062ab08a5c83c075e32355175c58b99e52187f
SwiftLint: 1b7561918a19e23bfed960e40759086e70f4dba5

PODFILE CHECKSUM: cf38654a5e8510ed542d9041b5d68b8d9cb61ea2
PODFILE CHECKSUM: a7ff5d1f3f24eef19cd4bc4e247d05b3e40d44de

COCOAPODS: 1.12.1

0 comments on commit 2c868ec

Please sign in to comment.