Skip to content

Commit

Permalink
optimize channel list
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed May 10, 2024
1 parent 604874c commit cde311f
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 41 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 更新日志

### v1.1.8

* 频道列表优化

### v1.1.7

* 可以通过二维码访问配置地址
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,8 @@ adb install my-tv-0.apk

## TODO

* 音量不同
* 收藏夹
* 节目增加预告
* 频道列表优化
* 节目列表焦点消失的问题

## 赞赏

Expand Down
46 changes: 37 additions & 9 deletions app/src/main/java/com/lizongying/mytv0/GroupAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.lizongying.mytv0

import android.content.Context
import android.util.Log
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.view.marginBottom
import androidx.core.view.marginStart
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lizongying.mytv0.databinding.GroupItemBinding
import com.lizongying.mytv0.models.TVGroupModel
Expand Down Expand Up @@ -93,17 +93,39 @@ class GroupAdapter(
view.onFocusChangeListener = onFocusChangeListener

view.setOnClickListener { _ ->
listener?.onItemClicked(tvListModel)
listener?.onItemClicked(position)
}

view.setOnKeyListener { _, keyCode, event: KeyEvent? ->
if (event?.action == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP && position == 0) {
recyclerView.smoothScrollToPosition(getItemCount() - 1)
val p = getItemCount() - 1

(recyclerView.layoutManager as? LinearLayoutManager)?.scrollToPositionWithOffset(
p,
0
)

recyclerView.postDelayed({
val v = recyclerView.findViewHolderForAdapterPosition(p)
v?.itemView?.isSelected = true
v?.itemView?.requestFocus()
}, 0)
}

if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && position == getItemCount() - 1) {
recyclerView.smoothScrollToPosition(0)
val p = 0

(recyclerView.layoutManager as? LinearLayoutManager)?.scrollToPositionWithOffset(
p,
0
)

recyclerView.postDelayed({
val v = recyclerView.findViewHolderForAdapterPosition(p)
v?.itemView?.isSelected = true
v?.itemView?.requestFocus()
}, 0)
}

return@setOnKeyListener listener?.onKey(keyCode) ?: false
Expand Down Expand Up @@ -138,16 +160,22 @@ class GroupAdapter(

fun toPosition(position: Int) {
recyclerView.post {
Log.i(TAG, "group smoothScrollToPosition $position")
recyclerView.scrollToPosition(position)
recyclerView.getChildAt(position)?.isSelected
recyclerView.getChildAt(position)?.requestFocus()
(recyclerView.layoutManager as? LinearLayoutManager)?.scrollToPositionWithOffset(
position,
0
)

recyclerView.postDelayed({
val viewHolder = recyclerView.findViewHolderForAdapterPosition(position)
viewHolder?.itemView?.isSelected = true
viewHolder?.itemView?.requestFocus()
}, 0)
}
}

interface ItemListener {
fun onItemFocusChange(tvListModel: TVListModel, hasFocus: Boolean)
fun onItemClicked(tvListModel: TVListModel)
fun onItemClicked(position: Int)
fun onKey(keyCode: Int): Boolean
}

Expand Down
40 changes: 35 additions & 5 deletions app/src/main/java/com/lizongying/mytv0/ListAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.view.ViewGroup.FOCUS_BLOCK_DESCENDANTS
import androidx.core.content.ContextCompat
import androidx.core.view.marginStart
import androidx.core.view.setPadding
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.lizongying.mytv0.databinding.ListItemBinding
Expand Down Expand Up @@ -125,11 +126,33 @@ class ListAdapter(
view.setOnKeyListener { _, keyCode, event: KeyEvent? ->
if (event?.action == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP && position == 0) {
recyclerView.smoothScrollToPosition(getItemCount() - 1)
val p = getItemCount() - 1

(recyclerView.layoutManager as? LinearLayoutManager)?.scrollToPositionWithOffset(
p,
0
)

recyclerView.postDelayed({
val v = recyclerView.findViewHolderForAdapterPosition(p)
v?.itemView?.isSelected = true
v?.itemView?.requestFocus()
}, 0)
}

if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && position == getItemCount() - 1) {
recyclerView.smoothScrollToPosition(0)
val p = 0

(recyclerView.layoutManager as? LinearLayoutManager)?.scrollToPositionWithOffset(
p,
0
)

recyclerView.postDelayed({
val v = recyclerView.findViewHolderForAdapterPosition(p)
v?.itemView?.isSelected = true
v?.itemView?.requestFocus()
}, 0)
}

return@setOnKeyListener listener?.onKey(this, keyCode) ?: false
Expand Down Expand Up @@ -202,9 +225,16 @@ class ListAdapter(

fun toPosition(position: Int) {
recyclerView.post {
recyclerView.scrollToPosition(position)
recyclerView.getChildAt(position)?.isSelected
recyclerView.getChildAt(position)?.requestFocus()
(recyclerView.layoutManager as? LinearLayoutManager)?.scrollToPositionWithOffset(
position,
0
)

recyclerView.postDelayed({
val viewHolder = recyclerView.findViewHolderForAdapterPosition(position)
viewHolder?.itemView?.isSelected = true
viewHolder?.itemView?.requestFocus()
}, 0)
}
}

Expand Down
28 changes: 24 additions & 4 deletions app/src/main/java/com/lizongying/mytv0/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ class MainActivity : FragmentActivity() {
"播放上次频道".showToast(Toast.LENGTH_LONG)
}
}

// TODO group position

val port = PortUtil.findFreePort()
if (port != -1) {
server = SimpleServer(this, port)
Expand All @@ -135,14 +138,13 @@ class MainActivity : FragmentActivity() {
if (tvModel.errInfo.value != null
&& tvModel.tv.id == TVList.position.value
) {
Log.i(TAG, "errInfo ${tvModel.tv.title} ${tvModel.errInfo.value}")
hideFragment(loadingFragment)
if (tvModel.errInfo.value == "") {
Log.i(TAG, "hideErrorFragment ${tvModel.errInfo.value.toString()}")
Log.i(TAG, "${tvModel.tv.title} 播放中")
hideErrorFragment()
showFragment(playerFragment)
} else {
Log.i(TAG, "showErrorFragment ${tvModel.errInfo.value.toString()}")
Log.i(TAG, "${tvModel.tv.title} ${tvModel.errInfo.value.toString()}")
hideFragment(playerFragment)
showErrorFragment(tvModel.errInfo.value.toString())
}
Expand Down Expand Up @@ -268,7 +270,7 @@ class MainActivity : FragmentActivity() {
}

fun onPlayEnd() {
val tvModel = TVList.getTVModelCurrent()
val tvModel = TVList.getTVModel()
if (SP.repeatInfo) {
infoFragment.show(tvModel)
if (SP.channelNum) {
Expand All @@ -278,27 +280,45 @@ class MainActivity : FragmentActivity() {
}

fun play(position: Int) {
val prevGroup = TVList.getTVModel().groupIndex
if (position > -1 && position < TVList.size()) {
TVList.setPosition(position)
val currentGroup = TVList.getTVModel().groupIndex
if (currentGroup != prevGroup) {
Log.i(TAG, "group change")
menuFragment.updateList(currentGroup)
}
} else {
Toast.makeText(this, "频道不存在", Toast.LENGTH_LONG).show()
}
}

fun prev() {
val prevGroup = TVList.getTVModel().groupIndex
var position = TVList.position.value?.dec() ?: 0
if (position == -1) {
position = TVList.size() - 1
}
TVList.setPosition(position)
val currentGroup = TVList.getTVModel().groupIndex
if (currentGroup != prevGroup) {
Log.i(TAG, "group change")
menuFragment.updateList(currentGroup)
}
}

fun next() {
val prevGroup = TVList.getTVModel().groupIndex
var position = TVList.position.value?.inc() ?: 0
if (position == TVList.size()) {
position = 0
}
TVList.setPosition(position)
val currentGroup = TVList.getTVModel().groupIndex
if (currentGroup != prevGroup) {
Log.i(TAG, "group change")
menuFragment.updateList(currentGroup)
}
}

private fun showFragment(fragment: Fragment) {
Expand Down
44 changes: 40 additions & 4 deletions app/src/main/java/com/lizongying/mytv0/MenuFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList
}
}

fun updateList(position: Int) {
val tvListModel = TVList.groupModel.getTVListModel(position)
Log.i(TAG, "updateList tvListModel $position ${tvListModel?.size()}")
if (tvListModel != null) {
(binding.list.adapter as ListAdapter).update(tvListModel)
TVList.groupModel.setPosition(position)
SP.positionGroup = position
}
}

private fun hideSelf() {
requireActivity().supportFragmentManager.beginTransaction()
.hide(this)
Expand All @@ -97,7 +107,7 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList
}
}

override fun onItemClicked(tvListModel: TVListModel) {
override fun onItemClicked(position: Int) {
}

override fun onItemFocusChange(tvModel: TVModel, hasFocus: Boolean) {
Expand All @@ -121,7 +131,18 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList
binding.group.visibility = GONE
groupAdapter.focusable(false)
listAdapter.focusable(true)
listAdapter.toPosition(listAdapter.tvListModel.position.value!!)
listAdapter.toPosition(TVList.getTVModel().listIndex)


if (TVList.getTVModel().groupIndex == TVList.groupModel.position.value!!) {
Log.i(
TAG,
"list on show toPosition ${TVList.getTVModel().tv.title} ${TVList.getTVModel().listIndex}/${listAdapter.tvListModel.size()}"
)
listAdapter.toPosition(TVList.getTVModel().listIndex)
} else {
listAdapter.toPosition(0)
}
return true
}

Expand Down Expand Up @@ -159,11 +180,21 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList
// groupAdapter.focusable(false)
// listAdapter.focusable(true)
// }

Log.i(
TAG,
"list on show toPosition ${listAdapter.tvListModel.position.value!!}/${listAdapter.tvListModel.size()}"
"groupIndex ${TVList.getTVModel().groupIndex} ${TVList.groupModel.position.value!!}"
)
listAdapter.toPosition(listAdapter.tvListModel.position.value!!)

if (TVList.getTVModel().groupIndex == TVList.groupModel.position.value!!) {
Log.i(
TAG,
"list on show toPosition ${TVList.getTVModel().tv.title} ${TVList.getTVModel().listIndex}/${listAdapter.tvListModel.size()}"
)
listAdapter.toPosition(TVList.getTVModel().listIndex)
} else {
listAdapter.toPosition(0)
}
}
if (binding.group.isVisible) {
// groupAdapter.focusable(true)
Expand All @@ -183,6 +214,11 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList
}
}

fun listAdapterToPosition(tvModel: TVModel) {
// listAdapter.toPosition(tvModel.index)
// Log.i(TAG, "listAdapterToPosition ${tvModel.tv.title} ${tvModel.index}/${listAdapter.tvListModel.size()} ${listAdapter.tvListModel.getTVModel(tvModel.index)?.tv?.title}")
}

override fun onResume() {
super.onResume()
// groupAdapter.toPosition(TVList.groupModel.position.value!!)
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/lizongying/mytv0/PlayerFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ class PlayerFragment : Fragment(), SurfaceHolder.Callback {
override fun onIsPlayingChanged(isPlaying: Boolean) {
super.onIsPlayingChanged(isPlaying)
if (isPlaying) {
Log.i(TAG, "播放中")
tvModel?.setErrInfo("")
} else {
Log.i(TAG, "播放停止")
Log.i(TAG, "${tvModel?.tv?.title} 播放停止")
// tvModel?.setErrInfo("播放停止")
}
}
Expand Down
Loading

0 comments on commit cde311f

Please sign in to comment.