From e9cb3d2f3770d062ec8162891a9c4699518589e8 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Wed, 17 Aug 2022 21:00:58 +0200 Subject: [PATCH] Fixing some sonarqube issues. --- .../android/HomeActivity.kt | 106 +++++++----------- 1 file changed, 43 insertions(+), 63 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 4ea2268..c77bbfe 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 @@ -408,6 +408,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar val drawerListener = object : DrawerLayout.DrawerListener { override fun onDrawerSlide(drawerView: View, slideOffset: Float) { + // We do nothing } override fun onDrawerOpened(drawerView: View) { @@ -419,6 +420,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar } override fun onDrawerStateChanged(newState: Int) { + // We do nothing } } @@ -469,6 +471,45 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar private fun handleDrawerItems() { tagsBadge = emptyMap() fun handleDrawerData(maybeDrawerData: DrawerData?, loadedFromCache: Boolean = false) { + fun createDrawerItem( + it: SelfossModel.Tag + ) { + val gd = GradientDrawable() + val gdColor = try { + Color.parseColor(it.color) + } catch (e: IllegalArgumentException) { + appColors.colorPrimary + } + + gd.setColor(gdColor) + gd.shape = GradientDrawable.RECTANGLE + gd.setSize(30, 30) + gd.cornerRadius = 30F + + val drawerItem = PrimaryDrawerItem() + .apply { + nameText = it.getTitleDecoded() + identifier = it.tag.longHash() + iconDrawable = gd + badgeStyle = BadgeStyle().apply { + textColor = ColorHolder.fromColor(Color.WHITE) + color = ColorHolder.fromColor(appColors.colorAccent) + } + onDrawerItemClickListener = { _, _, _ -> + repository.tagFilter = it + repository.sourceFilter = null + getElementsAccordingToTab() + fetchOnEmptyList() + false + } + } + if (it.unread > 0) { + drawerItem.badgeText = it.unread.toString() + } + + binding.mainDrawer.itemAdapter.add(drawerItem) + } + fun handleTags(maybeTags: List?) { if (maybeTags == null) { if (loadedFromCache) { @@ -482,38 +523,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar .filterNot { hiddenTags.contains(it.tag) } .sortedBy { it.unread == 0 } tagsBadge = filteredTags.map { - val gd = GradientDrawable() - val gdColor = try { - Color.parseColor(it.color) - } catch (e: IllegalArgumentException) { - appColors.colorPrimary - } - - gd.setColor(gdColor) - gd.shape = GradientDrawable.RECTANGLE - gd.setSize(30, 30) - gd.cornerRadius = 30F - val drawerItem = - PrimaryDrawerItem() - .apply { - nameText = it.getTitleDecoded() - identifier = it.tag.longHash() - iconDrawable = gd - badgeStyle = BadgeStyle().apply { - textColor = ColorHolder.fromColor(Color.WHITE) - color = ColorHolder.fromColor(appColors.colorAccent) } - onDrawerItemClickListener = { _,_,_ -> - repository.tagFilter = it - repository.sourceFilter = null - getElementsAccordingToTab() - fetchOnEmptyList() - false - } } - if (it.unread > 0) { - drawerItem.badgeText = it.unread.toString() - } - - binding.mainDrawer.itemAdapter.add(drawerItem) + createDrawerItem(it) (it.tag.longHash() to it.unread) }.toMap() @@ -534,37 +544,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar val filteredHiddenTags: List = maybeTags.filter { hiddenTags.contains(it.tag) } tagsBadge = filteredHiddenTags.map { - val gd = GradientDrawable() - val gdColor = try { - Color.parseColor(it.color) - } catch (e: IllegalArgumentException) { - appColors.colorPrimary - } - - gd.setColor(gdColor) - gd.shape = GradientDrawable.RECTANGLE - gd.setSize(30, 30) - gd.cornerRadius = 30F - val drawerItem = - PrimaryDrawerItem().apply { - nameText = it.getTitleDecoded() - identifier = it.tag.longHash() - iconDrawable = gd - badgeStyle = BadgeStyle().apply { - textColor = ColorHolder.fromColor(Color.WHITE) - color = ColorHolder.fromColor(appColors.colorAccent) } - onDrawerItemClickListener = { _,_,_ -> - repository.tagFilter = it - repository.sourceFilter = null - getElementsAccordingToTab() - fetchOnEmptyList() - false - } } - - if (it.unread > 0) { - drawerItem.badgeText = it.unread.toString() - } - binding.mainDrawer.itemAdapter.add(drawerItem) + createDrawerItem(it) (it.tag.longHash() to it.unread) }.toMap()