Skip to content

Commit 8f40331

Browse files
Merge pull request #2 from paulcoding810/feat/show-progress
Show page progress on posts, images, search
2 parents 623e1ac + d5bdac8 commit 8f40331

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.paulcoding.hviewer.ui.component
2+
3+
import androidx.compose.material3.Text
4+
import androidx.compose.runtime.Composable
5+
import androidx.compose.ui.unit.sp
6+
7+
@Composable
8+
fun HPageProgress(currentPage: Int, totalPage: Int) {
9+
Text("$currentPage/$totalPage", fontSize = 10.sp)
10+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import com.paulcoding.hviewer.model.SiteConfig
3838
import com.paulcoding.hviewer.ui.component.HBackIcon
3939
import com.paulcoding.hviewer.ui.component.HImage
4040
import com.paulcoding.hviewer.ui.component.HLoading
41+
import com.paulcoding.hviewer.ui.component.HPageProgress
4142
import com.paulcoding.hviewer.ui.component.HideSystemBars
4243
import me.saket.telephoto.zoomable.DoubleClickToZoomListener
4344
import me.saket.telephoto.zoomable.ZoomSpec
@@ -99,6 +100,10 @@ fun PostPage(siteConfig: SiteConfig, postUrl: String, goBack: () -> Unit) {
99100
HBackIcon { goBack() }
100101
},
101102
title = {},
103+
actions = {
104+
if (uiState.images.isNotEmpty())
105+
HPageProgress(uiState.postPage, uiState.postTotalPage)
106+
}
102107
)
103108
}
104109
}) {

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ import androidx.compose.runtime.Composable
2222
import androidx.compose.runtime.LaunchedEffect
2323
import androidx.compose.runtime.collectAsState
2424
import androidx.compose.runtime.getValue
25+
import androidx.compose.runtime.mutableStateOf
26+
import androidx.compose.runtime.remember
2527
import androidx.compose.runtime.rememberCoroutineScope
28+
import androidx.compose.runtime.setValue
2629
import androidx.compose.ui.Modifier
2730
import androidx.compose.ui.unit.dp
2831
import androidx.lifecycle.viewmodel.compose.viewModel
@@ -36,6 +39,7 @@ import com.paulcoding.hviewer.ui.component.HEmpty
3639
import com.paulcoding.hviewer.ui.component.HIcon
3740
import com.paulcoding.hviewer.ui.component.HImage
3841
import com.paulcoding.hviewer.ui.component.HLoading
42+
import com.paulcoding.hviewer.ui.component.HPageProgress
3943
import com.paulcoding.hviewer.ui.icon.Search
4044
import kotlinx.coroutines.launch
4145

@@ -53,10 +57,13 @@ fun PostsPage(
5357
val selectedTabIndex = pagerState.currentPage
5458
val currentPage = listTopic[selectedTabIndex]
5559
val scope = rememberCoroutineScope()
60+
var pageProgress by remember { mutableStateOf(1 to 1) }
61+
5662
Scaffold(topBar = {
5763
TopAppBar(title = { Text(currentPage.toCapital()) }, navigationIcon = {
5864
HBackIcon { goBack() }
5965
}, actions = {
66+
HPageProgress(pageProgress.first, pageProgress.second)
6067
HIcon(imageVector = Search) { navToSearch() }
6168
})
6269
}) { paddings ->
@@ -86,7 +93,12 @@ fun PostsPage(
8693
modifier = Modifier.fillMaxSize(),
8794
) { pageIndex ->
8895
val page = listTopic[pageIndex]
89-
PageContent(siteConfig, page) { postUrl ->
96+
PageContent(
97+
siteConfig,
98+
page,
99+
onPageChange = { currentPage, total ->
100+
pageProgress = currentPage to total
101+
}) { postUrl ->
90102
navToImages(postUrl)
91103
}
92104
}
@@ -95,7 +107,12 @@ fun PostsPage(
95107
}
96108

97109
@Composable
98-
fun PageContent(siteConfig: SiteConfig, topic: String, onClick: (String) -> Unit) {
110+
fun PageContent(
111+
siteConfig: SiteConfig,
112+
topic: String,
113+
onPageChange: (Int, Int) -> Unit,
114+
onClick: (String) -> Unit
115+
) {
99116
val viewModel: PostsViewModel = viewModel(
100117
factory = PostsViewModelFactory(siteConfig, topic),
101118
key = topic
@@ -119,6 +136,10 @@ fun PageContent(siteConfig: SiteConfig, topic: String, onClick: (String) -> Unit
119136
}
120137
}
121138

139+
LaunchedEffect(uiState.postsPage, uiState.postsTotalPage) {
140+
onPageChange(uiState.postsPage, uiState.postsTotalPage)
141+
}
142+
122143
LazyColumn(
123144
state = listState
124145
) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import com.paulcoding.hviewer.model.SiteConfig
3838
import com.paulcoding.hviewer.ui.component.HBackIcon
3939
import com.paulcoding.hviewer.ui.component.HEmpty
4040
import com.paulcoding.hviewer.ui.component.HLoading
41+
import com.paulcoding.hviewer.ui.component.HPageProgress
4142
import com.paulcoding.hviewer.ui.icon.EditIcon
4243
import com.paulcoding.hviewer.ui.page.posts.PostItemView
4344

@@ -52,6 +53,7 @@ fun SearchPage(
5253
val viewModel: SearchViewModel = viewModel(
5354
factory = SearchViewModelFactory(siteConfig),
5455
)
56+
val uiState by viewModel.stateFlow.collectAsState()
5557
var query by remember { mutableStateOf(viewModel.stateFlow.value.query) }
5658
val focusRequester = remember { androidx.compose.ui.focus.FocusRequester() }
5759
val focusManager = LocalFocusManager.current
@@ -69,7 +71,11 @@ fun SearchPage(
6971
Scaffold(topBar = {
7072
TopAppBar(title = { Text("Search") }, navigationIcon = {
7173
HBackIcon { goBack() }
72-
})
74+
},
75+
actions = {
76+
if (uiState.postItems.isNotEmpty())
77+
HPageProgress(uiState.postsPage, uiState.postsTotalPage)
78+
})
7379
}) { paddings ->
7480
Column(modifier = Modifier.padding(paddings)) {
7581
Row(

0 commit comments

Comments
 (0)