Skip to content

Commit 4533d98

Browse files
Introduce BookingUiState object to wrap all ui models
1 parent c33ad87 commit 4533d98

File tree

4 files changed

+86
-65
lines changed

4 files changed

+86
-65
lines changed

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/bookings/details/BookingDetailsScreen.kt

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import com.woocommerce.android.ui.bookings.compose.BookingAttendanceSection
2626
import com.woocommerce.android.ui.bookings.compose.BookingAttendanceStatus
2727
import com.woocommerce.android.ui.bookings.compose.BookingAttendanceStatusBottomSheet
2828
import com.woocommerce.android.ui.bookings.compose.BookingCustomerDetails
29+
import com.woocommerce.android.ui.bookings.compose.BookingCustomerDetailsModel
30+
import com.woocommerce.android.ui.bookings.compose.BookingPaymentDetailsModel
2931
import com.woocommerce.android.ui.bookings.compose.BookingPaymentSection
3032
import com.woocommerce.android.ui.bookings.compose.BookingStatus
3133
import com.woocommerce.android.ui.bookings.compose.BookingSummary
@@ -76,53 +78,45 @@ fun BookingDetailsScreen(
7678
.verticalScroll(rememberScrollState())
7779
.padding(innerPadding)
7880
) {
79-
viewState.bookingSummary?.let {
81+
viewState.bookingUiState?.let {
8082
BookingSummary(
81-
model = viewState.bookingSummary,
83+
model = viewState.bookingUiState.bookingSummary,
8284
modifier = Modifier.fillMaxWidth()
8385
)
84-
}
85-
viewState.bookingsAppointmentDetails?.let {
8686
BookingAppointmentDetails(
87-
model = viewState.bookingsAppointmentDetails,
87+
model = viewState.bookingUiState.bookingsAppointmentDetails,
8888
onCancelBooking = viewState.onCancelBooking,
8989
modifier = Modifier.fillMaxWidth()
9090
)
91-
}
92-
viewState.bookingCustomerDetails?.let {
9391
BookingCustomerDetails(
94-
model = viewState.bookingCustomerDetails,
92+
model = viewState.bookingUiState.bookingCustomerDetails,
9593
onEmailClick = {},
9694
onPhoneClick = {},
9795
modifier = Modifier.fillMaxWidth()
9896
)
99-
}
100-
viewState.bookingSummary?.let {
10197
BookingAttendanceSection(
102-
status = viewState.bookingSummary.attendanceStatus,
98+
status = viewState.bookingUiState.bookingSummary.attendanceStatus,
10399
onClick = { showAttendanceSheet.value = true },
104100
modifier = Modifier.fillMaxWidth()
105101
)
106-
}
107-
viewState.bookingPaymentDetails?.let {
108102
BookingPaymentSection(
109-
model = viewState.bookingPaymentDetails,
110-
status = viewState.bookingSummary.status,
103+
model = viewState.bookingUiState.bookingPaymentDetails,
104+
status = viewState.bookingUiState.bookingSummary.status,
111105
onMarkAsPaid = { onViewOrder(viewState.orderId) },
112106
onViewOrder = { onViewOrder(viewState.orderId) },
113107
onMarkAsRefunded = { onViewOrder(viewState.orderId) },
114108
modifier = Modifier.fillMaxWidth()
115109
)
116110
}
117111
}
118-
if (showAttendanceSheet.value) {
119-
BookingAttendanceStatusBottomSheet(
120-
onSelect = { status ->
121-
viewState.onAttendanceStatusSelected(status)
122-
},
123-
onDismiss = { showAttendanceSheet.value = false }
124-
)
125-
}
112+
}
113+
if (showAttendanceSheet.value) {
114+
BookingAttendanceStatusBottomSheet(
115+
onSelect = { status ->
116+
viewState.onAttendanceStatusSelected(status)
117+
},
118+
onDismiss = { showAttendanceSheet.value = false }
119+
)
126120
}
127121
}
128122
}
@@ -134,21 +128,39 @@ private fun BookingDetailsPreview() {
134128
BookingDetailsScreen(
135129
viewState = BookingDetailsViewState(
136130
toolbarTitle = "Booking #12345",
137-
bookingSummary = BookingSummaryModel(
138-
date = "05/07/2025, 11:00 AM",
139-
name = "Women’s Haircut",
140-
customerName = "Margarita Nikolaevna",
141-
attendanceStatus = BookingAttendanceStatus.CHECKED_IN,
142-
status = BookingStatus.Paid
131+
bookingUiState = BookingUiState(
132+
bookingSummary = BookingSummaryModel(
133+
date = "05/07/2025, 11:00 AM",
134+
name = "Women’s Haircut",
135+
customerName = "Margarita Nikolaevna",
136+
attendanceStatus = BookingAttendanceStatus.CHECKED_IN,
137+
status = BookingStatus.Paid
138+
),
139+
bookingsAppointmentDetails = BookingAppointmentDetailsModel(
140+
date = "Monday, 05 July 2025",
141+
time = "11:00 am - 12:00 pm",
142+
staff = "Marianne Renoir",
143+
location = "238 Willow Creek Drive, Montgomery AL 36109",
144+
duration = "60 min",
145+
price = "$55.00"
146+
),
147+
bookingCustomerDetails = BookingCustomerDetailsModel(
148+
name = "Margarita Nikolaevna",
149+
email = "[email protected]",
150+
phone = "+1 555-123-4567",
151+
billingAddressLines = listOf(
152+
"238 Willow Creek Drive",
153+
"Montgomery AL 36109",
154+
"United States"
155+
)
156+
),
157+
bookingPaymentDetails = BookingPaymentDetailsModel(
158+
service = "$55.00",
159+
tax = "$4.50",
160+
discount = "-",
161+
total = "$59.50"
162+
)
143163
),
144-
bookingsAppointmentDetails = BookingAppointmentDetailsModel(
145-
date = "Monday, 05 July 2025",
146-
time = "11:00 am - 12:00 pm",
147-
staff = "Marianne Renoir",
148-
location = "238 Willow Creek Drive, Montgomery AL 36109",
149-
duration = "60 min",
150-
price = "$55.00"
151-
)
152164
),
153165
onBack = {},
154166
onViewOrder = {}

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/bookings/details/BookingDetailsViewModel.kt

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,15 @@ class BookingDetailsViewModel @Inject constructor(
4848
}
4949

5050
private fun onAttendanceStatusSelected(status: BookingAttendanceStatus) {
51-
_state.update { current ->
52-
current.copy(
53-
bookingSummary = current.bookingSummary?.copy(attendanceStatus = status)
54-
)
51+
val bookingState = _state.value.bookingUiState
52+
if (bookingState != null) {
53+
_state.update { current ->
54+
current.copy(
55+
bookingUiState = bookingState.copy(
56+
bookingSummary = bookingState.bookingSummary.copy(attendanceStatus = status)
57+
)
58+
)
59+
}
5560
}
5661
}
5762

@@ -70,24 +75,26 @@ class BookingDetailsViewModel @Inject constructor(
7075
private fun updateStateWithBooking(booking: Booking) = with(bookingMapper) {
7176
_state.update { current ->
7277
current.copy(
73-
bookingSummary = booking.toBookingSummaryModel(),
74-
bookingsAppointmentDetails = booking.toAppointmentDetailsModel(),
75-
bookingCustomerDetails = BookingCustomerDetailsModel(
76-
name = "Margarita Nikolaevna",
77-
email = "[email protected]",
78-
phone = "+1 555-123-4567",
79-
billingAddressLines = listOf(
80-
"238 Willow Creek Drive",
81-
"Montgomery AL 36109",
82-
"United States"
78+
bookingUiState = BookingUiState(
79+
bookingSummary = booking.toBookingSummaryModel(),
80+
bookingsAppointmentDetails = booking.toAppointmentDetailsModel(),
81+
bookingCustomerDetails = BookingCustomerDetailsModel(
82+
name = "Margarita Nikolaevna",
83+
email = "[email protected]",
84+
phone = "+1 555-123-4567",
85+
billingAddressLines = listOf(
86+
"238 Willow Creek Drive",
87+
"Montgomery AL 36109",
88+
"United States"
89+
)
90+
),
91+
bookingPaymentDetails = BookingPaymentDetailsModel(
92+
service = "$55.00",
93+
tax = "$4.50",
94+
discount = "-",
95+
total = "$59.50"
8396
)
8497
),
85-
bookingPaymentDetails = BookingPaymentDetailsModel(
86-
service = "$55.00",
87-
tax = "$4.50",
88-
discount = "-",
89-
total = "$59.50"
90-
)
9198
)
9299
}
93100
}

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/bookings/details/BookingDetailsViewState.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ import com.woocommerce.android.ui.bookings.compose.BookingSummaryModel
99
data class BookingDetailsViewState(
1010
val toolbarTitle: String = "",
1111
val orderId: Long = 0L,
12-
val bookingSummary: BookingSummaryModel? = null,
13-
val bookingsAppointmentDetails: BookingAppointmentDetailsModel? = null,
14-
val bookingCustomerDetails: BookingCustomerDetailsModel? = null,
15-
val bookingPaymentDetails: BookingPaymentDetailsModel? = null,
12+
val bookingUiState: BookingUiState? = null,
1613
val onCancelBooking: () -> Unit = {},
1714
val onAttendanceStatusSelected: (BookingAttendanceStatus) -> Unit = { _ -> }
1815
)
16+
17+
data class BookingUiState(
18+
val bookingSummary: BookingSummaryModel,
19+
val bookingsAppointmentDetails: BookingAppointmentDetailsModel,
20+
val bookingCustomerDetails: BookingCustomerDetailsModel,
21+
val bookingPaymentDetails: BookingPaymentDetailsModel,
22+
)

WooCommerce/src/test/kotlin/com/woocommerce/android/ui/bookings/details/BookingDetailsViewModelTest.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class BookingDetailsViewModelTest : BaseUnitTest() {
7272
state.onAttendanceStatusSelected(BookingAttendanceStatus.CANCELLED)
7373

7474
// Then
75-
val updated = viewModel.state.value?.bookingSummary?.attendanceStatus
75+
val updated = viewModel.state.value?.bookingUiState?.bookingSummary?.attendanceStatus
7676
assertThat(updated).isEqualTo(BookingAttendanceStatus.CANCELLED)
7777
}
7878

@@ -92,9 +92,7 @@ class BookingDetailsViewModelTest : BaseUnitTest() {
9292

9393
// Then
9494
val state = viewModel.state.getOrAwaitValue()
95-
assertThat(state.bookingSummary).isNotNull
96-
assertThat(state.bookingsAppointmentDetails).isNotNull
97-
assertThat(state.bookingCustomerDetails).isNotNull
95+
assertThat(state.bookingUiState).isNotNull
9896
}
9997

10098
private fun createViewModel(

0 commit comments

Comments
 (0)