fix(navigation): dynamically update bottom navigation items

This commit is contained in:
Hy4ri committed 2026-06-19 18:02:49 +03:00
1 parent 67c633396e
commit 91ad09b5f2
2 files changed
+14 -6

No files matched your search

@@ -179,12 +179,11 @@ fun MainNavigation() {
val scope = rememberCoroutineScope()
// Read dynamic bottom-nav config.
// NB: deliberately NOT wrapped in remember { … } — AuthManager values change
// when the user customises items in Settings (saved to SharedPreferences).
// Without a key that tracks prefs changes, remember would cache the stale
// defaults and the NavigationBar would never reflect the user's picks.
// Reading fresh on every recomposition is negligible (pure map over 18 items).
val bottomNavItems = resolveBottomNavItems(AuthManager.getBottomNavItems())
// AuthManager.bottomNavItemsFlow is a StateFlow that emits updates when the user
// customises items in Settings. Using collectAsState() ensures the NavigationBar
// recomposes and reflects choices instantly.
val bottomNavItemsState by AuthManager.bottomNavItemsFlow.collectAsState()
val bottomNavItems = resolveBottomNavItems(bottomNavItemsState)
val bottomNavKeys = remember(bottomNavItems) { bottomNavItems.map { it.key }.toSet() }
// Sync primary screens to NavigationController
@@ -5,6 +5,9 @@ import android.content.SharedPreferences
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey
import com.m57.hermescontrol.theme.ThemePreference
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
/**
* Singleton that manages encrypted storage of the Hermes dashboard token
@@ -29,6 +32,9 @@ object AuthManager {
@Volatile
private var prefs: SharedPreferences? = null
private val _bottomNavItemsFlow = MutableStateFlow<List<String>>(emptyList())
val bottomNavItemsFlow: StateFlow<List<String>> = _bottomNavItemsFlow.asStateFlow()
/**
* Initialise the encrypted preferences.
* Call this once from Application.onCreate() or MainActivity.onCreate().
@@ -51,6 +57,8 @@ object AuthManager {
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM,
)
_bottomNavItemsFlow.value = getBottomNavItems()
}
}
@@ -134,5 +142,6 @@ object AuthManager {
/** Persist the ordered list of bottom-nav item keys (max 5, data-object names). */
fun setBottomNavItems(items: List<String>) {
requirePrefs().edit().putString(KEY_BOTTOM_NAV_ITEMS, items.joinToString(",")).apply()
_bottomNavItemsFlow.value = items
}
}