Skip to content

Commit 55001de

Browse files
committed
Make booking details content configurable after initialization
1 parent 4dcb1da commit 55001de

File tree

4 files changed

+58
-38
lines changed

4 files changed

+58
-38
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22
import struct Networking.Booking
33

44
extension BookingDetailsViewModel {
5-
struct AppointmentDetailsContent {
5+
final class AppointmentDetailsContent: ObservableObject {
66
struct Row: Identifiable {
77
let title: String
88
let value: String
@@ -12,9 +12,14 @@ extension BookingDetailsViewModel {
1212
}
1313
}
1414

15-
let rows: [Row]
15+
@Published var rows: [Row]
1616

1717
init(_ booking: Booking) {
18+
rows = []
19+
update(with: booking)
20+
}
21+
22+
func update(with booking: Booking) {
1823
let appointmentDate = booking.startDate.toString(dateStyle: .short, timeStyle: .none, timeZone: BookingListTab.utcTimeZone)
1924
let appointmentTimeFrame = [
2025
booking.startDate.toString(dateStyle: .none, timeStyle: .short, timeZone: BookingListTab.utcTimeZone),

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

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,28 @@ final class BookingDetailsViewModel: ObservableObject {
1212
}
1313
}
1414
private let headerContent: HeaderContent
15-
private let customerContent = CustomerContent()
15+
private let customerContent: CustomerContent
16+
private let appointmentDetailsContent: AppointmentDetailsContent
17+
private let attendanceContent: AttendanceContent
18+
private let paymentContent: PaymentContent
1619

1720
let navigationTitle: String
1821
@Published private(set) var sections: [Section] = []
1922

2023
init(booking: Booking, stores: StoresManager = ServiceLocator.stores) {
2124
self.booking = booking
2225
self.stores = stores
23-
self.resultsController = BookingDetailsResultsController(booking: booking)
24-
self.headerContent = HeaderContent(booking)
26+
27+
resultsController = BookingDetailsResultsController(booking: booking)
28+
29+
customerContent = CustomerContent()
30+
attendanceContent = AttendanceContent()
31+
headerContent = HeaderContent(booking)
32+
appointmentDetailsContent = AppointmentDetailsContent(booking)
33+
paymentContent = PaymentContent(booking: booking)
34+
2535
navigationTitle = Self.navigationTitle(for: booking)
36+
2637
setupSections()
2738
configureResultsController()
2839
updateDisplayProperties(from: booking)
@@ -39,18 +50,18 @@ private extension BookingDetailsViewModel {
3950

4051
let appointmentDetailsSection = Section(
4152
header: .title(Localization.appointmentDetailsSectionHeaderTitle.uppercased()),
42-
content: .appointmentDetails(AppointmentDetailsContent(booking))
53+
content: .appointmentDetails(appointmentDetailsContent)
4354
)
4455

4556
let attendanceSection = Section(
4657
header: .title(Localization.attendanceSectionHeaderTitle.uppercased()),
4758
footerText: Localization.attendanceSectionFooterText,
48-
content: .attendance(AttendanceContent())
59+
content: .attendance(attendanceContent)
4960
)
5061

5162
let paymentSection = Section(
5263
header: .title(Localization.paymentSectionHeaderTitle.uppercased()),
53-
content: .payment(PaymentContent(booking: booking))
64+
content: .payment(paymentContent)
5465
)
5566

5667
let bookingNotes = Section(
@@ -79,16 +90,26 @@ private extension BookingDetailsViewModel {
7990
}
8091

8192
func updateDisplayProperties(from booking: Booking) {
82-
if let billingAddress = booking.orderInfo?.customerInfo?.billingAddress {
93+
if let billingAddress = booking.orderInfo?.customerInfo?.billingAddress, !billingAddress.isEmpty {
8394
customerContent.update(with: billingAddress)
84-
headerContent.update(with: billingAddress)
8595
insertCustomerSectionIfAbsent()
8696
}
97+
headerContent.update(with: booking)
98+
appointmentDetailsContent.update(with: booking)
99+
paymentContent.update(with: booking)
87100
}
88101

89102
func insertCustomerSectionIfAbsent() {
90103
// Avoid adding if it already exists
91-
guard !sections.contains(where: { if case .customer = $0.content { return true } else { return false } }) else {
104+
let customerSectionExists = sections.contains {
105+
if case .customer = $0.content {
106+
return true
107+
}
108+
109+
return false
110+
}
111+
112+
guard !customerSectionExists else {
92113
return
93114
}
94115

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

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,35 @@
11
import Foundation
22
import struct Yosemite.Booking
3+
import struct Yosemite.BookingProductInfo
34
import struct Yosemite.Customer
4-
import struct Networking.Address
5+
import struct Yosemite.Address
56

67
extension BookingDetailsViewModel {
78
final class HeaderContent: ObservableObject {
8-
let bookingDate: String
9-
let status: [Status]
9+
@Published var bookingDate: String = ""
10+
@Published var status: [Status] = []
11+
@Published var serviceAndCustomerLine: String = ""
1012

11-
@Published var serviceAndCustomerLine: String
13+
init(_ booking: Booking) {
14+
update(with: booking)
15+
}
1216

13-
init(_ booking: Booking, customerName: String? = nil) {
17+
func update(with booking: Booking) {
1418
bookingDate = booking.startDate.toString(
1519
dateStyle: .short,
1620
timeStyle: .short,
1721
timeZone: BookingListTab.utcTimeZone
1822
)
1923

20-
/// Temporary hardcode for service name
21-
let serviceName = "Women's Haircut"
22-
if let customerName = customerName, !customerName.isEmpty {
23-
serviceAndCustomerLine = [
24-
serviceName,
25-
customerName
26-
].joined(separator: Constants.dotSeparator)
27-
} else {
28-
serviceAndCustomerLine = serviceName
29-
}
30-
31-
status = [.booked, .payAtLocation]
32-
}
33-
34-
func update(with address: Address) {
24+
let serviceName = booking.orderInfo?.productInfo?.name ?? ""
3525
let customerName = [
36-
address.firstName,
37-
address.lastName
26+
booking.orderInfo?.customerInfo?.billingAddress.firstName,
27+
booking.orderInfo?.customerInfo?.billingAddress.lastName
3828
]
3929
.compactMap { $0 }
4030
.filter { !$0.isEmpty }
4131
.joined(separator: " ")
4232

43-
/// Temporary hardcode for service name
44-
let serviceName = "Women's Haircut"
4533
if !customerName.isEmpty {
4634
serviceAndCustomerLine = [
4735
serviceName,
@@ -50,6 +38,8 @@ extension BookingDetailsViewModel {
5038
} else {
5139
serviceAndCustomerLine = serviceName
5240
}
41+
42+
status = [.booked, .payAtLocation]
5343
}
5444
}
5545
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ import struct Networking.Booking
33
import class WooFoundationCore.CurrencyFormatter
44

55
extension BookingDetailsViewModel {
6-
struct PaymentContent {
7-
let amounts: [Amount]
8-
let actions: [Action]
6+
final class PaymentContent: ObservableObject {
7+
@Published var amounts: [Amount] = []
8+
@Published var actions: [Action] = []
99

1010
init(booking: Booking) {
11+
update(with: booking)
12+
}
13+
14+
func update(with booking: Booking) {
1115
amounts = [
1216
.init(value: BookingDetailsViewModel.formatPrice(for: booking, priceString: booking.cost), type: .service),
1317
.init(value: BookingDetailsViewModel.formatPrice(for: booking, priceString: "0"), type: .tax),

0 commit comments

Comments
 (0)