1
0
mirror of https://github.com/TeamNewPipe/NewPipe.git synced 2024-11-25 12:32:31 +01:00

Add comment view model

This commit is contained in:
Isira Seneviratne 2024-07-02 18:19:17 +05:30
parent 02c5f2607a
commit c5d94a5b60
5 changed files with 41 additions and 44 deletions

View File

@ -294,11 +294,9 @@ dependencies {
implementation 'androidx.activity:activity-compose' implementation 'androidx.activity:activity-compose'
implementation 'androidx.compose.ui:ui-tooling-preview' implementation 'androidx.compose.ui:ui-tooling-preview'
implementation 'androidx.compose.ui:ui-text:1.7.0-beta04' // Needed for parsing HTML to AnnotatedString implementation 'androidx.compose.ui:ui-text:1.7.0-beta04' // Needed for parsing HTML to AnnotatedString
implementation 'com.github.nanihadesuka:LazyColumnScrollbar:2.1.0' implementation 'androidx.lifecycle:lifecycle-viewmodel-compose'
// Paging
implementation 'androidx.paging:paging-rxjava3:3.3.0'
implementation 'androidx.paging:paging-compose:3.3.0' implementation 'androidx.paging:paging-compose:3.3.0'
implementation 'com.github.nanihadesuka:LazyColumnScrollbar:2.1.0'
/** Debugging **/ /** Debugging **/
// Memory leak detection // Memory leak detection

View File

@ -144,14 +144,14 @@ fun Comment(comment: CommentsInfoItem) {
if (showReplies) { if (showReplies) {
ModalBottomSheet(onDismissRequest = { showReplies = false }) { ModalBottomSheet(onDismissRequest = { showReplies = false }) {
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
val flow = remember(coroutineScope) { val flow = remember {
Pager(PagingConfig(pageSize = 20, enablePlaceholders = false)) { Pager(PagingConfig(pageSize = 20, enablePlaceholders = false)) {
CommentsSource(comment.serviceId, comment.url, comment.replies) CommentsSource(comment.serviceId, comment.url, comment.replies)
}.flow }.flow
.cachedIn(coroutineScope) .cachedIn(coroutineScope)
} }
CommentSection(parentComment = comment, commentsData = flow) CommentSection(parentComment = comment, commentsFlow = flow)
} }
} }
} }

View File

@ -36,9 +36,9 @@ import org.schabi.newpipe.paging.CommentsDisabledException
@Composable @Composable
fun CommentSection( fun CommentSection(
parentComment: CommentsInfoItem? = null, parentComment: CommentsInfoItem? = null,
commentsData: Flow<PagingData<CommentsInfoItem>> commentsFlow: Flow<PagingData<CommentsInfoItem>>
) { ) {
val comments = commentsData.collectAsLazyPagingItems() val comments = commentsFlow.collectAsLazyPagingItems()
val itemCount by remember { derivedStateOf { comments.itemCount } } val itemCount by remember { derivedStateOf { comments.itemCount } }
Surface(color = MaterialTheme.colorScheme.background) { Surface(color = MaterialTheme.colorScheme.background) {
@ -113,7 +113,7 @@ private fun CommentSectionPreview(
@PreviewParameter(CommentDataProvider::class) pagingData: PagingData<CommentsInfoItem> @PreviewParameter(CommentDataProvider::class) pagingData: PagingData<CommentsInfoItem>
) { ) {
AppTheme { AppTheme {
CommentSection(commentsData = flowOf(pagingData)) CommentSection(commentsFlow = flowOf(pagingData))
} }
} }
@ -137,6 +137,6 @@ private fun CommentRepliesPreview() {
val flow = flowOf(PagingData.from(replies)) val flow = flowOf(PagingData.from(replies))
AppTheme { AppTheme {
CommentSection(parentComment = comment, commentsData = flow) CommentSection(parentComment = comment, commentsFlow = flow)
} }
} }

View File

@ -2,60 +2,37 @@ package org.schabi.newpipe.fragments.list.comments
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup 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.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.paging.Pager import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
import org.schabi.newpipe.compose.comment.CommentSection import org.schabi.newpipe.compose.comment.CommentSection
import org.schabi.newpipe.compose.theme.AppTheme import org.schabi.newpipe.compose.theme.AppTheme
import org.schabi.newpipe.paging.CommentsSource import org.schabi.newpipe.util.KEY_SERVICE_ID
import org.schabi.newpipe.util.NO_SERVICE_ID import org.schabi.newpipe.util.KEY_URL
import org.schabi.newpipe.viewmodels.CommentsViewModel
class CommentsFragment : Fragment() { class CommentsFragment : Fragment() {
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View { ) = ComposeView(requireContext()).apply {
val arguments = requireArguments() setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
val serviceId = arguments.getInt(SERVICE_ID, NO_SERVICE_ID) setContent {
val url = arguments.getString(URL) val viewModel = viewModel<CommentsViewModel>()
AppTheme {
return ComposeView(requireContext()).apply { CommentSection(commentsFlow = viewModel.comments)
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)
}
} }
} }
} }
companion object { companion object {
private const val SERVICE_ID = "serviceId"
private const val URL = "url"
@JvmStatic @JvmStatic
fun getInstance(serviceId: Int, url: String?) = CommentsFragment().apply { fun getInstance(serviceId: Int, url: String?) = CommentsFragment().apply {
arguments = bundleOf( arguments = bundleOf(KEY_SERVICE_ID to serviceId, KEY_URL to url)
SERVICE_ID to serviceId,
URL to url
)
} }
} }
} }

View File

@ -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<String>(KEY_URL)
val comments = Pager(PagingConfig(pageSize = 20, enablePlaceholders = false)) {
CommentsSource(serviceId, url, null)
}.flow
.cachedIn(viewModelScope)
}