mirror of
				https://github.com/recloudstream/cloudstream.git
				synced 2024-08-15 01:53:11 +00:00 
			
		
		
		
	Add BackPressedCallbackHelper to unify back pressed logic
This commit is contained in:
		
							parent
							
								
									77e1b87574
								
							
						
					
					
						commit
						d95af138b8
					
				
					 5 changed files with 53 additions and 98 deletions
				
			
		|  | @ -131,6 +131,8 @@ import com.lagradost.cloudstream3.utils.AppContextUtils.loadResult | ||||||
| import com.lagradost.cloudstream3.utils.AppContextUtils.loadSearchResult | import com.lagradost.cloudstream3.utils.AppContextUtils.loadSearchResult | ||||||
| import com.lagradost.cloudstream3.utils.AppContextUtils.setDefaultFocus | import com.lagradost.cloudstream3.utils.AppContextUtils.setDefaultFocus | ||||||
| import com.lagradost.cloudstream3.utils.AppContextUtils.updateHasTrailers | import com.lagradost.cloudstream3.utils.AppContextUtils.updateHasTrailers | ||||||
|  | import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.attachBackPressedCallback | ||||||
|  | import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.detachBackPressedCallback | ||||||
| import com.lagradost.cloudstream3.utils.BackupUtils.backup | import com.lagradost.cloudstream3.utils.BackupUtils.backup | ||||||
| import com.lagradost.cloudstream3.utils.BackupUtils.setUpBackup | import com.lagradost.cloudstream3.utils.BackupUtils.setUpBackup | ||||||
| import com.lagradost.cloudstream3.utils.BiometricAuthenticator.BiometricCallback | import com.lagradost.cloudstream3.utils.BiometricAuthenticator.BiometricCallback | ||||||
|  | @ -1566,7 +1568,12 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener, BiometricCa | ||||||
| 
 | 
 | ||||||
