Untitled
unknown
plain_text
19 days ago
12 kB
4
Indexable
Never
package com.mobily.activity.features.ecommerce.journey.device.view import android.graphics.Rect import android.os.Bundle import android.view.View import androidx.core.view.isVisible import androidx.core.widget.NestedScrollView import androidx.lifecycle.lifecycleScope import androidx.navigation.fragment.findNavController import androidx.paging.PagedList import androidx.recyclerview.widget.GridLayoutManager import com.algolia.instantsearch.core.number.range.Range import com.algolia.instantsearch.helper.android.item.StatsTextView import com.algolia.instantsearch.helper.android.list.autoScrollToStart import com.algolia.instantsearch.helper.filter.facet.connectView import com.algolia.instantsearch.helper.stats.connectView import com.mobily.activity.core.extension.gone import com.mobily.activity.core.extension.visible import com.mobily.activity.databinding.FragmentDeviceListV2EcommerceBinding import com.mobily.activity.features.algoliaSearch.utils.FilterUpdateEvent import com.mobily.activity.features.algoliaSearch.utils.StatsPresenterImpl import com.mobily.activity.features.ecommerce.data.repository.ECommerceCredHelper import com.mobily.activity.features.ecommerce.journey.device.algolia.AlgoliaHelper import com.mobily.activity.features.ecommerce.journey.device.base.EcommerceDeviceBaseFragment import com.mobily.activity.features.ecommerce.journey.device.data.ECommerceDeviceSeries import com.mobily.activity.features.ecommerce.journey.device.viewModel.ECommerceDeviceViewModel import com.mobily.activity.features.ecommerce.journey.device.viewModel.EcommerceDeviceCartViewModel import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch import org.greenrobot.eventbus.Subscribe import org.koin.androidx.viewmodel.ext.android.sharedViewModel import org.koin.androidx.viewmodel.ext.android.viewModel class ECommerceDeviceSeriesFragment : EcommerceDeviceBaseFragment<FragmentDeviceListV2EcommerceBinding>( FragmentDeviceListV2EcommerceBinding::inflate ) { private val eCommViewModel: ECommerceDeviceViewModel by viewModel() private val ecommCartViewModel:EcommerceDeviceCartViewModel by sharedViewModel() private val adapterFacet: EcommerceDeviceFilterAdapter = EcommerceDeviceFilterAdapter() private val seriesAdapter = ECommerceDeviceSeriesAdapter(::onSeriesSelected) private var isFilterImageShown = true private var isFilterUpdated = false private lateinit var ecommerceDeviceFilterDialogFragment: EcommerceDeviceFilterDialogFragment private var isLaunchedFirstTime = false private var range: Range<Int>? = null override fun layoutId() = -1 private fun handleDeviceSeries(eCommerceDeviceSeries: PagedList<ECommerceDeviceSeries>?) { eCommerceDeviceSeries?.let { hits -> if (hits.size > 0) { val maxPrice = hits.maxOf { it.maxPrice } val minPrice = hits.minOf { it.minPrice } val currentRange = Range(minPrice.toInt(), maxPrice.toInt()) if (range != currentRange) { if (!isLaunchedFirstTime) { isLaunchedFirstTime = true eCommViewModel.priceRangeConnector.viewModel.bounds.value = currentRange } else { if (isFilterUpdated) { isFilterUpdated = false eCommViewModel.priceRangeConnector.viewModel.range.value = currentRange } } } if (::ecommerceDeviceFilterDialogFragment.isInitialized && ecommerceDeviceFilterDialogFragment.isVisible) { ecommerceDeviceFilterDialogFragment.hideSlider(maxPrice == minPrice) } range = currentRange seriesAdapter.setHits(hits) eCommViewModel.algoliaConnection += eCommViewModel.devicesFacetList.connectView( adapterFacet, eCommViewModel.facetPresenterDeviceSubtypes ) val statsView = StatsTextView(binding.txtStats) eCommViewModel.algoliaConnection += eCommViewModel.statsConnector.connectView( statsView, StatsPresenterImpl(requireActivity(), isDevicesScreen = true) ) } stopShimmerLoading() hideShimmerViews() showActualViews() } } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) hideActualViews() showShimmerViews() startShimmerLoading() eCommViewModel.deviceSeries.observe(viewLifecycleOwner, ::handleDeviceSeries) binding.rvTypeFilter.adapter = adapterFacet binding.rvProductList.apply { itemAnimator = null adapter = seriesAdapter layoutManager = GridLayoutManager(requireActivity(), 2) autoScrollToStart(seriesAdapter) } binding.nestedScrollView.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { _, _, scrollY, _, oldScrollY -> val rect = Rect() val viewHeight = binding.llSort.height if (binding.llSort.getGlobalVisibleRect(rect) && binding.llSort.height === viewHeight && binding.llSort.width === rect.width() ) { /* this method will call when scroll reach bottom */ if (scrollY == 0) { if (!isFilterImageShown) { setFilterIconVisibility(true) isFilterImageShown = true } } } else { /* this method will call during scroll */ if (scrollY > oldScrollY) { if (isFilterImageShown) { setFilterIconVisibility(false) isFilterImageShown = false } } } }) setUpClickListeners() } private fun setUpClickListeners() { binding.btnFilterView.setOnClickListener { ecommerceDeviceFilterDialogFragment = EcommerceDeviceFilterDialogFragment.newInstance(eCommViewModel) ecommerceDeviceFilterDialogFragment .show(childFragmentManager, tag) } binding.ivSearch.setOnClickListener { navigator.showSearchProductScreen(requireActivity(), isGlobalSearch = true) } binding.btnBack.setOnClickListener { requireActivity().onBackPressed() } binding.ivDot.isVisible = ECommerceCredHelper.getShoppingCartId().isNotEmpty() binding.clCartLayout.setOnClickListener { ECommerceCredHelper.getShoppingCartId().apply { ecommCartViewModel.clearData() findNavController().navigate( ECommerceDeviceSeriesFragmentDirections.actionECommerceDeviceSeriesFragmentToEcommerceDeviceCartFragment( productOfferById = null, shoppingCartId = ifEmpty { null } ) ) } } binding.btnSorting.setOnClickListener { val dialogFragment = EcommerceDeviceSortDialogFragment.newInstance(eCommViewModel) dialogFragment.setCallback(callback = object : EcommerceDeviceSortDialogFragment.DialogItemClickListener { override fun onItemClick(position: Int) { when (position) { 0 -> eCommViewModel.sortByConnector.searcher.index = AlgoliaHelper.seriesFeaturedIndex 1 -> eCommViewModel.sortByConnector.searcher.index = AlgoliaHelper.seriesPriceAscIndex 2 -> eCommViewModel.sortByConnector.searcher.index = AlgoliaHelper.seriesPriceDescIndex 3 -> eCommViewModel.sortByConnector.searcher.index = AlgoliaHelper.seriesLatestIndex else -> eCommViewModel.sortByConnector.searcher.index = AlgoliaHelper.seriesFeaturedIndex } } }) dialogFragment.show(childFragmentManager, tag) } } private fun startShimmerLoading() { binding.shimmerLayout.shimmerContainer.startShimmer() binding.shimmerFilterSortLayout.shimmerStatsFiltersContainer.startShimmer() binding.shimmerDeviceFilterLayout.shimmerDeviceFilterContainer.startShimmer() } private fun stopShimmerLoading() { binding.shimmerLayout.shimmerContainer.stopShimmer() binding.shimmerFilterSortLayout.shimmerStatsFiltersContainer.stopShimmer() binding.shimmerDeviceFilterLayout.shimmerDeviceFilterContainer.stopShimmer() } private fun showShimmerViews() { binding.shimmerLayout.shimmerContainer.visible() binding.shimmerFilterSortLayout.shimmerStatsFiltersContainer.visible() binding.shimmerDeviceFilterLayout.shimmerDeviceFilterContainer.visible() } private fun hideActualViews() { binding.rvProductList.gone() binding.llSort.gone() binding.rvTypeFilter.gone() } private fun showActualViews() { binding.rvProductList.visible() binding.llSort.visible() binding.rvTypeFilter.visible() } private fun hideShimmerViews() { binding.shimmerLayout.shimmerContainer.gone() binding.shimmerFilterSortLayout.shimmerStatsFiltersContainer.gone() binding.shimmerDeviceFilterLayout.shimmerDeviceFilterContainer.gone() } override fun onStop() { super.onStop() stopShimmerLoading() } private fun setFilterIconVisibility(visible: Boolean = false) { lifecycleScope.launch(Dispatchers.Main) { delay(50) // Update the global flag indicating whether the filter image is shown EcommerceDeviceSubtypeViewHolder.isFilterImageShown = visible // Save the current state of the RecyclerView val state = binding.rvTypeFilter.layoutManager?.onSaveInstanceState() binding.appBarLayout.elevation = if (visible) 0f else 8f for (i in 0 until adapterFacet.itemCount) { // Get the view holder for the current item val viewHolder = binding.rvTypeFilter.findViewHolderForAdapterPosition(i) if (viewHolder != null) { viewHolder as EcommerceDeviceSubtypeViewHolder // Update the visibility of the image in the view holder viewHolder.animateFilterImage(visible) } } adapterFacet.notifyDataSetChanged() // Restore the previous state of the RecyclerView state?.let { binding.rvTypeFilter.layoutManager?.onRestoreInstanceState(it) } } } @Subscribe fun onEvent(forceUpdateEvent: FilterUpdateEvent) { isFilterUpdated = true } private fun onSeriesSelected( selectedSeries: ECommerceDeviceSeries ) { findNavController().navigate( ECommerceDeviceSeriesFragmentDirections.actionECommerceDeviceSeriesFragmentToDeviceDetailsFragment( ecommerceDeviceSeries = selectedSeries ) ) } }
Leave a Comment