Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.appcompat.app.AlertDialog
import androidx.core.net.toUri
import androidx.core.view.isGone
Expand Down Expand Up @@ -64,6 +65,9 @@ import com.lagradost.cloudstream3.utils.AppContextUtils.loadSearchResult
import com.lagradost.cloudstream3.utils.AppContextUtils.ownHide
import com.lagradost.cloudstream3.utils.AppContextUtils.ownShow
import com.lagradost.cloudstream3.utils.AppContextUtils.setDefaultFocus
import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper
import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.attachBackPressedCallback
import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.detachBackPressedCallback
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.EmptyEvent
Expand Down Expand Up @@ -566,6 +570,7 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(
}

override fun onDestroyView() {
(activity as? ComponentActivity)?.detachBackPressedCallback("HomeFragment_BackPress")
bottomSheetDialog?.ownHide()
super.onDestroyView()
}
Expand Down Expand Up @@ -626,6 +631,9 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(
@SuppressLint("SetTextI18n")
override fun onBindingCreated(binding: FragmentHomeBinding) {
context?.let { HomeChildItemAdapter.updatePosterSize(it) }
(activity as? ComponentActivity)?.attachBackPressedCallback("HomeFragment_BackPress") {
handleTvBackPress(this)
}
binding.apply {
//homeChangeApiLoading.setOnClickListener(apiChangeClickListener)
//homeChangeApiLoading.setOnClickListener(apiChangeClickListener)
Expand Down Expand Up @@ -884,4 +892,41 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(
}
}*/
}

private fun handleTvBackPress(helper: BackPressedCallbackHelper.CallbackHelper) {
// Only apply custom behavior on TV interface
if (!isLayout(TV)) {
helper.runDefault()
return
}
val currentFocus = activity?.currentFocus ?: run {
helper.runDefault()
return
}
// Check if focus is inside home_master_recycler
var parent = currentFocus.parent
var isInsideRecycler = false
while (parent != null) {
if (parent is View && parent.id == R.id.home_master_recycler) {
isInsideRecycler = true
break
}
parent = parent.parent
}
when {
// Case 1: Focus is within plugin content -> Move to plugin selector
isInsideRecycler -> {
binding?.homeMasterRecycler?.smoothScrollToPosition(0)
binding?.homeChangeApi?.requestFocus()
}
// Case 2: Focus is on plugin selector or nearby buttons -> Move to home navigation
currentFocus.id == R.id.home_change_api ||
currentFocus.id == R.id.home_preview_reload_provider ||
currentFocus.id == R.id.home_preview_search_button -> {
activity?.findViewById<View>(R.id.navigation_home)?.requestFocus()
}
// Case 3: Any other location -> Use default back behavior
else -> helper.runDefault()
}
}
}