From c5d94a5b60b16b9026b957a0a4e1732ab2e37219 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Tue, 2 Jul 2024 18:19:17 +0530 Subject: [PATCH] Add comment view model --- app/build.gradle | 6 +-- .../schabi/newpipe/compose/comment/Comment.kt | 4 +- .../newpipe/compose/comment/CommentSection.kt | 8 ++-- .../list/comments/CommentsFragment.kt | 45 +++++-------------- .../newpipe/viewmodels/CommentsViewModel.kt | 22 +++++++++ 5 files changed, 41 insertions(+), 44 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/viewmodels/CommentsViewModel.kt diff --git a/app/build.gradle b/app/build.gradle index 5929582b0..ed85f281a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -294,11 +294,9 @@ dependencies { implementation 'androidx.activity:activity-compose' implementation 'androidx.compose.ui:ui-tooling-preview' implementation 'androidx.compose.ui:ui-text:1.7.0-beta04' // Needed for parsing HTML to AnnotatedString - implementation 'com.github.nanihadesuka:LazyColumnScrollbar:2.1.0' - - // Paging - implementation 'androidx.paging:paging-rxjava3:3.3.0' + implementation 'androidx.lifecycle:lifecycle-viewmodel-compose' implementation 'androidx.paging:paging-compose:3.3.0' + implementation 'com.github.nanihadesuka:LazyColumnScrollbar:2.1.0' /** Debugging **/ // Memory leak detection diff --git a/app/src/main/java/org/schabi/newpipe/compose/comment/Comment.kt b/app/src/main/java/org/schabi/newpipe/compose/comment/Comment.kt index 97dce39a5..b372edff9 100644 --- a/app/src/main/java/org/schabi/newpipe/compose/comment/Comment.kt +++ b/app/src/main/java/org/schabi/newpipe/compose/comment/Comment.kt @@ -144,14 +144,14 @@ fun Comment(comment: CommentsInfoItem) { if (showReplies) { ModalBottomSheet(onDismissRequest = { showReplies = false }) { val coroutineScope = rememberCoroutineScope() - val flow = remember(coroutineScope) { + val flow = remember { Pager(PagingConfig(pageSize = 20, enablePlaceholders = false)) { CommentsSource(comment.serviceId, comment.url, comment.replies) }.flow .cachedIn(coroutineScope) } - CommentSection(parentComment = comment, commentsData = flow) + CommentSection(parentComment = comment, commentsFlow = flow) } } } diff --git a/app/src/main/java/org/schabi/newpipe/compose/comment/CommentSection.kt b/app/src/main/java/org/schabi/newpipe/compose/comment/CommentSection.kt index df2c470a9..8d1ef90ab 100644 --- a/app/src/main/java/org/schabi/newpipe/compose/comment/CommentSection.kt +++ b/app/src/main/java/org/schabi/newpipe/compose/comment/CommentSection.kt @@ -36,9 +36,9 @@ import org.schabi.newpipe.paging.CommentsDisabledException @Composable fun CommentSection( parentComment: CommentsInfoItem? = null, - commentsData: Flow> + commentsFlow: Flow> ) { - val comments = commentsData.collectAsLazyPagingItems() + val comments = commentsFlow.collectAsLazyPagingItems() val itemCount by remember { derivedStateOf { comments.itemCount } } Surface(color = MaterialTheme.colorScheme.background) { @@ -113,7 +113,7 @@ private fun CommentSectionPreview( @PreviewParameter(CommentDataProvider::class) pagingData: PagingData ) { AppTheme { - CommentSection(commentsData = flowOf(pagingData)) + CommentSection(commentsFlow = flowOf(pagingData)) } } @@ -137,6 +137,6 @@ private fun CommentRepliesPreview() { val flow = flowOf(PagingData.from(replies)) AppTheme { - CommentSection(parentComment = comment, commentsData = flow) + CommentSection(parentComment = comment, commentsFlow = flow) } } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsFragment.kt b/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsFragment.kt index 5933ea809..22cff9a53 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsFragment.kt +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsFragment.kt @@ -2,60 +2,37 @@ package org.schabi.newpipe.fragments.list.comments import android.os.Bundle import android.view.LayoutInflater -import android.view.View import android.view.ViewGroup -import androidx.compose.runtime.remember -import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.platform.ComposeView import androidx.compose.ui.platform.ViewCompositionStrategy import androidx.core.os.bundleOf import androidx.fragment.app.Fragment -import androidx.paging.Pager -import androidx.paging.PagingConfig -import androidx.paging.cachedIn +import androidx.lifecycle.viewmodel.compose.viewModel import org.schabi.newpipe.compose.comment.CommentSection import org.schabi.newpipe.compose.theme.AppTheme -import org.schabi.newpipe.paging.CommentsSource -import org.schabi.newpipe.util.NO_SERVICE_ID +import org.schabi.newpipe.util.KEY_SERVICE_ID +import org.schabi.newpipe.util.KEY_URL +import org.schabi.newpipe.viewmodels.CommentsViewModel class CommentsFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View { - val arguments = requireArguments() - val serviceId = arguments.getInt(SERVICE_ID, NO_SERVICE_ID) - val url = arguments.getString(URL) - - return ComposeView(requireContext()).apply { - setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) - setContent { - val coroutineScope = rememberCoroutineScope() - val flow = remember(coroutineScope) { - Pager(PagingConfig(pageSize = 20, enablePlaceholders = false)) { - CommentsSource(serviceId, url, null) - }.flow - .cachedIn(coroutineScope) - } - - AppTheme { - CommentSection(commentsData = flow) - } + ) = ComposeView(requireContext()).apply { + setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) + setContent { + val viewModel = viewModel() + AppTheme { + CommentSection(commentsFlow = viewModel.comments) } } } companion object { - private const val SERVICE_ID = "serviceId" - private const val URL = "url" - @JvmStatic fun getInstance(serviceId: Int, url: String?) = CommentsFragment().apply { - arguments = bundleOf( - SERVICE_ID to serviceId, - URL to url - ) + arguments = bundleOf(KEY_SERVICE_ID to serviceId, KEY_URL to url) } } } diff --git a/app/src/main/java/org/schabi/newpipe/viewmodels/CommentsViewModel.kt b/app/src/main/java/org/schabi/newpipe/viewmodels/CommentsViewModel.kt new file mode 100644 index 000000000..62babb186 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/viewmodels/CommentsViewModel.kt @@ -0,0 +1,22 @@ +package org.schabi.newpipe.viewmodels + +import androidx.lifecycle.SavedStateHandle +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import androidx.paging.Pager +import androidx.paging.PagingConfig +import androidx.paging.cachedIn +import org.schabi.newpipe.paging.CommentsSource +import org.schabi.newpipe.util.KEY_SERVICE_ID +import org.schabi.newpipe.util.KEY_URL +import org.schabi.newpipe.util.NO_SERVICE_ID + +class CommentsViewModel(savedStateHandle: SavedStateHandle) : ViewModel() { + private val serviceId = savedStateHandle[KEY_SERVICE_ID] ?: NO_SERVICE_ID + private val url = savedStateHandle.get(KEY_URL) + + val comments = Pager(PagingConfig(pageSize = 20, enablePlaceholders = false)) { + CommentsSource(serviceId, url, null) + }.flow + .cachedIn(viewModelScope) +}