11import Foundation
22import Yosemite
3+ import protocol Storage. StorageManagerType
34
45final class BookingDetailsViewModel : ObservableObject {
56 private let stores : StoresManager
67
78 private var booking : Booking
9+ private var bookingResource : BookingResource ?
810
911 // EntityListener: Update / Deletion Notifications.
1012 ///
@@ -15,24 +17,28 @@ final class BookingDetailsViewModel: ObservableObject {
1517 let navigationTitle : String
1618 @Published private( set) var sections : [ Section ] = [ ]
1719
18- init ( booking: Booking , stores: StoresManager = ServiceLocator . stores) {
20+ init ( booking: Booking ,
21+ stores: StoresManager = ServiceLocator . stores,
22+ storage: StorageManagerType = ServiceLocator . storageManager) {
1923 self . booking = booking
2024 self . stores = stores
2125
2226 navigationTitle = Self . navigationTitle ( for: booking)
23- setupSections ( with: booking)
27+ let resource = storage. viewStorage. loadBookingResource ( siteID: booking. siteID, resourceID: booking. resourceID) ? . toReadOnly ( )
28+ self . bookingResource = resource
29+ setupSections ( with: booking, resource: resource)
2430 configureEntityListener ( )
2531 }
2632
27- private func setupSections( with booking: Booking ) {
33+ private func setupSections( with booking: Booking , resource : BookingResource ? ) {
2834 let headerContent = HeaderContent ( booking)
2935 let headerSection = Section (
3036 content: . header( headerContent)
3137 )
3238
3339 let appointmentDetailsSection = Section (
3440 header: . title( Localization . appointmentDetailsSectionHeaderTitle. uppercased ( ) ) ,
35- content: . appointmentDetails( AppointmentDetailsContent ( booking) )
41+ content: . appointmentDetails( AppointmentDetailsContent ( booking, resource : resource ) )
3642 )
3743
3844 let customerSection : Section ? = {
@@ -75,6 +81,9 @@ final class BookingDetailsViewModel: ObservableObject {
7581
7682extension BookingDetailsViewModel {
7783 func syncData( ) async {
84+ if let resource = await fetchResource ( ) {
85+ self . bookingResource = resource // only update resource if fetching succeeds
86+ }
7887 await syncBooking ( )
7988 }
8089}
@@ -84,7 +93,7 @@ private extension BookingDetailsViewModel {
8493 entityListener. onUpsert = { [ weak self] booking in
8594 guard let self else { return }
8695 self . booking = booking
87- self . setupSections ( with: booking)
96+ self . setupSections ( with: booking, resource : bookingResource )
8897 }
8998 }
9099
@@ -96,6 +105,25 @@ private extension BookingDetailsViewModel {
96105 }
97106 }
98107
108+ @MainActor
109+ func fetchResource( ) async -> BookingResource ? {
110+ do {
111+ return try await withCheckedThrowingContinuation { continuation in
112+ stores. dispatch ( BookingAction . fetchResource ( siteID: booking. siteID, resourceID: booking. resourceID) { result in
113+ switch result {
114+ case . success( let resource) :
115+ continuation. resume ( returning: resource)
116+ case . failure( let error) :
117+ continuation. resume ( throwing: error)
118+ }
119+ } )
120+ }
121+ } catch {
122+ DDLogError ( " ⛔️ Error fetching resource for Booking: \( error) " )
123+ return nil
124+ }
125+ }
126+
99127 @MainActor
100128 func retrieveBooking( ) async throws {
101129 try await withCheckedThrowingContinuation { continuation in
0 commit comments