|
| 1 | +package org.schabi.newpipe.ui.components.video.comment |
| 2 | + |
| 3 | +import android.content.res.Configuration |
| 4 | +import androidx.compose.foundation.layout.padding |
| 5 | +import androidx.compose.foundation.lazy.LazyColumn |
| 6 | +import androidx.compose.foundation.lazy.rememberLazyListState |
| 7 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 8 | +import androidx.compose.material3.HorizontalDivider |
| 9 | +import androidx.compose.material3.MaterialTheme |
| 10 | +import androidx.compose.material3.ModalBottomSheet |
| 11 | +import androidx.compose.material3.Surface |
| 12 | +import androidx.compose.runtime.Composable |
| 13 | +import androidx.compose.runtime.remember |
| 14 | +import androidx.compose.runtime.rememberCoroutineScope |
| 15 | +import androidx.compose.ui.Modifier |
| 16 | +import androidx.compose.ui.graphics.Color |
| 17 | +import androidx.compose.ui.input.nestedscroll.nestedScroll |
| 18 | +import androidx.compose.ui.platform.rememberNestedScrollInteropConnection |
| 19 | +import androidx.compose.ui.tooling.preview.Preview |
| 20 | +import androidx.compose.ui.unit.dp |
| 21 | +import androidx.paging.LoadState |
| 22 | +import androidx.paging.Pager |
| 23 | +import androidx.paging.PagingConfig |
| 24 | +import androidx.paging.PagingData |
| 25 | +import androidx.paging.cachedIn |
| 26 | +import androidx.paging.compose.collectAsLazyPagingItems |
| 27 | +import kotlinx.coroutines.flow.Flow |
| 28 | +import kotlinx.coroutines.flow.flowOf |
| 29 | +import my.nanihadesuka.compose.LazyColumnScrollbar |
| 30 | +import my.nanihadesuka.compose.ScrollbarSettings |
| 31 | +import org.schabi.newpipe.R |
| 32 | +import org.schabi.newpipe.extractor.comments.CommentsInfoItem |
| 33 | +import org.schabi.newpipe.extractor.stream.Description |
| 34 | +import org.schabi.newpipe.paging.CommentsSource |
| 35 | +import org.schabi.newpipe.ui.components.common.LoadingIndicator |
| 36 | +import org.schabi.newpipe.ui.components.common.NoItemsMessage |
| 37 | +import org.schabi.newpipe.ui.theme.AppTheme |
| 38 | +import org.schabi.newpipe.ui.theme.md_theme_dark_primary |
| 39 | + |
| 40 | +@Composable |
| 41 | +fun CommentRepliesDialog( |
| 42 | + parentComment: CommentsInfoItem, |
| 43 | + onDismissRequest: () -> Unit, |
| 44 | +) { |
| 45 | + val coroutineScope = rememberCoroutineScope() |
| 46 | + val commentsFlow = remember { |
| 47 | + Pager(PagingConfig(pageSize = 20, enablePlaceholders = false)) { |
| 48 | + CommentsSource(parentComment.serviceId, parentComment.url, parentComment.replies) |
| 49 | + }.flow |
| 50 | + .cachedIn(coroutineScope) |
| 51 | + } |
| 52 | + |
| 53 | + CommentRepliesDialog(parentComment, commentsFlow, onDismissRequest) |
| 54 | +} |
| 55 | + |
| 56 | +@OptIn(ExperimentalMaterial3Api::class) |
| 57 | +@Composable |
| 58 | +private fun CommentRepliesDialog( |
| 59 | + parentComment: CommentsInfoItem, |
| 60 | + commentsFlow: Flow<PagingData<CommentsInfoItem>>, |
| 61 | + onDismissRequest: () -> Unit, |
| 62 | +) { |
| 63 | + val comments = commentsFlow.collectAsLazyPagingItems() |
| 64 | + val nestedScrollInterop = rememberNestedScrollInteropConnection() |
| 65 | + val state = rememberLazyListState() |
| 66 | + |
| 67 | + ModalBottomSheet(onDismissRequest = onDismissRequest) { |
| 68 | + Surface(color = MaterialTheme.colorScheme.background) { |
| 69 | + LazyColumnScrollbar( |
| 70 | + state = state, |
| 71 | + settings = ScrollbarSettings.Default.copy( |
| 72 | + thumbSelectedColor = md_theme_dark_primary, |
| 73 | + thumbUnselectedColor = Color.Red |
| 74 | + ) |
| 75 | + ) { |
| 76 | + LazyColumn( |
| 77 | + modifier = Modifier.nestedScroll(nestedScrollInterop), |
| 78 | + state = state |
| 79 | + ) { |
| 80 | + item { |
| 81 | + CommentRepliesHeader(comment = parentComment) |
| 82 | + HorizontalDivider(thickness = 1.dp) |
| 83 | + } |
| 84 | + |
| 85 | + if (comments.itemCount == 0) { |
| 86 | + item { |
| 87 | + val refresh = comments.loadState.refresh |
| 88 | + if (refresh is LoadState.Loading) { |
| 89 | + LoadingIndicator(modifier = Modifier.padding(top = 8.dp)) |
| 90 | + } else { |
| 91 | + val message = if (refresh is LoadState.Error) { |
| 92 | + R.string.error_unable_to_load_comments |
| 93 | + } else { |
| 94 | + R.string.no_comments |
| 95 | + } |
| 96 | + NoItemsMessage(message) |
| 97 | + } |
| 98 | + } |
| 99 | + } else { |
| 100 | + items(comments.itemCount) { |
| 101 | + Comment(comment = comments[it]!!) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +@Preview(name = "Light mode", uiMode = Configuration.UI_MODE_NIGHT_NO) |
| 111 | +@Preview(name = "Dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES) |
| 112 | +@Composable |
| 113 | +private fun CommentRepliesDialogPreview() { |
| 114 | + val comment = CommentsInfoItem( |
| 115 | + commentText = Description("Hello world!", Description.PLAIN_TEXT), |
| 116 | + uploaderName = "Test", |
| 117 | + likeCount = 100, |
| 118 | + isPinned = true, |
| 119 | + isHeartedByUploader = true |
| 120 | + ) |
| 121 | + val replies = (1..10).map { |
| 122 | + CommentsInfoItem( |
| 123 | + commentText = Description("Reply $it", Description.PLAIN_TEXT), |
| 124 | + uploaderName = "Test" |
| 125 | + ) |
| 126 | + } |
| 127 | + val flow = flowOf(PagingData.from(replies)) |
| 128 | + |
| 129 | + AppTheme { |
| 130 | + CommentRepliesDialog(comment, flow, onDismissRequest = {}) |
| 131 | + } |
| 132 | +} |
0 commit comments