From b5337a1cfbd764a7b83a50695e15992ae216b9fd Mon Sep 17 00:00:00 2001
From: Jace <54625750+Jacekun@users.noreply.github.com>
Date: Mon, 15 Aug 2022 14:40:19 +0800
Subject: [PATCH] [Feature] Filter subtitle using 'preferred media' language
---
.../cloudstream3/ui/player/GeneratorPlayer.kt | 26 ++++++++++++++++++-
.../ui/subtitles/SubtitlesFragment.kt | 15 +++++++++++
app/src/main/res/layout/subtitle_settings.xml | 13 ++++++++++
app/src/main/res/values/strings.xml | 2 ++
4 files changed, 55 insertions(+), 1 deletion(-)
diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt
index e38b5e1e..64906c3a 100644
--- a/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt
+++ b/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt
@@ -1004,12 +1004,25 @@ class GeneratorPlayer : FullScreenPlayer() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
+ var langFilterList = listOf()
+ var filterSubByLang = false
context?.let { ctx ->
val settingsManager = PreferenceManager.getDefaultSharedPreferences(ctx)
titleRez = settingsManager.getInt(ctx.getString(R.string.prefer_limit_title_rez_key), 3)
limitTitle = settingsManager.getInt(ctx.getString(R.string.prefer_limit_title_key), 0)
updateForcedEncoding(ctx)
+
+ filterSubByLang = settingsManager.getBoolean(getString(R.string.filter_sub_lang_key), false)
+ if (filterSubByLang) {
+ val langFromPrefMedia = settingsManager.getStringSet(
+ this.getString(R.string.provider_lang_key),
+ mutableSetOf("en")
+ )
+ langFilterList = langFromPrefMedia?.mapNotNull {
+ fromTwoLettersToLanguage(it)?.lowercase() ?: return@mapNotNull null
+ } ?: listOf()
+ }
}
unwrapBundle(savedInstanceState)
@@ -1062,7 +1075,18 @@ class GeneratorPlayer : FullScreenPlayer() {
}
observe(viewModel.currentSubs) { set ->
- currentSubs = set
+ val setOfSub = mutableSetOf()
+ if (langFilterList.isNotEmpty() && filterSubByLang) {
+ Log.i("subfilter", "Filtering subtitle")
+ langFilterList.forEach { lang ->
+ Log.i("subfilter", "Lang: $lang")
+ setOfSub += set.filter { it.name.contains(lang, ignoreCase = true) }
+ .toMutableSet()
+ }
+ currentSubs = setOfSub
+ } else {
+ currentSubs = set
+ }
player.setActiveSubtitles(set)
autoSelectSubtitles()
diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/subtitles/SubtitlesFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/subtitles/SubtitlesFragment.kt
index 0430597b..4d7c9fc1 100644
--- a/app/src/main/java/com/lagradost/cloudstream3/ui/subtitles/SubtitlesFragment.kt
+++ b/app/src/main/java/com/lagradost/cloudstream3/ui/subtitles/SubtitlesFragment.kt
@@ -16,6 +16,7 @@ import android.widget.Toast
import androidx.annotation.FontRes
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.Fragment
+import androidx.preference.PreferenceManager
import com.fasterxml.jackson.annotation.JsonProperty
import com.google.android.exoplayer2.text.Cue
import com.google.android.exoplayer2.ui.CaptionStyleCompat
@@ -415,6 +416,20 @@ class SubtitlesFragment : Fragment() {
return@setOnLongClickListener true
}
+ //Fetch current value from preference
+ context?.let { ctx ->
+ subtitles_filter_sub_lang?.isChecked = PreferenceManager.getDefaultSharedPreferences(ctx)
+ .getBoolean(getString(R.string.filter_sub_lang_key), false)
+ }
+ subtitles_filter_sub_lang?.setOnCheckedChangeListener { _, b ->
+ context?.let { ctx ->
+ PreferenceManager.getDefaultSharedPreferences(ctx)
+ .edit()
+ .putBoolean(getString(R.string.filter_sub_lang_key), b)
+ .apply()
+ }
+ }
+
subs_font.setFocusableInTv()
subs_font.setOnClickListener { textView ->
val fontTypes = listOf(
diff --git a/app/src/main/res/layout/subtitle_settings.xml b/app/src/main/res/layout/subtitle_settings.xml
index 3eef98d3..da0c46f2 100644
--- a/app/src/main/res/layout/subtitle_settings.xml
+++ b/app/src/main/res/layout/subtitle_settings.xml
@@ -169,6 +169,19 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />
+
+
subtitles_encoding_key
override_site_key
redo_setup_key
+ filter_sub_lang_key
%d %s | %sMB
@@ -561,6 +562,7 @@
Error
Remove closed captions from subtitles
Remove bloat from subtitles
+ Filter by preferred media language
Extras
Trailer
Link to stream