Fixed "open with" in library

This commit is contained in:
Blatzar 2023-01-22 18:54:16 +01:00
parent 019e9a0c4f
commit 30dbd3920e
3 changed files with 67 additions and 19 deletions

View file

@ -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<LibraryOpener>(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")
when (searchClickCallback.action) {
SEARCH_ACTION_SHOW_METADATA -> {
val syncId = (searchClickCallback.card as SyncAPI.LibraryItem).syncId
val syncName =
libraryViewModel.currentSyncApi?.syncIdName ?: return@callback
showPluginSelectionDialog(syncId, syncName)
when (searchClickCallback.action) {
SEARCH_ACTION_SHOW_METADATA -> {
activity?.showPluginSelectionDialog(
syncId,
syncName,
searchClickCallback.card.apiName
)
}
SEARCH_ACTION_LOAD -> {
val savedSelection = getKey<LibraryOpener>(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<LibraryOpener>(LIBRARY_FOLDER, syncName.name)
val savedSelection = getKey<LibraryOpener>(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)
}
}
}
}

View file

@ -457,7 +457,7 @@ object AppUtils {
}
}
fun AppCompatActivity.loadResult(
fun Activity.loadResult(
url: String,
apiName: String,
startAction: Int = 0,

View file

@ -111,6 +111,7 @@
<string name="result_tags">Genres</string>
<string name="result_share">Share</string>
<string name="result_open_in_browser">Open In Browser</string>
<string name="browser">Browser</string>
<string name="skip_loading">Skip Loading</string>
<string name="loading">Loading…</string>