Skip to content

Commit

Permalink
support collection
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed May 11, 2024
1 parent cde311f commit c319fd9
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 53 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## 更新日志

### v1.1.9

* 菜单打开时,不能打开频道列表
* 频道号大于1000以上时兼容样式
* 增加收藏功能

### v1.1.8

* 频道列表优化
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* 视频源可以设置为本地文件,格式如:file:///mnt/sdcard/tmp/channels.m3u
/channels.m3u
* 如果设置的是本地文件,则软件重新启动后不会自动更新。需要手动确认。
* 可能需要在设置页面请求权限
* 高版本可能需要授权

目前支持的配置格式:

Expand Down Expand Up @@ -75,8 +75,11 @@ adb install my-tv-0.apk

## TODO

* 收藏夹
* 节目增加预告
* 兼容4.0
* 插件商城
* UI
* 视频解码

## 赞赏

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/lizongying/mytv0/GroupAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class GroupAdapter(
private var listener: ItemListener? = null
private var focused: View? = null
private var defaultFocused = false
var defaultFocus: Int = -1
private var defaultFocus: Int = -1

var visiable = false

Expand Down
27 changes: 26 additions & 1 deletion app/src/main/java/com/lizongying/mytv0/ListAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ListAdapter(
private var listener: ItemListener? = null
private var focused: View? = null
private var defaultFocused = false
var defaultFocus: Int = -1
private var defaultFocus: Int = -1

var visiable = false

Expand Down Expand Up @@ -94,6 +94,8 @@ class ListAdapter(
view.isFocusableInTouchMode = true
// view.alpha = 0.8F

viewHolder.like(tvModel.like.value as Boolean)

if (!defaultFocused && position == defaultFocus) {
view.requestFocus()
defaultFocused = true
Expand Down Expand Up @@ -155,6 +157,11 @@ class ListAdapter(
}, 0)
}

if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
tvModel.setLike(!(tvModel.like.value as Boolean))
viewHolder.like(tvModel.like.value as Boolean)
}

return@setOnKeyListener listener?.onKey(this, keyCode) ?: false
}
false
Expand Down Expand Up @@ -221,6 +228,24 @@ class ListAdapter(
binding.root.setBackgroundResource(R.color.blur)
}
}

fun like(liked: Boolean) {
if (liked) {
binding.heart.setImageDrawable(
ContextCompat.getDrawable(
context,
R.drawable.ic_heart
)
)
} else {
binding.heart.setImageDrawable(
ContextCompat.getDrawable(
context,
R.drawable.ic_heart_empty
)
)
}
}
}

fun toPosition(position: Int) {
Expand Down
20 changes: 17 additions & 3 deletions app/src/main/java/com/lizongying/mytv0/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class MainActivity : FragmentActivity() {
private var settingFragment = SettingFragment()

private val handler = Handler(Looper.myLooper()!!)
private val delayHideMenu = 10000L
private val delayHideSetting = 60000L
private val delayHideMenu = 10 * 1000L
private val delayHideSetting = 3 * 60 * 1000L

private var doubleBackToExitPressedOnce = false

Expand Down Expand Up @@ -167,6 +167,18 @@ class MainActivity : FragmentActivity() {
}
}
}

tvModel.like.observe(this) { _ ->
if (tvModel.like.value != null) {
val liked = tvModel.like.value as Boolean
if (liked) {
TVList.groupModel.getTVListModel(0)?.replaceTVModel(tvModel)
} else {
TVList.groupModel.getTVListModel(0)?.removeTVModel(tvModel.tv.id)
}
SP.setLike(tvModel.tv.id, liked)
}
}
}
}

Expand Down Expand Up @@ -594,7 +606,9 @@ class MainActivity : FragmentActivity() {
}

KeyEvent.KEYCODE_DPAD_LEFT -> {
showFragment(menuFragment)
if (settingFragment.isHidden) {
showFragment(menuFragment)
}
}

KeyEvent.KEYCODE_DPAD_RIGHT -> {
Expand Down
29 changes: 19 additions & 10 deletions app/src/main/java/com/lizongying/mytv0/MenuFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList
}

fun updateList(position: Int) {
val tvListModel = TVList.groupModel.getTVListModel(position)
TVList.groupModel.setPosition(position)
SP.positionGroup = position
val tvListModel = TVList.groupModel.getTVListModel()
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
}
}

Expand Down Expand Up @@ -165,6 +165,15 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList
groupAdapter.toPosition(TVList.groupModel.position.value!!)
return true
}
// KeyEvent.KEYCODE_DPAD_RIGHT -> {
// binding.group.visibility = VISIBLE
// groupAdapter.focusable(true)
// listAdapter.focusable(false)
// listAdapter.clear()
// Log.i(TAG, "group toPosition on left")
// groupAdapter.toPosition(TVList.groupModel.position.value!!)
// return true
// }
}
return false
}
Expand All @@ -181,12 +190,17 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList
// listAdapter.focusable(true)
// }

