Skip to content

Commit

Permalink
Merge pull request #10844 from woocommerce/10830-better-tablet-produc…
Browse files Browse the repository at this point in the history
…ts-adapt-toolbar-for-the-2-pane-layout

[Better Tablet Products] Adapt toolbar for the 2 pane layout on the list part
  • Loading branch information
kidinov authored Feb 22, 2024
2 parents 568ad9b + 71a4007 commit cae1202
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 150 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

17.5
-----
- [*] [Internal] As side effect of adding tablet support, toolbar on the product list screen is not collapsible anymore [https://github.com/woocommerce/woocommerce-android/pull/10844]


17.4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.woocommerce.android.e2e.helpers.useMockedAPI
import com.woocommerce.android.e2e.screens.TabNavComponent
import com.woocommerce.android.e2e.screens.login.WelcomeScreen
import com.woocommerce.android.e2e.screens.products.ProductListScreen
import com.woocommerce.android.e2e.screens.shared.FilterScreen
import com.woocommerce.android.ui.login.LoginActivity
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
Expand Down Expand Up @@ -68,9 +67,6 @@ class ProductsRealAPI : TestBase() {
ProductListScreen()
.leaveSearchMode()

FilterScreen()
.leaveFilterScreenToProducts()

WelcomeScreen
.logoutIfNeeded(composeTestRule)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ package com.woocommerce.android.ui.products

import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.MenuItem.OnActionExpandListener
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.ActionMode
import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.SearchView.OnQueryTextListener
import androidx.core.view.MenuCompat
import androidx.core.view.MenuProvider
import androidx.core.view.ViewGroupCompat
import androidx.core.view.doOnPreDraw
import androidx.core.view.isVisible
Expand Down Expand Up @@ -43,6 +38,7 @@ import com.woocommerce.android.model.Product
import com.woocommerce.android.ui.base.TopLevelFragment
import com.woocommerce.android.ui.base.UIMessageResolver
import com.woocommerce.android.ui.feedback.SurveyType
import com.woocommerce.android.ui.main.AppBarStatus
import com.woocommerce.android.ui.main.MainActivity
import com.woocommerce.android.ui.main.MainNavigationRouter
import com.woocommerce.android.ui.products.ProductListViewModel.ProductListEvent.OpenProduct
Expand All @@ -68,11 +64,7 @@ class ProductListFragment :
TopLevelFragment(R.layout.fragment_product_list),
ProductSortAndFilterListener,
OnLoadMoreListener,
OnQueryTextListener,
OnActionExpandListener,
WCProductSearchTabView.ProductSearchTypeChangedListener,
ActionMode.Callback,
MenuProvider,
TabletLayoutSetupHelper.Screen {
companion object {
val TAG: String = ProductListFragment::class.java.simpleName
Expand All @@ -94,6 +86,9 @@ class ProductListFragment :
@Inject
lateinit var tabletLayoutSetupHelper: TabletLayoutSetupHelper

@Inject
lateinit var productListToolbar: ProductListToolbarHelper

private var _productAdapter: ProductListAdapter? = null
private val productAdapter: ProductListAdapter
get() = _productAdapter!!
Expand All @@ -106,10 +101,6 @@ class ProductListFragment :

private val skeletonView = SkeletonView()

private var searchMenuItem: MenuItem? = null
private var scanBarcodeMenuItem: MenuItem? = null
private var searchView: SearchView? = null

private var trashProductUndoSnack: Snackbar? = null
private var pendingTrashProductId: Long? = null

Expand All @@ -135,6 +126,9 @@ class ProductListFragment :
)
}

override val activityAppBarStatus: AppBarStatus
get() = AppBarStatus.Hidden

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -150,7 +144,6 @@ class ProductListFragment :
tabletLayoutSetupHelper.onViewCreated(this)

postponeEnterTransition()
requireActivity().addMenuProvider(this, viewLifecycleOwner, Lifecycle.State.RESUMED)

_binding = FragmentProductListBinding.bind(view)

Expand Down Expand Up @@ -183,16 +176,11 @@ class ProductListFragment :
initAddProductFab(binding.addProductButton)
addSelectionTracker()

when {
productListViewModel.isSearching() -> {
binding.productsSearchTabView.isVisible = true
binding.productsSearchTabView.show(this, productListViewModel.isSkuSearch())
}

else -> {
productListViewModel.reloadProductsFromDb(excludeProductId = pendingTrashProductId)
}
if (!productListViewModel.isSearching()) {
productListViewModel.reloadProductsFromDb(excludeProductId = pendingTrashProductId)
}

productListToolbar.onViewCreated(this, productListViewModel, binding)
}

private fun addSelectionTracker() {
Expand Down Expand Up @@ -230,12 +218,9 @@ class ProductListFragment :

override fun onDestroyView() {
skeletonView.hide()
disableSearchListeners()
searchView = null
_productAdapter = null
actionMode = null
tracker = null
searchMenuItem = null
binding.productsSearchTabView.hide()
super.onDestroyView()
_binding = null
Expand Down Expand Up @@ -267,119 +252,6 @@ class ProductListFragment :
super.onViewStateRestored(savedInstanceState)
}

override fun onCreateMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_product_list_fragment, menu)

searchMenuItem = menu.findItem(R.id.menu_search)
searchView = searchMenuItem?.actionView as SearchView?
searchView?.queryHint = getString(R.string.product_search_hint)
scanBarcodeMenuItem = menu.findItem(R.id.menu_scan_barcode)
}

override fun onPrepareMenu(menu: Menu) {
refreshOptionsMenu()
}

/**
* Use this rather than invalidateOptionsMenu() since that collapses the search menu item
*/
private fun refreshOptionsMenu() {
val showSearch = shouldShowSearchMenuItem()
searchMenuItem?.let { menuItem ->
if (menuItem.isVisible != showSearch) menuItem.isVisible = showSearch

val isSearchActive = productListViewModel.viewStateLiveData.liveData.value?.isSearchActive == true
if (menuItem.isActionViewExpanded != isSearchActive) {
if (isSearchActive) {
disableSearchListeners()
menuItem.expandActionView()
val queryHint = getSearchQueryHint()
searchView?.queryHint = queryHint
searchView?.setQuery(productListViewModel.viewStateLiveData.liveData.value?.query, false)
enableSearchListeners()
}
}
}
scanBarcodeMenuItem?.isVisible = !productListViewModel.isSquarePluginActive()
}

private fun getSearchQueryHint(): String {
return if (productListViewModel.viewStateLiveData.liveData.value?.isFilteringActive == true) {
getString(R.string.product_search_hint_active_filters)
} else {
getString(R.string.product_search_hint)
}
}

/**
* Prevent search from appearing when a child fragment is active
*/
private fun shouldShowSearchMenuItem(): Boolean {
val isChildShowing = (activity as? MainNavigationRouter)?.isChildFragmentShowing() ?: false
return !isChildShowing
}

override fun onMenuItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.menu_search -> {
AnalyticsTracker.track(AnalyticsEvent.PRODUCT_LIST_MENU_SEARCH_TAPPED)
enableSearchListeners()
true
}

R.id.menu_scan_barcode -> {
AnalyticsTracker.track(AnalyticsEvent.PRODUCT_LIST_PRODUCT_BARCODE_SCANNING_TAPPED)
ProductListFragmentDirections.actionProductListFragmentToScanToUpdateInventory().let {
findNavController().navigate(it)
}
searchMenuItem?.collapseActionView()
true
}

else -> false
}
}

