Feature: Refactor autodownload plugin to have multiple modes. (#518)

This commit is contained in:
Jace 2023-08-01 21:54:15 +08:00 committed by GitHub
parent a8ed8773de
commit 827cbbb0b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 78 additions and 27 deletions

View file

@ -813,6 +813,18 @@ enum class TvType(value: Int?) {
Others(12)
}
public enum class AutoDownloadMode(val value: Int) {
Disable(0),
FilterByLang(1),
All(2),
NsfwOnly(3)
;
companion object {
infix fun getEnum(value: Int): AutoDownloadMode? = AutoDownloadMode.values().firstOrNull { it.value == value }
}
}
// IN CASE OF FUTURE ANIME MOVIE OR SMTH
fun TvType.isMovieType(): Boolean {
return this == TvType.Movie || this == TvType.AnimeMovie || this == TvType.Torrent || this == TvType.Live

View file

@ -1092,13 +1092,10 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
loadAllOnlinePlugins(this@MainActivity)
}
//Automatically download not existing plugins
if (settingsManager.getBoolean(
getString(R.string.auto_download_plugins_key),
false
)
) {
PluginManager.downloadNotExistingPluginsAndLoad(this@MainActivity)
//Automatically download not existing plugins, using mode specified.
val auto_download_plugin = AutoDownloadMode.getEnum(settingsManager.getInt(getString(R.string.auto_download_plugins_key), 0)) ?: AutoDownloadMode.Disable
if (auto_download_plugin != AutoDownloadMode.Disable) {
PluginManager.downloadNotExistingPluginsAndLoad(this@MainActivity, auto_download_plugin)
}
}

View file

@ -290,7 +290,7 @@ object PluginManager {
* 2. Fetch all not downloaded plugins
* 3. Download them and reload plugins
**/
fun downloadNotExistingPluginsAndLoad(activity: Activity) {
fun downloadNotExistingPluginsAndLoad(activity: Activity, mode: AutoDownloadMode) {
val newDownloadPlugins = mutableListOf<String>()
val urls = (getKey<Array<RepositoryData>>(REPOSITORIES_KEY)
?: emptyArray()) + PREBUILT_REPOSITORIES
@ -304,6 +304,8 @@ object PluginManager {
// Iterate online repos and returns not downloaded plugins
val notDownloadedPlugins = onlinePlugins.mapNotNull { onlineData ->
val sitePlugin = onlineData.second
val tvtypes = sitePlugin.tvTypes ?: listOf()
//Don't include empty urls
if (sitePlugin.url.isBlank()) {
return@mapNotNull null
@ -318,22 +320,29 @@ object PluginManager {
return@mapNotNull null
}
//Omit lang not selected on language setting
val lang = sitePlugin.language ?: return@mapNotNull null
//If set to 'universal', don't skip any language
if (!providerLang.contains(AllLanguagesName) && !providerLang.contains(lang)) {
return@mapNotNull null
}
//Log.i(TAG, "sitePlugin lang => $lang")
//Omit NSFW, if disabled
sitePlugin.tvTypes?.let { tvtypes ->
if (!settingsForProvider.enableAdult) {
if (tvtypes.contains(TvType.NSFW.name)) {
return@mapNotNull null
}
//Omit non-NSFW if mode is set to NSFW only
if (mode == AutoDownloadMode.NsfwOnly) {
if (tvtypes.contains(TvType.NSFW.name) == false) {
return@mapNotNull null
}
}
//Omit NSFW, if disabled
if (!settingsForProvider.enableAdult) {
if (tvtypes.contains(TvType.NSFW.name)) {
return@mapNotNull null
}
}
//Omit lang not selected on language setting
if (mode == AutoDownloadMode.FilterByLang) {
val lang = sitePlugin.language ?: return@mapNotNull null
//If set to 'universal', don't skip any language
if (!providerLang.contains(AllLanguagesName) && !providerLang.contains(lang)) {
return@mapNotNull null
}
//Log.i(TAG, "sitePlugin lang => $lang")
}
val savedData = PluginData(
url = sitePlugin.url,
internalName = sitePlugin.internalName,
@ -697,4 +706,4 @@ object PluginManager {
return null
}
}
}
}

View file

@ -11,10 +11,14 @@ import androidx.appcompat.app.AlertDialog
import androidx.navigation.fragment.findNavController
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
import com.lagradost.cloudstream3.AcraApplication
import com.lagradost.cloudstream3.AutoDownloadMode
import com.lagradost.cloudstream3.CommonActivity.showToast
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.app
import com.lagradost.cloudstream3.databinding.LogcatBinding
import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.network.initClient
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.getPref
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setPaddingBottom
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setUpToolbar
@ -166,5 +170,25 @@ class SettingsUpdates : PreferenceFragmentCompat() {
}
return@setOnPreferenceClickListener true
}
getPref(R.string.auto_download_plugins_key)?.setOnPreferenceClickListener {
val settingsManager = PreferenceManager.getDefaultSharedPreferences(it.context)
val prefNames = resources.getStringArray(R.array.auto_download_plugin)
val prefValues = enumValues<AutoDownloadMode>().sortedBy { x -> x.value }.map { x -> x.value }
val current = settingsManager.getInt(getString(R.string.auto_download_plugins_pref), 0)
activity?.showBottomDialog(
prefNames.toList(),
prefValues.indexOf(current),
getString(R.string.automatic_plugin_download_mode_title),
true,
{}) {
settingsManager.edit().putInt(getString(R.string.auto_download_plugins_pref), prefValues[it]).apply()
(context ?: AcraApplication.context)?.let { ctx -> app.initClient(ctx) }
}
return@setOnPreferenceClickListener true
}
}
}

View file

@ -41,7 +41,14 @@
<item>0</item>
<item>1</item>
</array>
<!-- MainAPI enum: AutoDownloadMode -->
<array name="auto_download_plugin">
<item>@string/disable</item>
<item>@string/subtitles_filter_lang</item>
<item>@string/all</item>
<item>@string/nsfw</item>
</array>
<array name="player_pref_names">
<item>@string/player_settings_play_in_app</item>

View file

@ -7,6 +7,7 @@
<string name="auto_update_key" translatable="false">auto_update</string>
<string name="auto_update_plugins_key" translatable="false">auto_update_plugins</string>
<string name="auto_download_plugins_key" translatable="false">auto_download_plugins_key</string>
<string name="auto_download_plugins_pref" translatable="false">auto_download_plugins_pref</string>
<string name="skip_update_key" translatable="false">skip_update_key</string>
<string name="prerelease_update_key" translatable="false">prerelease_update</string>
<string name="manual_check_update_key" translatable="false">manual_check_update</string>
@ -249,6 +250,7 @@
<string name="pref_filter_search_quality">Hide selected video quality in search results</string>
<string name="automatic_plugin_updates">Automatic plugin updates</string>
<string name="automatic_plugin_download">Automatically download plugins</string>
<string name="automatic_plugin_download_mode_title">Select mode to filter plugins download</string>
<string name="automatic_plugin_download_summary">Automatically install all not yet installed plugins from added repositories.</string>
<string name="updates_settings">Show app updates</string>
<string name="updates_settings_des">Automatically search for new updates after starting the app.</string>
@ -482,6 +484,7 @@
<string name="authenticated_user" formatted="true">%s authenticated</string>
<string name="authenticated_user_fail" formatted="true">Could not log in at %s</string>
<!-- ============ -->
<string name="disable">Disable</string>
<string name="none">None</string>
<string name="normal">Normal</string>
<string name="all">All</string>

View file

@ -58,8 +58,7 @@
android:key="@string/auto_update_plugins_key"
android:title="@string/automatic_plugin_updates" />
<SwitchPreference
android:defaultValue="false"
<Preference
android:icon="@drawable/ic_baseline_extension_24"
android:key="@string/auto_download_plugins_key"
android:title="@string/automatic_plugin_download"
@ -77,4 +76,4 @@
android:title="@string/redo_setup_process"
app:key="@string/redo_setup_key" />
</PreferenceCategory>
</PreferenceScreen>
</PreferenceScreen>