save state of popup menu

This commit is contained in:
reduplicated 2023-01-06 17:51:34 +01:00
parent 657971d008
commit c57fce2abc
4 changed files with 77 additions and 20 deletions

View file

@ -18,6 +18,7 @@ import androidx.core.view.isGone
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.lifecycle.*
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.bottomsheet.BottomSheetBehavior
@ -48,6 +49,8 @@ import com.lagradost.cloudstream3.utils.AppUtils.addProgramsToContinueWatching
import com.lagradost.cloudstream3.utils.AppUtils.isRecyclerScrollable import com.lagradost.cloudstream3.utils.AppUtils.isRecyclerScrollable
import com.lagradost.cloudstream3.utils.AppUtils.loadResult import com.lagradost.cloudstream3.utils.AppUtils.loadResult
import com.lagradost.cloudstream3.utils.AppUtils.loadSearchResult import com.lagradost.cloudstream3.utils.AppUtils.loadSearchResult
import com.lagradost.cloudstream3.utils.AppUtils.ownHide
import com.lagradost.cloudstream3.utils.AppUtils.ownShow
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.cloudstream3.utils.DataStore.getKey import com.lagradost.cloudstream3.utils.DataStore.getKey
import com.lagradost.cloudstream3.utils.DataStore.setKey import com.lagradost.cloudstream3.utils.DataStore.setKey
@ -104,26 +107,32 @@ class HomeFragment : Fragment() {
val errorProfilePic = errorProfilePics.random() val errorProfilePic = errorProfilePics.random()
fun Activity.loadHomepageList( //fun Activity.loadHomepageList(
item: HomePageList, // item: HomePageList,
deleteCallback: (() -> Unit)? = null, // deleteCallback: (() -> Unit)? = null,
) { //) {
loadHomepageList( // loadHomepageList(
expand = HomeViewModel.ExpandableHomepageList(item, 1, false), // expand = HomeViewModel.ExpandableHomepageList(item, 1, false),
deleteCallback = deleteCallback, // deleteCallback = deleteCallback,
expandCallback = null // expandCallback = null
) // )
} //}
// returns a BottomSheetDialog that will be hidden with OwnHidden upon hide, and must be saved to be able call ownShow in onCreateView
fun Activity.loadHomepageList( fun Activity.loadHomepageList(
expand: HomeViewModel.ExpandableHomepageList, expand: HomeViewModel.ExpandableHomepageList,
deleteCallback: (() -> Unit)? = null, deleteCallback: (() -> Unit)? = null,
expandCallback: (suspend (String) -> HomeViewModel.ExpandableHomepageList?)? = null expandCallback: (suspend (String) -> HomeViewModel.ExpandableHomepageList?)? = null,
) { dismissCallback : (() -> Unit),
): BottomSheetDialog {
val context = this val context = this
val bottomSheetDialogBuilder = BottomSheetDialog(context) val bottomSheetDialogBuilder = BottomSheetDialog(context)
bottomSheetDialogBuilder.setContentView(R.layout.home_episodes_expanded) bottomSheetDialogBuilder.setContentView(R.layout.home_episodes_expanded)
val title = bottomSheetDialogBuilder.findViewById<TextView>(R.id.home_expanded_text)!! val title = bottomSheetDialogBuilder.findViewById<TextView>(R.id.home_expanded_text)!!
//title.findViewTreeLifecycleOwner().lifecycle.addObserver()
val item = expand.list val item = expand.list
title.text = item.name title.text = item.name
val recycle = val recycle =
@ -131,6 +140,23 @@ class HomeFragment : Fragment() {
val titleHolder = val titleHolder =
bottomSheetDialogBuilder.findViewById<FrameLayout>(R.id.home_expanded_drag_down)!! bottomSheetDialogBuilder.findViewById<FrameLayout>(R.id.home_expanded_drag_down)!!
// main {
//(bottomSheetDialogBuilder.ownerActivity as androidx.fragment.app.FragmentActivity?)?.supportFragmentManager?.fragments?.lastOrNull()?.viewLifecycleOwner?.apply {
// println("GOT LIFE: lifecycle $this")
// this.lifecycle.addObserver(object : DefaultLifecycleObserver {
// override fun onResume(owner: LifecycleOwner) {
// super.onResume(owner)
// println("onResume!!!!")
// bottomSheetDialogBuilder?.ownShow()
// }
// override fun onStop(owner: LifecycleOwner) {
// super.onStop(owner)
// bottomSheetDialogBuilder?.ownHide()
// }
// })
//}
// }
val delete = bottomSheetDialogBuilder.home_expanded_delete val delete = bottomSheetDialogBuilder.home_expanded_delete
delete.isGone = deleteCallback == null delete.isGone = deleteCallback == null
if (deleteCallback != null) { if (deleteCallback != null) {
@ -175,7 +201,8 @@ class HomeFragment : Fragment() {
recycle.adapter = SearchAdapter(item.list.toMutableList(), recycle) { callback -> recycle.adapter = SearchAdapter(item.list.toMutableList(), recycle) { callback ->
handleSearchClickCallback(this, callback) handleSearchClickCallback(this, callback)
if (callback.action == SEARCH_ACTION_LOAD || callback.action == SEARCH_ACTION_PLAY_FILE) { if (callback.action == SEARCH_ACTION_LOAD || callback.action == SEARCH_ACTION_PLAY_FILE) {
bottomSheetDialogBuilder.dismissSafe(this) bottomSheetDialogBuilder.ownHide() // we hide here because we want to resume it later
//bottomSheetDialogBuilder.dismissSafe(this)
} }
}.apply { }.apply {
hasNext = expand.hasNext hasNext = expand.hasNext
@ -216,12 +243,14 @@ class HomeFragment : Fragment() {
configEvent += spanListener configEvent += spanListener
bottomSheetDialogBuilder.setOnDismissListener { bottomSheetDialogBuilder.setOnDismissListener {
dismissCallback.invoke()
configEvent -= spanListener configEvent -= spanListener
} }
//(recycle.adapter as SearchAdapter).notifyDataSetChanged() //(recycle.adapter as SearchAdapter).notifyDataSetChanged()
bottomSheetDialogBuilder.show() bottomSheetDialogBuilder.show()
return bottomSheetDialogBuilder
} }
fun getPairList( fun getPairList(
@ -399,6 +428,7 @@ class HomeFragment : Fragment() {
): View? { ): View? {
//homeViewModel = //homeViewModel =
// ViewModelProvider(this).get(HomeViewModel::class.java) // ViewModelProvider(this).get(HomeViewModel::class.java)
bottomSheetDialog?.ownShow()
val layout = val layout =
if (isTvSettings()) R.layout.fragment_home_tv else R.layout.fragment_home if (isTvSettings()) R.layout.fragment_home_tv else R.layout.fragment_home
return inflater.inflate(layout, container, false) return inflater.inflate(layout, container, false)
@ -480,6 +510,8 @@ class HomeFragment : Fragment() {
private var currentApiName: String? = null private var currentApiName: String? = null
private var toggleRandomButton = false private var toggleRandomButton = false
private var bottomSheetDialog: BottomSheetDialog? = null
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
@ -626,8 +658,10 @@ class HomeFragment : Fragment() {
HomeParentItemAdapterPreview(mutableListOf(), { callback -> HomeParentItemAdapterPreview(mutableListOf(), { callback ->
homeHandleSearch(callback) homeHandleSearch(callback)
}, { item -> }, { item ->
activity?.loadHomepageList(item, expandCallback = { bottomSheetDialog = activity?.loadHomepageList(item, expandCallback = {
homeViewModel.expandAndReturn(it) homeViewModel.expandAndReturn(it)
}, dismissCallback = {
bottomSheetDialog = null
}) })
}, { name -> }, { name ->
homeViewModel.expand(name) homeViewModel.expand(name)

View file

@ -15,6 +15,7 @@ import androidx.core.view.isVisible
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.lagradost.cloudstream3.APIHolder.filterProviderByPreferredMedia import com.lagradost.cloudstream3.APIHolder.filterProviderByPreferredMedia
import com.lagradost.cloudstream3.APIHolder.filterSearchResultByFilmQuality import com.lagradost.cloudstream3.APIHolder.filterSearchResultByFilmQuality
import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull
@ -30,6 +31,7 @@ import com.lagradost.cloudstream3.ui.search.SearchAdapter
import com.lagradost.cloudstream3.ui.search.SearchClickCallback import com.lagradost.cloudstream3.ui.search.SearchClickCallback
import com.lagradost.cloudstream3.ui.search.SearchHelper import com.lagradost.cloudstream3.ui.search.SearchHelper
import com.lagradost.cloudstream3.ui.search.SearchViewModel import com.lagradost.cloudstream3.ui.search.SearchViewModel
import com.lagradost.cloudstream3.utils.AppUtils.ownShow
import com.lagradost.cloudstream3.utils.UIHelper import com.lagradost.cloudstream3.utils.UIHelper
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar
import com.lagradost.cloudstream3.utils.UIHelper.getSpanCount import com.lagradost.cloudstream3.utils.UIHelper.getSpanCount
@ -71,6 +73,8 @@ class QuickSearchFragment : Fragment() {
private var providers: Set<String>? = null private var providers: Set<String>? = null
private lateinit var searchViewModel: SearchViewModel private lateinit var searchViewModel: SearchViewModel
private var bottomSheetDialog: BottomSheetDialog? = null
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
container: ViewGroup?, container: ViewGroup?,
@ -80,7 +84,7 @@ class QuickSearchFragment : Fragment() {
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE
) )
searchViewModel = ViewModelProvider(this)[SearchViewModel::class.java] searchViewModel = ViewModelProvider(this)[SearchViewModel::class.java]
bottomSheetDialog?.ownShow()
return inflater.inflate(R.layout.quick_search, container, false) return inflater.inflate(R.layout.quick_search, container, false)
} }
@ -156,7 +160,9 @@ class QuickSearchFragment : Fragment() {
// else -> SearchHelper.handleSearchClickCallback(activity, callback) // else -> SearchHelper.handleSearchClickCallback(activity, callback)
//} //}
}, { item -> }, { item ->
activity?.loadHomepageList(item) bottomSheetDialog = activity?.loadHomepageList(item, dismissCallback = {
bottomSheetDialog = null
})
}) })
quick_search_master_recycler?.layoutManager = GridLayoutManager(context, 1) quick_search_master_recycler?.layoutManager = GridLayoutManager(context, 1)
} }

View file

@ -45,6 +45,7 @@ import com.lagradost.cloudstream3.ui.home.HomeFragment.Companion.updateChips
import com.lagradost.cloudstream3.ui.home.ParentItemAdapter import com.lagradost.cloudstream3.ui.home.ParentItemAdapter
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTrueTvSettings import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTrueTvSettings
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings
import com.lagradost.cloudstream3.utils.AppUtils.ownShow
import com.lagradost.cloudstream3.utils.Coroutines.main import com.lagradost.cloudstream3.utils.Coroutines.main
import com.lagradost.cloudstream3.utils.DataStore.getKey import com.lagradost.cloudstream3.utils.DataStore.getKey
import com.lagradost.cloudstream3.utils.DataStore.setKey import com.lagradost.cloudstream3.utils.DataStore.setKey
@ -85,6 +86,7 @@ class SearchFragment : Fragment() {
} }
private val searchViewModel: SearchViewModel by activityViewModels() private val searchViewModel: SearchViewModel by activityViewModels()
private var bottomSheetDialog: BottomSheetDialog? = null
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
@ -94,6 +96,7 @@ class SearchFragment : Fragment() {
activity?.window?.setSoftInputMode( activity?.window?.setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE
) )
bottomSheetDialog?.ownShow()
return inflater.inflate( return inflater.inflate(
if (isTvSettings()) R.layout.fragment_search_tv else R.layout.fragment_search, if (isTvSettings()) R.layout.fragment_search_tv else R.layout.fragment_search,
container, container,
@ -473,7 +476,9 @@ class SearchFragment : Fragment() {
ParentItemAdapter(mutableListOf(), { callback -> ParentItemAdapter(mutableListOf(), { callback ->
SearchHelper.handleSearchClickCallback(activity, callback) SearchHelper.handleSearchClickCallback(activity, callback)
}, { item -> }, { item ->
activity?.loadHomepageList(item) bottomSheetDialog = activity?.loadHomepageList(item, dismissCallback = {
bottomSheetDialog = null
})
}) })
val historyAdapter = SearchHistoryAdaptor(mutableListOf()) { click -> val historyAdapter = SearchHistoryAdaptor(mutableListOf()) { click ->

View file

@ -13,9 +13,7 @@ import android.media.tv.TvContract.Channels.COLUMN_INTERNAL_PROVIDER_ID
import android.net.ConnectivityManager import android.net.ConnectivityManager
import android.net.NetworkCapabilities import android.net.NetworkCapabilities
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.*
import android.os.Environment
import android.os.ParcelFileDescriptor
import android.provider.MediaStore import android.provider.MediaStore
import android.text.Spanned import android.text.Spanned
import android.util.Log import android.util.Log
@ -40,6 +38,7 @@ import com.google.android.gms.cast.framework.CastState
import com.google.android.gms.common.ConnectionResult import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.common.wrappers.Wrappers import com.google.android.gms.common.wrappers.Wrappers
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.lagradost.cloudstream3.* import com.lagradost.cloudstream3.*
import com.lagradost.cloudstream3.CommonActivity.showToast import com.lagradost.cloudstream3.CommonActivity.showToast
import com.lagradost.cloudstream3.MainActivity.Companion.afterRepositoryLoadedEvent import com.lagradost.cloudstream3.MainActivity.Companion.afterRepositoryLoadedEvent
@ -79,6 +78,19 @@ object AppUtils {
return if (layoutManager == null || adapter == null) false else layoutManager.findLastCompletelyVisibleItemPosition() < adapter.itemCount - 7 // bit more than 1 to make it more seamless return if (layoutManager == null || adapter == null) false else layoutManager.findLastCompletelyVisibleItemPosition() < adapter.itemCount - 7 // bit more than 1 to make it more seamless
} }
fun BottomSheetDialog?.ownHide() {
this?.hide()
}
fun BottomSheetDialog?.ownShow() {
// the reason for this is because show has a shitty animation we don't want
this?.window?.setWindowAnimations(-1)
this?.show()
Handler(Looper.getMainLooper()).postDelayed({
this?.window?.setWindowAnimations(R.style.Animation_Design_BottomSheetDialog)
},200)
}
//fun Context.deleteFavorite(data: SearchResponse) { //fun Context.deleteFavorite(data: SearchResponse) {
// if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return // if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
// normalSafeApiCall { // normalSafeApiCall {