diff --git a/app/src/main/java/com/lagradost/cloudstream3/mvvm/ArchComponentExt.kt b/app/src/main/java/com/lagradost/cloudstream3/mvvm/ArchComponentExt.kt index e5c03d64..afe956cc 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/mvvm/ArchComponentExt.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/mvvm/ArchComponentExt.kt @@ -49,10 +49,14 @@ inline fun debugWarning(assert: () -> Boolean, message: () -> String) { } } -fun LifecycleOwner.observe(liveData: LiveData, action: (t: T) -> Unit) { +fun LifecycleOwner.observe(liveData: LiveData, action: (t: T) -> Unit) { liveData.observe(this) { it?.let { t -> action(t) } } } +fun LifecycleOwner.observeNullable(liveData: LiveData, action: (t: T) -> Unit) { + liveData.observe(this) { action(it) } +} + inline fun some(value: T?): Some { return if (value == null) { Some.None 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 37f13c6c..bf39edc7 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 @@ -28,19 +28,13 @@ import com.hippo.unifile.UniFile import com.lagradost.cloudstream3.* import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull import com.lagradost.cloudstream3.CommonActivity.showToast -import com.lagradost.cloudstream3.mvvm.Resource -import com.lagradost.cloudstream3.mvvm.logError -import com.lagradost.cloudstream3.mvvm.normalSafeApiCall -import com.lagradost.cloudstream3.mvvm.observe +import com.lagradost.cloudstream3.mvvm.* import com.lagradost.cloudstream3.subtitles.AbstractSubtitleEntities import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.subtitleProviders import com.lagradost.cloudstream3.ui.player.CS3IPlayer.Companion.preferredAudioTrackLanguage import com.lagradost.cloudstream3.ui.player.CustomDecoder.Companion.updateForcedEncoding import com.lagradost.cloudstream3.ui.player.PlayerSubtitleHelper.Companion.toSubtitleMimeType -import com.lagradost.cloudstream3.ui.result.ResultEpisode -import com.lagradost.cloudstream3.ui.result.ResultFragment -import com.lagradost.cloudstream3.ui.result.SyncViewModel -import com.lagradost.cloudstream3.ui.result.setText +import com.lagradost.cloudstream3.ui.result.* import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings import com.lagradost.cloudstream3.ui.subtitles.SubtitlesFragment.Companion.getAutoSelectLanguageISO639_1 import com.lagradost.cloudstream3.utils.* @@ -63,6 +57,9 @@ import kotlinx.android.synthetic.main.player_select_source_and_subs.* import kotlinx.android.synthetic.main.player_select_source_and_subs.subtitles_click_settings import kotlinx.android.synthetic.main.player_select_tracks.* import kotlinx.coroutines.Job +import java.util.* +import kotlin.collections.ArrayList +import kotlin.collections.HashMap class GeneratorPlayer : FullScreenPlayer() { companion object { @@ -332,28 +329,54 @@ class GeneratorPlayer : FullScreenPlayer() { dialog.search_loading_bar.progressTintList = color dialog.search_loading_bar.indeterminateTintList = color - //Automatically search after entering Year - dialog.subtitles_search_year.setOnEditorActionListener(OnEditorActionListener { v, actionId, event -> - if (actionId == EditorInfo.IME_ACTION_DONE) { - dialog.subtitles_search.setQuery(dialog.subtitles_search.query, true) - return@OnEditorActionListener true + observeNullable(viewModel.currentSubtitleYear) { + // When year is changed search again + dialog.subtitles_search.setQuery(dialog.subtitles_search.query, true) + dialog.year_btt.text = it?.toString() ?: txt(R.string.none).asString(context) + } + + dialog.year_btt?.setOnClickListener { + val none = txt(R.string.none).asString(context) + val currentYear = Calendar.getInstance().get(Calendar.YEAR) + val earliestYear = 1900 + + val years = (currentYear downTo earliestYear).toList() + val options = listOf(none) + years.map { + it.toString() } - false - }) + + val selectedIndex = viewModel.currentSubtitleYear.value + ?.let { + // + 1 since none also takes a space + years.indexOf(it) + 1 + } + ?.takeIf { it >= 0 } ?: 0 + + activity?.showDialog( + options, + selectedIndex, + txt(R.string.year).asString(context), + true, { + }, { index -> + viewModel.setSubtitleYear(years.getOrNull(index - 1)) + } + ) + } dialog.subtitles_search.setOnQueryTextListener(object : androidx.appcompat.widget.SearchView.OnQueryTextListener { override fun onQueryTextSubmit(query: String?): Boolean { dialog.search_loading_bar?.show() ioSafe { - val year = dialog.subtitles_search_year?.text?.toString()?.toIntOrNull() val search = - AbstractSubtitleEntities.SubtitleSearch(query = query ?: return@ioSafe, + AbstractSubtitleEntities.SubtitleSearch( + query = query ?: return@ioSafe, imdb = imdbId, epNumber = currentTempMeta.episode, seasonNumber = currentTempMeta.season, lang = currentLanguageTwoLetters.ifBlank { null }, - year = year) + year = viewModel.currentSubtitleYear.value + ) val results = providers.amap { try { it.search(search) @@ -361,7 +384,7 @@ class GeneratorPlayer : FullScreenPlayer() { null } }.filterNotNull() - val max = results.map { it.size }.maxOrNull() ?: return@ioSafe + val max = results.maxOfOrNull { it.size } ?: return@ioSafe // very ugly val items = ArrayList() diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/player/PlayerGeneratorViewModel.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/player/PlayerGeneratorViewModel.kt index dc33f67c..7faf0cf5 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/player/PlayerGeneratorViewModel.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/player/PlayerGeneratorViewModel.kt @@ -35,6 +35,13 @@ class PlayerGeneratorViewModel : ViewModel() { private val _currentStamps = MutableLiveData>(emptyList()) val currentStamps: LiveData> = _currentStamps + private val _currentSubtitleYear = MutableLiveData(null) + val currentSubtitleYear: LiveData = _currentSubtitleYear + + fun setSubtitleYear(year: Int?) { + _currentSubtitleYear.postValue(year) + } + fun getId(): Int? { return generator?.getCurrentId() } diff --git a/app/src/main/res/layout/dialog_online_subtitles.xml b/app/src/main/res/layout/dialog_online_subtitles.xml index 4acd27be..7803e261 100644 --- a/app/src/main/res/layout/dialog_online_subtitles.xml +++ b/app/src/main/res/layout/dialog_online_subtitles.xml @@ -1,159 +1,158 @@ + xmlns:app="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:background="@null" + android:orientation="vertical"> + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_marginBottom="60dp" + android:baselineAligned="false" + android:orientation="horizontal"> + android:id="@+id/sort_subtitles_holder" + android:layout_width="0dp" + android:layout_height="match_parent" + android:layout_weight="50" + android:orientation="vertical"> + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="horizontal" + tools:ignore="UseCompoundDrawables"> + android:layout_width="match_parent" + android:layout_height="40dp" + android:layout_margin="10dp" + android:background="@drawable/search_background" + android:visibility="visible"> + android:layout_width="match_parent" + android:layout_height="30dp" + android:layout_gravity="center_vertical" + android:layout_marginEnd="30dp"> + app:queryBackground="@color/transparent" + app:queryHint="@string/search_hint" + app:searchIcon="@drawable/search_icon" + tools:ignore="RtlSymmetry"> - - - + + + + + + + + + + android:layout_margin="10dp" + android:background="?selectableItemBackgroundBorderless" + android:contentDescription="@string/change_providers_img_des" + android:nextFocusLeft="@id/main_search" + android:nextFocusRight="@id/main_search" + android:nextFocusUp="@id/nav_rail_view" + android:nextFocusDown="@id/search_autofit_results" + android:src="@drawable/ic_baseline_tune_24" + app:tint="?attr/textColor" /> - - - - + android:layout_rowWeight="1" + android:background="?attr/primaryBlackBackground" + android:nextFocusLeft="@id/sort_providers" + android:nextFocusRight="@id/cancel_btt" + android:requiresFadingEdge="vertical" + tools:listfooter="@layout/sort_bottom_footer_add_choice" + tools:listitem="@layout/sort_bottom_single_choice" /> + android:id="@+id/apply_btt_holder" + android:layout_width="match_parent" + android:layout_height="60dp" + android:layout_gravity="bottom" + android:layout_marginTop="-60dp" + android:gravity="bottom|end" + android:orientation="horizontal"> + android:id="@+id/apply_btt" + style="@style/WhiteButton" + android:layout_width="wrap_content" + android:layout_gravity="center_vertical|end" + android:text="@string/sort_apply" /> + android:id="@+id/cancel_btt" + style="@style/BlackButton" + android:layout_width="wrap_content" + android:layout_gravity="center_vertical|end" + android:text="@string/sort_cancel" /> diff --git a/gradlew b/gradlew old mode 100644 new mode 100755