From 9833a66a64e7f574c4a65cf154b4b0bb50de9dbf Mon Sep 17 00:00:00 2001 From: aminecmi Date: Thu, 8 Dec 2022 14:02:30 +0100 Subject: [PATCH] Cleaning tags duplications. --- .../android/fragments/FilterSheetFragment.kt | 119 +++++++----------- 1 file changed, 44 insertions(+), 75 deletions(-) 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 5ae9317..50fd8ac 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 @@ -13,6 +13,7 @@ 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.model.SelfossModel import bou.amine.apps.readerforselfossv2.repository.Repository import bou.amine.apps.readerforselfossv2.service.AppSettingsService import bou.amine.apps.readerforselfossv2.utils.getHtmlDecoded @@ -63,44 +64,7 @@ class FilterSheetFragment : BottomSheetDialogFragment(), DIAware { val hiddenTags = appSettingsService.getHiddenTags() tags.filterNot { hiddenTags.contains(it.tag) }.forEach { tag -> - val c = Chip(requireContext()) - c.text = tag.tag - - val gd = GradientDrawable() - val gdColor = try { - Color.parseColor(tag.color) - } catch (e: IllegalArgumentException) { - e.sendSilentlyWithAcraWithName("color issue " + tag.color) - resources.getColor(R.color.colorPrimary) - } - gd.setColor(gdColor) - gd.shape = GradientDrawable.RECTANGLE - gd.setSize(30, 30) - gd.cornerRadius = 30F - c.chipIcon = gd - - c.setOnCloseIconClickListener { - (it as Chip).isCloseIconVisible = false - selectedChip = null - repository.setTagFilter(null) - } - - c.setOnClickListener { - if (selectedChip != null) { - selectedChip!!.isCloseIconVisible = false - } - (it as Chip).isCloseIconVisible = true - selectedChip = it - repository.setTagFilter(tag) - - repository.setSourceFilter(null) - } - - if (repository.tagFilter.value?.equals(tag) == true) { - c.isCloseIconVisible = true - selectedChip = c - } - + val c = chipForTag(tag) tagGroup.addView(c) } @@ -109,43 +73,7 @@ class FilterSheetFragment : BottomSheetDialogFragment(), DIAware { binding.hiddenTagsGroup.visibility = VISIBLE tags.filter { hiddenTags.contains(it.tag) }.forEach { tag -> - val c = Chip(requireContext()) - c.text = tag.tag - - val gd = GradientDrawable() - val gdColor = try { - Color.parseColor(tag.color) - } catch (e: IllegalArgumentException) { - e.sendSilentlyWithAcraWithName("color issue " + tag.color) - resources.getColor(R.color.colorPrimary) - } - gd.setColor(gdColor) - gd.shape = GradientDrawable.RECTANGLE - gd.setSize(30, 30) - gd.cornerRadius = 30F - c.chipIcon = gd - - c.setOnCloseIconClickListener { - (it as Chip).isCloseIconVisible = false - selectedChip = null - repository.setTagFilter(null) - } - - c.setOnClickListener { - if (selectedChip != null) { - selectedChip!!.isCloseIconVisible = false - } - (it as Chip).isCloseIconVisible = true - selectedChip = it - repository.setTagFilter(tag) - - repository.setSourceFilter(null) - } - - if (repository.tagFilter.value?.equals(tag) == true) { - c.isCloseIconVisible = true - selectedChip = c - } + val c = chipForTag(tag) hiddenTagGroup.addView(c) } @@ -218,6 +146,47 @@ class FilterSheetFragment : BottomSheetDialogFragment(), DIAware { return binding.root } + private fun chipForTag(tag: SelfossModel.Tag): Chip { + val c = Chip(requireContext()) + c.text = tag.tag + + val gd = GradientDrawable() + val gdColor = try { + Color.parseColor(tag.color) + } catch (e: IllegalArgumentException) { + e.sendSilentlyWithAcraWithName("color issue " + tag.color) + resources.getColor(R.color.colorPrimary) + } + gd.setColor(gdColor) + gd.shape = GradientDrawable.RECTANGLE + gd.setSize(30, 30) + gd.cornerRadius = 30F + c.chipIcon = gd + + c.setOnCloseIconClickListener { + (it as Chip).isCloseIconVisible = false + selectedChip = null + repository.setTagFilter(null) + } + + c.setOnClickListener { + if (selectedChip != null) { + selectedChip!!.isCloseIconVisible = false + } + (it as Chip).isCloseIconVisible = true + selectedChip = it + repository.setTagFilter(tag) + + repository.setSourceFilter(null) + } + + if (repository.tagFilter.value?.equals(tag) == true) { + c.isCloseIconVisible = true + selectedChip = c + } + return c + } + companion object { const val TAG = "ModalBottomSheet" }