diff --git a/app/src/main/java/com/github/libretube/ui/fragments/CommentsMainFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/CommentsMainFragment.kt index 33e9d9945..1e282cbf3 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/CommentsMainFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/CommentsMainFragment.kt @@ -4,7 +4,6 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import androidx.core.view.isGone import androidx.core.view.isVisible import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels @@ -72,23 +71,26 @@ class CommentsMainFragment : Fragment() { } binding.commentsRV.adapter = commentsAdapter - if (viewModel.commentsPage.value?.comments.orEmpty().isEmpty()) { - binding.progress.isVisible = true + if (viewModel.commentsPage.value?.comments.isNullOrEmpty()) { viewModel.fetchComments() } else { binding.commentsRV.scrollToPosition(viewModel.currentCommentsPosition) } + viewModel.isLoading.observe(viewLifecycleOwner) { + _binding?.progress?.isVisible = it == true + } + // listen for new comments to be loaded viewModel.commentsPage.observe(viewLifecycleOwner) { + if (it == null) return@observe val viewBinding = _binding ?: return@observe - if (it == null) return@observe - viewBinding.progress.isGone = true if (it.disabled) { viewBinding.errorTV.isVisible = true return@observe } + commentsSheet?.updateFragmentInfo( false, "${getString(R.string.comments)} (${it.commentCount.formatShort()})" diff --git a/app/src/main/java/com/github/libretube/ui/fragments/CommentsRepliesFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/CommentsRepliesFragment.kt index 341198936..a4f721f01 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/CommentsRepliesFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/CommentsRepliesFragment.kt @@ -82,13 +82,11 @@ class CommentsRepliesFragment : Fragment() { ::repliesPage.isInitialized && repliesPage.nextpage != null ) { - fetchReplies(videoId, repliesPage.nextpage!!) { - repliesAdapter.updateItems(repliesPage.comments) - } + fetchReplies(videoId, repliesPage.nextpage!!) } } - loadInitialReplies(videoId, comment.repliesPage.orEmpty(), repliesAdapter) + loadInitialReplies(videoId, comment.repliesPage.orEmpty()) } override fun onDestroyView() { @@ -98,34 +96,32 @@ class CommentsRepliesFragment : Fragment() { private fun loadInitialReplies( videoId: String, - nextPage: String, - repliesAdapter: CommentsAdapter + nextPage: String ) { _binding?.progress?.isVisible = true - fetchReplies(videoId, nextPage) { - repliesAdapter.updateItems(it.comments) - _binding?.progress?.isGone = true - } + fetchReplies(videoId, nextPage) } - private fun fetchReplies( - videoId: String, - nextPage: String, - onFinished: (CommentsPage) -> Unit - ) { - lifecycleScope.launch(Dispatchers.IO) { + private fun fetchReplies(videoId: String, nextPage: String) { + _binding?.progress?.isVisible = true + + lifecycleScope.launch { if (isLoading) return@launch isLoading = true repliesPage = try { - RetrofitInstance.api.getCommentsNextPage(videoId, nextPage) + withContext(Dispatchers.IO) { + RetrofitInstance.api.getCommentsNextPage(videoId, nextPage) + } } catch (e: Exception) { Log.e(TAG(), "IOException, you might not have internet connection") return@launch + } finally { + _binding?.progress?.isGone = true } repliesPage.comments = repliesPage.comments.filterNonEmptyComments() withContext(Dispatchers.Main) { - onFinished.invoke(repliesPage) + repliesAdapter.updateItems(repliesPage.comments) } isLoading = false } diff --git a/app/src/main/java/com/github/libretube/ui/models/CommentsViewModel.kt b/app/src/main/java/com/github/libretube/ui/models/CommentsViewModel.kt index 7f69ea86a..c488dab0a 100644 --- a/app/src/main/java/com/github/libretube/ui/models/CommentsViewModel.kt +++ b/app/src/main/java/com/github/libretube/ui/models/CommentsViewModel.kt @@ -10,6 +10,7 @@ import com.github.libretube.extensions.TAG import com.github.libretube.ui.extensions.filterNonEmptyComments import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext class CommentsViewModel : ViewModel() { val commentsPage = MutableLiveData() @@ -17,11 +18,12 @@ class CommentsViewModel : ViewModel() { var videoId: String? = null var channelAvatar: String? = null + var handleLink: ((url: String) -> Unit)? = null + private var nextPage: String? = null - private var isLoading = false + var isLoading = MutableLiveData() var currentCommentsPosition = 0 var commentsSheetDismiss: (() -> Unit)? = null - var handleLink: ((url: String) -> Unit)? = null fun setCommentSheetExpand(value: Boolean?) { if (commentSheetExpand.value != value) { @@ -30,31 +32,43 @@ class CommentsViewModel : ViewModel() { } fun fetchComments() { - videoId ?: return - viewModelScope.launch(Dispatchers.IO) { - isLoading = true + val videoId = videoId ?: return + + isLoading.value = true + + viewModelScope.launch { val response = try { - RetrofitInstance.api.getComments(videoId!!) + withContext(Dispatchers.IO) { + RetrofitInstance.api.getComments(videoId) + } } catch (e: Exception) { Log.e(TAG(), e.toString()) return@launch + } finally { + isLoading.value = false } + nextPage = response.nextpage response.comments = response.comments.filterNonEmptyComments() commentsPage.postValue(response) - isLoading = false } } fun fetchNextComments() { - if (isLoading || nextPage == null || videoId == null) return - viewModelScope.launch(Dispatchers.IO) { - isLoading = true + if (isLoading.value == true || nextPage == null || videoId == null) return + + isLoading.value = true + + viewModelScope.launch { val response = try { - RetrofitInstance.api.getCommentsNextPage(videoId!!, nextPage!!) + withContext(Dispatchers.IO) { + RetrofitInstance.api.getCommentsNextPage(videoId!!, nextPage!!) + } } catch (e: Exception) { Log.e(TAG(), e.toString()) return@launch + } finally { + isLoading.value = false } val updatedPage = commentsPage.value?.apply { @@ -65,12 +79,11 @@ class CommentsViewModel : ViewModel() { nextPage = response.nextpage commentsPage.postValue(updatedPage) - isLoading = false } } fun reset() { - isLoading = false + isLoading.value = false nextPage = null commentsPage.value = null videoId = null diff --git a/app/src/main/res/layout/fragment_comments.xml b/app/src/main/res/layout/fragment_comments.xml index f5236bf0e..3978c14c6 100644 --- a/app/src/main/res/layout/fragment_comments.xml +++ b/app/src/main/res/layout/fragment_comments.xml @@ -1,7 +1,8 @@ - + android:layout_height="match_parent" + android:orientation="vertical"> - \ No newline at end of file + \ No newline at end of file