AquaStream/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt

652 lines
27 KiB
Kotlin
Raw Normal View History

2021-04-30 17:20:15 +00:00
package com.lagradost.cloudstream3.ui.home
2021-07-29 13:39:57 +00:00
import android.annotation.SuppressLint
2021-08-12 00:04:58 +00:00
import android.app.Activity
2021-12-26 00:05:10 +00:00
import android.content.Context
2021-07-29 15:16:08 +00:00
import android.content.Intent
2021-07-29 01:46:10 +00:00
import android.content.res.Configuration
2021-07-29 15:16:08 +00:00
import android.net.Uri
2021-04-30 17:20:15 +00:00
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2021-12-26 00:05:10 +00:00
import android.widget.*
import androidx.core.view.isVisible
2021-12-26 00:05:10 +00:00
import androidx.core.widget.NestedScrollView
2021-04-30 17:20:15 +00:00
import androidx.fragment.app.Fragment
2021-11-05 21:39:56 +00:00
import androidx.fragment.app.activityViewModels
2021-07-29 00:19:42 +00:00
import androidx.recyclerview.widget.GridLayoutManager
2021-10-22 13:23:48 +00:00
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSnapHelper
2021-07-29 00:19:42 +00:00
import androidx.recyclerview.widget.RecyclerView
2021-07-29 01:46:10 +00:00
import com.google.android.material.bottomsheet.BottomSheetDialog
2021-12-26 00:05:10 +00:00
import com.google.android.material.button.MaterialButton
2021-07-30 23:41:54 +00:00
import com.lagradost.cloudstream3.*
2021-07-29 15:16:08 +00:00
import com.lagradost.cloudstream3.APIHolder.apis
import com.lagradost.cloudstream3.APIHolder.filterProviderByPreferredMedia
2021-10-22 13:23:48 +00:00
import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull
import com.lagradost.cloudstream3.AcraApplication.Companion.getKey
import com.lagradost.cloudstream3.AcraApplication.Companion.setKey
2021-07-29 00:19:42 +00:00
import com.lagradost.cloudstream3.mvvm.Resource
2021-10-10 22:25:49 +00:00
import com.lagradost.cloudstream3.mvvm.logError
2021-07-29 00:19:42 +00:00
import com.lagradost.cloudstream3.mvvm.observe
2021-11-12 16:55:54 +00:00
import com.lagradost.cloudstream3.syncproviders.OAuth2API
2021-09-05 11:56:25 +00:00
import com.lagradost.cloudstream3.ui.APIRepository.Companion.noneApi
import com.lagradost.cloudstream3.ui.APIRepository.Companion.randomApi
2021-07-29 01:46:10 +00:00
import com.lagradost.cloudstream3.ui.AutofitRecyclerView
2021-07-30 23:41:54 +00:00
import com.lagradost.cloudstream3.ui.WatchType
2021-07-29 15:16:08 +00:00
import com.lagradost.cloudstream3.ui.result.START_ACTION_RESUME_LATEST
2021-08-25 15:28:25 +00:00
import com.lagradost.cloudstream3.ui.search.*
2021-09-12 14:10:22 +00:00
import com.lagradost.cloudstream3.ui.search.SearchFragment.Companion.filterSearchResponse
2021-07-30 23:41:54 +00:00
import com.lagradost.cloudstream3.ui.search.SearchHelper.handleSearchClickCallback
2021-11-28 12:18:01 +00:00
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings
import com.lagradost.cloudstream3.utils.AppUtils.loadSearchResult
2021-07-29 00:19:42 +00:00
import com.lagradost.cloudstream3.utils.DataStore.getKey
import com.lagradost.cloudstream3.utils.DataStore.setKey
2021-08-25 15:28:25 +00:00
import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.DataStoreHelper.removeLastWatched
2021-07-30 23:41:54 +00:00
import com.lagradost.cloudstream3.utils.DataStoreHelper.setResultWatchState
2021-07-29 01:46:10 +00:00
import com.lagradost.cloudstream3.utils.Event
2021-07-29 00:19:42 +00:00
import com.lagradost.cloudstream3.utils.HOMEPAGE_API
2021-12-12 02:33:17 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
2021-08-19 20:05:18 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar
2021-10-22 13:23:48 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbarView
2021-12-13 18:41:33 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.getSpanCount
2021-07-30 23:41:54 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.popupMenuNoIcons
2021-08-19 20:05:18 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.popupMenuNoIconsAndNoStringRes
2021-11-07 22:10:19 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.setImage
2021-10-22 13:23:48 +00:00
import com.lagradost.cloudstream3.widget.CenterZoomLayoutManager
2021-07-29 00:19:42 +00:00
import kotlinx.android.synthetic.main.fragment_home.*
2021-12-09 21:20:27 +00:00
import java.util.*
2021-04-30 17:20:15 +00:00
2021-12-09 21:20:27 +00:00
const val HOME_BOOKMARK_VALUE_LIST = "home_bookmarked_last_list"
2021-12-26 00:05:10 +00:00
const val HOME_PREF_HOMEPAGE = "home_pref_homepage"
2021-07-30 23:41:54 +00:00
2021-04-30 17:20:15 +00:00
class HomeFragment : Fragment() {
2021-08-12 00:04:58 +00:00
companion object {
val configEvent = Event<Int>()
var currentSpan = 1
fun Activity.loadHomepageList(item: HomePageList) {
val context = this
val bottomSheetDialogBuilder = BottomSheetDialog(context)
bottomSheetDialogBuilder.setContentView(R.layout.home_episodes_expanded)
val title = bottomSheetDialogBuilder.findViewById<TextView>(R.id.home_expanded_text)!!
title.text = item.name
val recycle = bottomSheetDialogBuilder.findViewById<AutofitRecyclerView>(R.id.home_expanded_recycler)!!
val titleHolder = bottomSheetDialogBuilder.findViewById<FrameLayout>(R.id.home_expanded_drag_down)!!
titleHolder.setOnClickListener {
2021-12-12 02:33:17 +00:00
bottomSheetDialogBuilder.dismissSafe(this)
2021-08-12 00:04:58 +00:00
}
// Span settings
recycle.spanCount = currentSpan
recycle.adapter = SearchAdapter(item.list, recycle) { callback ->
handleSearchClickCallback(this, callback)
2021-08-25 15:28:25 +00:00
if (callback.action == SEARCH_ACTION_LOAD || callback.action == SEARCH_ACTION_PLAY_FILE) {
2021-12-12 02:33:17 +00:00
bottomSheetDialogBuilder.dismissSafe(this)
2021-08-12 00:04:58 +00:00
}
}
val spanListener = { span: Int ->
recycle.spanCount = span
(recycle.adapter as SearchAdapter).notifyDataSetChanged()
}
configEvent += spanListener
bottomSheetDialogBuilder.setOnDismissListener {
configEvent -= spanListener
}
(recycle.adapter as SearchAdapter).notifyDataSetChanged()
bottomSheetDialogBuilder.show()
}
2021-12-26 00:05:10 +00:00
fun Context.selectHomepage(selectedApiName: String?, callback: (String) -> Unit) {
val validAPIs = filterProviderByPreferredMedia().toMutableList()
validAPIs.add(0, randomApi)
validAPIs.add(0, noneApi)
//val builder: AlertDialog.Builder = AlertDialog.Builder(this)
//builder.setView(R.layout.home_select_mainpage)
val builder =
BottomSheetDialog(this)
builder.setContentView(R.layout.home_select_mainpage)
builder.show()
builder.let { dialog ->
//dialog.window?.setGravity(Gravity.BOTTOM)
var currentApiName = selectedApiName
var currentValidApis: MutableList<MainAPI> = mutableListOf()
val preSelectedTypes = this.getKey<List<String>>(HOME_PREF_HOMEPAGE)
?.mapNotNull { listName -> TvType.values().firstOrNull { it.name == listName } }?.toMutableList()
?: mutableListOf(TvType.Movie, TvType.TvSeries)
val anime = dialog.findViewById<MaterialButton>(R.id.home_select_anime)
val cartoons = dialog.findViewById<MaterialButton>(R.id.home_select_cartoons)
val tvs = dialog.findViewById<MaterialButton>(R.id.home_select_tv_series)
val docs = dialog.findViewById<MaterialButton>(R.id.home_select_documentaries)
val movies = dialog.findViewById<MaterialButton>(R.id.home_select_movies)
val cancelBtt = dialog.findViewById<MaterialButton>(R.id.cancel_btt)
val applyBtt = dialog.findViewById<MaterialButton>(R.id.apply_btt)
cancelBtt?.setOnClickListener {
dialog.dismissSafe()
}
applyBtt?.setOnClickListener {
if (currentApiName != selectedApiName) {
currentApiName?.let(callback)
}
dialog.dismissSafe()
}
val listView = dialog.findViewById<ListView>(R.id.listview1)
val arrayAdapter = ArrayAdapter<String>(this, R.layout.sort_bottom_single_choice)
listView?.adapter = arrayAdapter
listView?.choiceMode = AbsListView.CHOICE_MODE_SINGLE
listView?.setOnItemClickListener { _, _, i, _ ->
if (!currentValidApis.isNullOrEmpty()) {
currentApiName = currentValidApis[i].name
//to switch to apply simply remove this
currentApiName?.let(callback)
dialog.dismissSafe()
}
}
val pairList = listOf(
Pair(anime, listOf(TvType.Anime, TvType.ONA, TvType.AnimeMovie)),
Pair(cartoons, listOf(TvType.Cartoon)),
Pair(tvs, listOf(TvType.TvSeries)),
Pair(docs, listOf(TvType.Documentary)),
Pair(movies, listOf(TvType.Movie, TvType.Torrent))
)
fun updateList() {
this.setKey(HOME_PREF_HOMEPAGE, preSelectedTypes)
arrayAdapter.clear()
currentValidApis = validAPIs.filter { api ->
api.hasMainPage && api.supportedTypes.any {
preSelectedTypes.contains(it)
}
}.toMutableList()
currentValidApis.addAll(0, validAPIs.subList(0, 2))
val names = currentValidApis.map { it.name }
val index = names.indexOf(currentApiName)
println("INDEX: $index")
listView?.setItemChecked(index, true)
arrayAdapter.notifyDataSetChanged()
arrayAdapter.addAll(names)
arrayAdapter.notifyDataSetChanged()
}
for ((button, validTypes) in pairList) {
val isValid = validAPIs.any { api -> validTypes.any { api.supportedTypes.contains(it) } }
button?.isVisible = isValid
if (isValid) {
fun buttonContains(): Boolean {
return preSelectedTypes.any { validTypes.contains(it) }
}
button?.isSelected = buttonContains()
button?.setOnClickListener {
preSelectedTypes.clear()
preSelectedTypes.addAll(validTypes)
for ((otherButton, _) in pairList) {
otherButton?.isSelected = false
}
button.isSelected = true
updateList()
}
button?.setOnLongClickListener {
if (!buttonContains()) {
button.isSelected = true
preSelectedTypes.addAll(validTypes)
} else {
button.isSelected = false
preSelectedTypes.removeAll(validTypes)
}
updateList()
return@setOnLongClickListener true
}
}
}
updateList()
}
}
2021-08-12 00:04:58 +00:00
}
2021-11-05 21:39:56 +00:00
private val homeViewModel: HomeViewModel by activityViewModels()
2021-04-30 17:20:15 +00:00
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
2021-11-05 21:39:56 +00:00
//homeViewModel =
// ViewModelProvider(this).get(HomeViewModel::class.java)
2021-07-29 00:19:42 +00:00
return inflater.inflate(R.layout.fragment_home, container, false)
}
2021-07-29 13:39:57 +00:00
private var currentHomePage: HomePageResponse? = null
private fun toggleMainVisibility(visible: Boolean) {
home_main_holder.isVisible = visible
2021-07-29 13:39:57 +00:00
}
2021-07-29 01:46:10 +00:00
private fun fixGrid() {
2021-12-13 18:41:33 +00:00
activity?.getSpanCount()?.let {
currentSpan = it
2021-07-29 01:46:10 +00:00
}
configEvent.invoke(currentSpan)
}
2021-07-29 15:16:08 +00:00
private val apiChangeClickListener = View.OnClickListener { view ->
2021-12-26 00:05:10 +00:00
view.context.selectHomepage(currentApiName) { api ->
homeViewModel.loadAndCancel(api)
}
/*val validAPIs = view.context?.filterProviderByPreferredMedia()?.toMutableList() ?: mutableListOf()
2021-09-05 11:56:25 +00:00
validAPIs.add(0, randomApi)
validAPIs.add(0, noneApi)
2021-07-29 15:16:08 +00:00
view.popupMenuNoIconsAndNoStringRes(validAPIs.mapIndexed { index, api -> Pair(index, api.name) }) {
homeViewModel.loadAndCancel(validAPIs[itemId].name)
2021-12-26 00:05:10 +00:00
}*/
2021-07-29 15:16:08 +00:00
}
2021-07-29 01:46:10 +00:00
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
fixGrid()
}
2021-07-30 23:41:54 +00:00
override fun onResume() {
super.onResume()
2021-09-25 13:00:59 +00:00
reloadStored()
2021-07-30 23:41:54 +00:00
}
2021-09-25 13:00:59 +00:00
/*
2021-07-30 23:41:54 +00:00
override fun onStop() {
backEvent -= ::handleBack
super.onStop()
2021-09-25 13:00:59 +00:00
}*/
2021-07-30 23:41:54 +00:00
private fun reloadStored() {
homeViewModel.loadResumeWatching()
val list = EnumSet.noneOf(WatchType::class.java)
getKey<IntArray>(HOME_BOOKMARK_VALUE_LIST)?.map { WatchType.fromInternalId(it) }?.let {
list.addAll(it)
2021-07-30 23:41:54 +00:00
}
homeViewModel.loadStoredData(list)
2021-07-30 23:41:54 +00:00
}
2021-09-25 13:00:59 +00:00
/*private fun handleBack(poppedFragment: Boolean) {
2021-07-30 23:41:54 +00:00
if (poppedFragment) {
reloadStored()
}
2021-09-25 13:00:59 +00:00
}*/
2021-07-30 23:41:54 +00:00
2021-12-26 00:05:10 +00:00
var currentApiName: String? = null
2021-10-22 13:23:48 +00:00
@SuppressLint("SetTextI18n")
2021-07-29 00:19:42 +00:00
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2021-07-29 01:46:10 +00:00
fixGrid()
2021-07-29 00:19:42 +00:00
2022-01-14 18:14:24 +00:00
home_change_api?.setOnClickListener(apiChangeClickListener)
home_change_api_loading?.setOnClickListener(apiChangeClickListener)
home_api_fab?.setOnClickListener(apiChangeClickListener)
2021-07-29 15:16:08 +00:00
2021-10-22 13:23:48 +00:00
observe(homeViewModel.apiName) { apiName ->
2021-12-26 00:05:10 +00:00
currentApiName = apiName
setKey(HOMEPAGE_API, apiName)
2021-10-22 13:23:48 +00:00
home_provider_name?.text = apiName
home_provider_meta_info?.isVisible = false
getApiFromNameNull(apiName)?.let { currentApi ->
val typeChoices = listOf(
Pair(R.string.movies, listOf(TvType.Movie)),
Pair(R.string.tv_series, listOf(TvType.TvSeries)),
Pair(R.string.documentaries, listOf(TvType.Documentary)),
2021-10-22 13:23:48 +00:00
Pair(R.string.cartoons, listOf(TvType.Cartoon)),
Pair(R.string.anime, listOf(TvType.Anime, TvType.ONA, TvType.AnimeMovie)),
Pair(R.string.torrent, listOf(TvType.Torrent)),
).filter { item -> currentApi.supportedTypes.any { type -> item.second.contains(type) } }
2021-10-27 19:45:15 +00:00
home_provider_meta_info?.text = typeChoices.joinToString(separator = ", ") { getString(it.first) }
2021-10-22 13:23:48 +00:00
home_provider_meta_info?.isVisible = true
}
2021-07-29 00:19:42 +00:00
}
observe(homeViewModel.randomItems) { items ->
if (items.isNullOrEmpty()) {
toggleMainVisibility(false)
} else {
val tempAdapter = home_main_poster_recyclerview.adapter as HomeChildItemAdapter?
// no need to reload if it has the same data
if (tempAdapter != null && tempAdapter.cardList == items) {
toggleMainVisibility(true)
return@observe
}
val randomSize = items.size
home_main_poster_recyclerview.adapter =
2021-11-28 12:18:01 +00:00
HomeChildItemAdapter(
items,
R.layout.home_result_big_grid,
nextFocusUp = home_main_poster_recyclerview.nextFocusUpId,
nextFocusDown = home_main_poster_recyclerview.nextFocusDownId
) { callback ->
handleSearchClickCallback(activity, callback)
}
2021-12-04 22:40:24 +00:00
home_main_poster_recyclerview?.post {
(home_main_poster_recyclerview?.layoutManager as CenterZoomLayoutManager?)?.let { manager ->
manager.updateSize(forceUpdate = true)
if (randomSize > 2) {
manager.scrollToPosition(randomSize / 2)
manager.snap { dx ->
home_main_poster_recyclerview?.post {
// this is the best I can do, fuck android for not including instant scroll
home_main_poster_recyclerview?.smoothScrollBy(dx, 0)
}
}
}
}
}
toggleMainVisibility(true)
}
}
2021-07-29 15:16:08 +00:00
observe(homeViewModel.page) { data ->
when (data) {
2021-07-29 00:19:42 +00:00
is Resource.Success -> {
2021-12-10 23:57:11 +00:00
home_loading_shimmer?.stopShimmer()
2021-07-29 15:16:08 +00:00
val d = data.value
2021-09-12 14:10:22 +00:00
2021-07-29 13:39:57 +00:00
currentHomePage = d
2021-09-12 14:10:22 +00:00
(home_master_recycler?.adapter as ParentItemAdapter?)?.items =
2021-12-11 00:26:54 +00:00
d?.items?.mapNotNull {
2021-10-10 22:25:49 +00:00
try {
HomePageList(it.name, it.list.filterSearchResponse())
2021-10-22 13:23:48 +00:00
} catch (e: Exception) {
2021-10-10 22:25:49 +00:00
logError(e)
null
}
2021-12-11 00:26:54 +00:00
} ?: listOf()
2021-09-12 14:10:22 +00:00
2021-07-29 00:19:42 +00:00
home_master_recycler?.adapter?.notifyDataSetChanged()
2021-07-29 15:16:08 +00:00
2021-12-10 23:57:11 +00:00
home_loading?.isVisible = false
home_loading_error?.isVisible = false
home_loaded?.isVisible = true
2021-07-29 00:19:42 +00:00
}
is Resource.Failure -> {
2021-12-10 23:57:11 +00:00
home_loading_shimmer?.stopShimmer()
2021-07-29 15:16:08 +00:00
result_error_text.text = data.errorString
home_reload_connectionerror.setOnClickListener(apiChangeClickListener)
home_reload_connection_open_in_browser.setOnClickListener { view ->
val validAPIs = apis//.filter { api -> api.hasMainPage }
view.popupMenuNoIconsAndNoStringRes(validAPIs.mapIndexed { index, api ->
Pair(
index,
api.name
)
}) {
val i = Intent(Intent.ACTION_VIEW)
i.data = Uri.parse(validAPIs[itemId].mainUrl)
startActivity(i)
}
}
2021-07-29 00:19:42 +00:00
2021-12-10 23:57:11 +00:00
home_loading?.isVisible = false
home_loading_error?.isVisible = true
home_loaded?.isVisible = false
2021-07-29 00:19:42 +00:00
}
is Resource.Loading -> {
2021-12-10 23:57:11 +00:00
home_loading_shimmer?.startShimmer()
home_loading?.isVisible = true
home_loading_error?.isVisible = false
home_loaded?.isVisible = false
2021-07-29 00:19:42 +00:00
}
}
}
2021-07-30 23:41:54 +00:00
val adapter: RecyclerView.Adapter<RecyclerView.ViewHolder> = ParentItemAdapter(listOf(), { callback ->
handleSearchClickCallback(activity, callback)
}, { item ->
2021-08-12 00:04:58 +00:00
activity?.loadHomepageList(item)
2021-07-29 00:54:27 +00:00
})
2021-12-09 21:20:27 +00:00
val toggleList = listOf(
Pair(home_type_watching_btt, WatchType.WATCHING),
Pair(home_type_completed_btt, WatchType.COMPLETED),
Pair(home_type_dropped_btt, WatchType.DROPPED),
Pair(home_type_on_hold_btt, WatchType.ONHOLD),
Pair(home_plan_to_watch_btt, WatchType.PLANTOWATCH),
)
for (item in toggleList) {
val watch = item.second
2021-12-26 00:05:10 +00:00
item.first?.setOnClickListener {
homeViewModel.loadStoredData(EnumSet.of(watch))
}
item.first?.setOnLongClickListener { itemView ->
2021-12-09 21:20:27 +00:00
val list = EnumSet.noneOf(WatchType::class.java)
itemView.context.getKey<IntArray>(HOME_BOOKMARK_VALUE_LIST)?.map { WatchType.fromInternalId(it) }?.let {
list.addAll(it)
}
if (list.contains(watch)) {
list.remove(watch)
} else {
list.add(watch)
}
homeViewModel.loadStoredData(list)
2021-12-10 19:48:21 +00:00
return@setOnLongClickListener true
}
2021-12-09 21:20:27 +00:00
}
2021-07-30 23:41:54 +00:00
observe(homeViewModel.availableWatchStatusTypes) { availableWatchStatusTypes ->
2021-12-09 21:20:27 +00:00
context?.setKey(
HOME_BOOKMARK_VALUE_LIST,
availableWatchStatusTypes.first.map { it.internalId }.toIntArray()
)
for (item in toggleList) {
val watch = item.second
item.first?.apply {
isVisible = availableWatchStatusTypes.second.contains(watch)
isSelected = availableWatchStatusTypes.first.contains(watch)
}
}
/*home_bookmark_select?.setOnClickListener {
2021-07-30 23:41:54 +00:00
it.popupMenuNoIcons(availableWatchStatusTypes.second.map { type ->
Pair(
type.internalId,
type.stringRes
)
}) {
homeViewModel.loadStoredData(it.context, WatchType.fromInternalId(this.itemId))
}
}
2021-12-09 21:20:27 +00:00
home_bookmarked_parent_item_title?.text = getString(availableWatchStatusTypes.first.stringRes)*/
2021-07-30 23:41:54 +00:00
}
2021-12-09 21:20:27 +00:00
observe(homeViewModel.bookmarks) { pair ->
home_bookmarked_holder.isVisible = pair.first
val bookmarks = pair.second
2021-07-30 23:41:54 +00:00
(home_bookmarked_child_recyclerview?.adapter as HomeChildItemAdapter?)?.cardList = bookmarks
home_bookmarked_child_recyclerview?.adapter?.notifyDataSetChanged()
2021-12-09 21:20:27 +00:00
home_bookmarked_child_more_info?.setOnClickListener {
2021-08-12 00:04:58 +00:00
activity?.loadHomepageList(
2021-07-30 23:41:54 +00:00
HomePageList(
2021-12-09 21:20:27 +00:00
getString(R.string.error_bookmarks_text), //home_bookmarked_parent_item_title?.text?.toString() ?: getString(R.string.error_bookmarks_text),
2021-07-30 23:41:54 +00:00
bookmarks
)
)
}
}
2021-08-25 15:28:25 +00:00
observe(homeViewModel.resumeWatching) { resumeWatching ->
home_watch_holder?.isVisible = resumeWatching.isNotEmpty()
2021-08-25 15:28:25 +00:00
(home_watch_child_recyclerview?.adapter as HomeChildItemAdapter?)?.cardList = resumeWatching
home_watch_child_recyclerview?.adapter?.notifyDataSetChanged()
home_watch_child_more_info?.setOnClickListener {
2021-08-25 15:28:25 +00:00
activity?.loadHomepageList(
HomePageList(
home_watch_parent_item_title?.text?.toString() ?: getString(R.string.continue_watching),
resumeWatching
)
)
}
}
2021-11-28 12:18:01 +00:00
home_bookmarked_child_recyclerview.adapter = HomeChildItemAdapter(
ArrayList(),
nextFocusUp = home_bookmarked_child_recyclerview?.nextFocusUpId,
nextFocusDown = home_bookmarked_child_recyclerview?.nextFocusDownId
) { callback ->
2021-07-30 23:41:54 +00:00
if (callback.action == SEARCH_ACTION_SHOW_METADATA) {
val id = callback.card.id
if (id != null) {
callback.view.popupMenuNoIcons(listOf(Pair(0, R.string.action_remove_from_bookmarks))) {
if (itemId == 0) {
setResultWatchState(id, WatchType.NONE.internalId)
2021-07-30 23:41:54 +00:00
reloadStored()
}
}
}
} else {
handleSearchClickCallback(activity, callback)
}
}
2021-11-28 12:18:01 +00:00
home_watch_child_recyclerview.adapter = HomeChildItemAdapter(
ArrayList(),
nextFocusUp = home_watch_child_recyclerview?.nextFocusUpId,
nextFocusDown = home_watch_child_recyclerview?.nextFocusDownId
) { callback ->
2021-08-25 15:28:25 +00:00
if (callback.action == SEARCH_ACTION_SHOW_METADATA) {
val id = callback.card.id
if (id != null) {
callback.view.popupMenuNoIcons(
listOf(
Pair(1, R.string.action_open_watching),
Pair(0, R.string.action_remove_watching)
)
) {
if (itemId == 1) {
handleSearchClickCallback(
activity,
2021-12-11 21:23:36 +00:00
SearchClickCallback(SEARCH_ACTION_LOAD, callback.view, -1, callback.card)
2021-08-25 15:28:25 +00:00
)
reloadStored()
}
if (itemId == 0) {
2021-09-05 11:56:25 +00:00
val card = callback.card
if (card is DataStoreHelper.ResumeWatchingResult) {
removeLastWatched(card.parentId)
2021-08-25 15:28:25 +00:00
reloadStored()
}
}
}
}
} else {
handleSearchClickCallback(activity, callback)
}
}
2021-10-22 13:23:48 +00:00
context?.fixPaddingStatusbarView(home_statusbar)
context?.fixPaddingStatusbar(home_loading_statusbar)
2021-07-29 00:19:42 +00:00
home_master_recycler.adapter = adapter
home_master_recycler.layoutManager = GridLayoutManager(context, 1)
2021-10-22 13:23:48 +00:00
LinearSnapHelper().attachToRecyclerView(home_main_poster_recyclerview) // snap
val centerLayoutManager = CenterZoomLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
centerLayoutManager.setOnSizeListener { index ->
2021-12-04 22:40:24 +00:00
(home_main_poster_recyclerview?.adapter as HomeChildItemAdapter?)?.cardList?.get(index)?.let { random ->
2021-10-22 13:23:48 +00:00
home_main_play.setOnClickListener {
activity.loadSearchResult(random, START_ACTION_RESUME_LATEST)
}
home_main_info.setOnClickListener {
activity.loadSearchResult(random)
}
home_main_text.text =
random.name + if (random is AnimeSearchResponse && !random.dubStatus.isNullOrEmpty()) {
random.dubStatus.joinToString(prefix = "", separator = " | ") { it.name }
} else ""
}
}
2021-12-04 22:40:24 +00:00
home_main_poster_recyclerview?.layoutManager = centerLayoutManager // scale
2021-10-22 13:23:48 +00:00
2021-07-30 23:41:54 +00:00
reloadStored()
2021-09-20 21:11:36 +00:00
val apiName = context?.getKey<String>(HOMEPAGE_API)
2021-10-22 13:23:48 +00:00
if (homeViewModel.apiName.value != apiName || apiName == null) {
//println("Caught home: " + homeViewModel.apiName.value + " at " + apiName)
homeViewModel.loadAndCancel(apiName)
2021-09-20 21:11:36 +00:00
}
2021-11-07 22:10:19 +00:00
2021-12-26 00:05:10 +00:00
home_loaded.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { view, _, scrollY, _, oldScrollY ->
val dy = scrollY - oldScrollY
if (dy > 0) { //check for scroll down
home_api_fab?.hide()
} else if (dy < -5) {
if (view?.context?.isTvSettings() == false) {
home_api_fab?.show()
}
}
})
2021-11-07 22:10:19 +00:00
// nice profile pic on homepage
home_profile_picture_holder?.isVisible = false
context?.let { ctx ->
2021-11-28 12:18:01 +00:00
// just in case
if (ctx.isTvSettings()) {
2021-12-26 00:05:10 +00:00
home_api_fab?.isVisible = false
home_change_api?.isVisible = true
home_change_api_loading?.isVisible = true
2021-11-28 12:18:01 +00:00
home_change_api_loading?.isFocusable = true
home_change_api_loading?.isFocusableInTouchMode = true
home_change_api?.isFocusable = true
home_change_api?.isFocusableInTouchMode = true
2021-12-09 21:20:27 +00:00
// home_bookmark_select?.isFocusable = true
// home_bookmark_select?.isFocusableInTouchMode = true
2021-12-26 00:05:10 +00:00
} else {
home_api_fab?.isVisible = true
home_change_api?.isVisible = false
home_change_api_loading?.isVisible = false
2021-11-28 12:18:01 +00:00
}
2021-11-12 16:55:54 +00:00
for (syncApi in OAuth2API.OAuth2Apis) {
val login = syncApi.loginInfo()
2021-11-07 22:10:19 +00:00
val pic = login?.profilePicture
if (pic != null) {
2021-11-07 22:10:19 +00:00
home_profile_picture.setImage(pic)
home_profile_picture_holder.isVisible = true
break
}
}
}
2021-04-30 17:20:15 +00:00
}
}