val groupIndex = TVList.getTVModel().groupIndex
Log.i(
TAG,
"groupIndex ${TVList.getTVModel().groupIndex} ${TVList.groupModel.position.value!!}"
"groupIndex $groupIndex ${TVList.groupModel.position.value!!}"
)

if (TVList.getTVModel().groupIndex == TVList.groupModel.position.value!!) {
if (groupIndex == TVList.groupModel.position.value!!) {
if (listAdapter.tvListModel.getIndex() != TVList.getTVModel().groupIndex) {
updateList(groupIndex)
}

Log.i(
TAG,
"list on show toPosition ${TVList.getTVModel().tv.title} ${TVList.getTVModel().listIndex}/${listAdapter.tvListModel.size()}"
Expand Down Expand Up @@ -214,11 +228,6 @@ 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
18 changes: 18 additions & 0 deletions app/src/main/java/com/lizongying/mytv0/SP.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ object SP {

private const val KEY_CHANNEL = "channel"

private const val KEY_LIKE = "like"

private lateinit var sp: SharedPreferences

/**
Expand Down Expand Up @@ -86,4 +88,20 @@ object SP {
var channel: Int
get() = sp.getInt(KEY_CHANNEL, 0)
set(value) = sp.edit().putInt(KEY_CHANNEL, value).apply()

fun getLike(id: Int): Boolean {
val stringSet = sp.getStringSet(KEY_LIKE, emptySet())
return stringSet?.contains(id.toString()) ?: false
}

fun setLike(id: Int, liked: Boolean) {
val stringSet = sp.getStringSet(KEY_LIKE, emptySet())?.toMutableSet() ?: mutableSetOf()
if (liked) {
stringSet.add(id.toString())
} else {
stringSet.remove(id.toString())
}

sp.edit().putStringSet(KEY_LIKE, stringSet).apply()
}
}
4 changes: 4 additions & 0 deletions app/src/main/java/com/lizongying/mytv0/models/TVGroupModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class TVGroupModel : ViewModel() {
getTVListModel(1)?.clear()
}

fun getTVListModel(): TVListModel? {
return getTVListModel(position.value as Int)
}

fun getTVListModel(idx: Int): TVListModel? {
if (idx >= size()) {
return null
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/lizongying/mytv0/models/TVList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ object TVList {
fun init(context: Context) {
_position.value = 0

groupModel.addTVListModel(TVListModel("我的收藏"))
groupModel.addTVListModel(TVListModel("全部频道"))
groupModel.addTVListModel(TVListModel("我的收藏", 0))
groupModel.addTVListModel(TVListModel("全部频道", 1))

appDirectory = context.filesDir
val file = File(appDirectory, FILE_NAME)
Expand Down Expand Up @@ -247,7 +247,7 @@ object TVList {
var listModelNew: MutableList<TVModel> = mutableListOf()
var groupIndex = 2
for ((k, v) in map) {
val tvListModel = TVListModel(k)
val tvListModel = TVListModel(k, groupIndex)
for ((listIndex, v1) in v.withIndex()) {
v1.groupIndex = groupIndex
v1.listIndex = listIndex
Expand Down
42 changes: 40 additions & 2 deletions app/src/main/java/com/lizongying/mytv0/models/TVListModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

class TVListModel(private val name: String) : ViewModel() {
class TVListModel(private val name: String, private val index: Int) : ViewModel() {
fun getName(): String {
return name
}

fun getIndex(): Int {
return index
}

private val _tvListModel = MutableLiveData<List<TVModel>>()
val tvListModel: LiveData<List<TVModel>>
get() = _tvListModel
Expand Down Expand Up @@ -44,13 +48,47 @@ class TVListModel(private val name: String) : ViewModel() {
_tvListModel.value = newList
}

fun removeTVModel(id: Int) {
if (_tvListModel.value == null) {
return
}
val newList = _tvListModel.value!!.toMutableList()
val iterator = newList.iterator()
while (iterator.hasNext()) {
if (iterator.next().tv.id == id) {
iterator.remove()
}
}
_tvListModel.value = newList
}

fun replaceTVModel(tvModel: TVModel) {
if (_tvListModel.value == null) {
_tvListModel.value = mutableListOf(tvModel)
return
}

val newList = _tvListModel.value!!.toMutableList()
var exists = false
val iterator = newList.iterator()
while (iterator.hasNext()) {
if (iterator.next().tv.id == tvModel.tv.id) {
exists = true
}
}
if (!exists) {
newList.add(tvModel)
_tvListModel.value = newList
}
}

fun clear() {
_tvListModel.value = mutableListOf()
setPosition(0)
}

fun getTVModel(): TVModel? {
return _tvListModel.value?.get(position.value as Int)
return getTVModel(position.value as Int)
}

fun getTVModel(idx: Int): TVModel? {
Expand Down
Loading

0 comments on commit c319fd9

Please sign in to comment.