diff --git a/app/src/main/java/com/github/libretube/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/activities/MainActivity.kt index 01adaeb15..27c49c31a 100644 --- a/app/src/main/java/com/github/libretube/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/MainActivity.kt @@ -33,12 +33,12 @@ import com.github.libretube.models.PlayerViewModel import com.github.libretube.models.SearchViewModel import com.github.libretube.models.SubscriptionsViewModel import com.github.libretube.services.ClosingService +import com.github.libretube.util.NavBarHelper import com.github.libretube.util.NetworkHelper import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.ThemeHelper import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.elevation.SurfaceColors -import com.google.android.material.navigation.NavigationBarView class MainActivity : BaseActivity() { @@ -93,13 +93,7 @@ class MainActivity : BaseActivity() { window.navigationBarColor = color // save start tab fragment id - startFragmentId = - when (PreferenceHelper.getString(PreferenceKeys.DEFAULT_TAB, "home")) { - "home" -> R.id.homeFragment - "subscriptions" -> R.id.subscriptionsFragment - "library" -> R.id.libraryFragment - else -> R.id.homeFragment - } + startFragmentId = NavBarHelper.applyNavBarStyle(binding.bottomNav) // set default tab as start fragment navController.graph.setStartDestination(startFragmentId) @@ -107,16 +101,6 @@ class MainActivity : BaseActivity() { // navigate to the default fragment navController.navigate(startFragmentId) - val labelVisibilityMode = when ( - PreferenceHelper.getString(PreferenceKeys.LABEL_VISIBILITY, "always") - ) { - "always" -> NavigationBarView.LABEL_VISIBILITY_LABELED - "selected" -> NavigationBarView.LABEL_VISIBILITY_SELECTED - "never" -> NavigationBarView.LABEL_VISIBILITY_UNLABELED - else -> NavigationBarView.LABEL_VISIBILITY_AUTO - } - binding.bottomNav.labelVisibilityMode = labelVisibilityMode - binding.bottomNav.setOnApplyWindowInsetsListener(null) binding.bottomNav.setOnItemSelectedListener { diff --git a/app/src/main/java/com/github/libretube/dialogs/NavBarOptionsDialog.kt b/app/src/main/java/com/github/libretube/dialogs/NavBarOptionsDialog.kt index 478295f6a..7b36d08d4 100644 --- a/app/src/main/java/com/github/libretube/dialogs/NavBarOptionsDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/NavBarOptionsDialog.kt @@ -63,7 +63,6 @@ class NavBarOptionsDialog : DialogFragment() { .setTitle(R.string.navigation_bar) .setView(binding.root) .setPositiveButton(R.string.okay) { _, _ -> - Log.e("options", adapter.items.toString()) NavBarHelper.setNavBarItems(adapter.items) } .setNegativeButton(R.string.cancel, null) 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 e4d7d5c12..f8b8eec14 100644 --- a/app/src/main/java/com/github/libretube/obj/NavBarItem.kt +++ b/app/src/main/java/com/github/libretube/obj/NavBarItem.kt @@ -1,7 +1,7 @@ package com.github.libretube.obj data class NavBarItem( - val resourceId: Int, - val titleResource: Int, + val id: Int = 0, + val titleResource: Int = 0, var isEnabled: Boolean = true ) 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 004af4554..38d4d0e30 100644 --- a/app/src/main/java/com/github/libretube/util/NavBarHelper.kt +++ b/app/src/main/java/com/github/libretube/util/NavBarHelper.kt @@ -1,9 +1,16 @@ package com.github.libretube.util +import android.util.Log +import android.view.Menu +import android.view.MenuItem import com.fasterxml.jackson.core.type.TypeReference import com.fasterxml.jackson.databind.ObjectMapper +import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.navBarItems +import com.github.libretube.extensions.TAG import com.github.libretube.obj.NavBarItem +import com.google.android.material.bottomnavigation.BottomNavigationView +import com.google.android.material.navigation.NavigationBarView object NavBarHelper { val preferenceKey = "nav_bar_items" @@ -30,4 +37,45 @@ object NavBarHelper { mapper.writeValueAsString(items) ) } + + /** + * Apply the bottom navigation style configured in the preferences + * @return Id of the start fragment + */ + fun applyNavBarStyle(bottomNav: BottomNavigationView): Int { + val labelVisibilityMode = when ( + PreferenceHelper.getString(PreferenceKeys.LABEL_VISIBILITY, "always") + ) { + "always" -> NavigationBarView.LABEL_VISIBILITY_LABELED + "selected" -> NavigationBarView.LABEL_VISIBILITY_SELECTED + "never" -> NavigationBarView.LABEL_VISIBILITY_UNLABELED + else -> NavigationBarView.LABEL_VISIBILITY_AUTO + } + bottomNav.labelVisibilityMode = labelVisibilityMode + + val navBarItems = getNavBarItems() + + val menuItems = mutableListOf() + // remove the old items + navBarItems.forEach { + menuItems.add( + bottomNav.menu.findItem(it.id) + ) + bottomNav.menu.removeItem(it.id) + } + + navBarItems.forEach { navBarItem -> + if (navBarItem.isEnabled) { + val menuItem = menuItems.filter { it.itemId == navBarItem.id }[0] + + bottomNav.menu.add( + menuItem.groupId, + menuItem.itemId, + Menu.NONE, + menuItem.title + ).icon = menuItem.icon + } + } + return navBarItems[0].id + } }