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