Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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,19 @@
// periphery:ignore:all - will be used for booking filters
import Foundation

/// Used to filter bookings by product
///
public struct BookingProductFilter: Codable, Hashable {
/// ID of the product
/// periphery:ignore - to be used later when applying filter
///
public let id: Int64
public let productID: Int64

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

public init(id: Int64,
name: String) {
self.id = id
public init(productID: Int64, name: String) {
self.productID = productID
self.name = name
}
}
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 @@ -5,17 +5,18 @@ struct SyncableListSelectorView<Syncable: ListSyncable>: View {
@State var selectedItem: Syncable.ModelType?

private let syncable: Syncable
private let initialSelection: (Syncable.ModelType?) -> Bool
private let onSelection: (Syncable.ModelType?) -> Void

private let viewPadding: CGFloat = 16

init(viewModel: SyncableListSelectorViewModel<Syncable>,
syncable: Syncable,
selectedItem: Syncable.ModelType?,
initialSelection: @escaping (Syncable.ModelType?) -> Bool,
onSelection: @escaping (Syncable.ModelType?) -> Void) {
self.viewModel = viewModel
self.syncable = syncable
self.selectedItem = selectedItem
self.initialSelection = initialSelection
self.onSelection = onSelection
}

Expand Down Expand Up @@ -68,7 +69,7 @@ private extension SyncableListSelectorView {

ForEach(items, id: \.self) { item in
optionRow(text: syncable.displayName(for: item),
isSelected: item == selectedItem,
isSelected: isItemSelected(item),
onSelection: { selectedItem = item })
}

Expand All @@ -82,6 +83,13 @@ private extension SyncableListSelectorView {
.background(Color(.listBackground))
}

func isItemSelected(_ item: Syncable.ModelType?) -> Bool {
if let selectedItem {
return item == selectedItem
}
return initialSelection(item)
}

func optionRow(text: String, isSelected: Bool, onSelection: @escaping () -> Void) -> some View {
HStack {
Text(text)
Expand Down
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,39 +343,63 @@ 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)
let viewModel = SyncableListSelectorViewModel(syncable: syncable)
let memberListSelectorView = SyncableListSelectorView(
viewModel: viewModel,
syncable: syncable,
selectedItem: selectedMember,
initialSelection: { $0 == selectedMember },
onSelection: { [weak self] resource in
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,
initialSelection: { $0?.productID == selectedProduct?.productID },
onSelection: { [weak self] product in
selected.selectedValue = {
guard let product else { return BookingProductFilter?.none }
return BookingProductFilter(productID: product.productID, name: product.name)
}()
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