From 748ed41096934d3a512a107ee534b65ab6c5fbd2 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Mon, 5 Dec 2022 22:29:06 +0100 Subject: [PATCH] Filters working. --- .../android/HomeActivity.kt | 4 +- .../android/fragments/FilterSheetFragment.kt | 23 +++++++++ .../src/main/res/layout/filter_fragment.xml | 47 +++++++++++++------ .../repository/RepositoryImpl.kt | 26 ++++++---- 4 files changed, 75 insertions(+), 25 deletions(-) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt index 80b24cb..97b998f 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt @@ -662,7 +662,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar }) } - private fun fetchOnEmptyList() { + fun fetchOnEmptyList() { binding.recyclerView.doOnNextLayout { // TODO: do if last element (or is empty ?) getElementsAccordingToTab(true) @@ -703,7 +703,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar binding.emptyText.visibility = View.GONE } - private fun getElementsAccordingToTab( + fun getElementsAccordingToTab( appendResults: Boolean = false ) { offset = if (appendResults && items.size > 0) { diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/FilterSheetFragment.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/FilterSheetFragment.kt index 53a803d..d9cdac1 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/FilterSheetFragment.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/FilterSheetFragment.kt @@ -10,6 +10,7 @@ import android.view.View import android.view.View.GONE import android.view.View.VISIBLE import android.view.ViewGroup +import bou.amine.apps.readerforselfossv2.android.HomeActivity import bou.amine.apps.readerforselfossv2.android.R import bou.amine.apps.readerforselfossv2.android.sendSilentlyWithAcraWithName import bou.amine.apps.readerforselfossv2.repository.Repository @@ -78,6 +79,7 @@ class FilterSheetFragment : BottomSheetDialogFragment(), DIAware { c.setOnCloseIconClickListener { (it as Chip).isCloseIconVisible = false tagChip = null + repository.setTagFilter(null) } c.setOnClickListener { @@ -86,7 +88,14 @@ class FilterSheetFragment : BottomSheetDialogFragment(), DIAware { } (it as Chip).isCloseIconVisible = true tagChip = it + repository.setTagFilter(tag) } + + if (repository.tagFilter.value?.equals(tag) == true) { + c.isCloseIconVisible = true + tagChip = c + } + tagGroup.addView(c) } @@ -122,6 +131,7 @@ class FilterSheetFragment : BottomSheetDialogFragment(), DIAware { c.setOnCloseIconClickListener { (it as Chip).isCloseIconVisible = false sourceChip = null + repository.setSourceFilter(null) } c.setOnClickListener { @@ -130,7 +140,15 @@ class FilterSheetFragment : BottomSheetDialogFragment(), DIAware { } (it as Chip).isCloseIconVisible = true sourceChip = it + repository.setSourceFilter(source) } + + + if (repository.sourceFilter.value?.equals(source) == true) { + c.isCloseIconVisible = true + sourceChip = c + } + sourceGroup.addView(c) } @@ -138,6 +156,11 @@ class FilterSheetFragment : BottomSheetDialogFragment(), DIAware { binding.filterView.visibility = VISIBLE } + binding.floatingActionButton2.setOnClickListener { + (activity as HomeActivity).getElementsAccordingToTab() + (activity as HomeActivity).fetchOnEmptyList() + dismiss() + } return binding.root } diff --git a/androidApp/src/main/res/layout/filter_fragment.xml b/androidApp/src/main/res/layout/filter_fragment.xml index 80a8354..e4ca520 100644 --- a/androidApp/src/main/res/layout/filter_fragment.xml +++ b/androidApp/src/main/res/layout/filter_fragment.xml @@ -2,6 +2,7 @@ @@ -13,13 +14,29 @@ app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toTopOf="parent" /> + app:layout_constraintTop_toTopOf="parent" + tools:visibility="gone" /> + android:visibility="gone" + tools:visibility="visible"> + + - - - - - + + + + + (null) + var tagFilter = _tagFilter.asStateFlow() + private var _sourceFilter = MutableStateFlow(null) + var sourceFilter = _sourceFilter.asStateFlow() var searchFilter: String? = null var offlineOverride = false @@ -47,8 +49,8 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap fetchedItems = api.getItems( displayedItems.type, offset = 0, - tagFilter?.tag, - sourceFilter?.id?.toLong(), + tagFilter.value?.tag, + sourceFilter.value?.id?.toLong(), searchFilter, null ) @@ -61,10 +63,10 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap (it.starred && displayedItems == ItemType.STARRED) } if (tagFilter != null) { - dbItems = dbItems.filter { it.tags.split(',').contains(tagFilter!!.tag) } + dbItems = dbItems.filter { it.tags.split(',').contains(tagFilter.value?.tag) } } if (sourceFilter != null) { - dbItems = dbItems.filter { it.sourcetitle == sourceFilter!!.title } + dbItems = dbItems.filter { it.sourcetitle == sourceFilter.value?.title } } fetchedItems = StatusAndData.succes( dbItems.map { it.toView() } @@ -88,8 +90,8 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap fetchedItems = api.getItems( displayedItems.type, offset, - tagFilter?.tag, - sourceFilter?.id?.toLong(), + tagFilter.value?.tag, + sourceFilter.value?.id?.toLong(), searchFilter, null ) @@ -505,4 +507,12 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap deleteDBAction(action) } } + + fun setTagFilter(tag: SelfossModel.Tag?) { + _tagFilter.value = tag + } + + fun setSourceFilter(source: SelfossModel.Source?) { + _sourceFilter.value = source + } } \ No newline at end of file