From 3434974efc0a3f6f33516854e460acb6f9641d89 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 26 Dec 2022 16:18:09 +0100 Subject: [PATCH] Use playlists layout for bookmarks --- .../ui/adapters/PlaylistBookmarkAdapter.kt | 75 +++++++++++++++---- .../ui/fragments/BookmarksFragment.kt | 3 +- .../viewholders/PlaylistBookmarkViewHolder.kt | 16 +++- .../main/res/layout/playlist_bookmark_row.xml | 2 +- app/src/main/res/layout/playlist_row.xml | 2 +- 5 files changed, 78 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/adapters/PlaylistBookmarkAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/PlaylistBookmarkAdapter.kt index 8f83d3564..27a832e28 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/PlaylistBookmarkAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/PlaylistBookmarkAdapter.kt @@ -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() { 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 { diff --git a/app/src/main/java/com/github/libretube/ui/fragments/BookmarksFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/BookmarksFragment.kt index 23f0e9335..47bfa84a5 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/BookmarksFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/BookmarksFragment.kt @@ -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 diff --git a/app/src/main/java/com/github/libretube/ui/viewholders/PlaylistBookmarkViewHolder.kt b/app/src/main/java/com/github/libretube/ui/viewholders/PlaylistBookmarkViewHolder.kt index fc768ea6a..6d6f55db9 100644 --- a/app/src/main/java/com/github/libretube/ui/viewholders/PlaylistBookmarkViewHolder.kt +++ b/app/src/main/java/com/github/libretube/ui/viewholders/PlaylistBookmarkViewHolder.kt @@ -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 + } +} diff --git a/app/src/main/res/layout/playlist_bookmark_row.xml b/app/src/main/res/layout/playlist_bookmark_row.xml index 5651a83d7..ae81b74d1 100644 --- a/app/src/main/res/layout/playlist_bookmark_row.xml +++ b/app/src/main/res/layout/playlist_bookmark_row.xml @@ -2,7 +2,7 @@