Skip to content

Commit d39b4e3

Browse files
committed
Fix custom tags navigation
1 parent 5e54759 commit d39b4e3

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed

app/src/main/java/com/paulcoding/hviewer/model/PostModel.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.paulcoding.hviewer.model
22

3+
import android.os.Parcelable
34
import androidx.room.Entity
45
import androidx.room.PrimaryKey
6+
import kotlinx.parcelize.Parcelize
57

68
data class PostData(
79
val images: List<String>,
@@ -36,10 +38,11 @@ data class PostItem(
3638
}
3739
}
3840

41+
@Parcelize
3942
data class Tag(
4043
val name: String = "",
4144
val url: String = "",
42-
)
45+
) : Parcelable
4346

4447
data class Posts(
4548
val posts: List<PostItem> = listOf(),

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fun AppEntry(intent: Intent?, appViewModel: AppViewModel) {
6161

6262
fun navToCustomTag(post: PostItem, tag: Tag) {
6363
appViewModel.setCurrentPost(post)
64-
appViewModel.setCurrentTag(tag)
64+
navController.currentBackStackEntry?.savedStateHandle?.set("tag", tag)
6565
navController.navigate(Route.CUSTOM_TAG)
6666
}
6767

@@ -159,14 +159,17 @@ fun AppEntry(intent: Intent?, appViewModel: AppViewModel) {
159159
)
160160
}
161161
animatedComposable(Route.CUSTOM_TAG) {
162-
CustomTagPage(
163-
appViewModel,
164-
navToCustomTag = { postItem, tag -> navToCustomTag(postItem, tag) },
165-
navToTabs = { navController.navigate(Route.TABS) },
166-
goBack = { navController.popBackStack() }
167-
) {
168-
navToImages(it)
169-
}
162+
val tag = navController.previousBackStackEntry?.savedStateHandle?.get<Tag>("tag")
163+
if (tag != null)
164+
CustomTagPage(
165+
appViewModel,
166+
tag = tag,
167+
navToCustomTag = { postItem, newTag -> navToCustomTag(postItem, newTag) },
168+
navToTabs = { navController.navigate(Route.TABS) },
169+
goBack = { navController.popBackStack() }
170+
) {
171+
navToImages(it)
172+
}
170173
}
171174
animatedComposable(Route.POST) {
172175
PostPage(

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import com.paulcoding.hviewer.helper.scriptsDir
1111
import com.paulcoding.hviewer.model.PostItem
1212
import com.paulcoding.hviewer.model.SiteConfig
1313
import com.paulcoding.hviewer.model.SiteConfigs
14-
import com.paulcoding.hviewer.model.Tag
1514
import com.paulcoding.hviewer.network.Github
1615
import com.paulcoding.hviewer.network.SiteConfigsState
1716
import com.paulcoding.hviewer.preference.Preferences
@@ -53,16 +52,9 @@ class AppViewModel : ViewModel() {
5352
_stateFlow.update { it.copy(post = post) }
5453
}
5554

56-
fun setCurrentTag(tag: Tag) {
57-
_stateFlow.update { it.copy(tag = tag) }
58-
}
59-
60-
fun getCurrentTag() = _stateFlow.value.tag
61-
6255
data class UiState(
6356
val post: PostItem = PostItem(),
6457
val url: String = "",
65-
val tag: Tag = Tag(),
6658
val isDevMode: Boolean = BuildConfig.DEBUG,
6759
val siteConfigs: SiteConfigs? = appContext.readConfigFile<SiteConfigs>().getOrNull(),
6860
val error: Throwable? = null,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ import com.paulcoding.hviewer.ui.page.AppViewModel
2525
@Composable
2626
fun CustomTagPage(
2727
appViewModel: AppViewModel,
28+
tag: Tag,
2829
goBack: () -> Unit,
2930
navToCustomTag: (PostItem, Tag) -> Unit,
3031
navToTabs: () -> Unit,
3132
navToImages: (PostItem) -> Unit
3233
) {
33-
val tag = appViewModel.getCurrentTag()
3434
var pageProgress by remember { mutableStateOf(1 to 1) }
3535
var tabsIconPosition by remember { mutableStateOf(Offset.Zero) }
3636
val tabs by appViewModel.tabs.collectAsState(initial = listOf())
@@ -62,7 +62,7 @@ fun CustomTagPage(
6262
},
6363
navToCustomTag = { post, newTag ->
6464
if (newTag.name != tag.name)
65-
navToCustomTag(post, tag)
65+
navToCustomTag(post, newTag)
6666
}) { post ->
6767
navToImages(post)
6868
}

0 commit comments

Comments
 (0)