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

330 lines
10 KiB
Kotlin
Raw Normal View History

2021-05-16 18:28:00 +00:00
package com.lagradost.cloudstream3.ui.result
import android.os.Bundle
import androidx.fragment.app.Fragment
2021-09-19 22:36:32 +00:00
import androidx.preference.PreferenceManager
2022-08-04 01:19:59 +00:00
import com.lagradost.cloudstream3.APIHolder.getApiDubstatusSettings
2022-08-04 22:26:33 +00:00
import com.lagradost.cloudstream3.DubStatus
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.SearchResponse
import com.lagradost.cloudstream3.TvType
import com.lagradost.cloudstream3.ui.result.EpisodeAdapter.Companion.getPlayerAction
import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.DataStoreHelper.getVideoWatchState
2021-06-15 16:07:20 +00:00
import com.lagradost.cloudstream3.utils.DataStoreHelper.getViewPos
import com.lagradost.cloudstream3.utils.Event
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
/**
* Future proofed way to mark episodes as watched
**/
enum class VideoWatchState {
/** Default value when no key is set */
None,
Watched
}
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,
/**
* Conveys if the episode itself is marked as watched
**/
val videoWatchState: VideoWatchState,
/** Sum of all previous season episode counts + episode */
val totalEpisodeIndex: Int? = null,
val airDate: Long? = null,
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,
totalEpisodeIndex: Int? = null,
airDate: Long? = null,
2021-06-15 16:07:20 +00:00
): ResultEpisode {
val posDur = getViewPos(id)
val videoWatchState = getVideoWatchState(id) ?: VideoWatchState.None
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,
videoWatchState,
totalEpisodeIndex,
airDate,
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
}
object ResultFragment {
private const val URL_BUNDLE = "url"
private const val API_NAME_BUNDLE = "apiName"
private const val SEASON_BUNDLE = "season"
private const val EPISODE_BUNDLE = "episode"
private const val START_ACTION_BUNDLE = "startAction"
private const val START_VALUE_BUNDLE = "startValue"
private const val RESTART_BUNDLE = "restart"
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)
2022-05-02 21:32:28 +00:00
}
putInt(START_ACTION_BUNDLE, startAction)
if (startValue != null)
2022-05-02 21:32:28 +00:00
putInt(START_VALUE_BUNDLE, startValue)
putBoolean(RESTART_BUNDLE, true)
2021-09-20 21:11:36 +00:00
}
}
2022-01-18 14:10:01 +00:00
fun newInstance(
url: String,
apiName: String,
startAction: Int = 0,
startValue: Int = 0
): Bundle {
return Bundle().apply {
putString(URL_BUNDLE, url)
putString(API_NAME_BUNDLE, apiName)
putInt(START_ACTION_BUNDLE, startAction)
putInt(START_VALUE_BUNDLE, startValue)
putBoolean(RESTART_BUNDLE, true)
2022-01-18 14:10:01 +00:00
}
}
2022-01-18 14:10:01 +00:00
fun updateUI(id: Int? = null) {
// updateUIListener?.invoke()
updateUIEvent.invoke(id)
2021-05-22 22:25:56 +00:00
}
val updateUIEvent = Event<Int?>()
//private var updateUIListener: (() -> Unit)? = null
2021-05-22 22:25:56 +00:00
2022-08-04 22:26:33 +00:00
2023-07-18 20:18:14 +00:00
//protected open val resultLayout = R.layout.fragment_result_swipe
/* override var layout = R.layout.fragment_result_swipe
2021-05-16 18:28:00 +00:00
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
2022-04-01 20:05:34 +00:00
return super.onCreateView(inflater, container, savedInstanceState)
//return inflater.inflate(resultLayout, container, false)
}
2021-05-16 18:28:00 +00:00
override fun onDestroyView() {
updateUIListener = null
super.onDestroyView()
}
2021-07-28 19:14:45 +00:00
override fun onResume() {
afterPluginsLoadedEvent += ::reloadViewModel
super.onResume()
activity?.let {
it.window?.navigationBarColor =
it.colorFromAttribute(R.attr.primaryBlackBackground)
}
}
2021-05-22 22:25:56 +00:00
override fun onDestroy() {
afterPluginsLoadedEvent -= ::reloadViewModel
super.onDestroy()
}
2021-07-29 15:16:08 +00:00
private fun updateUI() {
syncModel.updateUserData()
viewModel.reloadEpisodes()
}*/
2022-01-18 14:10:01 +00:00
data class StoredData(
val url: String,
val apiName: String,
val showFillers: Boolean,
val dubStatus: DubStatus,
val start: AutoResume?,
val playerAction: Int,
val restart : Boolean,
)
fun Fragment.getStoredData(): StoredData? {
val context = this.context ?: this.activity ?: return null
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
val url = arguments?.getString(URL_BUNDLE) ?: return null
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 playerAction = getPlayerAction(context)
val restart = arguments?.getBoolean(RESTART_BUNDLE) ?: false
if (restart) {
arguments?.putBoolean(RESTART_BUNDLE, false)
}
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, playerAction, restart)
}
/*private fun reloadViewModel(forceReload: Boolean) {
if (!viewModel.hasLoaded() || forceReload) {
val storedData = getStoredData(activity ?: context ?: return) ?: return
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-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()
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
}
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
if (storedData?.url != null) {
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
}
}
}*/
}