From 91ad09b5f2d2bed3d881ab7f40321d71bb26ed28 Mon Sep 17 00:00:00 2001 From: Hy4ri Date: Fri, 19 Jun 2026 18:02:49 +0300 Subject: [PATCH] fix(navigation): dynamically update bottom navigation items --- app/src/main/java/com/m57/hermescontrol/Navigation.kt | 11 +++++------ .../com/m57/hermescontrol/data/local/AuthManager.kt | 9 +++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/m57/hermescontrol/Navigation.kt b/app/src/main/java/com/m57/hermescontrol/Navigation.kt index c2722382..9fa9e80f 100644 --- a/app/src/main/java/com/m57/hermescontrol/Navigation.kt +++ b/app/src/main/java/com/m57/hermescontrol/Navigation.kt @@ -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 diff --git a/app/src/main/java/com/m57/hermescontrol/data/local/AuthManager.kt b/app/src/main/java/com/m57/hermescontrol/data/local/AuthManager.kt index fc399fd9..29389ab8 100644 --- a/app/src/main/java/com/m57/hermescontrol/data/local/AuthManager.kt +++ b/app/src/main/java/com/m57/hermescontrol/data/local/AuthManager.kt @@ -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>(emptyList()) + val bottomNavItemsFlow: StateFlow> = _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) { requirePrefs().edit().putString(KEY_BOTTOM_NAV_ITEMS, items.joinToString(",")).apply() + _bottomNavItemsFlow.value = items } }