Skip to content

Commit

Permalink
Add comment view model
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Jul 3, 2024
1 parent 8a97e45 commit 31f1fcf
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 44 deletions.
6 changes: 2 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -113,7 +113,7 @@ private fun CommentSectionPreview(
@PreviewParameter(CommentDataProvider::class) pagingData: PagingData<CommentsInfoItem>
) {
AppTheme {
CommentSection(commentsData = flowOf(pagingData))
CommentSection(commentsFlow = flowOf(pagingData))
}
}

Expand All @@ -137,6 +137,6 @@ private fun CommentRepliesPreview() {
val flow = flowOf(PagingData.from(replies))

AppTheme {
CommentSection(parentComment = comment, commentsData = flow)
CommentSection(parentComment = comment, commentsFlow = flow)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 31f1fcf

Please sign in to comment.