Skip to content

Commit 5e54759

Browse files
committed
Update hostsMap calculation
1 parent b42d340 commit 5e54759

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
package com.paulcoding.hviewer.ui
22

3+
import androidx.compose.runtime.Composable
4+
import androidx.compose.runtime.remember
35
import androidx.compose.runtime.staticCompositionLocalOf
6+
import com.paulcoding.hviewer.model.PostItem
47
import com.paulcoding.hviewer.model.SiteConfig
58

69
val 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+
}

app/src/main/java/com/paulcoding/hviewer/ui/page/AppEntry.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import androidx.compose.runtime.Composable
1111
import androidx.compose.runtime.CompositionLocalProvider
1212
import androidx.compose.runtime.LaunchedEffect
1313
import androidx.compose.runtime.collectAsState
14-
import androidx.compose.runtime.derivedStateOf
1514
import androidx.compose.runtime.getValue
16-
import androidx.compose.runtime.remember
1715
import androidx.compose.runtime.rememberUpdatedState
1816
import androidx.compose.ui.platform.LocalContext
1917
import androidx.navigation.NamedNavArgument
@@ -51,7 +49,8 @@ import com.paulcoding.hviewer.ui.page.web.WebPage
5149
fun 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) {

app/src/main/java/com/paulcoding/hviewer/ui/page/AppViewModel.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)