playlist bug fixes

This commit is contained in:
Bnyro 2022-08-08 15:22:08 +02:00
parent ed5dd02abf
commit 67132a92d6
7 changed files with 64 additions and 37 deletions

View File

@ -67,7 +67,7 @@ class PlaylistsAdapter(
builder.show() builder.show()
} }
root.setOnClickListener { root.setOnClickListener {
NavigationHelper.navigatePlaylist(root.context, playlist.id) NavigationHelper.navigatePlaylist(root.context, playlist.id, true)
} }
root.setOnLongClickListener { root.setOnLongClickListener {

View File

@ -171,7 +171,7 @@ class SearchAdapter(
root.context.getString(R.string.videoCount, item.videos.toString()) root.context.getString(R.string.videoCount, item.videos.toString())
} }
root.setOnClickListener { root.setOnClickListener {
NavigationHelper.navigatePlaylist(root.context, item.url) NavigationHelper.navigatePlaylist(root.context, item.url, false)
} }
root.setOnLongClickListener { root.setOnLongClickListener {
val playlistId = item.url!!.toID() val playlistId = item.url!!.toID()

View File

@ -9,6 +9,7 @@ import android.widget.Toast
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.Globals import com.github.libretube.Globals
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.adapters.PlaylistsAdapter import com.github.libretube.adapters.PlaylistsAdapter
@ -44,7 +45,7 @@ class LibraryFragment : BaseFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
binding.playlistRecView.layoutManager = LinearLayoutManager(view.context) binding.playlistRecView.layoutManager = LinearLayoutManager(requireContext())
token = PreferenceHelper.getToken() token = PreferenceHelper.getToken()
// hide watch history button of history disabled // hide watch history button of history disabled
@ -59,8 +60,7 @@ class LibraryFragment : BaseFragment() {
} }
if (token != "") { if (token != "") {
binding.boogh.visibility = View.GONE binding.loginOrRegister.visibility = View.GONE
binding.textLike.visibility = View.GONE
fetchPlaylists() fetchPlaylists()
binding.playlistRefresh.isEnabled = true binding.playlistRefresh.isEnabled = true
binding.playlistRefresh.setOnRefreshListener { binding.playlistRefresh.setOnRefreshListener {
@ -103,26 +103,32 @@ class LibraryFragment : BaseFragment() {
binding.playlistRefresh.isRefreshing = false binding.playlistRefresh.isRefreshing = false
} }
if (response.isNotEmpty()) { if (response.isNotEmpty()) {
runOnUiThread { binding.loginOrRegister.visibility = View.GONE
binding.boogh.visibility = View.GONE
binding.textLike.visibility = View.GONE
}
val playlistsAdapter = PlaylistsAdapter( val playlistsAdapter = PlaylistsAdapter(
response.toMutableList(), response.toMutableList(),
childFragmentManager, childFragmentManager,
requireActivity() requireActivity()
) )
// listen for playlists to become deleted
playlistsAdapter.registerAdapterDataObserver(object :
RecyclerView.AdapterDataObserver() {
override fun onChanged() {
Log.e(TAG, playlistsAdapter.itemCount.toString())
if (playlistsAdapter.itemCount == 0) {
binding.loginOrRegister.visibility = View.VISIBLE
}
super.onChanged()
}
})
binding.playlistRecView.adapter = playlistsAdapter binding.playlistRecView.adapter = playlistsAdapter
} else { } else {
runOnUiThread { runOnUiThread {
binding.boogh.apply { binding.loginOrRegister.visibility = View.VISIBLE
visibility = View.VISIBLE binding.boogh.setImageResource(R.drawable.ic_list)
setImageResource(R.drawable.ic_list) binding.textLike.text = getString(R.string.emptyList)
}
binding.textLike.apply {
visibility = View.VISIBLE
text = getString(R.string.emptyList)
}
} }
} }
} }

View File

@ -14,7 +14,6 @@ import com.github.libretube.adapters.PlaylistAdapter
import com.github.libretube.databinding.FragmentPlaylistBinding import com.github.libretube.databinding.FragmentPlaylistBinding
import com.github.libretube.dialogs.PlaylistOptionsDialog import com.github.libretube.dialogs.PlaylistOptionsDialog
import com.github.libretube.extensions.BaseFragment import com.github.libretube.extensions.BaseFragment
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.toID import com.github.libretube.util.toID
import retrofit2.HttpException import retrofit2.HttpException
@ -26,7 +25,7 @@ class PlaylistFragment : BaseFragment() {
private var playlistId: String? = null private var playlistId: String? = null
private var isOwner: Boolean = false private var isOwner: Boolean = false
var nextPage: String? = null private var nextPage: String? = null
private var playlistAdapter: PlaylistAdapter? = null private var playlistAdapter: PlaylistAdapter? = null
private var isLoading = true private var isLoading = true
@ -34,6 +33,7 @@ class PlaylistFragment : BaseFragment() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
arguments?.let { arguments?.let {
playlistId = it.getString("playlist_id") playlistId = it.getString("playlist_id")
isOwner = it.getBoolean("isOwner")
} }
} }
@ -61,7 +61,8 @@ class PlaylistFragment : BaseFragment() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
// load locally stored playlists with the auth api // load locally stored playlists with the auth api
RetrofitInstance.authApi.getPlaylist(playlistId!!) if (isOwner) RetrofitInstance.authApi.getPlaylist(playlistId!!)
else RetrofitInstance.api.getPlaylist(playlistId!!)
} catch (e: IOException) { } catch (e: IOException) {
println(e) println(e)
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")
@ -75,14 +76,10 @@ class PlaylistFragment : BaseFragment() {
runOnUiThread { runOnUiThread {
binding.playlistProgress.visibility = View.GONE binding.playlistProgress.visibility = View.GONE
binding.playlistName.text = response.name binding.playlistName.text = response.name
binding.playlistUploader.text = response.uploader binding.uploader.text = response.uploader
binding.playlistTotVideos.text = binding.videoCount.text =
getString(R.string.videoCount, response.videos.toString()) getString(R.string.videoCount, response.videos.toString())
val user = PreferenceHelper.getUsername()
// check whether the user owns the playlist
isOwner = response.uploaderUrl == null && response.uploader.equals(user, true)
// show playlist options // show playlist options
binding.optionsMenu.setOnClickListener { binding.optionsMenu.setOnClickListener {
val optionsDialog = val optionsDialog =
@ -100,6 +97,16 @@ class PlaylistFragment : BaseFragment() {
requireActivity(), requireActivity(),
childFragmentManager childFragmentManager
) )
// listen for playlist items to become deleted
playlistAdapter!!.registerAdapterDataObserver(object :
RecyclerView.AdapterDataObserver() {
override fun onChanged() {
binding.videoCount.text =
getString(R.string.videoCount, playlistAdapter!!.itemCount.toString())
}
})
binding.playlistRecView.adapter = playlistAdapter binding.playlistRecView.adapter = playlistAdapter
binding.playlistScrollview.viewTreeObserver binding.playlistScrollview.viewTreeObserver
.addOnScrollChangedListener { .addOnScrollChangedListener {
@ -111,8 +118,6 @@ class PlaylistFragment : BaseFragment() {
isLoading = true isLoading = true
fetchNextPage() fetchNextPage()
} }
} else {
// scroll view is not at bottom
} }
} }
@ -155,7 +160,10 @@ class PlaylistFragment : BaseFragment() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
// load locally stored playlists with the auth api // load locally stored playlists with the auth api
RetrofitInstance.authApi.getPlaylistNextPage( if (isOwner) RetrofitInstance.authApi.getPlaylistNextPage(
playlistId!!,
nextPage!!
) else RetrofitInstance.api.getPlaylistNextPage(
playlistId!!, playlistId!!,
nextPage!! nextPage!!
) )

View File

@ -10,7 +10,10 @@ import com.github.libretube.activities.MainActivity
import com.github.libretube.fragments.PlayerFragment import com.github.libretube.fragments.PlayerFragment
object NavigationHelper { object NavigationHelper {
fun navigateChannel(context: Context, channelId: String?) { fun navigateChannel(
context: Context,
channelId: String?
) {
if (channelId != null) { if (channelId != null) {
val activity = context as MainActivity val activity = context as MainActivity
val bundle = bundleOf("channel_id" to channelId) val bundle = bundleOf("channel_id" to channelId)
@ -28,7 +31,11 @@ object NavigationHelper {
} }
} }
fun navigateVideo(context: Context, videoId: String?, playlistId: String? = null) { fun navigateVideo(
context: Context,
videoId: String?,
playlistId: String? = null
) {
if (videoId != null) { if (videoId != null) {
val bundle = Bundle() val bundle = Bundle()
bundle.putString("videoId", videoId.toID()) bundle.putString("videoId", videoId.toID())
@ -45,10 +52,16 @@ object NavigationHelper {
} }
} }
fun navigatePlaylist(context: Context, playlistId: String?) { fun navigatePlaylist(
context: Context,
playlistId: String?,
isOwner: Boolean
) {
if (playlistId != null) { if (playlistId != null) {
val activity = context as MainActivity val activity = context as MainActivity
val bundle = bundleOf("playlist_id" to playlistId) val bundle = Bundle()
bundle.putString("playlist_id", playlistId)
bundle.putBoolean("isOwner", isOwner)
activity.navController.navigate(R.id.playlistFragment, bundle) activity.navController.navigate(R.id.playlistFragment, bundle)
} }
} }

View File

@ -7,7 +7,7 @@
tools:context=".fragments.LibraryFragment"> tools:context=".fragments.LibraryFragment">
<RelativeLayout <RelativeLayout
android:id="@+id/loginOrRegister2" android:id="@+id/loginOrRegister"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">

View File

@ -20,7 +20,7 @@
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">
<LinearLayout <LinearLayout
@ -48,14 +48,14 @@
</LinearLayout> </LinearLayout>
<TextView <TextView
android:id="@+id/playlist_uploader" android:id="@+id/uploader"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:padding="8dp" android:padding="8dp"
android:textStyle="bold" /> android:textStyle="bold" />
<TextView <TextView
android:id="@+id/playlist_totVideos" android:id="@+id/video_count"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:padding="8dp" /> android:padding="8dp" />