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

130 lines
4.6 KiB
Kotlin
Raw Normal View History

package com.github.libretube.helpers
2022-09-20 22:06:23 +05:30
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
import android.view.View
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,
""
2022-10-30 14:06:19 +05:30
).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(
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
2022-09-20 22:27:50 +05:30
).icon = menuItem.icon
}
}
if (navBarItems.filter { it.isVisible }.isEmpty()) bottomNav.visibility = View.GONE
2022-11-24 22:08:14 +05:30
return getStartFragmentId(bottomNav.context)
}
fun getStartFragmentId(context: Context): Int {
val pref = PreferenceHelper.getInt(PreferenceKeys.START_FRAGMENT, Int.MAX_VALUE)
val defaultNavItems = getDefaultNavBarItems(context)
return if (pref == Int.MAX_VALUE) {
getNavBarItems(context).firstOrNull { it.isVisible }?.itemId ?: R.id.homeFragment
2022-11-24 22:08:14 +05:30
} else {
defaultNavItems.get(pref).itemId
}
}
fun setStartFragment(context: Context, itemId: Int) {
val index = getDefaultNavBarItems(context).indexOfFirst { it.itemId == itemId }
PreferenceHelper.putInt(PreferenceKeys.START_FRAGMENT, index)
2022-09-20 22:27:50 +05:30
}
2022-09-20 22:06:23 +05:30
}