From c5714bb2062b9576d018fb1fd648e1ed15792945 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 19 Nov 2022 10:23:09 +0100 Subject: [PATCH 1/3] add pref to clear bookmarks --- .../com/github/libretube/constants/PreferenceKeys.kt | 1 + .../com/github/libretube/ui/adapters/PlaylistAdapter.kt | 5 +++-- .../github/libretube/ui/preferences/HistorySettings.kt | 8 ++++++++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/history_settings.xml | 9 +++++++++ 5 files changed, 22 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt index 0e2409470..cfe14370d 100644 --- a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt +++ b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt @@ -111,6 +111,7 @@ object PreferenceKeys { const val CLEAR_WATCH_POSITIONS = "clear_watch_positions" const val SHARE_WITH_TIME_CODE = "share_with_time_code" const val CONFIRM_UNSUBSCRIBE = "confirm_unsubscribing" + const val CLEAR_BOOKMARKS = "clear_bookmarks" /** * History diff --git a/app/src/main/java/com/github/libretube/ui/adapters/PlaylistAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/PlaylistAdapter.kt index cea41f117..e2eec73c9 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/PlaylistAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/PlaylistAdapter.kt @@ -10,6 +10,7 @@ import androidx.fragment.app.FragmentManager import androidx.recyclerview.widget.RecyclerView import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.obj.PlaylistId +import com.github.libretube.api.obj.StreamItem import com.github.libretube.databinding.PlaylistRowBinding import com.github.libretube.extensions.TAG import com.github.libretube.extensions.toID @@ -27,7 +28,7 @@ import retrofit2.HttpException import java.io.IOException class PlaylistAdapter( - private val videoFeed: MutableList, + private val videoFeed: MutableList, private val playlistId: String, private val isOwner: Boolean, private val childFragmentManager: FragmentManager @@ -37,7 +38,7 @@ class PlaylistAdapter( return videoFeed.size } - fun updateItems(newItems: List) { + fun updateItems(newItems: List) { val oldSize = videoFeed.size videoFeed.addAll(newItems) notifyItemRangeInserted(oldSize, videoFeed.size) diff --git a/app/src/main/java/com/github/libretube/ui/preferences/HistorySettings.kt b/app/src/main/java/com/github/libretube/ui/preferences/HistorySettings.kt index af4be7d11..45e4f811b 100644 --- a/app/src/main/java/com/github/libretube/ui/preferences/HistorySettings.kt +++ b/app/src/main/java/com/github/libretube/ui/preferences/HistorySettings.kt @@ -44,6 +44,14 @@ class HistorySettings : BasePreferenceFragment() { } true } + + val resetBookmarks = findPreference(PreferenceKeys.CLEAR_BOOKMARKS) + resetBookmarks?.setOnPreferenceClickListener { + showClearDialog(R.string.clear_bookmarks) { + Database.playlistBookmarkDao().deleteAll() + } + true + } } private fun showClearDialog(title: Int, action: () -> Unit) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 62d3231d3..6d34215f3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -383,6 +383,7 @@ What\'s trending now Bookmarks Bookmark + Clear bookmarks Download Service diff --git a/app/src/main/res/xml/history_settings.xml b/app/src/main/res/xml/history_settings.xml index 642b227d3..e20f559b8 100644 --- a/app/src/main/res/xml/history_settings.xml +++ b/app/src/main/res/xml/history_settings.xml @@ -59,4 +59,13 @@ + + + + + + \ No newline at end of file From 7211143398bc36d4a9277e6eaf8d084f3a52137b Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 19 Nov 2022 10:52:27 +0100 Subject: [PATCH 2/3] add a bookmarks fragment to the library --- .../ui/adapters/PlaylistBookmarkAdapter.kt | 17 ++++- .../ui/fragments/BookmarksFragment.kt | 41 +++++++++++ .../libretube/ui/fragments/HomeFragment.kt | 9 ++- .../libretube/ui/fragments/LibraryFragment.kt | 8 ++- .../main/res/layout/fragment_bookmarks.xml | 36 ++++++++++ app/src/main/res/layout/fragment_library.xml | 72 ++++++++----------- .../main/res/layout/playlist_bookmark_row.xml | 2 +- app/src/main/res/navigation/nav.xml | 5 ++ app/src/main/res/values/strings.xml | 1 + 9 files changed, 144 insertions(+), 47 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/ui/fragments/BookmarksFragment.kt create mode 100644 app/src/main/res/layout/fragment_bookmarks.xml 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 b93704cc8..16ada3c47 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 @@ -6,13 +6,15 @@ import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.RecyclerView import com.github.libretube.databinding.PlaylistBookmarkRowBinding import com.github.libretube.db.obj.PlaylistBookmark +import com.github.libretube.extensions.toDp import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet import com.github.libretube.ui.viewholders.PlaylistBookmarkViewHolder import com.github.libretube.util.ImageHelper import com.github.libretube.util.NavigationHelper class PlaylistBookmarkAdapter( - private val bookmarks: List + private val bookmarks: List, + 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) @@ -26,6 +28,12 @@ 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).toDp(root.context.resources).toInt() + root.layoutParams = params + } + ImageHelper.loadImage(bookmark.thumbnailUrl, thumbnail) playlistName.text = bookmark.playlistName uploaderName.text = bookmark.uploader @@ -46,4 +54,11 @@ class PlaylistBookmarkAdapter( } } } + + companion object { + enum class BookmarkMode { + HOME, + FRAGMENT + } + } } 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 new file mode 100644 index 000000000..23f0e9335 --- /dev/null +++ b/app/src/main/java/com/github/libretube/ui/fragments/BookmarksFragment.kt @@ -0,0 +1,41 @@ +package com.github.libretube.ui.fragments + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.recyclerview.widget.GridLayoutManager +import com.github.libretube.databinding.FragmentBookmarksBinding +import com.github.libretube.db.DatabaseHolder.Companion.Database +import com.github.libretube.extensions.awaitQuery +import com.github.libretube.ui.adapters.PlaylistBookmarkAdapter +import com.github.libretube.ui.base.BaseFragment + +class BookmarksFragment : BaseFragment() { + private lateinit var binding: FragmentBookmarksBinding + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + binding = FragmentBookmarksBinding.inflate(layoutInflater) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + val bookmarks = awaitQuery { + Database.playlistBookmarkDao().getAll() + } + + if (bookmarks.isEmpty()) return + + binding.bookmarksRV.layoutManager = GridLayoutManager(context, 2) + binding.bookmarksRV.adapter = PlaylistBookmarkAdapter(bookmarks) + + binding.bookmarksRV.visibility = View.VISIBLE + binding.emptyBookmarks.visibility = View.GONE + } +} diff --git a/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt index 29c5f75fc..ccb7e2d18 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt @@ -53,6 +53,10 @@ class HomeFragment : BaseFragment() { findNavController().navigate(R.id.libraryFragment) } + binding.bookmarksTV.setOnClickListener { + findNavController().navigate(R.id.bookmarksFragment) + } + binding.refresh.setOnRefreshListener { binding.refresh.isRefreshing = true lifecycleScope.launch(Dispatchers.IO) { @@ -123,7 +127,10 @@ class HomeFragment : BaseFragment() { runOnUiThread { makeVisible(binding.bookmarksTV, binding.bookmarksRV) binding.bookmarksRV.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) - binding.bookmarksRV.adapter = PlaylistBookmarkAdapter(bookmarkedPlaylists) + binding.bookmarksRV.adapter = PlaylistBookmarkAdapter( + bookmarkedPlaylists, + PlaylistBookmarkAdapter.Companion.BookmarkMode.HOME + ) } } } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt index b1f99ca54..33047a666 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt @@ -55,13 +55,17 @@ class LibraryFragment : BaseFragment() { val watchHistoryEnabled = PreferenceHelper.getBoolean(PreferenceKeys.WATCH_HISTORY_TOGGLE, true) if (!watchHistoryEnabled) { - binding.showWatchHistory.visibility = View.GONE + binding.watchHistory.visibility = View.GONE } else { - binding.showWatchHistory.setOnClickListener { + binding.watchHistory.setOnClickListener { findNavController().navigate(R.id.watchHistoryFragment) } } + binding.bookmarks.setOnClickListener { + findNavController().navigate(R.id.bookmarksFragment) + } + binding.downloads.setOnClickListener { findNavController().navigate(R.id.downloadsFragment) } diff --git a/app/src/main/res/layout/fragment_bookmarks.xml b/app/src/main/res/layout/fragment_bookmarks.xml new file mode 100644 index 000000000..f874db5dd --- /dev/null +++ b/app/src/main/res/layout/fragment_bookmarks.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_library.xml b/app/src/main/res/layout/fragment_library.xml index 98dbbcb49..e6d1574c8 100644 --- a/app/src/main/res/layout/fragment_library.xml +++ b/app/src/main/res/layout/fragment_library.xml @@ -47,61 +47,49 @@ android:layout_height="wrap_content" android:orientation="vertical"> - + android:drawablePadding="10dp" + android:paddingHorizontal="15dp" + android:paddingVertical="12dp" + android:text="@string/watch_history" + android:textSize="18sp" + android:textStyle="bold" + app:drawableStartCompat="@drawable/ic_time_outlined" /> - + - - - - - - - - - - - + android:drawablePadding="10dp" + android:paddingHorizontal="15dp" + android:paddingVertical="12dp" + android:text="@string/downloads" + android:textSize="18sp" + android:textStyle="bold" + app:drawableStartCompat="@drawable/ic_download" /> + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6d34215f3..660b59b0b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -384,6 +384,7 @@ Bookmarks Bookmark Clear bookmarks + No bookmarks yet! Download Service From 48564ed3c69c4cf6ce46e811dd6498cdbf383efe Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 19 Nov 2022 10:58:51 +0100 Subject: [PATCH 3/3] fix http 400 --- .../github/libretube/ui/fragments/HomeFragment.kt | 13 +++++++++---- app/src/main/res/xml/history_settings.xml | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt index ccb7e2d18..e40ccf5fc 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt @@ -1,6 +1,7 @@ package com.github.libretube.ui.fragments import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -60,16 +61,16 @@ class HomeFragment : BaseFragment() { binding.refresh.setOnRefreshListener { binding.refresh.isRefreshing = true lifecycleScope.launch(Dispatchers.IO) { - fetchHome(LocaleHelper.getTrendingRegion(requireContext())) + fetchHome() } } lifecycleScope.launch(Dispatchers.IO) { - fetchHome(LocaleHelper.getTrendingRegion(requireContext())) + fetchHome() } } - private suspend fun fetchHome(trendingRegion: String) { + private suspend fun fetchHome() { val token = PreferenceHelper.getToken() runOrError { val feed = SubscriptionHelper.getFeed().withMaxSize(20) @@ -86,7 +87,9 @@ class HomeFragment : BaseFragment() { } runOrError { - val trending = RetrofitInstance.api.getTrending(trendingRegion).withMaxSize(10) + val trending = RetrofitInstance.api.getTrending( + LocaleHelper.getTrendingRegion(requireContext()) + ).withMaxSize(10) if (trending.isEmpty()) return@runOrError runOnUiThread { makeVisible(binding.trendingRV, binding.trendingTV) @@ -100,6 +103,7 @@ class HomeFragment : BaseFragment() { } runOrError { + if (token == "") return@runOrError val playlists = RetrofitInstance.authApi.getUserPlaylists(token).withMaxSize(20) if (playlists.isEmpty()) return@runOrError runOnUiThread { @@ -141,6 +145,7 @@ class HomeFragment : BaseFragment() { action.invoke() } catch (e: Exception) { e.localizedMessage?.let { context?.toastFromMainThread(it) } + Log.e("fetching home tab", e.toString()) } } } diff --git a/app/src/main/res/xml/history_settings.xml b/app/src/main/res/xml/history_settings.xml index e20f559b8..9baac7e82 100644 --- a/app/src/main/res/xml/history_settings.xml +++ b/app/src/main/res/xml/history_settings.xml @@ -61,7 +61,7 @@ -