From beb2962676e1e5bca59b5eac3b066bcf76d0139c Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 22 Sep 2022 15:36:32 +0200 Subject: [PATCH] dont hardcode navbar defaults --- .../com/github/libretube/obj/NavBarItem.kt | 2 +- .../ui/adapters/NavBarOptionsAdapter.kt | 2 +- .../ui/dialogs/NavBarOptionsDialog.kt | 2 +- .../com/github/libretube/util/NavBarHelper.kt | 38 +++++++++---------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/github/libretube/obj/NavBarItem.kt b/app/src/main/java/com/github/libretube/obj/NavBarItem.kt index f8b8eec14..eac14a1c7 100644 --- a/app/src/main/java/com/github/libretube/obj/NavBarItem.kt +++ b/app/src/main/java/com/github/libretube/obj/NavBarItem.kt @@ -2,6 +2,6 @@ package com.github.libretube.obj data class NavBarItem( val id: Int = 0, - val titleResource: Int = 0, + val title: String = "", var isEnabled: Boolean = true ) diff --git a/app/src/main/java/com/github/libretube/ui/adapters/NavBarOptionsAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/NavBarOptionsAdapter.kt index 55f6513bb..0877e420b 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/NavBarOptionsAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/NavBarOptionsAdapter.kt @@ -28,7 +28,7 @@ class NavBarOptionsAdapter( override fun onBindViewHolder(holder: NavBarOptionsViewHolder, position: Int) { val item = items[position] holder.binding.apply { - title.text = root.context.getString(item.titleResource) + title.text = item.title checkbox.isChecked = item.isEnabled checkbox.setOnClickListener { if (!checkbox.isChecked && getEnabledItemsCount() < 2) { diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/NavBarOptionsDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/NavBarOptionsDialog.kt index e9d0c62a7..0d7833f7f 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/NavBarOptionsDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/NavBarOptionsDialog.kt @@ -18,7 +18,7 @@ class NavBarOptionsDialog : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { binding = DialogNavbarOptionsBinding.inflate(layoutInflater) - val options = NavBarHelper.getNavBarItems() + val options = NavBarHelper.getNavBarItems(requireContext()) val adapter = NavBarOptionsAdapter(options.toMutableList()) diff --git a/app/src/main/java/com/github/libretube/util/NavBarHelper.kt b/app/src/main/java/com/github/libretube/util/NavBarHelper.kt index c99906b88..32f66fda1 100644 --- a/app/src/main/java/com/github/libretube/util/NavBarHelper.kt +++ b/app/src/main/java/com/github/libretube/util/NavBarHelper.kt @@ -1,7 +1,11 @@ package com.github.libretube.util +import android.content.Context import android.view.Menu +import android.view.MenuInflater import android.view.MenuItem +import android.widget.PopupMenu +import androidx.core.view.forEach import com.fasterxml.jackson.core.type.TypeReference import com.fasterxml.jackson.databind.ObjectMapper import com.github.libretube.R @@ -11,26 +15,11 @@ import com.google.android.material.bottomnavigation.BottomNavigationView import com.google.android.material.navigation.NavigationBarView object NavBarHelper { - val preferenceKey = "nav_bar_items" + private const val preferenceKey = "nav_bar_items" - val defaultNavBarItems = listOf( - NavBarItem( - R.id.homeFragment, - R.string.startpage - ), - NavBarItem( - R.id.subscriptionsFragment, - R.string.subscriptions - ), - NavBarItem( - R.id.libraryFragment, - R.string.library - ) - ) + private val mapper = ObjectMapper() - val mapper = ObjectMapper() - - fun getNavBarItems(): List { + fun getNavBarItems(context: Context): List { return try { val type = object : TypeReference>() {} mapper.readValue( @@ -41,6 +30,17 @@ object NavBarHelper { type ) } catch (e: Exception) { + val p = PopupMenu(context, null) + MenuInflater(context).inflate(R.menu.bottom_menu, p.menu) + val defaultNavBarItems = mutableListOf() + p.menu.forEach { + defaultNavBarItems.add( + NavBarItem( + it.itemId, + it.title.toString() + ) + ) + } return defaultNavBarItems } } @@ -67,7 +67,7 @@ object NavBarHelper { } bottomNav.labelVisibilityMode = labelVisibilityMode - val navBarItems = getNavBarItems() + val navBarItems = getNavBarItems(bottomNav.context) val menuItems = mutableListOf() // remove the old items