From 30dbd3920e0783a359e3caaa9552d56b0f112eba Mon Sep 17 00:00:00 2001 From: Blatzar <46196380+Blatzar@users.noreply.github.com> Date: Sun, 22 Jan 2023 18:54:16 +0100 Subject: [PATCH] Fixed "open with" in library --- .../ui/library/LibraryFragment.kt | 83 +++++++++++++++---- .../lagradost/cloudstream3/utils/AppUtils.kt | 2 +- app/src/main/res/values/strings.xml | 1 + 3 files changed, 67 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryFragment.kt index 996c5a33..fa70d61b 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryFragment.kt @@ -1,16 +1,20 @@ package com.lagradost.cloudstream3.ui.library +import android.app.Activity import android.content.Context import android.os.Bundle import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.annotation.StringRes import androidx.appcompat.widget.SearchView import androidx.fragment.app.activityViewModels import com.google.android.material.tabs.TabLayoutMediator +import com.lagradost.cloudstream3.APIHolder import com.lagradost.cloudstream3.APIHolder.allProviders import com.lagradost.cloudstream3.AcraApplication.Companion.getKey +import com.lagradost.cloudstream3.AcraApplication.Companion.openBrowser import com.lagradost.cloudstream3.AcraApplication.Companion.setKey import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.mvvm.debugAssert @@ -20,6 +24,7 @@ import com.lagradost.cloudstream3.syncproviders.SyncIdName import com.lagradost.cloudstream3.ui.result.txt import com.lagradost.cloudstream3.ui.search.SEARCH_ACTION_LOAD import com.lagradost.cloudstream3.ui.search.SEARCH_ACTION_SHOW_METADATA +import com.lagradost.cloudstream3.utils.AppUtils.loadResult import com.lagradost.cloudstream3.utils.AppUtils.loadSearchResult import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showBottomDialog import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar @@ -28,9 +33,11 @@ import kotlinx.android.synthetic.main.fragment_library.* const val LIBRARY_FOLDER = "library_folder" -enum class LibraryOpenerType { - Provider, - Browser, +enum class LibraryOpenerType(@StringRes val stringRes: Int) { + Default(R.string.default_subtitles), // TODO FIX AFTER MERGE + Provider(R.string.none), + Browser(R.string.browser), + None(R.string.none), } /** Used to store how the user wants to open said poster */ @@ -111,31 +118,41 @@ class LibraryFragment : Fragment() { /** * Shows a plugin selection dialogue and saves the response **/ - fun showPluginSelectionDialog(key: String, syncId: SyncIdName) { + fun Activity.showPluginSelectionDialog( + key: String, + syncId: SyncIdName, + apiName: String? = null, + ) { val availableProviders = allProviders.filter { it.supportedSyncNames.contains(syncId) - }.map { it.name } + }.map { it.name } + + // Add the api if it exists + (APIHolder.getApiFromNameNull(apiName)?.let { listOf(it.name) } ?: emptyList()) - val baseOptions = listOf(LibraryOpenerType.Browser.name) + val baseOptions = listOf( + txt(LibraryOpenerType.Default.stringRes).asString(this), + txt(LibraryOpenerType.None.stringRes).asString(this), + txt(LibraryOpenerType.Browser.stringRes).asString(this), + ) val items = baseOptions + availableProviders val savedSelection = getKey(LIBRARY_FOLDER, key) val selectedIndex = when { - savedSelection == null -> -1 + savedSelection == null -> 0 // If provider savedSelection.openType == LibraryOpenerType.Provider && savedSelection.providerData?.apiName != null -> { availableProviders.indexOf(savedSelection.providerData.apiName) .takeIf { it != -1 } - ?.plus(baseOptions.size) ?: -1 + ?.plus(baseOptions.size) ?: 0 } // Else base option else -> baseOptions.indexOf(savedSelection.openType.name) } - activity?.showBottomDialog( + this.showBottomDialog( items, selectedIndex, "Open with", @@ -164,7 +181,7 @@ class LibraryFragment : Fragment() { provider_selector?.setOnClickListener { val syncName = libraryViewModel.currentSyncApi?.syncIdName ?: return@setOnClickListener - showPluginSelectionDialog(syncName.name, syncName) + activity?.showPluginSelectionDialog(syncName.name, syncName) } viewpager?.setPageTransformer(LibraryScrollTransformer()) @@ -176,25 +193,55 @@ class LibraryFragment : Fragment() { sort_fab?.extend() } }) callback@{ searchClickCallback -> - // To prevent future accidents debugAssert({ searchClickCallback.card !is SyncAPI.LibraryItem }, { "searchClickCallback ${searchClickCallback.card} is not a LibraryItem" }) - val syncId = (searchClickCallback.card as SyncAPI.LibraryItem).syncId - println("SEARCH CLICK $searchClickCallback") + val syncId = (searchClickCallback.card as SyncAPI.LibraryItem).syncId + val syncName = + libraryViewModel.currentSyncApi?.syncIdName ?: return@callback + when (searchClickCallback.action) { SEARCH_ACTION_SHOW_METADATA -> { - val syncName = - libraryViewModel.currentSyncApi?.syncIdName ?: return@callback - showPluginSelectionDialog(syncId, syncName) + activity?.showPluginSelectionDialog( + syncId, + syncName, + searchClickCallback.card.apiName + ) } + SEARCH_ACTION_LOAD -> { - val savedSelection = getKey(LIBRARY_FOLDER, syncId) - activity?.loadSearchResult(searchClickCallback.card) + // This basically first selects the individual opener and if that is default then + // selects the whole list opener + val savedListSelection = + getKey(LIBRARY_FOLDER, syncName.name) + val savedSelection = getKey(LIBRARY_FOLDER, syncId).takeIf { + it?.openType != LibraryOpenerType.Default + } ?: savedListSelection + + when (savedSelection?.openType) { + null, LibraryOpenerType.Default -> { + // Prevents opening MAL/AniList as a provider + if (APIHolder.getApiFromNameNull(searchClickCallback.card.apiName) != null) { + activity?.loadSearchResult( + searchClickCallback.card + ) + } + } + LibraryOpenerType.None -> {} + LibraryOpenerType.Provider -> + savedSelection.providerData?.apiName?.let { apiName -> + activity?.loadResult( + searchClickCallback.card.url, + apiName, + ) + } + LibraryOpenerType.Browser -> + openBrowser(searchClickCallback.card.url) + } } } } 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 f25d20ba..64c4a554 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/AppUtils.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/AppUtils.kt @@ -457,7 +457,7 @@ object AppUtils { } } - fun AppCompatActivity.loadResult( + fun Activity.loadResult( url: String, apiName: String, startAction: Int = 0, diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e1af1e0e..5896e922 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -111,6 +111,7 @@ Genres Share Open In Browser + Browser Skip Loading Loading…