From f110c610d62481485c8bae853c6fc5f56a3831b9 Mon Sep 17 00:00:00 2001 From: LooKeR Date: Sun, 7 Nov 2021 23:00:23 +0530 Subject: [PATCH] Add: Sort Order of Installed app will be same as main page (Closes #65) Improve: Move AppsList*.kt(Products*.kt) to different package --- .../looker/droidify/screen/TabsFragment.kt | 31 ++++++++++--------- .../appsList/AppListFragment.kt} | 11 ++++--- .../AppListViewModel.kt} | 4 +-- 3 files changed, 24 insertions(+), 22 deletions(-) rename src/main/kotlin/com/looker/droidify/{screen/ProductsFragment.kt => ui/appsList/AppListFragment.kt} (95%) rename src/main/kotlin/com/looker/droidify/ui/{ProductsViewModel.kt => appsList/AppListViewModel.kt} (96%) diff --git a/src/main/kotlin/com/looker/droidify/screen/TabsFragment.kt b/src/main/kotlin/com/looker/droidify/screen/TabsFragment.kt index 9b19fdc2..6fcdd893 100644 --- a/src/main/kotlin/com/looker/droidify/screen/TabsFragment.kt +++ b/src/main/kotlin/com/looker/droidify/screen/TabsFragment.kt @@ -22,6 +22,7 @@ import com.looker.droidify.databinding.TabsToolbarBinding import com.looker.droidify.entity.ProductItem import com.looker.droidify.service.Connection import com.looker.droidify.service.SyncService +import com.looker.droidify.ui.appsList.AppListFragment import com.looker.droidify.utility.RxUtils import com.looker.droidify.utility.Utils import com.looker.droidify.utility.extension.android.* @@ -85,7 +86,7 @@ class TabsFragment : ScreenFragment() { private val syncConnection = Connection(SyncService::class.java, onBind = { _, _ -> viewPager?.let { - val source = ProductsFragment.Source.values()[it.currentItem] + val source = AppListFragment.Source.values()[it.currentItem] updateUpdateNotificationBlocker(source) } }) @@ -96,9 +97,9 @@ class TabsFragment : ScreenFragment() { private var needSelectUpdates = false - private val productFragments: Sequence + private val productFragments: Sequence get() = if (host == null) emptySequence() else - childFragmentManager.fragments.asSequence().mapNotNull { it as? ProductsFragment } + childFragmentManager.fragments.asSequence().mapNotNull { it as? AppListFragment } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -212,9 +213,9 @@ class TabsFragment : ScreenFragment() { viewPager = ViewPager2(content.context).apply { id = R.id.fragment_pager adapter = object : FragmentStateAdapter(this@TabsFragment) { - override fun getItemCount(): Int = ProductsFragment.Source.values().size - override fun createFragment(position: Int): Fragment = ProductsFragment( - ProductsFragment + override fun getItemCount(): Int = AppListFragment.Source.values().size + override fun createFragment(position: Int): Fragment = AppListFragment( + AppListFragment .Source.values()[position] ) } @@ -225,7 +226,7 @@ class TabsFragment : ScreenFragment() { viewPager?.let { TabLayoutMediator(layout.tabs, it) { tab, position -> - tab.text = getString(ProductsFragment.Source.values()[position].titleResId) + tab.text = getString(AppListFragment.Source.values()[position].titleResId) }.attach() } @@ -336,7 +337,7 @@ class TabsFragment : ScreenFragment() { override fun onAttachFragment(childFragment: Fragment) { super.onAttachFragment(childFragment) - if (view != null && childFragment is ProductsFragment) { + if (view != null && childFragment is AppListFragment) { childFragment.setSearchQuery(searchQuery) childFragment.setSection(section) childFragment.setOrder(Preferences[Preferences.Key.SortOrder].order) @@ -365,7 +366,7 @@ class TabsFragment : ScreenFragment() { if (view != null) { val viewPager = viewPager viewPager?.setCurrentItem( - ProductsFragment.Source.UPDATES.ordinal, + AppListFragment.Source.UPDATES.ordinal, allowSmooth && viewPager.isLaidOut ) } else { @@ -373,8 +374,8 @@ class TabsFragment : ScreenFragment() { } } - private fun updateUpdateNotificationBlocker(activeSource: ProductsFragment.Source) { - val blockerFragment = if (activeSource == ProductsFragment.Source.UPDATES) { + private fun updateUpdateNotificationBlocker(activeSource: AppListFragment.Source) { + val blockerFragment = if (activeSource == AppListFragment.Source.UPDATES) { productFragments.find { it.source == activeSource } } else { null @@ -464,9 +465,9 @@ class TabsFragment : ScreenFragment() { positionOffsetPixels: Int ) { val layout = layout!! - val fromSections = ProductsFragment.Source.values()[position].sections + val fromSections = AppListFragment.Source.values()[position].sections val toSections = if (positionOffset <= 0f) fromSections else - ProductsFragment.Source.values()[position + 1].sections + AppListFragment.Source.values()[position + 1].sections val offset = if (fromSections != toSections) { if (fromSections) 1f - positionOffset else positionOffset } else { @@ -484,7 +485,7 @@ class TabsFragment : ScreenFragment() { } override fun onPageSelected(position: Int) { - val source = ProductsFragment.Source.values()[position] + val source = AppListFragment.Source.values()[position] updateUpdateNotificationBlocker(source) sortOrderMenu!!.first.apply { isVisible = source.order @@ -501,7 +502,7 @@ class TabsFragment : ScreenFragment() { } override fun onPageScrollStateChanged(state: Int) { - val source = ProductsFragment.Source.values()[viewPager!!.currentItem] + val source = AppListFragment.Source.values()[viewPager!!.currentItem] layout!!.sectionChange.isEnabled = state != ViewPager2.SCROLL_STATE_DRAGGING && source.sections if (state == ViewPager2.SCROLL_STATE_IDLE) { diff --git a/src/main/kotlin/com/looker/droidify/screen/ProductsFragment.kt b/src/main/kotlin/com/looker/droidify/ui/appsList/AppListFragment.kt similarity index 95% rename from src/main/kotlin/com/looker/droidify/screen/ProductsFragment.kt rename to src/main/kotlin/com/looker/droidify/ui/appsList/AppListFragment.kt index 1aa5924f..9741ad92 100644 --- a/src/main/kotlin/com/looker/droidify/screen/ProductsFragment.kt +++ b/src/main/kotlin/com/looker/droidify/ui/appsList/AppListFragment.kt @@ -1,4 +1,4 @@ -package com.looker.droidify.screen +package com.looker.droidify.ui.appsList import android.database.Cursor import android.os.Bundle @@ -14,7 +14,8 @@ import com.looker.droidify.R import com.looker.droidify.database.CursorOwner import com.looker.droidify.database.Database import com.looker.droidify.entity.ProductItem -import com.looker.droidify.ui.ProductsViewModel +import com.looker.droidify.screen.BaseFragment +import com.looker.droidify.screen.ProductsAdapter import com.looker.droidify.utility.RxUtils import com.looker.droidify.widget.RecyclerFastScroller import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers @@ -23,9 +24,9 @@ import io.reactivex.rxjava3.disposables.Disposable import io.reactivex.rxjava3.schedulers.Schedulers import kotlinx.coroutines.flow.collect -class ProductsFragment() : BaseFragment(), CursorOwner.Callback { +class AppListFragment() : BaseFragment(), CursorOwner.Callback { - private val viewModel: ProductsViewModel by viewModels() + private val viewModel: AppListViewModel by viewModels() companion object { private const val EXTRA_SOURCE = "source" @@ -34,7 +35,7 @@ class ProductsFragment() : BaseFragment(), CursorOwner.Callback { enum class Source(val titleResId: Int, val sections: Boolean, val order: Boolean) { AVAILABLE(R.string.available, true, true), - INSTALLED(R.string.installed, false, false), + INSTALLED(R.string.installed, false, true), UPDATES(R.string.updates, false, false) } diff --git a/src/main/kotlin/com/looker/droidify/ui/ProductsViewModel.kt b/src/main/kotlin/com/looker/droidify/ui/appsList/AppListViewModel.kt similarity index 96% rename from src/main/kotlin/com/looker/droidify/ui/ProductsViewModel.kt rename to src/main/kotlin/com/looker/droidify/ui/appsList/AppListViewModel.kt index 3502822d..3f1adb37 100644 --- a/src/main/kotlin/com/looker/droidify/ui/ProductsViewModel.kt +++ b/src/main/kotlin/com/looker/droidify/ui/appsList/AppListViewModel.kt @@ -1,4 +1,4 @@ -package com.looker.droidify.ui +package com.looker.droidify.ui.appsList import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope @@ -10,7 +10,7 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch -class ProductsViewModel : ViewModel() { +class AppListViewModel : ViewModel() { private val _order = MutableStateFlow(ProductItem.Order.LAST_UPDATE) private val _sections = MutableStateFlow(ProductItem.Section.All)