File tree Expand file tree Collapse file tree 3 files changed +22
-6
lines changed
app/src/main/java/com/paulcoding/hviewer/ui Expand file tree Collapse file tree 3 files changed +22
-6
lines changed Original file line number Diff line number Diff line change 11package com.paulcoding.hviewer.ui
22
3+ import androidx.compose.runtime.Composable
4+ import androidx.compose.runtime.remember
35import androidx.compose.runtime.staticCompositionLocalOf
6+ import com.paulcoding.hviewer.model.PostItem
47import com.paulcoding.hviewer.model.SiteConfig
58
69val LocalHostsMap = staticCompositionLocalOf<Map <String , SiteConfig >> { mapOf () }
10+
11+ @Composable
12+ fun rememberSiteConfig (postItem : PostItem ): SiteConfig {
13+ val hostsMap = LocalHostsMap .current
14+ return remember { hostsMap[postItem.getHost()] ? : SiteConfig () }
15+ }
16+
17+ @Composable
18+ fun rememberSiteConfig (url : String ): SiteConfig {
19+ val hostsMap = LocalHostsMap .current
20+ val host = url.split(" /" ).getOrNull(2 )
21+ return remember { hostsMap[host] ? : SiteConfig () }
22+ }
Original file line number Diff line number Diff line change @@ -11,9 +11,7 @@ import androidx.compose.runtime.Composable
1111import androidx.compose.runtime.CompositionLocalProvider
1212import androidx.compose.runtime.LaunchedEffect
1313import androidx.compose.runtime.collectAsState
14- import androidx.compose.runtime.derivedStateOf
1514import androidx.compose.runtime.getValue
16- import androidx.compose.runtime.remember
1715import androidx.compose.runtime.rememberUpdatedState
1816import androidx.compose.ui.platform.LocalContext
1917import androidx.navigation.NamedNavArgument
@@ -51,7 +49,8 @@ import com.paulcoding.hviewer.ui.page.web.WebPage
5149fun AppEntry (intent : Intent ? , appViewModel : AppViewModel ) {
5250 val navController = rememberNavController()
5351 val appState by appViewModel.stateFlow.collectAsState()
54- val hostsMap by remember { derivedStateOf { appState.siteConfigs?.toHostsMap() ? : mapOf () } }
52+ val hostsMap by appViewModel.hostsMap.collectAsState()
53+
5554 val context = LocalContext .current
5655
5756 fun navToImages (post : PostItem ) {
Original file line number Diff line number Diff line change @@ -35,6 +35,9 @@ class AppViewModel : ViewModel() {
3535 private var _stateFlow = MutableStateFlow (UiState ())
3636 val stateFlow = _stateFlow .asStateFlow()
3737
38+ val hostsMap = _stateFlow .map { it.siteConfigs?.toHostsMap() ? : mapOf () }
39+ .stateIn(viewModelScope, SharingStarted .Eagerly , mapOf ())
40+
3841 private var _tabs = MutableStateFlow (listOf<PostItem >())
3942 val tabs = _tabs .asStateFlow()
4043
@@ -140,9 +143,7 @@ class AppViewModel : ViewModel() {
140143 }
141144
142145 fun getCurrentSiteConfig (): SiteConfig {
143- val hostMap = _stateFlow .value.siteConfigs?.toHostsMap()
144- ? : throw Exception (" Host map is null: ${_stateFlow .value.siteConfigs} " )
145- return hostMap[_stateFlow .value.post.getHost()]
146+ return hostsMap.value[_stateFlow .value.post.getHost()]
146147 ? : throw (Exception (" No site config found for ${stateFlow.value.post.url} " ))
147148 }
148149
You can’t perform that action at this time.
0 commit comments