LibreTube/app/src/main/java/com/github/libretube/util/NavBarHelper.kt

82 lines
2.6 KiB
Kotlin
Raw Normal View History

2022-09-20 22:06:23 +05:30
package com.github.libretube.util
2022-09-20 22:27:50 +05:30
import android.util.Log
import android.view.Menu
import android.view.MenuItem
2022-09-20 22:06:23 +05:30
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
2022-09-20 22:27:50 +05:30
import com.github.libretube.constants.PreferenceKeys
2022-09-20 22:06:23 +05:30
import com.github.libretube.constants.navBarItems
2022-09-20 22:27:50 +05:30
import com.github.libretube.extensions.TAG
2022-09-20 22:06:23 +05:30
import com.github.libretube.obj.NavBarItem
2022-09-20 22:27:50 +05:30
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.navigation.NavigationBarView
2022-09-20 22:06:23 +05:30
object NavBarHelper {
val preferenceKey = "nav_bar_items"
val mapper = ObjectMapper()
fun getNavBarItems(): List<NavBarItem> {
return try {
val type = object : TypeReference<List<NavBarItem>>() {}
mapper.readValue(
PreferenceHelper.getString(
preferenceKey,
""
),
type
)
} catch (e: Exception) {
return navBarItems
}
}
fun setNavBarItems(items: List<NavBarItem>) {
PreferenceHelper.putString(
preferenceKey,
mapper.writeValueAsString(items)
)
}
2022-09-20 22:27:50 +05:30
/**
* 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<MenuItem>()
// 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
}
2022-09-20 22:06:23 +05:30
}