Skip to content

Commit 1533312

Browse files
TV UI fixes (#612)
* TV UI fixes
1 parent 2ae5b6c commit 1533312

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

app/src/main/java/com/lagradost/cloudstream3/ui/result/ActorAdaptor.kt

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.lagradost.cloudstream3.ui.result
33
import android.view.LayoutInflater
44
import android.view.View
55
import android.view.ViewGroup
6+
import androidx.annotation.IdRes
7+
import androidx.annotation.LayoutRes
68
import androidx.core.view.isVisible
79
import androidx.recyclerview.widget.DiffUtil
810
import androidx.recyclerview.widget.RecyclerView
@@ -12,7 +14,10 @@ import com.lagradost.cloudstream3.R
1214
import com.lagradost.cloudstream3.databinding.CastItemBinding
1315
import com.lagradost.cloudstream3.utils.UIHelper.setImage
1416

15-
class ActorAdaptor(private val focusCallback : (View?) -> Unit = {}) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
17+
class ActorAdaptor(
18+
private var nextFocusUpId: Int? = null,
19+
private val focusCallback: (View?) -> Unit = {}
20+
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
1621
data class ActorMetaData(
1722
var isInverted: Boolean,
1823
val actor: ActorData,
@@ -22,7 +27,8 @@ class ActorAdaptor(private val focusCallback : (View?) -> Unit = {}) : RecyclerV
2227

2328
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
2429
return CardViewHolder(
25-
CastItemBinding.inflate(LayoutInflater.from(parent.context), parent, false), focusCallback
30+
CastItemBinding.inflate(LayoutInflater.from(parent.context), parent, false),
31+
focusCallback
2632
)
2733
}
2834

@@ -64,10 +70,10 @@ class ActorAdaptor(private val focusCallback : (View?) -> Unit = {}) : RecyclerV
6470
}
6571
}
6672

67-
private class CardViewHolder
73+
private inner class CardViewHolder
6874
constructor(
6975
val binding: CastItemBinding,
70-
private val focusCallback : (View?) -> Unit = {}
76+
private val focusCallback: (View?) -> Unit = {}
7177
) :
7278
RecyclerView.ViewHolder(binding.root) {
7379

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

87+
// Fix tv focus escaping the recyclerview
88+
if (position == 0) {
89+
itemView.nextFocusLeftId = R.id.result_cast_items
90+
} else if ((position - 1) == itemCount) {
91+
itemView.nextFocusRightId = R.id.result_cast_items
92+
}
93+
nextFocusUpId?.let {
94+
itemView.nextFocusUpId = it
95+
}
96+
8197
itemView.setOnFocusChangeListener { v, hasFocus ->
82-
if(hasFocus) {
98+
if (hasFocus) {
8399
focusCallback(v)
84100
}
85101
}

app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragmentTv.kt

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,20 @@ class ResultFragmentTv : Fragment() {
114114
}
115115
}
116116

117-
private fun hasNoFocus(): Boolean {
118-
val focus = activity?.currentFocus
119-
if (focus == null || !focus.isVisible) return true
120-
return focus == binding?.resultRoot
117+
// private fun hasNoFocus(): Boolean {
118+
// val focus = activity?.currentFocus
119+
// if (focus == null || !focus.isVisible) return true
120+
// return focus == binding?.resultRoot
121+
// }
122+
123+
/**
124+
* Force focus any play button.
125+
* Note that this will steal any focus if the episode loading is too slow (unlikely).
126+
*/
127+
private fun focusPlayButton() {
128+
binding?.resultPlayMovie?.requestFocus()
129+
binding?.resultPlaySeries?.requestFocus()
130+
binding?.resultResumeSeries?.requestFocus()
121131
}
122132

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

416-
resultCastItems.adapter = ActorAdaptor {
426+
val aboveCast = listOf(
427+
binding?.resultEpisodesShow,
428+
binding?.resultBookmarkButton,
429+
).firstOrNull {
430+
it?.isVisible == true
431+
}
432+
resultCastItems.adapter = ActorAdaptor(aboveCast?.id) {
417433
toggleEpisodes(false)
418434
}
419435
}
@@ -454,9 +470,7 @@ class ResultFragmentTv : Fragment() {
454470
resultPlaySeries.isVisible = false
455471
resultResumeSeries.isVisible = true
456472

457-
if (hasNoFocus()) {
458-
resultResumeSeries.requestFocus()
459-
}
473+
focusPlayButton()
460474

461475
resultResumeSeries.text =
462476
if (resume.isMovie) context?.getString(R.string.play_movie_button) else context?.getNameFull(
@@ -539,9 +553,7 @@ class ResultFragmentTv : Fragment() {
539553
)
540554
return@setOnLongClickListener true
541555
}
542-
if (hasNoFocus()) {
543-
resultPlayMovie.requestFocus()
544-
}
556+
focusPlayButton()
545557
}
546558
}
547559
}
@@ -663,6 +675,7 @@ class ResultFragmentTv : Fragment() {
663675
)
664676
return@setOnLongClickListener true
665677
}
678+
focusPlayButton()
666679
}
667680

668681
/*

app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsFragment.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ class SettingsFragment : Fragment() {
205205
}
206206
}
207207
}
208+
209+
// Default focus on TV
210+
if (isTrueTv) {
211+
settingsGeneral.requestFocus()
212+
}
208213
}
209214
}
210215
}

app/src/main/res/layout/fragment_search_tv.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
android:id="@+id/search_autofit_results"
100100
android:layout_width="match_parent"
101101
android:layout_height="match_parent"
102-
102+
android:layout_marginStart="@dimen/navbar_width"
103103
android:background="?attr/primaryBlackBackground"
104104
android:clipToPadding="false"
105105
android:descendantFocusability="afterDescendants"

0 commit comments

Comments
 (0)