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

112 lines
3.8 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-10-30 14:06:19 +05:30
import android.util.Log
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-10-30 14:06:19 +05:30
import androidx.core.view.get
import androidx.core.view.size
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
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-10-30 14:06:19 +05:30
private const val SEPARATOR = ","
2022-09-20 22:33:18 +05:30
2022-10-30 14:06:19 +05:30
// contains "-" -> invisible menu item, else -> visible menu item
fun getNavBarItems(context: Context): List<MenuItem> {
val prefItems = try {
PreferenceHelper.getString(
PreferenceKeys.NAVBAR_ITEMS,
""
).split(SEPARATOR)
2022-09-20 22:06:23 +05:30
} catch (e: Exception) {
2022-10-30 14:06:19 +05:30
Log.e("fail to parse nav items", e.toString())
return getDefaultNavBarItems(context)
}
val p = PopupMenu(context, null)
MenuInflater(context).inflate(R.menu.bottom_menu, p.menu)
if (prefItems.size == p.menu.size) {
val navBarItems = mutableListOf<MenuItem>()
prefItems.forEach {
navBarItems.add(
p.menu[it.replace("-", "").toInt()].apply {
this.isVisible = !it.contains("-")
}
2022-09-22 19:06:32 +05:30
)
}
2022-10-30 14:06:19 +05:30
return navBarItems
2022-09-20 22:06:23 +05:30
}
2022-10-30 14:06:19 +05:30
return getDefaultNavBarItems(context)
2022-09-20 22:06:23 +05:30
}
2022-10-30 14:06:19 +05:30
private fun getDefaultNavBarItems(context: Context): List<MenuItem> {
val p = PopupMenu(context, null)
MenuInflater(context).inflate(R.menu.bottom_menu, p.menu)
val navBarItems = mutableListOf<MenuItem>()
p.menu.forEach {
navBarItems.add(it)
}
return navBarItems
}
fun setNavBarItems(items: List<MenuItem>, context: Context) {
val prefString = mutableListOf<String>()
val defaultNavBarItems = getDefaultNavBarItems(context)
items.forEach { newItem ->
val index = defaultNavBarItems.indexOfFirst { newItem.itemId == it.itemId }
prefString.add(if (newItem.isVisible) index.toString() else "-$index")
}
2022-09-20 22:06:23 +05:30
PreferenceHelper.putString(
2022-10-30 14:06:19 +05:30
PreferenceKeys.NAVBAR_ITEMS,
prefString.joinToString(SEPARATOR)
2022-09-20 22:06:23 +05:30
)
}
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, "selected")
2022-09-20 22:27:50 +05:30
) {
"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(
2022-10-30 14:06:19 +05:30
bottomNav.menu.findItem(it.itemId)
2022-09-20 22:27:50 +05:30
)
2022-10-30 14:06:19 +05:30
bottomNav.menu.removeItem(it.itemId)
2022-09-20 22:27:50 +05:30
}
navBarItems.forEach { navBarItem ->
2022-10-30 14:06:19 +05:30
if (navBarItem.isVisible) {
val menuItem = menuItems.first { it.itemId == navBarItem.itemId }
2022-09-20 22:27:50 +05:30
bottomNav.menu.add(
menuItem.groupId,
menuItem.itemId,
Menu.NONE,
menuItem.title
).icon = menuItem.icon
}
}
2022-10-30 14:06:19 +05:30
return navBarItems.first { it.isVisible }.itemId
2022-09-20 22:27:50 +05:30
}
2022-09-20 22:06:23 +05:30
}