Skip to content

Commit 8ed229e

Browse files
committed
Calculate section insertion indexes instead of hardcoding
1 parent 78de80e commit 8ed229e

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

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

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,13 @@ private extension BookingDetailsViewModel {
8383
func updateDisplayProperties(from booking: Booking) {
8484
navigationTitle = Self.navigationTitle(for: booking)
8585

86+
headerContent.update(with: booking)
87+
88+
setupCustomerSectionVisibility()
8689
if let billingAddress = booking.orderInfo?.customerInfo?.billingAddress, !billingAddress.isEmpty {
8790
customerContent.update(with: billingAddress)
88-
insertCustomerSectionIfAbsent()
89-
} else {
90-
deleteCustomerSectionIfPresent()
9191
}
9292

93-
headerContent.update(with: booking)
9493
appointmentDetailsContent.update(with: booking, resource: bookingResource)
9594

9695
setupAttendanceSectionVisibility()
@@ -99,6 +98,14 @@ private extension BookingDetailsViewModel {
9998
paymentContent.update(with: booking)
10099
}
101100

101+
func setupCustomerSectionVisibility() {
102+
if let billingAddress = booking.orderInfo?.customerInfo?.billingAddress, !billingAddress.isEmpty {
103+
insertCustomerSectionIfAbsent()
104+
} else {
105+
deleteCustomerSectionIfPresent()
106+
}
107+
}
108+
102109
func setupAttendanceSectionVisibility() {
103110
if booking.attendanceStatus == .cancelled || booking.bookingStatus == .cancelled {
104111
deleteAttendanceSectionIfPresent()
@@ -108,23 +115,46 @@ private extension BookingDetailsViewModel {
108115
}
109116

110117
func insertAttendanceSectionIfAbsent() {
118+
guard let insertAfterIndex = sections.firstIndex(where: {
119+
if case .customer = $0.content {
120+
return true
121+
}
122+
return false
123+
}) ?? sections.firstIndex(where: {
124+
if case .appointmentDetails = $0.content {
125+
return true
126+
}
127+
return false
128+
}) else {
129+
return
130+
}
131+
111132
insertSectionIfAbsent(
112133
section: Section(
113134
header: .title(Localization.attendanceSectionHeaderTitle.uppercased()),
114135
footerText: Localization.attendanceSectionFooterText,
115136
content: .attendance(attendanceContent)
116137
),
117-
at: 3
138+
at: insertAfterIndex + 1
118139
)
119140
}
120141

121142
func insertCustomerSectionIfAbsent() {
143+
guard let insertAfterIndex = sections.firstIndex(where: {
144+
if case .appointmentDetails = $0.content {
145+
return true
146+
}
147+
return false
148+
}) else {
149+
return
150+
}
151+
122152
insertSectionIfAbsent(
123153
section: Section(
124154
header: .title(Localization.customerSectionHeaderTitle.uppercased()),
125155
content: .customer(customerContent)
126156
),
127-
at: 2
157+
at: insertAfterIndex + 1
128158
)
129159
}
130160

0 commit comments

Comments
 (0)