Skip to content

Commit

Permalink
TV UI fixes (#612)
Browse files Browse the repository at this point in the history
* TV UI fixes
  • Loading branch information
CranberrySoup authored Sep 18, 2023
1 parent 2ae5b6c commit 1533312
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.lagradost.cloudstream3.ui.result
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -12,7 +14,10 @@ import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.databinding.CastItemBinding
import com.lagradost.cloudstream3.utils.UIHelper.setImage

class ActorAdaptor(private val focusCallback : (View?) -> Unit = {}) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
class ActorAdaptor(
private var nextFocusUpId: Int? = null,
private val focusCallback: (View?) -> Unit = {}
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
data class ActorMetaData(
var isInverted: Boolean,
val actor: ActorData,
Expand All @@ -22,7 +27,8 @@ class ActorAdaptor(private val focusCallback : (View?) -> Unit = {}) : RecyclerV

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return CardViewHolder(
CastItemBinding.inflate(LayoutInflater.from(parent.context), parent, false), focusCallback
CastItemBinding.inflate(LayoutInflater.from(parent.context), parent, false),
focusCallback
)
}

Expand Down Expand Up @@ -64,10 +70,10 @@ class ActorAdaptor(private val focusCallback : (View?) -> Unit = {}) : RecyclerV
}
}

private class CardViewHolder
private inner class CardViewHolder
constructor(
val binding: CastItemBinding,
private val focusCallback : (View?) -> Unit = {}
private val focusCallback: (View?) -> Unit = {}
) :
RecyclerView.ViewHolder(binding.root) {

Expand All @@ -78,8 +84,18 @@ class ActorAdaptor(private val focusCallback : (View?) -> Unit = {}) : RecyclerV
Pair(actor.voiceActor?.image, actor.actor.image)
}

// Fix tv focus escaping the recyclerview
if (position == 0) {
itemView.nextFocusLeftId = R.id.result_cast_items
} else if ((position - 1) == itemCount) {
itemView.nextFocusRightId = R.id.result_cast_items
}
nextFocusUpId?.let {
itemView.nextFocusUpId = it
}

itemView.setOnFocusChangeListener { v, hasFocus ->
if(hasFocus) {
if (hasFocus) {
focusCallback(v)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,20 @@ class ResultFragmentTv : Fragment() {
}
}

private fun hasNoFocus(): Boolean {
val focus = activity?.currentFocus
if (focus == null || !focus.isVisible) return true
return focus == binding?.resultRoot
// private fun hasNoFocus(): Boolean {
// val focus = activity?.currentFocus
// if (focus == null || !focus.isVisible) return true
// return focus == binding?.resultRoot
// }

/**
* Force focus any play button.
* Note that this will steal any focus if the episode loading is too slow (unlikely).
*/
private fun focusPlayButton() {
binding?.resultPlayMovie?.requestFocus()
binding?.resultPlaySeries?.requestFocus()
binding?.resultResumeSeries?.requestFocus()
}

private fun setRecommendations(rec: List<SearchResponse>?, validApiName: String?) {
Expand Down Expand Up @@ -413,7 +423,13 @@ class ResultFragmentTv : Fragment() {
setHorizontal()
}

resultCastItems.adapter = ActorAdaptor {
val aboveCast = listOf(
binding?.resultEpisodesShow,
binding?.resultBookmarkButton,
).firstOrNull {
it?.isVisible == true
}
resultCastItems.adapter = ActorAdaptor(aboveCast?.id) {
toggleEpisodes(false)
}
}
Expand Down Expand Up @@ -454,9 +470,7 @@ class ResultFragmentTv : Fragment() {
resultPlaySeries.isVisible = false
resultResumeSeries.isVisible = true

if (hasNoFocus()) {
resultResumeSeries.requestFocus()
}
focusPlayButton()

resultResumeSeries.text =
if (resume.isMovie) context?.getString(R.string.play_movie_button) else context?.getNameFull(
Expand Down Expand Up @@ -539,9 +553,7 @@ class ResultFragmentTv : Fragment() {
)
return@setOnLongClickListener true
}
if (hasNoFocus()) {
resultPlayMovie.requestFocus()
}
focusPlayButton()
}
}
}
Expand Down Expand Up @@ -663,6 +675,7 @@ class ResultFragmentTv : Fragment() {
)
return@setOnLongClickListener true
}
focusPlayButton()
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ class SettingsFragment : Fragment() {
}
}
}

// Default focus on TV
if (isTrueTv) {
settingsGeneral.requestFocus()
}
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_search_tv.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
android:id="@+id/search_autofit_results"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:layout_marginStart="@dimen/navbar_width"
android:background="?attr/primaryBlackBackground"
android:clipToPadding="false"
android:descendantFocusability="afterDescendants"
Expand Down

0 comments on commit 1533312

Please sign in to comment.