private fun disableSearchListeners() {
searchMenuItem?.setOnActionExpandListener(null)
searchView?.setOnQueryTextListener(null)
}

private fun enableSearchListeners() {
searchMenuItem?.setOnActionExpandListener(this)
searchView?.setOnQueryTextListener(this)
}

override fun onQueryTextSubmit(query: String): Boolean {
productListViewModel.onSearchRequested()
org.wordpress.android.util.ActivityUtils.hideKeyboard(activity)
return true
}

override fun onQueryTextChange(newText: String): Boolean {
productListViewModel.onSearchQueryChanged(newText)
return true
}

override fun onProductSearchTypeChanged(isSkuSearch: Boolean) {
productListViewModel.onSearchTypeChanged(isSkuSearch)
}

override fun onMenuItemActionExpand(item: MenuItem): Boolean {
productListViewModel.onSearchOpened()
onSearchViewActiveChanged(isActive = true)
binding.productsSearchTabView.show(this)
return true
}

override fun onMenuItemActionCollapse(item: MenuItem): Boolean {
productListViewModel.onSearchClosed()
updateActivityTitle()
onSearchViewActiveChanged(isActive = false)
binding.productsSearchTabView.hide()
return true
}

private fun setIsRefreshing(isRefreshing: Boolean) {
binding.productsRefreshLayout.isRefreshing = isRefreshing
}
Expand Down Expand Up @@ -539,14 +411,12 @@ class ProductListFragment :
actionMode = (requireActivity() as AppCompatActivity)
.startSupportActionMode(this@ProductListFragment)
delayMultiSelection()
onListSelectionActiveChanged(isActive = true, expandToolbar = false)
enableProductsRefresh(false)
enableProductSortAndFiltersCard(false)
}

ProductListViewModel.ProductListState.Browsing -> {
actionMode?.finish()
onListSelectionActiveChanged(isActive = false, expandToolbar = !productListViewModel.isSearching())
enableProductsRefresh(true)
enableProductSortAndFiltersCard(true)
}
Expand Down Expand Up @@ -613,8 +483,6 @@ class ProductListFragment :
}
}

override fun getFragmentTitle() = getString(R.string.products)

override fun scrollToTop() {
binding.productsRecycler.smoothScrollToPosition(0)
}
Expand Down Expand Up @@ -711,7 +579,7 @@ class ProductListFragment :

private fun onProductClick(remoteProductId: Long, sharedView: View?) {
if (shouldPreventDetailNavigation(remoteProductId)) return
disableSearchListeners()
productListToolbar.disableSearchListeners()
(activity as? MainNavigationRouter)?.let { router ->
if (sharedView == null) {
router.showProductDetail(remoteProductId, enableTrash = true)
Expand Down
Loading

0 comments on commit cae1202

Please sign in to comment.