Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: チャンネル一覧画面をページネーション可能に変更 (#1896) #1911

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@ import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridItemSpan
import androidx.compose.foundation.lazy.staggeredgrid.rememberLazyStaggeredGridState
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.google.accompanist.swiperefresh.SwipeRefresh
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import net.pantasystem.milktea.common.PageableState
import net.pantasystem.milktea.common.StateContent
import net.pantasystem.milktea.common.ui.isScrollToTheEnd
import net.pantasystem.milktea.data.infrastructure.channel.ChannelListType
import net.pantasystem.milktea.model.channel.Channel


@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ChannelListStateScreen(
Expand All @@ -27,7 +35,7 @@ fun ChannelListStateScreen(
navigateToDetailView: (Channel.Id) -> Unit = {}
) {


val scrollController = rememberLazyStaggeredGridState()
val pagingState = uiState.getByType(listType)

val swipeRefreshState = rememberSwipeRefreshState(isRefreshing = false)
Expand All @@ -36,6 +44,16 @@ fun ChannelListStateScreen(
viewModel.clearAndLoad(listType)
}

LaunchedEffect(null) {
snapshotFlow {
scrollController.isScrollToTheEnd()
}.distinctUntilChanged().onEach {
if(it) {
viewModel.loadOld(listType)
}
}.launchIn(this)
}

SwipeRefresh(
state = swipeRefreshState,
onRefresh = {
Expand All @@ -47,6 +65,7 @@ fun ChannelListStateScreen(
LazyVerticalStaggeredGrid(
columns = StaggeredGridCells.Adaptive(350.dp),
modifier = Modifier.fillMaxSize(),
state = scrollController
) {
when (val content = pagingState.content) {
is StateContent.Exist -> {
Expand All @@ -73,6 +92,14 @@ fun ChannelListStateScreen(
}
)
}
item(span = StaggeredGridItemSpan.FullLine){
Row(
Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center
) {
CircularProgressIndicator()
}
}
}
is StateContent.NotExist -> {
item {
Expand Down Expand Up @@ -113,4 +140,4 @@ fun ReachedElement() {
) {
CircularProgressIndicator()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ class ChannelViewModel @Inject constructor(
}
}

fun loadOld(type: ChannelListType) {
viewModelScope.launch {
val model = when (type) {
ChannelListType.OWNED -> ownedChannelPagingModel
ChannelListType.FOLLOWED -> followedChannelPagingModel
ChannelListType.FEATURED -> featuredChannelPagingModel
}

PreviousPagingController(
model,
model,
model,
model,
).loadPrevious()
}
}

fun follow(channelId: Channel.Id) {
viewModelScope.launch {
runCancellableCatching {
Expand Down