@@ -4,30 +4,28 @@ import SwiftUI // Added for withAnimation
44
55final 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
110107extension BookingDetailsViewModel {
111108 func syncData( ) async {
112- await syncOrder ( )
109+ await syncBooking ( )
113110 }
114111}
115112
116113private 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
146145extension 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