From 2e9f5d48e687b3a5c6c36be7b84fd03457f4f58b Mon Sep 17 00:00:00 2001 From: Luna712 <142361265+Luna712@users.noreply.github.com> Date: Fri, 13 Oct 2023 14:48:28 -0600 Subject: [PATCH] Add support to TV for favorites --- .../syncproviders/providers/LocalList.kt | 19 +++++---- .../ui/result/ResultFragmentTv.kt | 42 ++++++++++++++++++- .../main/res/layout/fragment_result_tv.xml | 15 ++++++- app/src/main/res/values/strings.xml | 2 + 4 files changed, 66 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/LocalList.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/LocalList.kt index 89f97c00..71bb2633 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/LocalList.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/LocalList.kt @@ -76,10 +76,11 @@ class LocalList : SyncAPI { val baseMap = WatchType.values().filter { it != WatchType.NONE }.associate { // None is not something to display it.stringRes to emptyList() - } + if (!isTv) { + } + mapOf( + R.string.favorites_list_name to emptyList() + ) + if (!isTv) { mapOf( R.string.subscription_list_name to emptyList(), - R.string.favorites_list_name to emptyList() ) } else { emptyMap() @@ -91,19 +92,19 @@ class LocalList : SyncAPI { } } + val favoritesMap = mapOf(R.string.favorites_list_name to getAllFavorites().mapNotNull { + it.toLibraryItem() + }) + // Don't show subscriptions or favorites on TV val result = if (isTv) { - baseMap + watchStatusMap + baseMap + watchStatusMap + favoritesMap } else { - val subscriptionList = mapOf(R.string.subscription_list_name to getAllSubscriptions().mapNotNull { + val subscriptionsMap = mapOf(R.string.subscription_list_name to getAllSubscriptions().mapNotNull { it.toLibraryItem() }) - val favoritesList = mapOf(R.string.favorites_list_name to getAllFavorites().mapNotNull { - it.toLibraryItem() - }) - - baseMap + watchStatusMap + subscriptionList + favoritesList + baseMap + watchStatusMap + subscriptionsMap + favoritesMap } result diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragmentTv.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragmentTv.kt index 5e4869cc..13734b67 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragmentTv.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragmentTv.kt @@ -8,6 +8,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.view.animation.DecelerateInterpolator +import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.core.view.isGone import androidx.core.view.isVisible @@ -17,6 +18,7 @@ import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.RecyclerView import com.google.android.material.bottomsheet.BottomSheetDialog import com.lagradost.cloudstream3.APIHolder.updateHasTrailers +import com.lagradost.cloudstream3.CommonActivity import com.lagradost.cloudstream3.DubStatus import com.lagradost.cloudstream3.LoadResponse import com.lagradost.cloudstream3.MainActivity.Companion.afterPluginsLoadedEvent @@ -265,6 +267,7 @@ class ResultFragmentTv : Fragment() { resultEpisodesShow.onFocusChangeListener = rightListener resultDescription.onFocusChangeListener = leftListener resultBookmarkButton.onFocusChangeListener = leftListener + resultFavoriteButton.onFocusChangeListener = leftListener resultEpisodesShow.setOnClickListener { // toggle, to make it more touch accessable just in case someone thinks that a // tv layout is better but is using a touch device @@ -283,7 +286,8 @@ class ResultFragmentTv : Fragment() { resultPlaySeries, resultResumeSeries, resultPlayTrailer, - resultBookmarkButton + resultBookmarkButton, + resultFavoriteButton ) for (requestView in views) { if (!requestView.isVisible) continue @@ -424,6 +428,7 @@ class ResultFragmentTv : Fragment() { val aboveCast = listOf( binding?.resultEpisodesShow, binding?.resultBookmarkButton, + binding?.resultFavoriteButton, ).firstOrNull { it?.isVisible == true } @@ -532,6 +537,41 @@ class ResultFragmentTv : Fragment() { } } + observeNullable(viewModel.favoriteStatus) { isFavorite -> + binding?.resultFavoriteButton?.apply { + isVisible = isFavorite != null + if (isFavorite == null) return@observeNullable + + val drawable = if (isFavorite) { + R.drawable.ic_baseline_favorite_24 + } else { + R.drawable.ic_baseline_favorite_border_24 + } + + val text = if (isFavorite) { + R.string.action_remove_from_favorites + } else { + R.string.action_add_to_favorites + } + + setIconResource(drawable) + setText(text) + setOnClickListener { + val isFavorite = viewModel.toggleFavoriteStatus() ?: return@setOnClickListener + + val message = if (isFavorite) { + R.string.favorite_added + } else { + R.string.favorite_removed + } + + val name = (viewModel.page.value as? Resource.Success)?.value?.title + ?: txt(R.string.no_data).asStringNull(context) ?: "" + CommonActivity.showToast(txt(message, name), Toast.LENGTH_SHORT) + } + } + } + observeNullable(viewModel.movie) { data -> binding?.apply { resultPlayMovie.isVisible = data is Resource.Success diff --git a/app/src/main/res/layout/fragment_result_tv.xml b/app/src/main/res/layout/fragment_result_tv.xml index feaf6fbc..bcf3dd84 100644 --- a/app/src/main/res/layout/fragment_result_tv.xml +++ b/app/src/main/res/layout/fragment_result_tv.xml @@ -310,19 +310,30 @@ https://developer.android.com/design/ui/tv/samples/jet-fit style="@style/ResultButtonTV" android:nextFocusRight="@id/result_description" android:nextFocusUp="@id/result_play_trailer" - android:nextFocusDown="@id/result_episodes_show" + android:nextFocusDown="@id/result_favorite_button" android:text="@string/type_none" android:visibility="visible" app:icon="@drawable/ic_baseline_bookmark_24" /> + + Favorites %s added to favorites %s removed from favorites + Add to favorites + Remove from favorites