From f0e429436f92855708cb4e22ac121c59c8732eff Mon Sep 17 00:00:00 2001
From: Luna712 <142361265+Luna712@users.noreply.github.com>
Date: Sat, 28 Oct 2023 17:20:04 -0600
Subject: [PATCH] Use TV results layout on emulator (#710)
* Use TV results layout on emulator
* Add subscription support to emulator
* Update
---
.../ui/result/ResultFragmentTv.kt | 45 ++++++++++++++++++-
.../lagradost/cloudstream3/utils/AppUtils.kt | 2 +-
.../main/res/layout/fragment_result_tv.xml | 15 ++++++-
app/src/main/res/values/strings.xml | 2 +
4 files changed, 60 insertions(+), 4 deletions(-)
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 c13854e071..396ec86310 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
@@ -28,6 +28,7 @@ import com.lagradost.cloudstream3.databinding.FragmentResultTvBinding
import com.lagradost.cloudstream3.mvvm.Resource
import com.lagradost.cloudstream3.mvvm.observe
import com.lagradost.cloudstream3.mvvm.observeNullable
+import com.lagradost.cloudstream3.services.SubscriptionWorkManager
import com.lagradost.cloudstream3.ui.WatchType
import com.lagradost.cloudstream3.ui.download.DownloadButtonSetup
import com.lagradost.cloudstream3.ui.player.ExtractorLinkGenerator
@@ -37,6 +38,7 @@ import com.lagradost.cloudstream3.ui.result.ResultFragment.updateUIEvent
import com.lagradost.cloudstream3.ui.search.SEARCH_ACTION_FOCUSED
import com.lagradost.cloudstream3.ui.search.SearchAdapter
import com.lagradost.cloudstream3.ui.search.SearchHelper
+import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isEmulatorSettings
import com.lagradost.cloudstream3.utils.AppUtils.getNameFull
import com.lagradost.cloudstream3.utils.AppUtils.html
import com.lagradost.cloudstream3.utils.AppUtils.isRtl
@@ -287,7 +289,8 @@ class ResultFragmentTv : Fragment() {
resultResumeSeries,
resultPlayTrailer,
resultBookmarkButton,
- resultFavoriteButton
+ resultFavoriteButton,
+ resultSubscribeButton
)
for (requestView in views) {
if (!requestView.isVisible) continue
@@ -429,6 +432,7 @@ class ResultFragmentTv : Fragment() {
binding?.resultEpisodesShow,
binding?.resultBookmarkButton,
binding?.resultFavoriteButton,
+ binding?.resultSubscribeButton,
).firstOrNull {
it?.isVisible == true
}
@@ -574,6 +578,45 @@ class ResultFragmentTv : Fragment() {
}
}
+ observeNullable(viewModel.subscribeStatus) { isSubscribed ->
+ binding?.resultSubscribeButton?.apply {
+ isVisible = isSubscribed != null && context.isEmulatorSettings()
+ if (isSubscribed == null) return@observeNullable
+
+ val drawable = if (isSubscribed) {
+ R.drawable.ic_baseline_notifications_active_24
+ } else {
+ R.drawable.baseline_notifications_none_24
+ }
+
+ val text = if (isSubscribed) {
+ R.string.action_unsubscribe
+ } else {
+ R.string.action_subscribe
+ }
+
+ setIconResource(drawable)
+ setText(text)
+ setOnClickListener {
+ viewModel.toggleSubscriptionStatus(context) { newStatus: Boolean? ->
+ if (newStatus == null) return@toggleSubscriptionStatus
+
+ val message = if (newStatus) {
+ // Kinda icky to have this here, but it works.
+ SubscriptionWorkManager.enqueuePeriodicWork(context)
+ R.string.subscription_new
+ } else {
+ R.string.subscription_deleted
+ }
+
+ 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/java/com/lagradost/cloudstream3/utils/AppUtils.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/AppUtils.kt
index 48917889e4..1be966b665 100644
--- a/app/src/main/java/com/lagradost/cloudstream3/utils/AppUtils.kt
+++ b/app/src/main/java/com/lagradost/cloudstream3/utils/AppUtils.kt
@@ -583,7 +583,7 @@ object AppUtils {
//private val viewModel: ResultViewModel by activityViewModels()
private fun getResultsId(): Int {
- return if (isTrueTvSettings()) {
+ return if (isTvSettings()) {
R.id.global_to_navigation_results_tv
} else {
R.id.global_to_navigation_results_phone
diff --git a/app/src/main/res/layout/fragment_result_tv.xml b/app/src/main/res/layout/fragment_result_tv.xml
index 51add4cbb7..9311d54992 100644
--- a/app/src/main/res/layout/fragment_result_tv.xml
+++ b/app/src/main/res/layout/fragment_result_tv.xml
@@ -325,19 +325,30 @@ https://developer.android.com/design/ui/tv/samples/jet-fit
style="@style/ResultButtonTV"
android:nextFocusRight="@id/result_description"
android:nextFocusUp="@id/result_bookmark_button"
- android:nextFocusDown="@id/result_episodes_show"
+ android:nextFocusDown="@id/result_subscribe_button"
android:text="@string/action_add_to_favorites"
android:visibility="visible"
app:icon="@drawable/ic_baseline_favorite_border_24" />
+
+
Subscribed to %s
Unsubscribed from %s
Episode %d released!
+ Subscribe
+ Unsubscribe
Profile %d
Wi-Fi
Mobile data