Skip to content

Commit

Permalink
fix save state
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Feb 21, 2024
1 parent 52d3568 commit 10f261b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 29 deletions.
15 changes: 11 additions & 4 deletions app/src/main/java/com/lizongying/mytv/CardAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.lizongying.mytv

import android.graphics.Color
import android.view.ContextThemeWrapper
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.view.animation.ScaleAnimation
Expand All @@ -18,13 +19,13 @@ class CardAdapter(
private val recyclerView: RecyclerView,
private val owner: LifecycleOwner,
private var tvListViewModel: TVListViewModel,
var defaultFocus: Int,
) :
RecyclerView.Adapter<CardAdapter.ViewHolder>() {

private var listener: ItemListener? = null
private var focused: View? = null
private var defaultFocused = false
var defaultFocus: Int = -1

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val cardView = object :
Expand Down Expand Up @@ -74,8 +75,14 @@ class CardAdapter(

cardView.onFocusChangeListener = onFocusChangeListener

cardView.setOnKeyListener { v, keyCode, event ->
listener?.onKey(keyCode)
cardView.setOnClickListener { _ ->
listener?.onItemClicked(item)
}

cardView.setOnKeyListener { _, keyCode, event: KeyEvent? ->
if (event?.action == KeyEvent.ACTION_DOWN) {
return@setOnKeyListener listener?.onKey(keyCode) ?: false
}
false
}

Expand Down Expand Up @@ -128,7 +135,7 @@ class CardAdapter(
interface ItemListener {
fun onItemFocusChange(tvViewModel: TVViewModel, hasFocus: Boolean)
fun onItemClicked(tvViewModel: TVViewModel)
fun onKey(keyCode: Int)
fun onKey(keyCode: Int): Boolean
}

fun setItemListener(listener: ItemListener) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/lizongying/mytv/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ class MainActivity : FragmentActivity() {


fun onKey(keyCode: Int): Boolean {
Log.i(TAG, "keyCode $keyCode")
when (keyCode) {
KeyEvent.KEYCODE_0 -> {
showChannel("0")
Expand Down
52 changes: 28 additions & 24 deletions app/src/main/java/com/lizongying/mytv/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -58,9 +59,6 @@ class MainFragment : Fragment(), CardAdapter.ItemListener {
sharedPref = (activity as? MainActivity)?.sharedPref!!

itemPosition = sharedPref.getInt(POSITION, 0)
if (itemPosition >= tvListViewModel.size()) {
itemPosition = 0
}

view?.post {
val content = binding.content
Expand All @@ -85,7 +83,6 @@ class MainFragment : Fragment(), CardAdapter.ItemListener {
itemBinding.rowItems,
viewLifecycleOwner,
tvListViewModelCurrent,
itemPosition
)
rowList.add(itemBinding.rowItems)

Expand All @@ -97,7 +94,7 @@ class MainFragment : Fragment(), CardAdapter.ItemListener {
itemBinding.rowItems.layoutManager =
GridLayoutManager(context, 6)
itemBinding.rowItems.layoutParams.height =
dpToPx(100 * ((tvListViewModelCurrent.size() + 6 - 1) / 6))
dpToPx(92 * ((tvListViewModelCurrent.size() + 6 - 1) / 6))

itemBinding.rowItems.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
Expand All @@ -106,22 +103,6 @@ class MainFragment : Fragment(), CardAdapter.ItemListener {
}
})

itemBinding.rowItems.setOnKeyListener { v, keyCode, event ->
Log.i(TAG, "itemBinding.rowItems.setOnKeyListener ")
false
}

itemBinding.row.setOnKeyListener { v, keyCode, event ->
Log.i(TAG, "itemBinding.row.setOnKeyListener ")
false
}

itemBinding.root.setOnKeyListener { v, keyCode, event ->
Log.i(TAG, "itemBinding.root.setOnKeyListener ")
false
}


val itemDecoration = context?.let { GrayOverlayItemDecoration(it) }
if (itemDecoration != null) {
itemBinding.rowItems.addItemDecoration(itemDecoration)
Expand All @@ -138,6 +119,18 @@ class MainFragment : Fragment(), CardAdapter.ItemListener {
mUpdateProgramRunnable = UpdateProgramRunnable()
handler.post(mUpdateProgramRunnable)

if (itemPosition >= tvListViewModel.size()) {
itemPosition = 0
}

val tv = tvListViewModel.getTVViewModel(itemPosition)
for (i in rowList) {
if (i.tag as Int == tv?.getRowPosition()) {
((i as RecyclerView).adapter as CardAdapter).defaultFocus = itemPosition
break
}
}

tvListViewModel.setItemPosition(itemPosition)
tvListViewModel.tvListViewModel.value?.forEach { tvViewModel ->
tvViewModel.errInfo.observe(viewLifecycleOwner) { _ ->
Expand Down Expand Up @@ -192,10 +185,21 @@ class MainFragment : Fragment(), CardAdapter.ItemListener {
super.onViewCreated(view, savedInstanceState)
}

override fun onKey(keyCode: Int) {
override fun onKey(keyCode: Int): Boolean {
if (this.isHidden) {
(activity as MainActivity).onKey(keyCode)
when (keyCode) {
KeyEvent.KEYCODE_DPAD_UP -> {
(activity as MainActivity).onKey(keyCode)
return true
}

KeyEvent.KEYCODE_DPAD_DOWN -> {
(activity as MainActivity).onKey(keyCode)
return true
}
}
}
return false
}

override fun onItemFocusChange(tvViewModel: TVViewModel, hasFocus: Boolean) {
Expand Down Expand Up @@ -342,7 +346,7 @@ class MainFragment : Fragment(), CardAdapter.ItemListener {
putInt(POSITION, itemPosition)
apply()
}
Log.i(TAG, "$POSITION saved")
Log.i(TAG, "$POSITION $itemPosition saved")
}

override fun onDestroy() {
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/lizongying/mytv/Request.kt
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@ class Request {
Log.e(TAG, "info status error")
if (tvModel.tokenRetryTimes < tvModel.tokenRetryMaxTimes) {
tvModel.tokenRetryTimes++
if (needAuth)
fetchVideo(tvModel)
} else {
if (!tvModel.getTV().mustToken) {
Expand Down

0 comments on commit 10f261b

Please sign in to comment.