Skip to content

Commit 033c995

Browse files
committed
Fetch customer from API before applying filter to display name correctly
1 parent 8956252 commit 033c995

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.woocommerce.android.ui.orders
22

33
import androidx.lifecycle.SavedStateHandle
4+
import com.woocommerce.android.model.Order
45
import com.woocommerce.android.viewmodel.MultiLiveEvent
56
import com.woocommerce.android.viewmodel.ScopedViewModel
67
import dagger.hilt.android.lifecycle.HiltViewModel
@@ -32,15 +33,15 @@ class OrdersCommunicationViewModel @Inject constructor(
3233
triggerEvent(CommunicationEvent.OrdersLoaded)
3334
}
3435

35-
fun applyCustomerFilter(customerId: Long) {
36-
triggerEvent(CommunicationEvent.CustomerFilterRequested(customerId))
36+
fun applyCustomerFilter(customer: Order.Customer) {
37+
triggerEvent(CommunicationEvent.CustomerFilterRequested(customer))
3738
}
3839

3940
sealed class CommunicationEvent : MultiLiveEvent.Event() {
4041
data class OrderTrashed(val orderId: Long) : CommunicationEvent()
4142
data object OrdersEmptyNotified : CommunicationEvent()
4243
data object OrdersLoadingNotified : CommunicationEvent()
4344
data object OrdersLoaded : CommunicationEvent()
44-
data class CustomerFilterRequested(val customerId: Long) : CommunicationEvent()
45+
data class CustomerFilterRequested(val customer: Order.Customer) : CommunicationEvent()
4546
}
4647
}

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,9 +886,9 @@ class OrderDetailFragment :
886886
}
887887

888888
private fun onViewCustomerOrdersClicked(order: Order) {
889-
val customerId = order.customer?.customerId?.takeIf { it > 0 } ?: return
889+
val customer = order.customer?.takeIf { (it.customerId ?: 0) > 0 } ?: return
890890

891-
communicationViewModel.applyCustomerFilter(customerId)
891+
communicationViewModel.applyCustomerFilter(customer)
892892
if (!requireContext().isTwoPanesShouldBeUsed) {
893893
findNavController().popBackStack(R.id.orders, false)
894894
}

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

Lines changed: 22 additions & 8 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
@@ -66,6 +67,7 @@ import com.woocommerce.android.ui.orders.OrdersCommunicationViewModel
6667
import com.woocommerce.android.ui.orders.creation.CodeScannerStatus
6768
import com.woocommerce.android.ui.orders.creation.GoogleBarcodeFormatMapper.BarcodeFormat
6869
import com.woocommerce.android.ui.orders.creation.OrderCreateEditViewModel
70+
import com.woocommerce.android.ui.orders.creation.customerlist.CustomerListRepository
6971
import com.woocommerce.android.ui.orders.details.OrderStatusSelectorDialog
7072
import com.woocommerce.android.ui.orders.filters.data.OrderFiltersRepository
7173
import com.woocommerce.android.ui.orders.filters.data.OrderListFilterCategory
@@ -78,6 +80,7 @@ import com.woocommerce.android.util.StringUtils
7880
import com.woocommerce.android.viewmodel.MultiLiveEvent
7981
import com.woocommerce.android.widgets.WCEmptyView.EmptyViewType
8082
import dagger.hilt.android.AndroidEntryPoint
83+
import kotlinx.coroutines.launch
8184
import org.wordpress.android.util.ToastUtils
8285
import javax.inject.Inject
8386
import org.wordpress.android.util.ActivityUtils as WPActivityUtils
@@ -117,6 +120,9 @@ class OrderListFragment :
117120
@Inject
118121
internal lateinit var orderFiltersRepository: OrderFiltersRepository
119122

123+
@Inject
124+
internal lateinit var customerListRepository: CustomerListRepository
125+
120126
private var tracker: SelectionTracker<Long>? = null
121127
private var actionMode: ActionMode? = null
122128
private val selectionPredicate = MutableMultipleSelectionPredicate<Long>(
@@ -691,7 +697,7 @@ class OrderListFragment :
691697
}
692698

693699
is OrdersCommunicationViewModel.CommunicationEvent.CustomerFilterRequested -> {
694-
applyCustomerFilter(event.customerId)
700+
applyCustomerFilter(event.customer)
695701
}
696702

697703
else -> event.isHandled = false
@@ -924,14 +930,22 @@ class OrderListFragment :
924930
}
925931
}
926932

927-
fun applyCustomerFilter(customerId: Long) {
933+
fun applyCustomerFilter(customer: Order.Customer) {
928934
searchQuery = ""
929-
orderFiltersRepository.setSelectedFilters(
930-
OrderListFilterCategory.CUSTOMER,
931-
listOf(customerId.toString())
932-
)
933-
viewModel.loadOrders()
934-
uiMessageResolver.showSnack(R.string.order_list_customer_filter_applied)
935+
936+
viewLifecycleOwner.lifecycleScope.launch {
937+
customer.customerId?.let { customerId ->
938+
customerListRepository.fetchCustomerByRemoteId(customerId)
939+
940+
orderFiltersRepository.setSelectedFilters(
941+
OrderListFilterCategory.CUSTOMER,
942+
listOf(customerId.toString())
943+
)
944+
}
945+
946+
viewModel.loadOrders()
947+
uiMessageResolver.showSnack(R.string.order_list_customer_filter_applied)
948+
}
935949
}
936950

937951
private fun showOrderFilters() {

0 commit comments

Comments
 (0)