-
Notifications
You must be signed in to change notification settings - Fork 121
CIAB Bookings: Add eligibility check for Bookings tab #16139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
143eb23
Add new feature flag for ciab bookings tab
itsmeichigo b1fed43
Add logic for BookingsTabEligibilityChecker
itsmeichigo 0cb2ff5
Update tests
itsmeichigo dc2e280
Ignore periphery warnings
itsmeichigo d87bac3
Update periphery comment
itsmeichigo bd9d56d
optimize fetching products from storage when checking products
itsmeichigo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
WooCommerce/Classes/Bookings/BookingsTabEligibilityChecker.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| // periphery:ignore:all | ||
| import Foundation | ||
| import Yosemite | ||
| import Experiments | ||
|
|
||
| protocol BookingsTabEligibilityCheckerProtocol { | ||
| /// Checks the initial visibility of the Bookings tab. | ||
| func checkInitialVisibility() -> Bool | ||
| /// Checks the final visibility of the Bookings tab. | ||
| func checkVisibility() async -> Bool | ||
| } | ||
|
|
||
| final class BookingsTabEligibilityChecker: BookingsTabEligibilityCheckerProtocol { | ||
| private let site: Site | ||
| private let stores: StoresManager | ||
| private let featureFlagService: FeatureFlagService | ||
| private let ciabEligibilityChecker: CIABEligibilityCheckerProtocol | ||
| private let userDefaults: UserDefaults | ||
|
|
||
| init(site: Site, | ||
| stores: StoresManager = ServiceLocator.stores, | ||
| featureFlagService: FeatureFlagService = ServiceLocator.featureFlagService, | ||
| ciabEligibilityChecker: CIABEligibilityCheckerProtocol = CIABEligibilityChecker(), | ||
| userDefaults: UserDefaults = .standard) { | ||
| self.site = site | ||
| self.stores = stores | ||
| self.featureFlagService = featureFlagService | ||
| self.ciabEligibilityChecker = ciabEligibilityChecker | ||
| self.userDefaults = userDefaults | ||
| } | ||
|
|
||
| /// Checks the initial visibility of the Bookings tab using cached result. | ||
| func checkInitialVisibility() -> Bool { | ||
| userDefaults.loadCachedBookingsTabVisibility(siteID: site.siteID) | ||
| } | ||
|
|
||
| /// Checks the final visibility of the Bookings tab. | ||
| func checkVisibility() async -> Bool { | ||
| // Check feature flag | ||
| guard featureFlagService.isFeatureFlagEnabled(.ciabBookings) else { | ||
| return false | ||
| } | ||
|
|
||
| // Check if current site is NOT CIAB (bookings only for non-CIAB sites) | ||
| guard ciabEligibilityChecker.isSiteCIAB(site) else { | ||
| return false | ||
| } | ||
|
|
||
| // Cache the result | ||
| let isVisible = await checkIfStoreHasBookableProducts() | ||
| userDefaults.cacheBookingsTabVisibility(siteID: site.siteID, isVisible: isVisible) | ||
|
|
||
| return isVisible | ||
| } | ||
| } | ||
|
|
||
| // MARK: Private helpers | ||
| // | ||
| private extension BookingsTabEligibilityChecker { | ||
| @MainActor | ||
| func checkIfStoreHasBookableProducts() async -> Bool { | ||
| await withCheckedContinuation { continuation in | ||
| stores.dispatch(ProductAction.checkIfStoreHasProducts(siteID: site.siteID, type: .booking) { result in | ||
| let hasBookableProducts = (try? result.get()) ?? false | ||
| continuation.resume(returning: hasBookableProducts) | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| extension UserDefaults { | ||
| func loadCachedBookingsTabVisibility(siteID: Int64) -> Bool { | ||
| guard let cachedValue = self[.ciabBookingsTabAvailable] as? [String: Bool], | ||
| let availability = cachedValue[siteID.description] else { | ||
| return false | ||
| } | ||
| return availability | ||
| } | ||
|
|
||
| func cacheBookingsTabVisibility(siteID: Int64, isVisible: Bool) { | ||
| var cache = (self[.ciabBookingsTabAvailable] as? [String: Bool]) ?? [:] | ||
| cache[siteID.description] = isVisible | ||
| self[.ciabBookingsTabAvailable] = cache | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Should we also update
StorageType.loadProducts()and include product type into query instead of doing local filtering after a storage fetch.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good thinking! I optimized the code in bd9d56d, let me know what you think @RafaelKayumov.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thx!