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.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

View File

@ -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)
}
}
}

View File

@ -36,9 +36,9 @@ import org.schabi.newpipe.paging.CommentsDisabledException
@Composable
fun CommentSection(
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 } }
Surface(color = MaterialTheme.colorScheme.background) {
@ -113,7 +113,7 @@ private fun CommentSectionPreview(
@PreviewParameter(CommentDataProvider::class) pagingData: PagingData<CommentsInfoItem>
) {
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)
}
}

View File

@ -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<CommentsViewModel>()
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)
}
}
}

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)
}