|             if (isLayout(TV or EMULATOR)) { |             if (isLayout(TV or EMULATOR)) { | ||||||
|                 if (navDestination.matchDestination(R.id.navigation_home)) { |                 if (navDestination.matchDestination(R.id.navigation_home)) { | ||||||
|                     attachBackPressedCallback() |                     attachBackPressedCallback { | ||||||
|  |                         showConfirmExitDialog() | ||||||
|  |                         window?.navigationBarColor = | ||||||
|  |                             colorFromAttribute(R.attr.primaryGrayBackground) | ||||||
|  |                         updateLocale() | ||||||
|  |                     } | ||||||
|                 } else detachBackPressedCallback() |                 } else detachBackPressedCallback() | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  | @ -1811,28 +1818,6 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener, BiometricCa | ||||||
|         finish() |         finish() | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private var backPressedCallback: OnBackPressedCallback? = null |  | ||||||
| 
 |  | ||||||
|     private fun attachBackPressedCallback() { |  | ||||||
|         if (backPressedCallback == null) { |  | ||||||
|             backPressedCallback = object : OnBackPressedCallback(true) { |  | ||||||
|                 override fun handleOnBackPressed() { |  | ||||||
|                     showConfirmExitDialog() |  | ||||||
|                     window?.navigationBarColor = |  | ||||||
|                         colorFromAttribute(R.attr.primaryGrayBackground) |  | ||||||
|                     updateLocale() |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         backPressedCallback?.isEnabled = true |  | ||||||
|         onBackPressedDispatcher.addCallback(this, backPressedCallback ?: return) |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     private fun detachBackPressedCallback() { |  | ||||||
|         backPressedCallback?.isEnabled = false |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     suspend fun checkGithubConnectivity(): Boolean { |     suspend fun checkGithubConnectivity(): Boolean { | ||||||
|         return try { |         return try { | ||||||
|             app.get( |             app.get( | ||||||
|  |  | ||||||
|  | @ -7,7 +7,6 @@ import android.text.format.Formatter.formatShortFileSize | ||||||
| import android.view.LayoutInflater | import android.view.LayoutInflater | ||||||
| import android.view.View | import android.view.View | ||||||
| import android.view.ViewGroup | import android.view.ViewGroup | ||||||
| import androidx.activity.OnBackPressedCallback |  | ||||||
| import androidx.core.view.isVisible | import androidx.core.view.isVisible | ||||||
| import androidx.fragment.app.Fragment | import androidx.fragment.app.Fragment | ||||||
| import androidx.lifecycle.ViewModelProvider | import androidx.lifecycle.ViewModelProvider | ||||||
|  | @ -20,6 +19,8 @@ import com.lagradost.cloudstream3.ui.result.setLinearListLayout | ||||||
| import com.lagradost.cloudstream3.ui.settings.Globals.EMULATOR | import com.lagradost.cloudstream3.ui.settings.Globals.EMULATOR | ||||||
| import com.lagradost.cloudstream3.ui.settings.Globals.PHONE | import com.lagradost.cloudstream3.ui.settings.Globals.PHONE | ||||||
| import com.lagradost.cloudstream3.ui.settings.Globals.isLayout | import com.lagradost.cloudstream3.ui.settings.Globals.isLayout | ||||||
|  | import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.attachBackPressedCallback | ||||||
|  | import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.detachBackPressedCallback | ||||||
| import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar | import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar | ||||||
| import com.lagradost.cloudstream3.utils.UIHelper.setAppBarNoScrollFlagsOnTV | import com.lagradost.cloudstream3.utils.UIHelper.setAppBarNoScrollFlagsOnTV | ||||||
| import com.lagradost.cloudstream3.utils.VideoDownloadManager | import com.lagradost.cloudstream3.utils.VideoDownloadManager | ||||||
|  | @ -160,7 +161,9 @@ class DownloadChildFragment : Fragment() { | ||||||
|     private fun handleSelectedChange(selected: MutableList<VisualDownloadCached>) { |     private fun handleSelectedChange(selected: MutableList<VisualDownloadCached>) { | ||||||
|         if (selected.isNotEmpty()) { |         if (selected.isNotEmpty()) { | ||||||
|             binding?.downloadDeleteAppbar?.isVisible = true |             binding?.downloadDeleteAppbar?.isVisible = true | ||||||
|             attachBackPressedCallback() |             activity?.attachBackPressedCallback { | ||||||
|  |                 downloadsViewModel.setIsMultiDeleteState(false) | ||||||
|  |             } | ||||||
| 
 | 
 | ||||||
|             binding?.btnDelete?.setOnClickListener { |             binding?.btnDelete?.setOnClickListener { | ||||||
|                 context?.let { ctx -> |                 context?.let { ctx -> | ||||||
|  | @ -204,27 +207,4 @@ class DownloadChildFragment : Fragment() { | ||||||
|         } |         } | ||||||
|         downloadDeleteEventListener?.let { VideoDownloadManager.downloadDeleteEvent += it } |         downloadDeleteEventListener?.let { VideoDownloadManager.downloadDeleteEvent += it } | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
|     private var backPressedCallback: OnBackPressedCallback? = null |  | ||||||
| 
 |  | ||||||
|     private fun attachBackPressedCallback() { |  | ||||||
|         if (backPressedCallback == null) { |  | ||||||
|             backPressedCallback = object : OnBackPressedCallback(true) { |  | ||||||
|                 override fun handleOnBackPressed() { |  | ||||||
|                     downloadsViewModel.setIsMultiDeleteState(false) |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         backPressedCallback?.isEnabled = true |  | ||||||
| 
 |  | ||||||
|         activity?.onBackPressedDispatcher?.addCallback( |  | ||||||
|             activity ?: return, |  | ||||||
|             backPressedCallback ?: return |  | ||||||
|         ) |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     private fun detachBackPressedCallback() { |  | ||||||
|         backPressedCallback?.isEnabled = false |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  | @ -17,7 +17,6 @@ import android.view.ViewGroup | ||||||
| import android.widget.LinearLayout | import android.widget.LinearLayout | ||||||
| import android.widget.TextView | import android.widget.TextView | ||||||
| import android.widget.Toast | import android.widget.Toast | ||||||
| import androidx.activity.OnBackPressedCallback |  | ||||||
| import androidx.activity.result.contract.ActivityResultContracts | import androidx.activity.result.contract.ActivityResultContracts | ||||||
| import androidx.annotation.StringRes | import androidx.annotation.StringRes | ||||||
| import androidx.appcompat.app.AppCompatActivity | import androidx.appcompat.app.AppCompatActivity | ||||||
|  | @ -43,6 +42,8 @@ import com.lagradost.cloudstream3.ui.result.setLinearListLayout | ||||||
| import com.lagradost.cloudstream3.ui.settings.Globals.TV | import com.lagradost.cloudstream3.ui.settings.Globals.TV | ||||||
| import com.lagradost.cloudstream3.ui.settings.Globals.isLayout | import com.lagradost.cloudstream3.ui.settings.Globals.isLayout | ||||||
| import com.lagradost.cloudstream3.utils.AppContextUtils.loadResult | import com.lagradost.cloudstream3.utils.AppContextUtils.loadResult | ||||||
|  | import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.attachBackPressedCallback | ||||||
|  | import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.detachBackPressedCallback | ||||||
| import com.lagradost.cloudstream3.utils.DOWNLOAD_EPISODE_CACHE | import com.lagradost.cloudstream3.utils.DOWNLOAD_EPISODE_CACHE | ||||||
| import com.lagradost.cloudstream3.utils.DataStore | import com.lagradost.cloudstream3.utils.DataStore | ||||||
| import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe | import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe | ||||||
|  | @ -241,7 +242,9 @@ class DownloadFragment : Fragment() { | ||||||
|         if (selectedItems.isNotEmpty()) { |         if (selectedItems.isNotEmpty()) { | ||||||
|             binding?.downloadDeleteAppbar?.isVisible = true |             binding?.downloadDeleteAppbar?.isVisible = true | ||||||
|             binding?.downloadStorageAppbar?.isVisible = false |             binding?.downloadStorageAppbar?.isVisible = false | ||||||
|             attachBackPressedCallback() |             activity?.attachBackPressedCallback { | ||||||
|  |                 downloadsViewModel.setIsMultiDeleteState(false) | ||||||
|  |             } | ||||||
| 
 | 
 | ||||||
|             binding?.btnDelete?.setOnClickListener { |             binding?.btnDelete?.setOnClickListener { | ||||||
|                 context?.let { ctx -> |                 context?.let { ctx -> | ||||||
|  | @ -384,27 +387,4 @@ class DownloadFragment : Fragment() { | ||||||
|         val selectedVideoUri = result?.data?.data ?: return@registerForActivityResult |         val selectedVideoUri = result?.data?.data ?: return@registerForActivityResult | ||||||
|         playUri(activity ?: return@registerForActivityResult, selectedVideoUri) |         playUri(activity ?: return@registerForActivityResult, selectedVideoUri) | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
|     private var backPressedCallback: OnBackPressedCallback? = null |  | ||||||
| 
 |  | ||||||
|     private fun attachBackPressedCallback() { |  | ||||||
|         if (backPressedCallback == null) { |  | ||||||
|             backPressedCallback = object : OnBackPressedCallback(true) { |  | ||||||
|                 override fun handleOnBackPressed() { |  | ||||||
|                     downloadsViewModel.setIsMultiDeleteState(false) |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         backPressedCallback?.isEnabled = true |  | ||||||
| 
 |  | ||||||
|         activity?.onBackPressedDispatcher?.addCallback( |  | ||||||
|             activity ?: return, |  | ||||||
|             backPressedCallback ?: return |  | ||||||
|         ) |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     private fun detachBackPressedCallback() { |  | ||||||
|         backPressedCallback?.isEnabled = false |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  | @ -7,7 +7,6 @@ import android.os.Bundle | ||||||
| import android.view.View | import android.view.View | ||||||
| import android.view.ViewGroup | import android.view.ViewGroup | ||||||
| import android.widget.FrameLayout | import android.widget.FrameLayout | ||||||
| import androidx.activity.OnBackPressedCallback |  | ||||||
| import androidx.core.view.isGone | import androidx.core.view.isGone | ||||||
| import androidx.core.view.isVisible | import androidx.core.view.isVisible | ||||||
| import com.lagradost.cloudstream3.CommonActivity.screenHeight | import com.lagradost.cloudstream3.CommonActivity.screenHeight | ||||||
|  | @ -17,6 +16,8 @@ import com.lagradost.cloudstream3.R | ||||||
| import com.lagradost.cloudstream3.ui.player.CSPlayerEvent | import com.lagradost.cloudstream3.ui.player.CSPlayerEvent | ||||||
| import com.lagradost.cloudstream3.ui.player.PlayerEventSource | import com.lagradost.cloudstream3.ui.player.PlayerEventSource | ||||||
| import com.lagradost.cloudstream3.ui.player.SubtitleData | import com.lagradost.cloudstream3.ui.player.SubtitleData | ||||||
|  | import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.attachBackPressedCallback | ||||||
|  | import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.detachBackPressedCallback | ||||||
| 
 | 
 | ||||||
| open class ResultTrailerPlayer : ResultFragmentPhone() { | open class ResultTrailerPlayer : ResultFragmentPhone() { | ||||||
| 
 | 
 | ||||||
|  | @ -156,7 +157,9 @@ open class ResultTrailerPlayer : ResultFragmentPhone() { | ||||||
|         uiReset() |         uiReset() | ||||||
| 
 | 
 | ||||||
|         if (isFullScreenPlayer) { |         if (isFullScreenPlayer) { | ||||||
|             attachBackPressedCallback() |             activity?.attachBackPressedCallback { | ||||||
|  |                 updateFullscreen(false) | ||||||
|  |             } | ||||||
|         } else detachBackPressedCallback() |         } else detachBackPressedCallback() | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | @ -175,27 +178,4 @@ open class ResultTrailerPlayer : ResultFragmentPhone() { | ||||||
|             fixPlayerSize() |             fixPlayerSize() | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
|     private var backPressedCallback: OnBackPressedCallback? = null |  | ||||||
| 
 |  | ||||||
|     private fun attachBackPressedCallback() { |  | ||||||
|         if (backPressedCallback == null) { |  | ||||||
|             backPressedCallback = object : OnBackPressedCallback(true) { |  | ||||||
|                 override fun handleOnBackPressed() { |  | ||||||
|                     updateFullscreen(false) |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         backPressedCallback?.isEnabled = true |  | ||||||
| 
 |  | ||||||
|         activity?.onBackPressedDispatcher?.addCallback( |  | ||||||
|             activity ?: return, |  | ||||||
|             backPressedCallback ?: return |  | ||||||
|         ) |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     private fun detachBackPressedCallback() { |  | ||||||
|         backPressedCallback?.isEnabled = false |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  | @ -0,0 +1,30 @@ | ||||||
|  | package com.lagradost.cloudstream3.utils | ||||||
|  | 
 | ||||||
|  | import androidx.activity.ComponentActivity | ||||||
|  | import androidx.activity.OnBackPressedCallback | ||||||
|  | 
 | ||||||
|  | object BackPressedCallbackHelper { | ||||||
|  |     private var backPressedCallback: OnBackPressedCallback? = null | ||||||
|  | 
 | ||||||
|  |     fun ComponentActivity.attachBackPressedCallback(callback: () -> Unit) { | ||||||
|  |         if (backPressedCallback == null) { | ||||||
|  |             backPressedCallback = object : OnBackPressedCallback(true) { | ||||||
|  |                 override fun handleOnBackPressed() { | ||||||
|  |                     callback.invoke() | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         backPressedCallback?.isEnabled = true | ||||||
|  | 
 | ||||||
|  |         onBackPressedDispatcher.addCallback( | ||||||
|  |             this@attachBackPressedCallback, | ||||||
|  |             backPressedCallback ?: return | ||||||
|  |         ) | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     fun detachBackPressedCallback() { | ||||||
|  |         backPressedCallback?.isEnabled = false | ||||||
|  |         backPressedCallback = null | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue