AquaStream/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragment.kt

1015 lines
40 KiB
Kotlin
Raw Normal View History

2021-05-16 18:28:00 +00:00
package com.lagradost.cloudstream3.ui.result
2021-05-18 13:43:32 +00:00
import android.annotation.SuppressLint
import android.content.Context
2021-09-03 09:13:34 +00:00
import android.content.Intent
2021-07-17 15:56:26 +00:00
import android.content.Intent.*
2021-12-10 19:48:21 +00:00
import android.content.res.ColorStateList
2021-06-10 15:15:14 +00:00
import android.net.Uri
2022-04-02 01:38:55 +00:00
import android.os.Build
2021-05-16 18:28:00 +00:00
import android.os.Bundle
2022-04-02 01:38:55 +00:00
import android.text.Editable
2021-05-16 18:28:00 +00:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2022-08-18 00:54:05 +00:00
import android.widget.AbsListView
import android.widget.ArrayAdapter
import android.widget.ImageView
2021-05-18 13:43:32 +00:00
import androidx.appcompat.app.AlertDialog
import androidx.core.view.isGone
2021-09-19 22:36:32 +00:00
import androidx.core.view.isVisible
2022-04-02 01:38:55 +00:00
import androidx.core.widget.doOnTextChanged
2022-01-07 19:27:25 +00:00
import androidx.lifecycle.ViewModelProvider
2021-09-19 22:36:32 +00:00
import androidx.preference.PreferenceManager
2022-02-04 20:49:35 +00:00
import com.discord.panels.OverlappingPanelsLayout
2021-06-06 18:06:01 +00:00
import com.google.android.material.button.MaterialButton
2022-08-04 01:19:59 +00:00
import com.lagradost.cloudstream3.APIHolder.getApiDubstatusSettings
2022-08-18 00:54:05 +00:00
import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull
import com.lagradost.cloudstream3.APIHolder.updateHasTrailers
2022-08-04 22:26:33 +00:00
import com.lagradost.cloudstream3.DubStatus
import com.lagradost.cloudstream3.MainActivity.Companion.afterPluginsLoadedEvent
2022-08-04 22:26:33 +00:00
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.SearchResponse
import com.lagradost.cloudstream3.TvType
2022-08-03 00:04:03 +00:00
import com.lagradost.cloudstream3.mvvm.*
import com.lagradost.cloudstream3.syncproviders.providers.Kitsu
2021-06-15 23:25:58 +00:00
import com.lagradost.cloudstream3.ui.WatchType
2022-08-03 17:57:38 +00:00
import com.lagradost.cloudstream3.ui.download.DOWNLOAD_ACTION_DOWNLOAD
2021-07-24 20:50:57 +00:00
import com.lagradost.cloudstream3.ui.download.DownloadButtonSetup.handleDownloadClick
2022-08-03 17:27:49 +00:00
import com.lagradost.cloudstream3.ui.download.EasyDownloadButton
2021-11-20 00:41:37 +00:00
import com.lagradost.cloudstream3.ui.quicksearch.QuickSearchFragment
2022-03-29 21:50:07 +00:00
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTrueTvSettings
2021-12-10 19:48:21 +00:00
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings
2022-08-03 17:27:49 +00:00
import com.lagradost.cloudstream3.utils.*
import com.lagradost.cloudstream3.utils.AppUtils.getNameFull
2022-06-29 01:20:23 +00:00
import com.lagradost.cloudstream3.utils.AppUtils.html
2022-01-07 19:27:25 +00:00
import com.lagradost.cloudstream3.utils.AppUtils.loadCache
import com.lagradost.cloudstream3.utils.AppUtils.openBrowser
2022-08-18 00:54:05 +00:00
import com.lagradost.cloudstream3.utils.Coroutines.ioWorkSafe
2022-08-03 17:27:49 +00:00
import com.lagradost.cloudstream3.utils.Coroutines.main
2021-06-15 16:07:20 +00:00
import com.lagradost.cloudstream3.utils.DataStoreHelper.getViewPos
2021-12-10 19:48:21 +00:00
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showBottomDialog
import com.lagradost.cloudstream3.utils.UIHelper.colorFromAttribute
2022-08-03 00:04:03 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar
import com.lagradost.cloudstream3.utils.UIHelper.hideKeyboard
2021-05-18 13:43:32 +00:00
import kotlinx.android.synthetic.main.fragment_result.*
2022-08-04 22:26:33 +00:00
import kotlinx.android.synthetic.main.fragment_result.result_cast_items
import kotlinx.android.synthetic.main.fragment_result.result_cast_text
import kotlinx.android.synthetic.main.fragment_result.result_coming_soon
import kotlinx.android.synthetic.main.fragment_result.result_data_holder
import kotlinx.android.synthetic.main.fragment_result.result_description
import kotlinx.android.synthetic.main.fragment_result.result_download_movie
import kotlinx.android.synthetic.main.fragment_result.result_episode_loading
import kotlinx.android.synthetic.main.fragment_result.result_episodes
import kotlinx.android.synthetic.main.fragment_result.result_error_text
import kotlinx.android.synthetic.main.fragment_result.result_finish_loading
import kotlinx.android.synthetic.main.fragment_result.result_info
import kotlinx.android.synthetic.main.fragment_result.result_loading
import kotlinx.android.synthetic.main.fragment_result.result_loading_error
import kotlinx.android.synthetic.main.fragment_result.result_meta_duration
import kotlinx.android.synthetic.main.fragment_result.result_meta_rating
import kotlinx.android.synthetic.main.fragment_result.result_meta_site
import kotlinx.android.synthetic.main.fragment_result.result_meta_type
import kotlinx.android.synthetic.main.fragment_result.result_meta_year
import kotlinx.android.synthetic.main.fragment_result.result_movie_download_icon
import kotlinx.android.synthetic.main.fragment_result.result_movie_download_text
import kotlinx.android.synthetic.main.fragment_result.result_movie_download_text_precentage
import kotlinx.android.synthetic.main.fragment_result.result_movie_progress_downloaded
import kotlinx.android.synthetic.main.fragment_result.result_movie_progress_downloaded_holder
import kotlinx.android.synthetic.main.fragment_result.result_next_airing
import kotlinx.android.synthetic.main.fragment_result.result_next_airing_time
import kotlinx.android.synthetic.main.fragment_result.result_no_episodes
import kotlinx.android.synthetic.main.fragment_result.result_play_movie
import kotlinx.android.synthetic.main.fragment_result.result_reload_connection_open_in_browser
import kotlinx.android.synthetic.main.fragment_result.result_reload_connectionerror
import kotlinx.android.synthetic.main.fragment_result.result_resume_parent
import kotlinx.android.synthetic.main.fragment_result.result_resume_progress_holder
import kotlinx.android.synthetic.main.fragment_result.result_resume_series_progress
import kotlinx.android.synthetic.main.fragment_result.result_resume_series_progress_text
import kotlinx.android.synthetic.main.fragment_result.result_resume_series_title
import kotlinx.android.synthetic.main.fragment_result.result_tag
import kotlinx.android.synthetic.main.fragment_result.result_tag_holder
import kotlinx.android.synthetic.main.fragment_result.result_title
import kotlinx.android.synthetic.main.fragment_result.result_vpn
2022-02-04 20:49:35 +00:00
import kotlinx.android.synthetic.main.fragment_result_swipe.*
2022-08-04 22:26:33 +00:00
import kotlinx.android.synthetic.main.fragment_result_tv.*
2022-04-01 20:05:34 +00:00
import kotlinx.android.synthetic.main.result_sync.*
import kotlinx.coroutines.delay
2022-08-03 00:04:03 +00:00
import kotlinx.coroutines.runBlocking
2022-08-06 18:36:45 +00:00
2021-07-29 15:16:08 +00:00
const val START_ACTION_RESUME_LATEST = 1
2021-08-25 15:28:25 +00:00
const val START_ACTION_LOAD_EP = 2
2021-05-18 13:43:32 +00:00
data class ResultEpisode(
2022-01-07 19:27:25 +00:00
val headerName: String,
2021-05-18 13:43:32 +00:00
val name: String?,
2021-06-10 15:15:14 +00:00
val poster: String?,
2021-05-18 13:43:32 +00:00
val episode: Int,
2022-08-01 01:00:48 +00:00
val seasonIndex: Int?, // this is the "season" index used season names
val season: Int?, // this is the display
2021-06-14 16:58:43 +00:00
val data: String,
2021-05-18 13:43:32 +00:00
val apiName: String,
val id: Int,
2021-05-22 22:25:56 +00:00
val index: Int,
2021-06-15 16:07:20 +00:00
val position: Long, // time in MS
2021-06-10 15:15:14 +00:00
val duration: Long, // duration in MS
2021-06-26 19:32:50 +00:00
val rating: Int?,
val description: String?,
2021-09-19 22:36:32 +00:00
val isFiller: Boolean?,
2022-01-07 19:27:25 +00:00
val tvType: TvType,
val parentId: Int,
2021-05-18 13:43:32 +00:00
)
2021-05-16 18:28:00 +00:00
2021-06-15 16:07:20 +00:00
fun ResultEpisode.getRealPosition(): Long {
if (duration <= 0) return 0
val percentage = position * 100 / duration
if (percentage <= 5 || percentage >= 95) return 0
return position
}
2021-06-16 17:40:02 +00:00
fun ResultEpisode.getDisplayPosition(): Long {
if (duration <= 0) return 0
val percentage = position * 100 / duration
if (percentage <= 1) return 0
if (percentage <= 5) return 5 * duration / 100
if (percentage >= 95) return duration
return position
}
fun buildResultEpisode(
2022-01-07 19:27:25 +00:00
headerName: String,
name: String? = null,
poster: String? = null,
2021-06-15 16:07:20 +00:00
episode: Int,
2022-08-01 01:00:48 +00:00
seasonIndex: Int? = null,
season: Int? = null,
2021-06-15 16:07:20 +00:00
data: String,
apiName: String,
id: Int,
index: Int,
rating: Int? = null,
description: String? = null,
isFiller: Boolean? = null,
2022-01-07 19:27:25 +00:00
tvType: TvType,
parentId: Int,
2021-06-15 16:07:20 +00:00
): ResultEpisode {
val posDur = getViewPos(id)
2021-06-26 19:32:50 +00:00
return ResultEpisode(
2022-01-07 19:27:25 +00:00
headerName,
2021-06-26 19:32:50 +00:00
name,
2021-06-15 16:07:20 +00:00
poster,
episode,
2022-08-01 01:00:48 +00:00
seasonIndex,
2021-06-15 16:07:20 +00:00
season,
data,
apiName,
id,
index,
posDur?.position ?: 0,
2021-06-26 19:32:50 +00:00
posDur?.duration ?: 0,
rating,
2021-11-30 17:59:52 +00:00
description,
2022-01-07 19:27:25 +00:00
isFiller,
tvType,
parentId,
2021-06-26 19:32:50 +00:00
)
2021-06-15 16:07:20 +00:00
}
2021-07-29 15:16:08 +00:00
/** 0f-1f */
2021-06-10 15:15:14 +00:00
fun ResultEpisode.getWatchProgress(): Float {
2021-07-29 15:16:08 +00:00
return (getDisplayPosition() / 1000).toFloat() / (duration / 1000).toFloat()
2021-06-10 15:15:14 +00:00
}
2022-08-04 22:26:33 +00:00
open class ResultFragment : ResultTrailerPlayer() {
2021-05-22 22:25:56 +00:00
companion object {
2022-05-02 21:32:28 +00:00
const val URL_BUNDLE = "url"
const val API_NAME_BUNDLE = "apiName"
const val SEASON_BUNDLE = "season"
const val EPISODE_BUNDLE = "episode"
const val START_ACTION_BUNDLE = "startAction"
const val START_VALUE_BUNDLE = "startValue"
const val RESTART_BUNDLE = "restart"
2022-08-04 22:26:33 +00:00
2022-05-02 21:32:28 +00:00
fun newInstance(
card: SearchResponse, startAction: Int = 0, startValue: Int? = null
): Bundle {
return Bundle().apply {
putString(URL_BUNDLE, card.url)
putString(API_NAME_BUNDLE, card.apiName)
if (card is DataStoreHelper.ResumeWatchingResult) {
if (card.season != null)
putInt(SEASON_BUNDLE, card.season)
if (card.episode != null)
putInt(EPISODE_BUNDLE, card.episode)
}
putInt(START_ACTION_BUNDLE, startAction)
if (startValue != null)
putInt(START_VALUE_BUNDLE, startValue)
2022-08-04 01:19:59 +00:00
2022-05-02 21:32:28 +00:00
putBoolean(RESTART_BUNDLE, true)
}
}
2021-12-17 12:42:25 +00:00
fun newInstance(
url: String,
apiName: String,
startAction: Int = 0,
startValue: Int = 0
): Bundle {
2021-09-20 21:11:36 +00:00
return Bundle().apply {
2022-05-02 21:32:28 +00:00
putString(URL_BUNDLE, url)
putString(API_NAME_BUNDLE, apiName)
putInt(START_ACTION_BUNDLE, startAction)
putInt(START_VALUE_BUNDLE, startValue)
putBoolean(RESTART_BUNDLE, true)
2021-05-18 13:43:32 +00:00
}
2021-09-20 21:11:36 +00:00
}
2022-01-18 14:10:01 +00:00
fun updateUI() {
updateUIListener?.invoke()
}
2022-01-31 20:47:59 +00:00
private var updateUIListener: (() -> Unit)? = null
2021-05-22 22:25:56 +00:00
}
2022-08-06 16:08:20 +00:00
open fun setTrailers(trailers: List<ExtractorLink>?) {}
2022-08-04 22:26:33 +00:00
protected lateinit var viewModel: ResultViewModel2 //by activityViewModels()
protected lateinit var syncModel: SyncViewModel
protected open val resultLayout = R.layout.fragment_result_swipe
2021-05-16 18:28:00 +00:00
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
2021-05-18 13:43:32 +00:00
savedInstanceState: Bundle?,
2021-05-16 18:28:00 +00:00
): View? {
2022-01-07 19:27:25 +00:00
viewModel =
2022-08-01 01:00:48 +00:00
ViewModelProvider(this)[ResultViewModel2::class.java]
2022-04-01 20:05:34 +00:00
syncModel =
ViewModelProvider(this)[SyncViewModel::class.java]
2022-08-04 22:26:33 +00:00
return inflater.inflate(resultLayout, container, false)
2021-05-16 18:28:00 +00:00
}
2022-08-03 17:27:49 +00:00
private var downloadButton: EasyDownloadButton? = null
2021-07-28 19:14:45 +00:00
override fun onDestroyView() {
updateUIListener = null
2021-07-28 19:14:45 +00:00
(result_episodes?.adapter as EpisodeAdapter?)?.killAdapter()
2022-08-03 17:27:49 +00:00
downloadButton?.dispose()
2022-08-04 22:26:33 +00:00
2021-07-28 19:14:45 +00:00
super.onDestroyView()
}
2021-06-16 00:15:07 +00:00
override fun onResume() {
afterPluginsLoadedEvent += ::reloadViewModel
2021-06-16 00:15:07 +00:00
super.onResume()
activity?.let {
it.window?.navigationBarColor =
2021-09-19 20:33:39 +00:00
it.colorFromAttribute(R.attr.primaryBlackBackground)
2021-06-16 00:15:07 +00:00
}
2021-05-22 22:25:56 +00:00
}
override fun onDestroy() {
afterPluginsLoadedEvent -= ::reloadViewModel
super.onDestroy()
}
2021-06-16 16:54:07 +00:00
/// 0 = LOADING, 1 = ERROR LOADING, 2 = LOADED
2021-07-02 18:46:18 +00:00
private fun updateVisStatus(state: Int) {
2021-06-16 16:54:07 +00:00
when (state) {
0 -> {
2021-12-10 19:48:21 +00:00
result_bookmark_fab?.isGone = true
result_loading?.isVisible = true
result_finish_loading?.isVisible = false
result_loading_error?.isVisible = false
2021-06-16 16:54:07 +00:00
}
1 -> {
2021-12-10 19:48:21 +00:00
result_bookmark_fab?.isGone = true
result_loading?.isVisible = false
result_finish_loading?.isVisible = false
result_loading_error?.isVisible = true
2022-08-02 00:43:42 +00:00
result_reload_connection_open_in_browser?.isVisible = true
2021-06-16 16:54:07 +00:00
}
2 -> {
result_bookmark_fab?.isGone = isTrueTvSettings()
2021-12-10 19:48:21 +00:00
result_bookmark_fab?.extend()
2022-08-20 01:06:35 +00:00
//if (result_bookmark_button?.context?.isTrueTvSettings() == true) {
// when {
// result_play_movie?.isVisible == true -> {
// result_play_movie?.requestFocus()
// }
// result_resume_series_button?.isVisible == true -> {
// result_resume_series_button?.requestFocus()
// }
// else -> {
// result_bookmark_button?.requestFocus()
// }
// }
//}
2021-10-09 21:59:37 +00:00
2021-12-10 19:48:21 +00:00
result_loading?.isVisible = false
result_finish_loading?.isVisible = true
result_loading_error?.isVisible = false
2021-06-16 16:54:07 +00:00
}
}
}
2022-08-04 22:26:33 +00:00
open fun setRecommendations(rec: List<SearchResponse>?, validApiName: String?) {
2021-12-13 18:41:33 +00:00
}
2021-07-29 15:16:08 +00:00
2022-01-18 14:10:01 +00:00
private fun updateUI() {
2022-04-08 19:38:19 +00:00
syncModel.updateUserData()
2022-01-18 14:10:01 +00:00
viewModel.reloadEpisodes()
}
2022-08-06 16:08:20 +00:00
open fun updateMovie(data: ResourceSome<Pair<UiText, ResultEpisode>>) {
2022-08-05 23:41:35 +00:00
when (data) {
is ResourceSome.Success -> {
data.value.let { (text, ep) ->
result_play_movie.setText(text)
result_play_movie?.setOnClickListener {
viewModel.handleAction(
activity,
EpisodeClickEvent(ACTION_CLICK_DEFAULT, ep)
)
}
result_play_movie?.setOnLongClickListener {
viewModel.handleAction(
activity,
EpisodeClickEvent(ACTION_SHOW_OPTIONS, ep)
)
return@setOnLongClickListener true
}
main {
val file =
2022-08-18 00:54:05 +00:00
ioWorkSafe {
2022-08-05 23:41:35 +00:00
context?.let {
VideoDownloadManager.getDownloadFileInfoAndUpdateSettings(
it,
ep.id
)
}
}
downloadButton?.dispose()
downloadButton = EasyDownloadButton()
downloadButton?.setUpMoreButton(
file?.fileLength,
file?.totalBytes,
2022-08-18 00:54:05 +00:00
result_movie_progress_downloaded ?: return@main,
result_movie_download_icon ?: return@main,
result_movie_download_text ?: return@main,
result_movie_download_text_precentage ?: return@main,
result_download_movie ?: return@main,
2022-08-05 23:41:35 +00:00
true,
VideoDownloadHelper.DownloadEpisodeCached(
ep.name,
ep.poster,
0,
null,
ep.id,
ep.id,
null,
null,
System.currentTimeMillis(),
)
) { click ->
2022-08-06 16:08:20 +00:00
when (click.action) {
2022-08-05 23:41:35 +00:00
DOWNLOAD_ACTION_DOWNLOAD -> {
viewModel.handleAction(
activity,
EpisodeClickEvent(ACTION_DOWNLOAD_EPISODE, ep)
)
}
else -> handleDownloadClick(activity, click)
}
}
result_movie_progress_downloaded_holder?.isVisible = true
}
}
}
else -> {
result_movie_progress_downloaded_holder?.isVisible = false
result_play_movie?.isVisible = false
}
}
}
2022-08-06 16:08:20 +00:00
open fun updateEpisodes(episodes: ResourceSome<List<ResultEpisode>>) {
2022-08-05 23:41:35 +00:00
when (episodes) {
is ResourceSome.None -> {
result_episode_loading?.isVisible = false
result_episodes?.isVisible = false
}
is ResourceSome.Loading -> {
result_episode_loading?.isVisible = true
result_episodes?.isVisible = false
}
is ResourceSome.Success -> {
result_episodes?.isVisible = true
result_episode_loading?.isVisible = false
/*
* Okay so what is this fuckery?
* Basically Android TV will crash if you request a new focus while
* the adapter gets updated.
*
* This means that if you load thumbnails and request a next focus at the same time
* the app will crash without any way to catch it!
*
* How to bypass this?
* This code basically steals the focus for 500ms and puts it in an inescapable view
* then lets out the focus by requesting focus to result_episodes
*/
// Do not use this.isTv, that is the player
val isTv = isTvSettings()
val hasEpisodes =
!(result_episodes?.adapter as? EpisodeAdapter?)?.cardList.isNullOrEmpty()
if (isTv && hasEpisodes) {
// Make it impossible to focus anywhere else!
temporary_no_focus?.isFocusable = true
temporary_no_focus?.requestFocus()
}
2022-08-05 23:41:35 +00:00
(result_episodes?.adapter as? EpisodeAdapter?)?.updateList(episodes.value)
if (isTv && hasEpisodes) main {
delay(500)
temporary_no_focus?.isFocusable = false
// This might make some people sad as it changes the focus when leaving an episode :(
result_episodes?.requestFocus()
}
2022-08-05 23:41:35 +00:00
}
}
}
data class StoredData(
val url: String?,
val apiName: String,
val showFillers: Boolean,
val dubStatus: DubStatus,
val start: AutoResume?
)
private fun getStoredData(context: Context): StoredData? {
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
val url = arguments?.getString(URL_BUNDLE)
val apiName = arguments?.getString(API_NAME_BUNDLE) ?: return null
val showFillers =
settingsManager.getBoolean(context.getString(R.string.show_fillers_key), false)
val dubStatus = if (context.getApiDubstatusSettings()
.contains(DubStatus.Dubbed)
) DubStatus.Dubbed else DubStatus.Subbed
val startAction = arguments?.getInt(START_ACTION_BUNDLE)
val start = startAction?.let { action ->
val startValue = arguments?.getInt(START_VALUE_BUNDLE)
val resumeEpisode = arguments?.getInt(EPISODE_BUNDLE)
val resumeSeason = arguments?.getInt(SEASON_BUNDLE)
arguments?.remove(START_VALUE_BUNDLE)
arguments?.remove(START_ACTION_BUNDLE)
AutoResume(
startAction = action,
id = startValue,
episode = resumeEpisode,
season = resumeSeason
)
}
return StoredData(url, apiName, showFillers, dubStatus, start)
}
private fun reloadViewModel(success: Boolean = false) {
if (!viewModel.hasLoaded()) {
val storedData = getStoredData(activity ?: context ?: return) ?: return
//viewModel.clear()
viewModel.load(
activity,
storedData.url ?: return,
storedData.apiName,
storedData.showFillers,
storedData.dubStatus,
storedData.start
)
}
}
2021-05-18 13:43:32 +00:00
@SuppressLint("SetTextI18n")
2021-05-16 18:28:00 +00:00
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2022-07-24 02:49:15 +00:00
result_cast_items?.adapter = ActorAdaptor()
2022-06-16 23:58:55 +00:00
2022-01-18 14:10:01 +00:00
updateUIListener = ::updateUI
2022-05-02 21:32:28 +00:00
val restart = arguments?.getBoolean(RESTART_BUNDLE) ?: false
2021-09-20 21:11:36 +00:00
if (restart) {
2022-05-02 21:32:28 +00:00
arguments?.putBoolean(RESTART_BUNDLE, false)
2021-09-20 21:11:36 +00:00
}
activity?.window?.decorView?.clearFocus()
hideKeyboard()
context?.updateHasTrailers()
2022-01-07 19:27:25 +00:00
activity?.loadCache()
2022-02-04 20:49:35 +00:00
activity?.fixPaddingStatusbar(result_top_bar)
2021-09-19 20:33:39 +00:00
//activity?.fixPaddingStatusbar(result_barstatus)
2021-06-06 18:06:01 +00:00
/* val backParameter = result_back.layoutParams as FrameLayout.LayoutParams
backParameter.setMargins(
backParameter.leftMargin,
backParameter.topMargin + requireContext().getStatusBarHeight(),
backParameter.rightMargin,
backParameter.bottomMargin
)
result_back.layoutParams = backParameter*/
2021-06-16 16:54:07 +00:00
2021-05-20 15:22:28 +00:00
// activity?.fixPaddingStatusbar(result_toolbar)
2021-05-18 13:43:32 +00:00
val storedData = (activity ?: context)?.let {
getStoredData(it)
2022-08-04 01:19:59 +00:00
}
syncModel.addFromUrl(storedData?.url)
2021-09-20 21:11:36 +00:00
val api = getApiFromNameNull(storedData?.apiName)
2021-05-18 13:43:32 +00:00
2022-08-06 18:36:45 +00:00
result_episodes?.adapter =
2021-06-16 16:54:07 +00:00
EpisodeAdapter(
2022-08-15 14:44:46 +00:00
api?.hasDownloadSupport == true,
2021-07-24 20:50:57 +00:00
{ episodeClick ->
2022-08-02 00:43:42 +00:00
viewModel.handleAction(activity, episodeClick)
2021-07-24 20:50:57 +00:00
},
{ downloadClickEvent ->
2022-08-02 00:43:42 +00:00
handleDownloadClick(activity, downloadClickEvent)
2021-07-24 20:50:57 +00:00
}
)
2021-07-17 14:14:25 +00:00
2021-06-15 23:25:58 +00:00
2021-12-10 19:48:21 +00:00
observe(viewModel.watchStatus) { watchType ->
result_bookmark_button?.text = getString(watchType.stringRes)
result_bookmark_fab?.text = getString(watchType.stringRes)
2021-12-12 02:33:17 +00:00
if (watchType == WatchType.NONE) {
2021-12-10 19:48:21 +00:00
result_bookmark_fab?.context?.colorFromAttribute(R.attr.white)
} else {
result_bookmark_fab?.context?.colorFromAttribute(R.attr.colorPrimary)
}?.let {
val colorState = ColorStateList.valueOf(it)
result_bookmark_fab?.iconTint = colorState
result_bookmark_fab?.setTextColor(colorState)
}
result_bookmark_fab?.setOnClickListener { fab ->
2021-12-12 02:33:17 +00:00
activity?.showBottomDialog(
2021-12-10 19:48:21 +00:00
WatchType.values().map { fab.context.getString(it.stringRes) }.toList(),
watchType.ordinal,
fab.context.getString(R.string.action_add_to_bookmarks),
2021-12-10 23:57:11 +00:00
showApply = false,
2021-12-10 19:48:21 +00:00
{}) {
viewModel.updateWatchStatus(WatchType.values()[it])
2021-12-10 19:48:21 +00:00
}
}
2022-08-20 01:06:35 +00:00
result_bookmark_button?.setOnClickListener { fab ->
activity?.showBottomDialog(
WatchType.values().map { fab.context.getString(it.stringRes) }.toList(),
watchType.ordinal,
fab.context.getString(R.string.action_add_to_bookmarks),
showApply = false,
{}) {
viewModel.updateWatchStatus(WatchType.values()[it])
}
}
2021-06-15 23:25:58 +00:00
}
// This is to band-aid FireTV navigation
2022-08-28 23:52:15 +00:00
val isTv = isTvSettings()
2022-08-05 23:41:35 +00:00
result_season_button?.isFocusableInTouchMode = isTv
result_episode_select?.isFocusableInTouchMode = isTv
result_dub_select?.isFocusableInTouchMode = isTv
2022-04-02 01:38:55 +00:00
context?.let { ctx ->
val arrayAdapter = ArrayAdapter<String>(ctx, R.layout.sort_bottom_single_choice)
/*
-1 -> None
0 -> Watching
1 -> Completed
2 -> OnHold
3 -> Dropped
4 -> PlanToWatch
5 -> ReWatching
*/
val items = listOf(
R.string.none,
R.string.type_watching,
R.string.type_completed,
R.string.type_on_hold,
R.string.type_dropped,
R.string.type_plan_to_watch,
R.string.type_re_watching
).map { ctx.getString(it) }
arrayAdapter.addAll(items)
result_sync_check?.choiceMode = AbsListView.CHOICE_MODE_SINGLE
result_sync_check?.adapter = arrayAdapter
UIHelper.setListViewHeightBasedOnItems(result_sync_check)
result_sync_check?.setOnItemClickListener { _, _, which, _ ->
syncModel.setStatus(which - 1)
}
result_sync_rating?.addOnChangeListener { _, value, _ ->
syncModel.setScore(value.toInt())
}
result_sync_add_episode?.setOnClickListener {
syncModel.setEpisodesDelta(1)
}
result_sync_sub_episode?.setOnClickListener {
syncModel.setEpisodesDelta(-1)
}
2022-04-02 22:06:35 +00:00
result_sync_current_episodes?.doOnTextChanged { text, _, before, count ->
if (count == before) return@doOnTextChanged
2022-04-02 01:38:55 +00:00
text?.toString()?.toIntOrNull()?.let { ep ->
syncModel.setEpisodes(ep)
}
}
}
2022-04-02 22:06:35 +00:00
observe(syncModel.synced) { list ->
result_sync_names?.text =
list.filter { it.isSynced && it.hasAccount }.joinToString { it.name }
2022-04-03 01:13:02 +00:00
2022-04-22 18:53:58 +00:00
val newList = list.filter { it.isSynced && it.hasAccount }
2022-04-08 19:38:19 +00:00
2022-04-03 01:13:02 +00:00
result_mini_sync?.isVisible = newList.isNotEmpty()
(result_mini_sync?.adapter as? ImageAdapter?)?.updateList(newList.mapNotNull { it.icon })
2022-04-02 22:06:35 +00:00
}
2022-04-03 01:13:02 +00:00
var currentSyncProgress = 0
2022-04-03 15:00:50 +00:00
fun setSyncMaxEpisodes(totalEpisodes: Int?) {
result_sync_episodes?.max = (totalEpisodes ?: 0) * 1000
normalSafeApiCall {
val ctx = result_sync_max_episodes?.context
result_sync_max_episodes?.text =
totalEpisodes?.let { episodes ->
ctx?.getString(R.string.sync_total_episodes_some)?.format(episodes)
} ?: run {
ctx?.getString(R.string.sync_total_episodes_none)
}
}
}
2022-04-02 01:38:55 +00:00
observe(syncModel.metadata) { meta ->
when (meta) {
is Resource.Success -> {
val d = meta.value
2022-04-03 01:13:02 +00:00
result_sync_episodes?.progress = currentSyncProgress * 1000
2022-04-03 15:00:50 +00:00
setSyncMaxEpisodes(d.totalEpisodes)
2022-08-03 01:02:08 +00:00
2022-08-02 00:43:42 +00:00
viewModel.setMeta(d, syncModel.getSyncs())
2022-04-02 01:38:55 +00:00
}
is Resource.Loading -> {
result_sync_max_episodes?.text =
result_sync_max_episodes?.context?.getString(R.string.sync_total_episodes_none)
}
else -> {}
}
}
observe(syncModel.userData) { status ->
2022-04-01 20:05:34 +00:00
var closed = false
when (status) {
is Resource.Failure -> {
result_sync_loading_shimmer?.stopShimmer()
result_sync_loading_shimmer?.isVisible = false
result_sync_holder?.isVisible = false
2022-04-03 21:41:28 +00:00
closed = true
2022-04-01 20:05:34 +00:00
}
is Resource.Loading -> {
result_sync_loading_shimmer?.startShimmer()
result_sync_loading_shimmer?.isVisible = true
result_sync_holder?.isVisible = false
}
is Resource.Success -> {
result_sync_loading_shimmer?.stopShimmer()
result_sync_loading_shimmer?.isVisible = false
result_sync_holder?.isVisible = true
val d = status.value
result_sync_rating?.value = d.score?.toFloat() ?: 0.0f
2022-04-02 01:38:55 +00:00
result_sync_check?.setItemChecked(d.status + 1, true)
val watchedEpisodes = d.watchedEpisodes ?: 0
2022-04-03 01:13:02 +00:00
currentSyncProgress = watchedEpisodes
2022-04-03 15:00:50 +00:00
d.maxEpisodes?.let {
// don't directly call it because we don't want to override metadata observe
setSyncMaxEpisodes(it)
}
2022-04-02 01:38:55 +00:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
result_sync_episodes?.setProgress(watchedEpisodes * 1000, true)
} else {
result_sync_episodes?.progress = watchedEpisodes * 1000
}
result_sync_current_episodes?.text =
Editable.Factory.getInstance()?.newEditable(watchedEpisodes.toString())
normalSafeApiCall { // format might fail
context?.getString(R.string.sync_score_format)?.format(d.score ?: 0)?.let {
result_sync_score_text?.text = it
}
}
2022-04-01 20:05:34 +00:00
}
null -> {
closed = false
}
}
result_overlapping_panels?.setStartPanelLockState(if (closed) OverlappingPanelsLayout.LockState.CLOSE else OverlappingPanelsLayout.LockState.UNLOCKED)
}
2022-08-03 17:27:49 +00:00
observe(viewModel.resumeWatching) { resume ->
when (resume) {
is Some.Success -> {
result_resume_parent?.isVisible = true
val value = resume.value
value.progress?.let { progress ->
result_resume_series_title?.apply {
isVisible = !value.isMovie
text =
if (value.isMovie) null else activity?.getNameFull(
value.result.name,
value.result.episode,
value.result.season
)
2022-02-04 14:46:48 +00:00
}
2022-08-03 17:27:49 +00:00
result_resume_series_progress_text.setText(progress.progressLeft)
2022-02-04 14:46:48 +00:00
result_resume_series_progress?.apply {
2022-08-03 17:27:49 +00:00
isVisible = true
this.max = progress.maxProgress
this.progress = progress.progress
2022-02-04 14:46:48 +00:00
}
2022-08-03 17:27:49 +00:00
result_resume_progress_holder?.isVisible = true
} ?: run {
result_resume_progress_holder?.isVisible = false
result_resume_series_progress?.isVisible = false
result_resume_series_title?.isVisible = false
result_resume_series_progress_text?.isVisible = false
2022-02-04 14:46:48 +00:00
}
2022-02-06 15:10:45 +00:00
2022-08-03 17:27:49 +00:00
result_resume_series_button?.isVisible = !value.isMovie
2022-08-04 22:26:33 +00:00
result_resume_series_button_play?.isVisible = !value.isMovie
2022-08-04 22:26:33 +00:00
val click = View.OnClickListener {
2022-08-03 17:27:49 +00:00
viewModel.handleAction(
activity,
2022-05-14 17:07:34 +00:00
EpisodeClickEvent(
2022-08-03 17:27:49 +00:00
ACTION_PLAY_EPISODE_IN_PLAYER, value.result
2022-05-14 17:07:34 +00:00
)
)
2021-08-25 15:28:25 +00:00
}
2022-08-04 22:26:33 +00:00
result_resume_series_button?.setOnClickListener(click)
result_resume_series_button_play?.setOnClickListener(click)
2021-08-25 15:28:25 +00:00
}
2022-08-03 17:27:49 +00:00
is Some.None -> {
result_resume_parent?.isVisible = false
}
2021-07-29 15:16:08 +00:00
}
}
2022-08-03 17:27:49 +00:00
2022-08-01 01:00:48 +00:00
observe(viewModel.episodes) { episodes ->
2022-08-05 23:41:35 +00:00
updateEpisodes(episodes)
2021-05-22 22:25:56 +00:00
}
2022-08-01 02:46:43 +00:00
result_cast_items?.setOnFocusChangeListener { _, hasFocus ->
// Always escape focus
if (hasFocus) result_bookmark_button?.requestFocus()
}
2022-04-01 20:05:34 +00:00
result_sync_set_score?.setOnClickListener {
2022-04-02 01:38:55 +00:00
syncModel.publishUserData()
2022-04-01 20:05:34 +00:00
}
2022-08-01 02:46:43 +00:00
observe(viewModel.trailers) { trailers ->
setTrailers(trailers.flatMap { it.mirros }) // I dont care about subtitles yet!
}
observe(viewModel.recommendations) { recommendations ->
setRecommendations(recommendations, null)
2021-07-17 14:14:25 +00:00
}
2022-08-03 00:04:03 +00:00
observe(viewModel.movie) { data ->
2022-08-05 23:41:35 +00:00
updateMovie(data)
2022-08-03 00:04:03 +00:00
}
2022-08-01 01:00:48 +00:00
observe(viewModel.page) { data ->
2021-05-18 13:43:32 +00:00
when (data) {
is Resource.Success -> {
val d = data.value
2021-09-19 22:36:32 +00:00
2022-04-03 01:13:02 +00:00
updateVisStatus(2)
2021-06-16 16:54:07 +00:00
2022-08-01 01:00:48 +00:00
result_vpn.setText(d.vpnText)
result_info.setText(d.metaText)
result_no_episodes.setText(d.noEpisodesFoundText)
result_title.setText(d.titleText)
result_meta_site.setText(d.apiName)
result_meta_type.setText(d.typeText)
result_meta_year.setText(d.yearText)
result_meta_duration.setText(d.durationText)
result_meta_rating.setText(d.ratingText)
result_cast_text.setText(d.actorsText)
2022-08-01 02:46:43 +00:00
result_next_airing.setText(d.nextAiringEpisode)
2022-08-01 01:00:48 +00:00
result_next_airing_time.setText(d.nextAiringDate)
result_poster.setImage(d.posterImage)
2022-08-03 00:04:03 +00:00
2022-08-28 23:52:15 +00:00
if (d.posterImage != null && !isTrueTvSettings())
2022-08-03 00:04:03 +00:00
result_poster_holder?.setOnClickListener {
try {
context?.let { ctx ->
runBlocking {
val sourceBuilder = AlertDialog.Builder(ctx)
sourceBuilder.setView(R.layout.result_poster)
val sourceDialog = sourceBuilder.create()
sourceDialog.show()
sourceDialog.findViewById<ImageView?>(R.id.imgPoster)
?.apply {
setImage(d.posterImage)
setOnClickListener {
sourceDialog.dismissSafe()
}
}
}
}
} catch (e: Exception) {
logError(e)
}
}
2022-08-01 01:00:48 +00:00
2021-06-14 00:00:29 +00:00
2022-08-01 01:00:48 +00:00
result_cast_items?.isVisible = d.actors != null
(result_cast_items?.adapter as ActorAdaptor?)?.apply {
updateList(d.actors ?: emptyList())
}
2021-06-10 15:15:14 +00:00
2022-08-06 18:36:45 +00:00
result_open_in_browser?.isVisible = d.url.startsWith("http")
2022-04-03 01:13:02 +00:00
result_open_in_browser?.setOnClickListener {
val i = Intent(ACTION_VIEW)
i.data = Uri.parse(d.url)
try {
startActivity(i)
} catch (e: Exception) {
logError(e)
2021-06-10 15:15:14 +00:00
}
2022-04-03 01:13:02 +00:00
}
2021-06-10 15:15:14 +00:00
2022-04-03 01:13:02 +00:00
result_search?.setOnClickListener {
2022-08-01 01:00:48 +00:00
QuickSearchFragment.pushSearch(activity, d.title)
2022-04-03 01:13:02 +00:00
}
2021-11-20 00:41:37 +00:00
2022-04-03 01:13:02 +00:00
result_share?.setOnClickListener {
try {
val i = Intent(ACTION_SEND)
i.type = "text/plain"
2022-08-01 01:00:48 +00:00
i.putExtra(EXTRA_SUBJECT, d.title)
2022-04-03 01:13:02 +00:00
i.putExtra(EXTRA_TEXT, d.url)
2022-08-01 01:00:48 +00:00
startActivity(createChooser(i, d.title))
2022-04-03 01:13:02 +00:00
} catch (e: Exception) {
logError(e)
2021-06-10 15:15:14 +00:00
}
2022-04-03 01:13:02 +00:00
}
2021-06-10 15:15:14 +00:00
2022-05-15 18:38:32 +00:00
if (syncModel.addSyncs(d.syncData)) {
syncModel.updateMetaAndUser()
syncModel.updateSynced()
} else {
syncModel.addFromUrl(d.url)
2022-04-03 21:41:28 +00:00
}
2022-08-02 00:43:42 +00:00
result_description.setTextHtml(d.plotText)
2022-08-06 16:08:20 +00:00
if (this !is ResultFragmentTv) // dont want this clickable on tv layout
result_description?.setOnClickListener { view ->
view.context?.let { ctx ->
val builder: AlertDialog.Builder =
AlertDialog.Builder(ctx, R.style.AlertDialogCustom)
builder.setMessage(d.plotText.asString(ctx).html())
.setTitle(d.plotHeaderText.asString(ctx))
.show()
}
2022-04-03 01:13:02 +00:00
}
2021-05-22 22:25:56 +00:00
2022-08-01 02:46:43 +00:00
2022-04-03 01:13:02 +00:00
result_tag?.removeAllViews()
2021-06-06 18:06:01 +00:00
2022-04-03 01:13:02 +00:00
d.comingSoon.let { soon ->
result_coming_soon?.isVisible = soon
result_data_holder?.isGone = soon
}
2022-03-20 23:59:17 +00:00
2022-04-03 01:13:02 +00:00
val tags = d.tags
2022-08-01 01:00:48 +00:00
result_tag_holder?.isVisible = tags.isNotEmpty()
2022-08-01 02:46:43 +00:00
if (tags.isNotEmpty()) {
2022-04-03 01:13:02 +00:00
//result_tag_holder?.visibility = VISIBLE
2022-08-28 23:52:15 +00:00
val isOnTv = isTrueTvSettings()
2022-04-03 01:13:02 +00:00
for ((index, tag) in tags.withIndex()) {
val viewBtt = layoutInflater.inflate(R.layout.result_tag, null)
val btt = viewBtt.findViewById<MaterialButton>(R.id.result_tag_card)
btt.text = tag
2022-04-03 21:41:28 +00:00
btt.isFocusable = !isOnTv
btt.isClickable = !isOnTv
2022-04-03 01:13:02 +00:00
result_tag?.addView(viewBtt, index)
2021-06-26 14:44:53 +00:00
}
2022-04-03 01:13:02 +00:00
}
2021-05-18 13:43:32 +00:00
}
is Resource.Failure -> {
result_error_text.text = storedData?.url?.plus("\n") + data.errorString
2021-06-16 16:54:07 +00:00
updateVisStatus(1)
}
is Resource.Loading -> {
updateVisStatus(0)
2021-05-18 13:43:32 +00:00
}
}
}
2021-09-19 22:36:32 +00:00
context?.let { ctx ->
2022-08-04 01:19:59 +00:00
2022-08-20 01:06:35 +00:00
//result_bookmark_button?.isVisible = ctx.isTvSettings()
2021-12-10 19:48:21 +00:00
2021-09-19 22:36:32 +00:00
val settingsManager = PreferenceManager.getDefaultSharedPreferences(ctx)
2021-06-16 16:54:07 +00:00
Kitsu.isEnabled =
settingsManager.getBoolean(ctx.getString(R.string.show_kitsu_posters_key), true)
if (storedData?.url != null) {
2021-09-19 22:36:32 +00:00
result_reload_connectionerror.setOnClickListener {
viewModel.load(
activity,
storedData.url,
storedData.apiName,
storedData.showFillers,
storedData.dubStatus,
storedData.start
)
2021-08-29 17:15:09 +00:00
}
2021-06-16 16:54:07 +00:00
result_reload_connection_open_in_browser?.setOnClickListener {
val i = Intent(ACTION_VIEW)
i.data = Uri.parse(storedData.url)
2022-03-14 12:44:54 +00:00
try {
startActivity(i)
} catch (e: Exception) {
logError(e)
}
}
result_open_in_browser?.isVisible = storedData.url.startsWith("http")
2022-03-14 12:44:54 +00:00
result_open_in_browser?.setOnClickListener {
val i = Intent(ACTION_VIEW)
i.data = Uri.parse(storedData.url)
try {
startActivity(i)
} catch (e: Exception) {
2022-01-30 22:02:57 +00:00
logError(e)
}
}
2022-04-03 21:41:28 +00:00
// bloats the navigation on tv
2022-08-28 23:52:15 +00:00
if (!isTrueTvSettings()) {
2022-04-03 21:41:28 +00:00
result_meta_site?.setOnClickListener {
it.context?.openBrowser(storedData.url)
2022-04-03 21:41:28 +00:00
}
result_meta_site?.isFocusable = true
} else {
result_meta_site?.isFocusable = false
2021-09-19 22:36:32 +00:00
}
2022-08-01 02:46:43 +00:00
if (restart || !viewModel.hasLoaded()) {
2022-01-07 19:27:25 +00:00
//viewModel.clear()
viewModel.load(
activity,
storedData.url,
storedData.apiName,
storedData.showFillers,
storedData.dubStatus,
storedData.start
)
2021-09-12 15:57:07 +00:00
}
}
2021-06-16 16:54:07 +00:00
}
2022-02-04 20:49:35 +00:00
}
}