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

96 lines
3.1 KiB
Kotlin
Raw Normal View History

2022-09-20 22:06:23 +05:30
package com.github.libretube.util
2022-09-22 19:06:32 +05:30
import android.content.Context
2022-09-20 22:27:50 +05:30
import android.view.Menu
2022-09-22 19:06:32 +05:30
import android.view.MenuInflater
2022-09-20 22:27:50 +05:30
import android.view.MenuItem
2022-09-22 19:06:32 +05:30
import android.widget.PopupMenu
import androidx.core.view.forEach
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:33:18 +05:30
import com.github.libretube.R
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.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 {
2022-09-22 19:06:32 +05:30
private const val preferenceKey = "nav_bar_items"
2022-09-20 22:33:18 +05:30
2022-09-22 19:06:32 +05:30
private val mapper = ObjectMapper()
2022-09-20 22:06:23 +05:30
2022-09-22 19:06:32 +05:30
fun getNavBarItems(context: Context): List<NavBarItem> {
2022-09-20 22:06:23 +05:30
return try {
val type = object : TypeReference<List<NavBarItem>>() {}
mapper.readValue(
PreferenceHelper.getString(
preferenceKey,
""
),
type
)
} catch (e: Exception) {
2022-09-22 19:06:32 +05:30
val p = PopupMenu(context, null)
MenuInflater(context).inflate(R.menu.bottom_menu, p.menu)
val defaultNavBarItems = mutableListOf<NavBarItem>()
p.menu.forEach {
defaultNavBarItems.add(
NavBarItem(
it.itemId,
it.title.toString()
)
)
}
2022-09-20 22:33:18 +05:30
return defaultNavBarItems
2022-09-20 22:06:23 +05:30
}
}
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
2022-09-22 19:06:32 +05:30
val navBarItems = getNavBarItems(bottomNav.context)
2022-09-20 22:27:50 +05:30
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
}