From a8f5dcc9430e9b1fcb4af1034036cd71da0c340e Mon Sep 17 00:00:00 2001 From: LagradOst Date: Thu, 3 Feb 2022 22:23:00 +0100 Subject: [PATCH] android tv popup (#390) --- .../cloudstream3/ui/home/HomeFragment.kt | 147 +++++++++++++----- .../utils/SingleSelectionHelper.kt | 70 ++++++++- app/src/main/res/layout/options_popup_tv.xml | 44 ++++++ .../sort_bottom_single_choice_color.xml | 18 +++ app/src/main/res/values/strings.xml | 1 + 5 files changed, 238 insertions(+), 42 deletions(-) create mode 100644 app/src/main/res/layout/options_popup_tv.xml create mode 100644 app/src/main/res/layout/sort_bottom_single_choice_color.xml diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt index 68082bae..01d4e3ab 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt @@ -48,11 +48,11 @@ import com.lagradost.cloudstream3.utils.DataStoreHelper.removeLastWatched import com.lagradost.cloudstream3.utils.DataStoreHelper.setResultWatchState import com.lagradost.cloudstream3.utils.Event import com.lagradost.cloudstream3.utils.HOMEPAGE_API +import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showOptionSelectStringRes import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbarView import com.lagradost.cloudstream3.utils.UIHelper.getSpanCount -import com.lagradost.cloudstream3.utils.UIHelper.popupMenuNoIcons import com.lagradost.cloudstream3.utils.UIHelper.popupMenuNoIconsAndNoStringRes import com.lagradost.cloudstream3.utils.UIHelper.setImage import com.lagradost.cloudstream3.utils.UIHelper.setImageBlur @@ -597,19 +597,62 @@ class HomeFragment : Fragment() { nextFocusDown = home_bookmarked_child_recyclerview?.nextFocusDownId ) { callback -> if (callback.action == SEARCH_ACTION_SHOW_METADATA) { - val id = callback.card.id - if (id != null) { - callback.view.popupMenuNoIcons( - listOf( - Pair( - 0, - R.string.action_remove_from_bookmarks + activity?.showOptionSelectStringRes( + callback.view, + callback.card.posterUrl, + listOf( + R.string.action_open_watching, + R.string.action_remove_from_bookmarks, + ), + listOf( + R.string.action_open_play, + R.string.action_open_watching, + R.string.action_remove_from_bookmarks + ) + ) { (isTv, actionId) -> + fun play() { + activity.loadSearchResult(callback.card, START_ACTION_RESUME_LATEST) + reloadStored() + } + + fun remove() { + setResultWatchState(callback.card.id, WatchType.NONE.internalId) + reloadStored() + } + + fun info() { + handleSearchClickCallback( + activity, + SearchClickCallback( + SEARCH_ACTION_LOAD, + callback.view, + -1, + callback.card ) ) - ) { - if (itemId == 0) { - setResultWatchState(id, WatchType.NONE.internalId) - reloadStored() + reloadStored() + } + + if (isTv) { + when (actionId) { + 0 -> { + play() + } + 1 -> { + info() + } + 2 -> { + remove() + } + } + } else { + when (actionId) { + 0 -> { + info() + } + 1 -> { + remove() + } } } } @@ -624,34 +667,68 @@ class HomeFragment : Fragment() { nextFocusDown = home_watch_child_recyclerview?.nextFocusDownId ) { callback -> if (callback.action == SEARCH_ACTION_SHOW_METADATA) { - val id = callback.card.id - if (id != null) { - callback.view.popupMenuNoIcons( - listOf( - Pair(1, R.string.action_open_watching), - Pair(0, R.string.action_remove_watching) - ) - ) { - if (itemId == 1) { - handleSearchClickCallback( - activity, - SearchClickCallback( - SEARCH_ACTION_LOAD, - callback.view, - -1, - callback.card - ) - ) + activity?.showOptionSelectStringRes( + callback.view, + callback.card.posterUrl, + listOf( + R.string.action_open_watching, + R.string.action_remove_watching + ), + listOf( + R.string.action_open_play, + R.string.action_open_watching, + R.string.action_remove_watching + ) + ) { (isTv, actionId) -> + fun play() { + activity.loadSearchResult(callback.card, START_ACTION_RESUME_LATEST) + reloadStored() + } + + fun remove() { + val card = callback.card + if (card is DataStoreHelper.ResumeWatchingResult) { + removeLastWatched(card.parentId) reloadStored() } - if (itemId == 0) { - val card = callback.card - if (card is DataStoreHelper.ResumeWatchingResult) { - removeLastWatched(card.parentId) - reloadStored() + } + + fun info() { + handleSearchClickCallback( + activity, + SearchClickCallback( + SEARCH_ACTION_LOAD, + callback.view, + -1, + callback.card + ) + ) + reloadStored() + } + + if (isTv) { + when (actionId) { + 0 -> { + play() + } + 1 -> { + info() + } + 2 -> { + remove() + } + } + } else { + when (actionId) { + 0 -> { + info() + } + 1 -> { + remove() } } } + } } else { homeHandleSearch(callback) diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/SingleSelectionHelper.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/SingleSelectionHelper.kt index 67fe99e7..ce49bac1 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/SingleSelectionHelper.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/SingleSelectionHelper.kt @@ -2,17 +2,71 @@ package com.lagradost.cloudstream3.utils import android.app.Activity import android.app.Dialog +import android.view.View import android.widget.* import androidx.appcompat.app.AlertDialog -import androidx.core.view.isVisible -import androidx.core.view.marginLeft -import androidx.core.view.marginRight -import androidx.core.view.marginTop +import androidx.core.view.* import com.google.android.material.bottomsheet.BottomSheetDialog import com.lagradost.cloudstream3.R +import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe +import com.lagradost.cloudstream3.utils.UIHelper.popupMenuNoIconsAndNoStringRes +import com.lagradost.cloudstream3.utils.UIHelper.setImage object SingleSelectionHelper { + fun Activity.showOptionSelectStringRes( + view: View?, + poster: String?, + options: List, + tvOptions: List = listOf(), + callback: (Pair) -> Unit + ) { + this.showOptionSelect(view, poster, options.map { this.getString(it) },tvOptions.map { this.getString(it) }, callback) + } + + private fun Activity.showOptionSelect( + view: View?, + poster: String?, + options: List, + tvOptions: List, + callback: (Pair) -> Unit + ) { + if (this.isTvSettings()) { + val builder = + AlertDialog.Builder(this, R.style.AlertDialogCustom) + .setView(R.layout.options_popup_tv) + + val dialog = builder.create() + dialog.show() + + dialog.findViewById(R.id.listview1)?.let { listView -> + listView.choiceMode = AbsListView.CHOICE_MODE_SINGLE + listView.adapter = ArrayAdapter(this, R.layout.sort_bottom_single_choice_color).apply { + addAll(tvOptions) + } + + listView.setOnItemClickListener { _, _, i, _ -> + callback.invoke(Pair(true,i)) + dialog.dismissSafe(this) + } + } + + dialog.findViewById(R.id.imageView)?.apply { + isGone = poster.isNullOrEmpty() + setImage(poster) + } + } else { + view?.popupMenuNoIconsAndNoStringRes(options.mapIndexed { index, s -> + Pair( + index, + s + ) + }) { + callback(Pair(false,this.itemId)) + } + } + } + fun Activity.showDialog( dialog: Dialog, items: List, @@ -98,7 +152,8 @@ object SingleSelectionHelper { callback: (List) -> Unit, ) { val builder = - AlertDialog.Builder(this, R.style.AlertDialogCustom).setView(R.layout.bottom_selection_dialog) + AlertDialog.Builder(this, R.style.AlertDialogCustom) + .setView(R.layout.bottom_selection_dialog) val dialog = builder.create() dialog.show() @@ -114,7 +169,8 @@ object SingleSelectionHelper { callback: (Int) -> Unit, ) { val builder = - AlertDialog.Builder(this, R.style.AlertDialogCustom).setView(R.layout.bottom_selection_dialog) + AlertDialog.Builder(this, R.style.AlertDialogCustom) + .setView(R.layout.bottom_selection_dialog) val dialog = builder.create() dialog.show() @@ -151,7 +207,7 @@ object SingleSelectionHelper { name, showApply, false, - { if(it.isNotEmpty()) callback.invoke(it.first()) }, + { if (it.isNotEmpty()) callback.invoke(it.first()) }, dismissCallback ) } diff --git a/app/src/main/res/layout/options_popup_tv.xml b/app/src/main/res/layout/options_popup_tv.xml new file mode 100644 index 00000000..4205bcd1 --- /dev/null +++ b/app/src/main/res/layout/options_popup_tv.xml @@ -0,0 +1,44 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/sort_bottom_single_choice_color.xml b/app/src/main/res/layout/sort_bottom_single_choice_color.xml new file mode 100644 index 00000000..707e2b31 --- /dev/null +++ b/app/src/main/res/layout/sort_bottom_single_choice_color.xml @@ -0,0 +1,18 @@ + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f714e0c5..9424cce2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -157,6 +157,7 @@ Remove More Info + @string/home_play A VPN might be needed for this provider to work correctly This provider is a torrent, a VPN is recommended