Merge pull request #2510 from Bnyro/master

Use playlists layout for bookmarks
This commit is contained in:
Bnyro 2022-12-26 16:18:32 +01:00 committed by GitHub
commit 3d93986aed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 78 additions and 20 deletions

View File

@ -1,13 +1,17 @@
package com.github.libretube.ui.adapters
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.databinding.PlaylistBookmarkRowBinding
import com.github.libretube.databinding.PlaylistRowBinding
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.obj.PlaylistBookmark
import com.github.libretube.enums.PlaylistType
import com.github.libretube.extensions.toPixel
import com.github.libretube.extensions.query
import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet
import com.github.libretube.ui.viewholders.PlaylistBookmarkViewHolder
import com.github.libretube.util.ImageHelper
@ -18,12 +22,15 @@ class PlaylistBookmarkAdapter(
private val bookmarkMode: BookmarkMode = BookmarkMode.FRAGMENT
) : RecyclerView.Adapter<PlaylistBookmarkViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlaylistBookmarkViewHolder {
val binding = PlaylistBookmarkRowBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
return PlaylistBookmarkViewHolder(binding)
val layoutInflater = LayoutInflater.from(parent.context)
return when (bookmarkMode) {
BookmarkMode.HOME -> PlaylistBookmarkViewHolder(
PlaylistBookmarkRowBinding.inflate(layoutInflater, parent, false)
)
BookmarkMode.FRAGMENT -> PlaylistBookmarkViewHolder(
PlaylistRowBinding.inflate(layoutInflater, parent, false)
)
}
}
override fun getItemCount(): Int {
@ -32,13 +39,7 @@ class PlaylistBookmarkAdapter(
override fun onBindViewHolder(holder: PlaylistBookmarkViewHolder, position: Int) {
val bookmark = bookmarks[position]
holder.binding.apply {
if (bookmarkMode == BookmarkMode.HOME) {
val params = root.layoutParams
params.width = (210).toPixel().toInt()
root.layoutParams = params
}
holder.playlistBookmarkBinding?.apply {
ImageHelper.loadImage(bookmark.thumbnailUrl, thumbnail)
playlistName.text = bookmark.playlistName
uploaderName.text = bookmark.uploader
@ -62,6 +63,52 @@ class PlaylistBookmarkAdapter(
true
}
}
holder.playlistBinding?.apply {
var isBookmarked = true
ImageHelper.loadImage(bookmark.thumbnailUrl, playlistThumbnail)
playlistTitle.text = bookmark.playlistName
playlistDescription.text = bookmark.uploader
deletePlaylist.setImageResource(R.drawable.ic_bookmark)
deletePlaylist.setOnClickListener {
isBookmarked = !isBookmarked
deletePlaylist.setImageResource(
if (isBookmarked) R.drawable.ic_bookmark else R.drawable.ic_bookmark_outlined
)
query {
if (!isBookmarked) {
DatabaseHolder.Database.playlistBookmarkDao()
.deleteById(bookmark.playlistId)
} else {
DatabaseHolder.Database.playlistBookmarkDao()
.insertAll(bookmark)
}
}
}
deletePlaylist.visibility = View.VISIBLE
root.setOnClickListener {
NavigationHelper.navigatePlaylist(
root.context,
bookmark.playlistId,
PlaylistType.PUBLIC
)
}
root.setOnLongClickListener {
PlaylistOptionsBottomSheet(
playlistId = bookmark.playlistId,
playlistName = bookmark.playlistName ?: "",
playlistType = PlaylistType.PUBLIC
).show(
(root.context as AppCompatActivity).supportFragmentManager
)
true
}
}
}
companion object {

View File

@ -5,6 +5,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.databinding.FragmentBookmarksBinding
import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.extensions.awaitQuery
@ -32,7 +33,7 @@ class BookmarksFragment : BaseFragment() {
if (bookmarks.isEmpty()) return
binding.bookmarksRV.layoutManager = GridLayoutManager(context, 2)
binding.bookmarksRV.layoutManager = LinearLayoutManager(context)
binding.bookmarksRV.adapter = PlaylistBookmarkAdapter(bookmarks)
binding.bookmarksRV.visibility = View.VISIBLE

View File

@ -2,7 +2,17 @@ package com.github.libretube.ui.viewholders
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.databinding.PlaylistBookmarkRowBinding
import com.github.libretube.databinding.PlaylistRowBinding
class PlaylistBookmarkViewHolder(
val binding: PlaylistBookmarkRowBinding
) : RecyclerView.ViewHolder(binding.root)
class PlaylistBookmarkViewHolder : RecyclerView.ViewHolder {
var playlistBookmarkBinding: PlaylistBookmarkRowBinding? = null
var playlistBinding: PlaylistRowBinding? = null
constructor(binding: PlaylistBookmarkRowBinding) : super(binding.root) {
playlistBookmarkBinding = binding
}
constructor(binding: PlaylistRowBinding) : super(binding.root) {
playlistBinding = binding
}
}

View File

@ -2,7 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/rounded_ripple"

View File

@ -93,7 +93,7 @@
android:id="@+id/delete_playlist"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:padding="8dp"
android:src="@drawable/ic_delete"
android:visibility="gone"