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()
}
root.setOnClickListener {
NavigationHelper.navigatePlaylist(root.context, playlist.id)
NavigationHelper.navigatePlaylist(root.context, playlist.id, true)
}
root.setOnLongClickListener {

View File

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

View File

@ -9,6 +9,7 @@ import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.Globals
import com.github.libretube.R
import com.github.libretube.adapters.PlaylistsAdapter
@ -44,7 +45,7 @@ class LibraryFragment : BaseFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.playlistRecView.layoutManager = LinearLayoutManager(view.context)
binding.playlistRecView.layoutManager = LinearLayoutManager(requireContext())
token = PreferenceHelper.getToken()
// hide watch history button of history disabled
@ -59,8 +60,7 @@ class LibraryFragment : BaseFragment() {
}
if (token != "") {
binding.boogh.visibility = View.GONE
binding.textLike.visibility = View.GONE
binding.loginOrRegister.visibility = View.GONE
fetchPlaylists()
binding.playlistRefresh.isEnabled = true
binding.playlistRefresh.setOnRefreshListener {
@ -103,26 +103,32 @@ class LibraryFragment : BaseFragment() {
binding.playlistRefresh.isRefreshing = false
}
if (response.isNotEmpty()) {
runOnUiThread {
binding.boogh.visibility = View.GONE
binding.textLike.visibility = View.GONE
}
binding.loginOrRegister.visibility = View.GONE
val playlistsAdapter = PlaylistsAdapter(
response.toMutableList(),
childFragmentManager,
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
} else {
runOnUiThread {
binding.boogh.apply {
visibility = View.VISIBLE
setImageResource(R.drawable.ic_list)
}
binding.textLike.apply {
visibility = View.VISIBLE
text = getString(R.string.emptyList)
}
binding.loginOrRegister.visibility = View.VISIBLE
binding.boogh.setImageResource(R.drawable.ic_list)
binding.textLike.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.dialogs.PlaylistOptionsDialog
import com.github.libretube.extensions.BaseFragment
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.toID
import retrofit2.HttpException
@ -26,7 +25,7 @@ class PlaylistFragment : BaseFragment() {
private var playlistId: String? = null
private var isOwner: Boolean = false
var nextPage: String? = null
private var nextPage: String? = null
private var playlistAdapter: PlaylistAdapter? = null
private var isLoading = true
@ -34,6 +33,7 @@ class PlaylistFragment : BaseFragment() {
super.onCreate(savedInstanceState)
arguments?.let {
playlistId = it.getString("playlist_id")
isOwner = it.getBoolean("isOwner")
}
}
@ -61,7 +61,8 @@ class PlaylistFragment : BaseFragment() {
lifecycleScope.launchWhenCreated {
val response = try {
// 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) {
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
@ -75,14 +76,10 @@ class PlaylistFragment : BaseFragment() {
runOnUiThread {
binding.playlistProgress.visibility = View.GONE
binding.playlistName.text = response.name
binding.playlistUploader.text = response.uploader
binding.playlistTotVideos.text =
binding.uploader.text = response.uploader
binding.videoCount.text =
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
binding.optionsMenu.setOnClickListener {
val optionsDialog =
@ -100,6 +97,16 @@ class PlaylistFragment : BaseFragment() {
requireActivity(),
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.playlistScrollview.viewTreeObserver
.addOnScrollChangedListener {
@ -111,8 +118,6 @@ class PlaylistFragment : BaseFragment() {
isLoading = true
fetchNextPage()
}
} else {
// scroll view is not at bottom
}
}
@ -155,7 +160,10 @@ class PlaylistFragment : BaseFragment() {
lifecycleScope.launchWhenCreated {
val response = try {
// load locally stored playlists with the auth api
RetrofitInstance.authApi.getPlaylistNextPage(
if (isOwner) RetrofitInstance.authApi.getPlaylistNextPage(
playlistId!!,
nextPage!!
) else RetrofitInstance.api.getPlaylistNextPage(
playlistId!!,
nextPage!!
)

View File

@ -10,7 +10,10 @@ import com.github.libretube.activities.MainActivity
import com.github.libretube.fragments.PlayerFragment
object NavigationHelper {
fun navigateChannel(context: Context, channelId: String?) {
fun navigateChannel(
context: Context,
channelId: String?
) {
if (channelId != null) {
val activity = context as MainActivity
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) {
val bundle = Bundle()
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) {
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)
}
}

View File

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

View File

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