Skip to content

Commit 2cdb4db

Browse files
committed
Fix customer name display in order filter from order details
Fixed an issue where clicking "View customer orders" from order details showed the customer ID instead of the customer name in the filter view. Implemented a SharedFlow event system in OrderFiltersRepository to trigger the existing onCustomerSelected() logic, which fetches and displays the customer name properly.
1 parent b3adfe2 commit 2cdb4db

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/filters/OrderFilterCategoriesViewModel.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ class OrderFilterCategoriesViewModel @Inject constructor(
9191
savedState[OLD_FILTER_SELECTION_KEY] = oldFilterSelection
9292
}
9393
}
94+
95+
launch {
96+
orderFilterRepository.customerSelectedEvent.collect { customer ->
97+
onCustomerSelected(customer)
98+
}
99+
}
94100
}
95101

96102
fun onFilterCategorySelected(filterCategory: OrderFilterCategoryUiModel) {
@@ -367,12 +373,7 @@ class OrderFilterCategoriesViewModel @Inject constructor(
367373

368374
private suspend fun getCustomerDisplayValueFrom(customerId: Long?): String =
369375
customerId?.let { id ->
370-
orderFilterRepository.customerFilterCustomer?.let { customer ->
371-
buildString {
372-
val fullName = "${customer.firstName.orEmpty()} ${customer.lastName.orEmpty()}".trim()
373-
append(fullName.ifBlank { customer.email ?: customer.username.orEmpty() })
374-
}
375-
} ?: customerStore.getCustomerByRemoteId(selectedSite.get(), id)
376+
customerStore.getCustomerByRemoteId(selectedSite.get(), id)
376377
?.let { customer ->
377378
(customer.firstName + " " + customer.lastName)
378379
.ifBlank { customer.email }

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/filters/data/OrderFiltersRepository.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import com.woocommerce.android.tools.SelectedSite
77
import com.woocommerce.android.ui.orders.filters.data.OrderListFilterCategory.CUSTOMER
88
import com.woocommerce.android.ui.orders.filters.data.OrderListFilterCategory.PRODUCT
99
import kotlinx.coroutines.CoroutineScope
10+
import kotlinx.coroutines.flow.MutableSharedFlow
11+
import kotlinx.coroutines.flow.SharedFlow
12+
import kotlinx.coroutines.flow.asSharedFlow
1013
import kotlinx.coroutines.flow.distinctUntilChanged
1114
import kotlinx.coroutines.flow.launchIn
1215
import kotlinx.coroutines.flow.onEach
@@ -23,15 +26,19 @@ class OrderFiltersRepository @Inject constructor(
2326

2427
var customerFilter: Long? = null
2528

26-
var customerFilterCustomer: Order.Customer? = null
29+
private val _customerSelectedEvent = MutableSharedFlow<Order.Customer>(replay = 0)
30+
val customerSelectedEvent: SharedFlow<Order.Customer> = _customerSelectedEvent.asSharedFlow()
31+
32+
suspend fun selectCustomer(customer: Order.Customer) {
33+
_customerSelectedEvent.emit(customer)
34+
}
2735

2836
init {
2937
selectedSite.observe()
3038
.distinctUntilChanged { old, new -> old?.id == new?.id }
3139
.onEach {
3240
productFilter = null
3341
customerFilter = null
34-
customerFilterCustomer = null
3542
}.launchIn(appCoroutineScope)
3643
}
3744

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/list/OrderListFragment.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import androidx.core.view.doOnPreDraw
2020
import androidx.core.view.isVisible
2121
import androidx.fragment.app.activityViewModels
2222
import androidx.fragment.app.viewModels
23+
import androidx.lifecycle.lifecycleScope
2324
import androidx.navigation.findNavController
2425
import androidx.navigation.fragment.NavHostFragment
2526
import androidx.navigation.fragment.findNavController
@@ -77,6 +78,7 @@ import com.woocommerce.android.util.StringUtils
7778
import com.woocommerce.android.viewmodel.MultiLiveEvent
7879
import com.woocommerce.android.widgets.WCEmptyView.EmptyViewType
7980
import dagger.hilt.android.AndroidEntryPoint
81+
import kotlinx.coroutines.launch
8082
import org.wordpress.android.util.ToastUtils
8183
import javax.inject.Inject
8284
import org.wordpress.android.util.ActivityUtils as WPActivityUtils
@@ -925,10 +927,9 @@ class OrderListFragment :
925927

926928
fun applyCustomerFilter(customer: Order.Customer) {
927929
searchQuery = ""
928-
val customerId = customer.customerId ?: return
929-
930-
orderFiltersRepository.customerFilter = customerId
931-
orderFiltersRepository.customerFilterCustomer = customer
930+
viewLifecycleOwner.lifecycleScope.launch {
931+
orderFiltersRepository.selectCustomer(customer)
932+
}
932933
viewModel.loadOrders()
933934

934935
uiMessageResolver.showSnack(R.string.order_list_customer_filter_applied)

0 commit comments

Comments
 (0)