Skip to content

Commit 7f42204

Browse files
committed
Add callback to pass Customer data between views
1 parent 1605feb commit 7f42204

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

WooCommerce/Classes/ViewRelated/Orders/Order Creation/CustomerSection/CustomerSearchUICommand.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ final class CustomerSearchUICommand: SearchUICommand {
1717

1818
var resynchronizeModels: (() -> Void) = {}
1919

20+
var onDidSelectSearchResult: ((Customer) -> Void)
21+
2022
private let siteID: Int64
2123

22-
init(siteID: Int64) {
24+
init(siteID: Int64, onDidSelectSearchResult: @escaping ((Customer) -> Void)) {
2325
self.siteID = siteID
26+
self.onDidSelectSearchResult = onDidSelectSearchResult
2427
}
2528

2629
func createResultsController() -> ResultsController<StorageCustomer> {
2730
let storageManager = ServiceLocator.storageManager
28-
let predicate = NSPredicate(format: "siteID == %lld", siteID)
2931
let descriptor = NSSortDescriptor(keyPath: \StorageCustomer.customerID, ascending: false)
3032
return ResultsController<StorageCustomer>(storageManager: storageManager, sortedBy: [descriptor])
3133
}
@@ -37,7 +39,7 @@ final class CustomerSearchUICommand: SearchUICommand {
3739
func createCellViewModel(model: Customer) -> TitleAndSubtitleAndStatusTableViewCell.ViewModel {
3840
return CellViewModel(
3941
id: "\(model.customerID)",
40-
title: "\(model.firstName ?? "") \(model.lastName ?? ""))",
42+
title: "\(model.firstName ?? "") \(model.lastName ?? "")",
4143
subtitle: model.email,
4244
accessibilityLabel: "",
4345
status: "",
@@ -59,7 +61,10 @@ final class CustomerSearchUICommand: SearchUICommand {
5961

6062
func didSelectSearchResult(model: Customer, from viewController: UIViewController, reloadData: () -> Void, updateActionButton: () -> Void) {
6163
// Not implemented yet
62-
print("Selected ID: \(model.customerID) - Name: \(String(describing: model.firstName))")
64+
print("1 - Customer tapped")
65+
print("2 - Customer ID: \(model.customerID) - Name: \(model.firstName ?? ""))")
66+
// Customer data will go up to EditOrderAddressForm, via OrderCustomerListView completion handler
67+
onDidSelectSearchResult(model)
6368
}
6469

6570
func searchResultsPredicate(keyword: String) -> NSPredicate? {

WooCommerce/Classes/ViewRelated/Orders/Order Creation/CustomerSection/OrderCustomerListView.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import Yosemite
23
import SwiftUI
34

45
/// `SwiftUI` wrapper for `SearchViewController` using `CustomerSearchUICommand`
@@ -7,11 +8,13 @@ struct OrderCustomerListView: UIViewControllerRepresentable {
78

89
let siteID: Int64
910

11+
let onCustomerTapped: ((Customer) -> Void)
12+
1013
func makeUIViewController(context: Context) -> WooNavigationController {
1114

1215
let viewController = SearchViewController(
1316
storeID: siteID,
14-
command: CustomerSearchUICommand(siteID: siteID),
17+
command: CustomerSearchUICommand(siteID: siteID, onDidSelectSearchResult: onCustomerTapped),
1518
cellType: TitleAndSubtitleAndStatusTableViewCell.self,
1619
// Must conform to SearchResultCell.
1720
// TODO: Proper cell for this cellType.

WooCommerce/Classes/ViewRelated/Orders/Order Details/Address Edit/EditOrderAddressForm.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ struct EditOrderAddressForm<ViewModel: AddressFormViewModelProtocol>: View {
171171
}
172172
.notice($viewModel.notice)
173173
.sheet(isPresented: $showingCustomerSearch, content: {
174-
OrderCustomerListView(siteID: viewModel.siteID)
174+
OrderCustomerListView(siteID: viewModel.siteID, onCustomerTapped: { customer in
175+
// Not implemented yet.
176+
print("3 - Customer Callback. Fill Order data with Customer details")
177+
print("4 - Customer ID: \(customer.customerID) - Name: \(customer.firstName ?? ""))")
178+
})
175179
})
176180
}
177181

0 commit comments

Comments
 (0)