Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
// periphery:ignore:all - will be used for booking filters
import Foundation

/// Used to filter bookings by product
///
public struct BookingProductFilter: Codable, Hashable {
/// The underlying product
public let product: Product
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small concern — I wonder if we really need to store the entire Product here. Having both product, id, and name feels a bit redundant and might make it easier for other parts of the code to start depending on BookingProductFilter for full product data instead of just treating it as a lightweight filter value.

Would it make sense for this type to only keep the minimal info needed for filtering (e.g. id and name) and drop the product property? That would keep the model focused on its purpose and avoid any unintended coupling later on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a valid point! I added an update in b6ed021 with the following changes:

  • Removed the full product model from BookingProductFilter.
  • Updated SyncableListSelectorView to accept a closure for checking initial selection rather than comparing full models.
  • Updated FilterListViewController following the selection logic change.


/// ID of the product
/// periphery:ignore - to be used later when applying filter
///
public let id: Int64

/// Name of the product
///
public let name: String

public init(id: Int64,
name: String) {
self.id = id
self.name = name
public init(product: Product) {
self.id = product.productID
self.name = product.name
self.product = product
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import Foundation
import Yosemite

/// Syncable implementation for booking services/events (bookable product) filtering
struct BookableProductListSyncable: ListSyncable {
typealias StorageType = StorageProduct
typealias ModelType = Product

let siteID: Int64

var title: String { Localization.title }

var emptyStateMessage: String { Localization.noMembersFound }

// MARK: - ResultsController Configuration

func createPredicate() -> NSPredicate {
NSPredicate(format: "siteID == %lld AND productTypeKey == %@", siteID, ProductType.booking.rawValue)
}

func createSortDescriptors() -> [NSSortDescriptor] {
[NSSortDescriptor(key: "productID", ascending: false)]
}

// MARK: - Sync Configuration

func createSyncAction(
pageNumber: Int,
pageSize: Int,
completion: @escaping (Result<Bool, Error>) -> Void
) -> Action {
ProductAction.synchronizeProducts(
siteID: siteID,
pageNumber: pageNumber,
pageSize: pageSize,
stockStatus: nil,
productStatus: nil,
productType: .booking,
productCategory: nil,
sortOrder: .dateDescending,
productIDs: [],
excludedProductIDs: [],
shouldDeleteStoredProductsOnFirstPage: true,
onCompletion: completion
)
}

// MARK: - Display Configuration

func displayName(for item: Product) -> String {
item.name
}
}

private extension BookableProductListSyncable {
enum Localization {
static let title = NSLocalizedString(
"bookingServiceEventSelectorView.title",
value: "Service / Event",
comment: "Title of the booking service/event selector view"
)
static let noMembersFound = NSLocalizedString(
"bookingServiceEventSelectorView.noMembersFound",
value: "No service or event found",
comment: "Text on the empty view of the booking service/event selector view"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ final class BookingFiltersViewModel: FilterListViewModel {
filterTypeViewModels = [
teamMemberFilterViewModel,
productFilterViewModel,
customerFilterViewModel,
attendanceStatusFilterViewModel,
paymentStatusFilterViewModel,
customerFilterViewModel,
dateTimeFilterViewModel
]
}
Expand Down Expand Up @@ -188,11 +188,11 @@ extension BookingFiltersViewModel.BookingListFilter {
selectedValue: filters.teamMember)
case .product(let siteID):
return FilterTypeViewModel(title: title,
listSelectorConfig: .products(siteID: siteID),
listSelectorConfig: .bookableProduct(siteID: siteID),
selectedValue: filters.product)
case .customer(let siteID):
return FilterTypeViewModel(title: title,
listSelectorConfig: .customer(siteID: siteID),
listSelectorConfig: .customer(siteID: siteID, source: .booking),
selectedValue: filters.customer)
case .attendanceStatus:
let options: [BookingAttendanceStatus?] = [nil, .booked, .checkedIn, .cancelled, .noShow]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Foundation

extension CustomerSelectorViewController.Configuration {
static let configurationForBookingFilter = CustomerSelectorViewController.Configuration(
title: BookingFilterLocalization.customerSelectorTitle,
disallowSelectingGuest: true,
guestDisallowedMessage: BookingFilterLocalization.guestSelectionDisallowedError,
disallowCreatingCustomer: true,
showGuestLabel: true,
shouldTrackCustomerAdded: false,
isModal: false,
hideDetailText: true
)

enum BookingFilterLocalization {
static let customerSelectorTitle = NSLocalizedString(
"configurationForBookingFilter.customerName",
value: "Customer name",
comment: "Title for the screen to select customer in booking filtering."
)
static let guestSelectionDisallowedError = NSLocalizedString(
"configurationForBookingFilter.guestSelectionDisallowedError",
value: "This user is a guest, and guests can’t be used for filtering bookings.",
comment: "Error message when selecting guest customer in booking filtering"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ enum FilterListValueSelectorConfig {
// Filter list selector for products
case products(siteID: Int64)
// Filter list selector for customer
case customer(siteID: Int64)
case customer(siteID: Int64, source: FilterSource)
// Filter list selector for booking team member
case bookingResource(siteID: Int64)
// Filter list selector for bookable product
case bookableProduct(siteID: Int64)

}

Expand Down Expand Up @@ -341,26 +343,31 @@ private extension FilterListViewController {
}()
self.listSelector.present(controller, animated: true)

case .customer(let siteID):
case .customer(let siteID, let source):
let configuration: CustomerSelectorViewController.Configuration = {
switch source {
case .booking: .configurationForBookingFilter
case .orders: .configurationForOrderFilter
case .products: fatalError("Customer filter not supported!")
}
}()
let selectedCustomerID = (selected.selectedValue as? CustomerFilter)?.id
let controller: CustomerSelectorViewController = {
return CustomerSelectorViewController(
siteID: siteID,
configuration: .configurationForOrderFilter,
configuration: configuration,
addressFormViewModel: nil,
selectedCustomerID: selectedCustomerID,
onCustomerSelected: { [weak self] customer in
selected.selectedValue = CustomerFilter(customer: customer)

self?.updateUI(numberOfActiveFilters: self?.viewModel.filterTypeViewModels.numberOfActiveFilters ?? 0)
self?.listSelector.reloadData()
self?.listSelector.dismiss(animated: true)
}
)
}()

self.listSelector.present(
WooNavigationController(rootViewController: controller),
animated: true
)
self.listSelector.navigationController?.pushViewController(controller, animated: true)
case .bookingResource(let siteID):
let selectedMember = selected.selectedValue as? BookingResource
let syncable = TeamMemberListSyncable(siteID: siteID)
Expand All @@ -373,7 +380,26 @@ private extension FilterListViewController {
selected.selectedValue = resource
self?.updateUI(numberOfActiveFilters: self?.viewModel.filterTypeViewModels.numberOfActiveFilters ?? 0)
self?.listSelector.reloadData()
self?.listSelector.navigationController?.popViewController(animated: true)
}
)
let hostingController = UIHostingController(rootView: memberListSelectorView)
listSelector.navigationController?.pushViewController(hostingController, animated: true)

case .bookableProduct(let siteID):
let selectedProduct = selected.selectedValue as? BookingProductFilter
let syncable = BookableProductListSyncable(siteID: siteID)
let viewModel = SyncableListSelectorViewModel(syncable: syncable)
let memberListSelectorView = SyncableListSelectorView(
viewModel: viewModel,
syncable: syncable,
selectedItem: selectedProduct?.product,
onSelection: { [weak self] product in
selected.selectedValue = {
guard let product else { return BookingProductFilter?.none }
return BookingProductFilter(product: product)
}()
self?.updateUI(numberOfActiveFilters: self?.viewModel.filterTypeViewModels.numberOfActiveFilters ?? 0)
self?.listSelector.reloadData()
}
)
let hostingController = UIHostingController(rootView: memberListSelectorView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,20 @@ final class CustomerSearchUICommand: SearchUICommand {
// Whether to hide button for creating customer in empty state
private let disallowCreatingCustomer: Bool

// Whether to hide the detail text (username or "Guest") in each customer row
private let hideDetailText: Bool

// The currently selected customer ID to show checkmark
private var selectedCustomerID: Int64?

init(siteID: Int64,
loadResultsWhenSearchTermIsEmpty: Bool = false,
showSearchFilters: Bool = false,
showGuestLabel: Bool = false,
shouldTrackCustomerAdded: Bool = true,
disallowCreatingCustomer: Bool = false,
hideDetailText: Bool = false,
selectedCustomerID: Int64? = nil,
stores: StoresManager = ServiceLocator.stores,
analytics: Analytics = ServiceLocator.analytics,
featureFlagService: FeatureFlagService = ServiceLocator.featureFlagService,
Expand All @@ -80,6 +88,8 @@ final class CustomerSearchUICommand: SearchUICommand {
self.showGuestLabel = showGuestLabel
self.shouldTrackCustomerAdded = shouldTrackCustomerAdded
self.disallowCreatingCustomer = disallowCreatingCustomer
self.hideDetailText = hideDetailText
self.selectedCustomerID = selectedCustomerID
self.stores = stores
self.analytics = analytics
self.featureFlagService = featureFlagService
Expand All @@ -89,6 +99,10 @@ final class CustomerSearchUICommand: SearchUICommand {
self.onDidFinishSyncingAllCustomersFirstPage = onDidFinishSyncingAllCustomersFirstPage
}

func updateSelectedCustomerID(_ customerID: Int64?) {
self.selectedCustomerID = customerID
}

var hideCancelButton: Bool {
featureFlagService.isFeatureFlagEnabled(.betterCustomerSelectionInOrder)
}
Expand Down Expand Up @@ -143,9 +157,12 @@ final class CustomerSearchUICommand: SearchUICommand {
let storageManager = ServiceLocator.storageManager
let predicate = NSPredicate(format: "siteID == %lld", siteID)
let newCustomerSelectorIsEnabled = featureFlagService.isFeatureFlagEnabled(.betterCustomerSelectionInOrder)
let descriptor = newCustomerSelectorIsEnabled ?
NSSortDescriptor(keyPath: \StorageCustomer.firstName, ascending: true) : NSSortDescriptor(keyPath: \StorageCustomer.customerID, ascending: false)
return ResultsController<StorageCustomer>(storageManager: storageManager, matching: predicate, sortedBy: [descriptor])
let descriptors = newCustomerSelectorIsEnabled ?
[
NSSortDescriptor(keyPath: \StorageCustomer.firstName, ascending: true),
NSSortDescriptor(keyPath: \StorageCustomer.customerID, ascending: true)
] : [NSSortDescriptor(keyPath: \StorageCustomer.customerID, ascending: false)]
return ResultsController<StorageCustomer>(storageManager: storageManager, matching: predicate, sortedBy: descriptors)
}

func createStarterViewController() -> UIViewController? {
Expand All @@ -171,7 +188,11 @@ final class CustomerSearchUICommand: SearchUICommand {
}

func createCellViewModel(model: Customer) -> UnderlineableTitleAndSubtitleAndDetailTableViewCell.ViewModel {
let detail = showGuestLabel && model.customerID == 0 ? Localization.guestLabel : model.username ?? ""
let detail: String = {
guard !hideDetailText else { return "" }
return showGuestLabel && model.customerID == 0 ? Localization.guestLabel : model.username ?? ""
}()
let isSelected = selectedCustomerID == model.customerID

return CellViewModel(
id: "\(model.customerID)",
Expand All @@ -181,7 +202,8 @@ final class CustomerSearchUICommand: SearchUICommand {
subtitle: model.email,
accessibilityLabel: "",
detail: detail,
underlinedText: searchTerm?.count ?? 0 > 1 ? searchTerm : "" // Only underline the search term if it's longer than 1 character
underlinedText: searchTerm?.count ?? 0 > 1 ? searchTerm : "", // Only underline the search term if it's longer than 1 character
isSelected: isSelected
)
}

Expand Down
Loading