Skip to content

Commit 4dcb1da

Browse files
committed
Use booking info data instead of order
1 parent cfa7bba commit 4dcb1da

File tree

2 files changed

+59
-58
lines changed

2 files changed

+59
-58
lines changed

WooCommerce/Classes/ViewModels/Booking Details/BookingDetailsResultsController.swift

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,40 @@ import Storage
44

55
final class BookingDetailsResultsController {
66
private let storageManager: StorageManagerType
7-
private let booking: Yosemite.Booking
7+
private let bookingResultsController: ResultsController<StorageBooking>
88

9-
private lazy var orderResultsController: ResultsController<StorageOrder> = {
10-
let predicate = NSPredicate(
11-
format: "siteID = %ld AND orderID = %ld",
12-
booking.siteID,
13-
booking.orderID
14-
)
15-
return ResultsController<StorageOrder>(
16-
storageManager: storageManager,
17-
matching: predicate,
18-
sortedBy: []
19-
)
20-
}()
21-
22-
var order: Yosemite.Order? {
23-
orderResultsController.fetchedObjects.first
9+
var booking: Yosemite.Booking? {
10+
bookingResultsController.fetchedObjects.first
2411
}
2512

2613
init(booking: Yosemite.Booking, storageManager: StorageManagerType = ServiceLocator.storageManager) {
27-
self.booking = booking
2814
self.storageManager = storageManager
15+
16+
bookingResultsController = ResultsController<StorageBooking>(
17+
storageManager: storageManager,
18+
matching: NSPredicate(
19+
format: "siteID = %ld AND bookingID = %ld",
20+
booking.siteID,
21+
booking.bookingID
22+
),
23+
sortedBy: []
24+
)
2925
}
3026

3127
func configure(onReload: @escaping () -> Void) {
32-
orderResultsController.onDidChangeContent = {
28+
bookingResultsController.onDidChangeContent = {
3329
onReload()
3430
}
3531

36-
orderResultsController.onDidResetContent = { [weak self] in
37-
try? self?.orderResultsController.performFetch()
32+
bookingResultsController.onDidResetContent = { [weak self] in
33+
try? self?.bookingResultsController.performFetch()
3834
onReload()
3935
}
4036

4137
do {
42-
try orderResultsController.performFetch()
38+
try bookingResultsController.performFetch()
4339
} catch {
44-
DDLogError("⛔️ Unable to fetch Order for Booking: \(error)")
40+
DDLogError("⛔️ Unable to fetch Booking: \(error)")
4541
}
4642
}
4743
}

WooCommerce/Classes/ViewModels/Booking Details/BookingDetailsViewModel.swift

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,28 @@ import SwiftUI // Added for withAnimation
44

55
final class BookingDetailsViewModel: ObservableObject {
66
private let stores: StoresManager
7-
private lazy var resultsController = BookingDetailsResultsController(booking: booking)
7+
private let resultsController: BookingDetailsResultsController
88

9-
private let booking: Booking
10-
private let headerContent: HeaderContent
11-
private let customerContent = CustomerContent()
12-
private(set) var order: Order? {
9+
private var booking: Booking {
1310
didSet {
14-
guard let order else {
15-
return
16-
}
17-
updateCustomerInfo(from: order)
11+
updateDisplayProperties(from: booking)
1812
}
1913
}
14+
private let headerContent: HeaderContent
15+
private let customerContent = CustomerContent()
2016

2117
let navigationTitle: String
2218
@Published private(set) var sections: [Section] = []
2319

2420
init(booking: Booking, stores: StoresManager = ServiceLocator.stores) {
2521
self.booking = booking
2622
self.stores = stores
23+
self.resultsController = BookingDetailsResultsController(booking: booking)
2724
self.headerContent = HeaderContent(booking)
2825
navigationTitle = Self.navigationTitle(for: booking)
2926
setupSections()
3027
configureResultsController()
28+
updateDisplayProperties(from: booking)
3129
}
3230
}
3331

@@ -71,22 +69,21 @@ private extension BookingDetailsViewModel {
7169

7270
func configureResultsController() {
7371
resultsController.configure { [weak self] in
74-
self?.order = self?.resultsController.order
72+
if let newBooking = self?.resultsController.booking {
73+
self?.booking = newBooking
74+
}
75+
}
76+
if let newBooking = resultsController.booking {
77+
self.booking = newBooking
7578
}
76-
self.order = resultsController.order
7779
}
7880

79-
func updateCustomerInfo(from order: Order) {
80-
guard
81-
let billingAddress = order.billingAddress,
82-
!billingAddress.isEmpty
83-
else {
84-
return
81+
func updateDisplayProperties(from booking: Booking) {
82+
if let billingAddress = booking.orderInfo?.customerInfo?.billingAddress {
83+
customerContent.update(with: billingAddress)
84+
headerContent.update(with: billingAddress)
85+
insertCustomerSectionIfAbsent()
8586
}
86-
87-
customerContent.update(with: billingAddress)
88-
headerContent.update(with: billingAddress)
89-
insertCustomerSectionIfAbsent()
9087
}
9188

9289
func insertCustomerSectionIfAbsent() {
@@ -109,34 +106,36 @@ private extension BookingDetailsViewModel {
109106

110107
extension BookingDetailsViewModel {
111108
func syncData() async {
112-
await syncOrder()
109+
await syncBooking()
113110
}
114111
}
115112

116113
private extension BookingDetailsViewModel {
117-
func syncOrder() async {
118-
guard booking.orderID > 0 else {
114+
func syncBooking() async {
115+
guard booking.bookingID > 0 else {
119116
return
120117
}
121118

122119
do {
123-
try await fetchRemoteOrder()
120+
try await fetchRemoteBooking()
124121
} catch {
125-
DDLogError("⛔️ Error synchronizing Order for Booking: \(error)")
122+
DDLogError("⛔️ Error synchronizing Booking: \(error)")
126123
}
127124
}
128125

129126
@MainActor
130-
func fetchRemoteOrder() async throws {
127+
func fetchRemoteBooking() async throws {
131128
return try await withCheckedThrowingContinuation { continuation in
132-
let action = OrderAction.retrieveOrder(
129+
let action = BookingAction.synchronizeBooking(
133130
siteID: booking.siteID,
134-
orderID: booking.orderID
135-
) { _, error in
136-
if let error {
131+
bookingID: booking.bookingID
132+
) { result in
133+
switch result {
134+
case .success:
135+
continuation.resume(returning: ())
136+
case .failure(let error):
137137
continuation.resume(throwing: error)
138138
}
139-
continuation.resume(returning: ())
140139
}
141140
stores.dispatch(action)
142141
}
@@ -145,10 +144,16 @@ private extension BookingDetailsViewModel {
145144

146145
extension BookingDetailsViewModel {
147146
var cancellationAlertMessage: String {
148-
// Temporary hardcoded
149-
//TODO: - replace with associated customer data
150-
let productName = "Women's Haircut"
151-
let customerName = "Margarita Nikolaevna"
147+
let productName = booking.orderInfo?.productInfo?.name ?? ""
148+
149+
let customerName: String = {
150+
guard let address = booking.orderInfo?.customerInfo?.billingAddress else {
151+
return ""
152+
}
153+
return [address.firstName, address.lastName]
154+
.compactMap { $0 }
155+
.joined(separator: " ")
156+
}()
152157

153158
let date = booking.startDate.formatted(
154159
date: .long,

0 commit comments

Comments
 (0)