Add: Check the selected category on launch/resume

This commit is contained in:
machiav3lli 2022-02-20 02:56:41 +01:00
parent 555bf1f14f
commit e7295d90d6
2 changed files with 24 additions and 10 deletions

View File

@ -6,9 +6,11 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.Scaffold import androidx.compose.material.Scaffold
import androidx.core.view.children
import com.google.android.material.chip.Chip import com.google.android.material.chip.Chip
import com.looker.droidify.R import com.looker.droidify.R
import com.looker.droidify.content.Preferences import com.looker.droidify.content.Preferences
import com.looker.droidify.database.entity.Category
import com.looker.droidify.database.entity.Repository import com.looker.droidify.database.entity.Repository
import com.looker.droidify.databinding.FragmentExploreXBinding import com.looker.droidify.databinding.FragmentExploreXBinding
import com.looker.droidify.entity.Section import com.looker.droidify.entity.Section
@ -64,15 +66,24 @@ class ExploreFragment : MainNavFragmentX() {
} }
} }
viewModel.categories.observe(viewLifecycleOwner) { viewModel.categories.observe(viewLifecycleOwner) {
binding.categories.removeAllViews() binding.categories.apply {
binding.categories.addView(Chip(requireContext(), null, R.attr.chipStyle).apply { removeAllViews()
addView(Chip(requireContext(), null, R.attr.chipStyle).apply {
setText(R.string.all_applications) setText(R.string.all_applications)
id = R.id.SHOW_ALL
}) })
it.forEach { it.forEach {
binding.categories.addView(Chip(requireContext(), null, R.attr.chipStyle).apply { addView(Chip(requireContext(), null, R.attr.chipStyle).apply {
text = it text = it
}) })
} }
val selectedSection = viewModel.sections.value
check(
children.filterNotNull()
.find { it is Chip && selectedSection is Category && it.text == selectedSection.name }?.id
?: R.id.SHOW_ALL
)
}
} }
binding.categories.setOnCheckedChangeListener { group, checkedId -> binding.categories.setOnCheckedChangeListener { group, checkedId ->
group.findViewById<Chip>(checkedId).let { group.findViewById<Chip>(checkedId).let {

View File

@ -19,9 +19,12 @@ class MainNavFragmentViewModelX(
secondarySource: Source secondarySource: Source
) : ViewModel() { ) : ViewModel() {
private val order = MutableLiveData(Order.LAST_UPDATE) var order = MutableLiveData(Order.LAST_UPDATE)
private val sections = MutableLiveData<Section>(Section.All) private set
private val searchQuery = MutableLiveData("") var sections = MutableLiveData<Section>(Section.All)
private set
var searchQuery = MutableLiveData("")
private set
fun request(source: Source): Request { fun request(source: Source): Request {
var mSearchQuery = "" var mSearchQuery = ""