move the comments into a bottom sheet

This commit is contained in:
Bnyro 2022-11-26 17:53:40 +01:00
parent 9c4928b862
commit 35602aaaca
3 changed files with 92 additions and 89 deletions

View File

@ -27,7 +27,6 @@ import androidx.annotation.RequiresApi
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.core.net.toUri
import androidx.core.os.bundleOf
import androidx.core.view.isEmpty
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
@ -66,7 +65,6 @@ import com.github.libretube.services.BackgroundMode
import com.github.libretube.services.DownloadService
import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.adapters.ChaptersAdapter
import com.github.libretube.ui.adapters.CommentsAdapter
import com.github.libretube.ui.adapters.VideosAdapter
import com.github.libretube.ui.base.BaseFragment
import com.github.libretube.ui.dialogs.AddToPlaylistDialog
@ -78,6 +76,7 @@ import com.github.libretube.ui.extensions.setupSubscriptionButton
import com.github.libretube.ui.interfaces.OnlinePlayerOptions
import com.github.libretube.ui.models.PlayerViewModel
import com.github.libretube.ui.sheets.BaseBottomSheet
import com.github.libretube.ui.sheets.CommentsSheet
import com.github.libretube.ui.sheets.PlayingQueueSheet
import com.github.libretube.util.BackgroundHelper
import com.github.libretube.util.DashHelper
@ -137,13 +136,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
private var eId: Int = 0
private var transitioning = false
/**
* for the comments
*/
private var commentsAdapter: CommentsAdapter? = null
private var nextPage: String? = null
private var isLoading = true
/**
* for the player
*/
@ -331,7 +323,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
}
binding.commentsToggle.setOnClickListener {
toggleComments()
CommentsSheet(videoId!!).show(childFragmentManager)
}
playerBinding.queueToggle.visibility = View.VISIBLE
@ -383,19 +375,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
)
}
binding.playerScrollView.viewTreeObserver
.addOnScrollChangedListener {
if (binding.playerScrollView.getChildAt(0).bottom
== (binding.playerScrollView.height + binding.playerScrollView.scrollY) &&
nextPage != null
) {
fetchNextComments()
}
}
binding.commentsRecView.layoutManager = LinearLayoutManager(view?.context)
binding.commentsRecView.setItemViewCacheSize(20)
binding.relatedRecView.layoutManager = VideosAdapter.getLayout(requireContext())
binding.alternativeTrendingRec.layoutManager = LinearLayoutManager(
@ -474,17 +453,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
}
}
private fun toggleComments() {
if (binding.commentsRecView.isVisible) {
binding.commentsRecView.visibility = View.GONE
binding.relatedRecView.visibility = View.VISIBLE
} else {
binding.commentsRecView.visibility = View.VISIBLE
binding.relatedRecView.visibility = View.GONE
if (binding.commentsRecView.isEmpty()) fetchComments()
}
}
override fun onStart() {
super.onStart()
@ -649,9 +617,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
initializePlayerNotification()
if (PlayerHelper.sponsorBlockEnabled) fetchSponsorBlockSegments()
// show comments if related streams are disabled or the alternative related streams layout is chosen
if (!PlayerHelper.relatedStreamsEnabled || PlayerHelper.alternativeVideoLayout) toggleComments()
// add the video to the watch history
if (PlayerHelper.watchHistoryEnabled) {
DatabaseHelper.addToWatchHistory(videoId!!, streams)
@ -740,8 +705,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
if (nextVideoId != null) {
videoId = nextVideoId
// forces the comments to reload for the new video
binding.commentsRecView.adapter = null
// TODO Reset comments
playVideo()
}
}
@ -1265,47 +1229,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
nowPlayingNotification.updatePlayerNotification(videoId!!, streams)
}
private fun fetchComments() {
lifecycleScope.launchWhenCreated {
val commentsResponse = try {
RetrofitInstance.api.getComments(videoId!!)
} catch (e: IOException) {
println(e)
Log.e(TAG(), "IOException, you might not have internet connection")
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response")
return@launchWhenCreated
}
commentsAdapter = CommentsAdapter(videoId!!, commentsResponse.comments)
binding.commentsRecView.adapter = commentsAdapter
nextPage = commentsResponse.nextpage
isLoading = false
}
}
private fun fetchNextComments() {
lifecycleScope.launchWhenCreated {
if (!isLoading) {
isLoading = true
val response = try {
RetrofitInstance.api.getCommentsNextPage(videoId!!, nextPage!!)
} catch (e: IOException) {
println(e)
Log.e(TAG(), "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response," + e.response())
return@launchWhenCreated
}
nextPage = response.nextpage
commentsAdapter?.updateItems(response.comments)
isLoading = false
}
}
}
/**
* Use the sensor mode if auto fullscreen is enabled
*/

View File

@ -0,0 +1,89 @@
package com.github.libretube.ui.sheets
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.databinding.BottomSheetBinding
import com.github.libretube.extensions.TAG
import com.github.libretube.ui.adapters.CommentsAdapter
import retrofit2.HttpException
import java.io.IOException
class CommentsSheet(
private val videoId: String
) : ExpandedBottomSheet() {
private lateinit var binding: BottomSheetBinding
private var commentsAdapter: CommentsAdapter? = null
private var nextPage: String? = null
private var isLoading = true
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = BottomSheetBinding.inflate(layoutInflater)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.optionsRecycler.layoutManager = LinearLayoutManager(requireContext())
binding.optionsRecycler.setItemViewCacheSize(20)
binding.optionsRecycler.viewTreeObserver
.addOnScrollChangedListener {
if (!binding.optionsRecycler.canScrollVertically(1)) {
fetchNextComments()
}
}
fetchComments()
}
private fun fetchComments() {
lifecycleScope.launchWhenCreated {
val commentsResponse = try {
RetrofitInstance.api.getComments(videoId)
} catch (e: IOException) {
println(e)
Log.e(TAG(), "IOException, you might not have internet connection")
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response")
return@launchWhenCreated
}
commentsAdapter = CommentsAdapter(videoId, commentsResponse.comments)
binding.optionsRecycler.adapter = commentsAdapter
nextPage = commentsResponse.nextpage
isLoading = false
}
}
private fun fetchNextComments() {
lifecycleScope.launchWhenCreated {
if (!isLoading) {
isLoading = true
val response = try {
RetrofitInstance.api.getCommentsNextPage(videoId, nextPage!!)
} catch (e: IOException) {
println(e)
Log.e(TAG(), "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response," + e.response())
return@launchWhenCreated
}
nextPage = response.nextpage
commentsAdapter?.updateItems(response.comments)
isLoading = false
}
}
}
}

View File

@ -346,15 +346,6 @@
android:animateLayoutChanges="true"
android:descendantFocusability="blocksDescendants">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/comments_recView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:nestedScrollingEnabled="false"
android:visibility="gone" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/related_rec_view"
android:layout_width="match_parent"