Skip to content

Commit

Permalink
fix crash
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Aug 15, 2024
1 parent 31ec3c2 commit 131c189
Show file tree
Hide file tree
Showing 12 changed files with 535 additions and 509 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 更新日誌

### v1.3.7.7

* 修復閃退問題

### v1.3.7.6

* 增加錯誤上報
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ adb install my-tv-0.apk
* 多源管理
* 如果上次播放頻道不在收藏?
* 當list為空,顯示group
* 默認頻道菜單顯示

## 讚賞

Expand Down
13 changes: 11 additions & 2 deletions app/src/main/java/com/lizongying/mytv0/ChannelFragment.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lizongying.mytv0

import MainViewModel
import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
Expand All @@ -8,8 +9,8 @@ import android.view.ViewGroup
import androidx.core.view.marginEnd
import androidx.core.view.marginTop
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.lizongying.mytv0.databinding.ChannelBinding
import com.lizongying.mytv0.models.TVList
import com.lizongying.mytv0.models.TVModel

class ChannelFragment : Fragment() {
Expand All @@ -21,6 +22,8 @@ class ChannelFragment : Fragment() {
private var channel = 0
private var channelCount = 0

private lateinit var viewModel: MainViewModel

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
Expand All @@ -47,6 +50,12 @@ class ChannelFragment : Fragment() {
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val context = requireActivity()
viewModel = ViewModelProvider(context)[MainViewModel::class.java]
}

fun show(tvViewModel: TVModel) {
handler.removeCallbacks(hideRunnable)
handler.removeCallbacks(playRunnable)
Expand All @@ -56,7 +65,7 @@ class ChannelFragment : Fragment() {
}

fun show(channel: String) {
if (TVList.groupModel.getCurrent()!!.tv.id > 10 && TVList.groupModel.getCurrent()!!.tv.id == this.channel - 1) {
if (viewModel.groupModel.getCurrent()!!.tv.id > 10 && viewModel.groupModel.getCurrent()!!.tv.id == this.channel - 1) {
this.channel = 0
channelCount = 0
}
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/com/lizongying/mytv0/InitializerProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package com.lizongying.mytv0
import android.content.ContentProvider
import android.content.ContentValues
import android.net.Uri
import com.lizongying.mytv0.models.TVList

internal class InitializerProvider : ContentProvider() {

// Happens before Application#onCreate.It's fine to init something here
override fun onCreate(): Boolean {
SP.init(context!!)
TVList.init(context!!)
return true
}

Expand Down
135 changes: 70 additions & 65 deletions app/src/main/java/com/lizongying/mytv0/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.lizongying.mytv0.models.TVList
import java.util.Locale


Expand Down Expand Up @@ -98,6 +97,7 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)

viewModel = ViewModelProvider(this)[MainViewModel::class.java]
viewModel.init(this)

if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
Expand Down Expand Up @@ -133,11 +133,11 @@ class MainActivity : AppCompatActivity() {
fun ready(tag: String) {
Log.i(TAG, "ready $tag")
ok++
if (ok == 3) {
if (ok == 4) {
Log.i(TAG, "watch")
TVList.groupModel.change.observe(this) { _ ->
viewModel.groupModel.change.observe(this) { _ ->
Log.i(TAG, "groupModel changed")
if (TVList.groupModel.tvGroup.value != null) {
if (viewModel.groupModel.tvGroup.value != null) {
watch()
Log.i(TAG, "menuFragment update")
menuFragment.update()
Expand All @@ -151,22 +151,22 @@ class MainActivity : AppCompatActivity() {
// "播放收藏频道".showToast()
// }


val prevGroup = TVList.groupModel.positionValue
Log.i(TAG, "SP.channel ${SP.channel}")
val tvModel = if (SP.channel > 0) {
val position = if (SP.channel < TVList.listModel.size) {
// R.string.play_default_channel.showToast()
SP.channel - 1

} else {
// R.string.default_channel_out_of_range.showToast()
SP.channel = 0
0
}
TVList.groupModel.getPosition(position)
} else {
Log.i(TAG, "group ${TVList.groupModel.positionValue}")
viewModel.channelsOk.observe(this) { it ->
if (it) {
val prevGroup = viewModel.groupModel.positionValue
Log.i(TAG, "SP.channel ${SP.channel}")
val tvModel = if (SP.channel > 0) {
val position = if (SP.channel < viewModel.listModel.size) {
// R.string.play_default_channel.showToast()
SP.channel - 1
} else {
// R.string.default_channel_out_of_range.showToast()
SP.channel = 0
0
}
viewModel.groupModel.getPosition(position)
} else {
Log.i(TAG, "group ${viewModel.groupModel.positionValue}")
// if (SP.position < 0 || SP.position >= TVList.groupModel.getTVListModelNotFilter(1)!!
// .size()
// ) {
Expand All @@ -176,33 +176,38 @@ class MainActivity : AppCompatActivity() {
// // R.string.play_last_channel.showToast()
// SP.position
// }
TVList.groupModel.getCurrent()
}
tvModel?.setReady()
Log.i(TAG, "ready tv ${tvModel!!.tv.name}")
TVList.groupModel.setPositionPlaying(TVList.groupModel.positionValue)
TVList.groupModel.getTVListModelNotFilter(TVList.groupModel.positionValue)?.let {
Log.i(TAG, "list name ${it.getName()}")
it.setPositionPlaying(it.positionValue)
}

val currentGroup = TVList.groupModel.positionValue
if (currentGroup != prevGroup) {
Log.i(TAG, "group change")
menuFragment.updateList(currentGroup)
}
viewModel.groupModel.getCurrent()
}
tvModel?.setReady()
viewModel.groupModel.setPositionPlaying(viewModel.groupModel.positionValue)
viewModel.groupModel.getTVListModelNotFilter(viewModel.groupModel.positionValue)
?.let {
Log.i(TAG, "list name ${it.getName()}")
it.setPositionPlaying(it.positionValue)
}

val currentGroup = viewModel.groupModel.positionValue
if (currentGroup != prevGroup) {
Log.i(TAG, "group change")
menuFragment.updateList(currentGroup)
}

TVList.groupModel.isInLikeMode = SP.defaultLike && TVList.groupModel.positionValue == 0
if (TVList.groupModel.isInLikeMode) {
viewModel.groupModel.isInLikeMode =
SP.defaultLike && viewModel.groupModel.positionValue == 0
if (viewModel.groupModel.isInLikeMode) {
// R.string.favorite_mode.showToast()
} else {
} else {
// R.string.standard_mode.showToast()
}

// TODO group position
viewModel.updateEPG()
}
}

// TODO group position
viewModel.updateEPG()
server = SimpleServer(this, viewModel)

server = SimpleServer(this)
viewModel.updateConfig()
}
}

Expand All @@ -211,7 +216,7 @@ class MainActivity : AppCompatActivity() {
}

private fun watch() {
TVList.listModel.forEach { tvModel ->
viewModel.listModel.forEach { tvModel ->
tvModel.errInfo.observe(this) { _ ->

if (tvModel.errInfo.value != null
Expand Down Expand Up @@ -252,9 +257,9 @@ class MainActivity : AppCompatActivity() {
if (tvModel.like.value != null && tvModel.tv.id != -1) {
val liked = tvModel.like.value as Boolean
if (liked) {
TVList.groupModel.getTVListModel(0)?.replaceTVModel(tvModel)
viewModel.groupModel.getTVListModel(0)?.replaceTVModel(tvModel)
} else {
TVList.groupModel.getTVListModel(0)?.removeTVModel(tvModel.tv.id)
viewModel.groupModel.getTVListModel(0)?.removeTVModel(tvModel.tv.id)
}
SP.setLike(tvModel.tv.id, liked)
}
Expand Down Expand Up @@ -366,7 +371,7 @@ class MainActivity : AppCompatActivity() {
}

fun onPlayEnd() {
val tvModel = TVList.groupModel.getCurrent()!!
val tvModel = viewModel.groupModel.getCurrent()!!
if (SP.repeatInfo) {
infoFragment.show(tvModel)
if (SP.channelNum) {
Expand All @@ -376,18 +381,18 @@ class MainActivity : AppCompatActivity() {
}

fun play(position: Int) {
if (position > -1 && position < TVList.groupModel.getTVListModelNotFilter(1)!!.size()) {
val prevGroup = TVList.groupModel.positionValue
val tvModel = TVList.groupModel.getPosition(position)
if (position > -1 && position < viewModel.groupModel.getTVListModelNotFilter(1)!!.size()) {
val prevGroup = viewModel.groupModel.positionValue
val tvModel = viewModel.groupModel.getPosition(position)

tvModel!!.setReady()
Log.i(TAG, "play tv ${tvModel.tv.name}")
TVList.groupModel.setPositionPlaying(TVList.groupModel.positionValue)
TVList.groupModel.getTVListModelNotFilter(TVList.groupModel.positionValue)?.let {
viewModel.groupModel.setPositionPlaying(viewModel.groupModel.positionValue)
viewModel.groupModel.getTVListModelNotFilter(viewModel.groupModel.positionValue)?.let {
it.setPositionPlaying(it.positionValue)
}

val currentGroup = TVList.groupModel.positionValue
val currentGroup = viewModel.groupModel.positionValue
if (currentGroup != prevGroup) {
menuFragment.updateList(currentGroup)
}
Expand All @@ -397,50 +402,50 @@ class MainActivity : AppCompatActivity() {
}

fun prev() {
val prevGroup = TVList.groupModel.positionValue
val prevGroup = viewModel.groupModel.positionValue
val tvModel =
if (SP.defaultLike && TVList.groupModel.isInLikeMode && TVList.groupModel.getTVListModel(
if (SP.defaultLike && viewModel.groupModel.isInLikeMode && viewModel.groupModel.getTVListModel(
0
) != null
) {
TVList.groupModel.getPrev(true)
viewModel.groupModel.getPrev(true)
} else {
TVList.groupModel.getPrev()
viewModel.groupModel.getPrev()
}

tvModel!!.setReady()
Log.i(TAG, "tv ${tvModel.tv.name}")
TVList.groupModel.setPositionPlaying(TVList.groupModel.positionValue)
TVList.groupModel.getTVListModelNotFilter(TVList.groupModel.positionValue)?.let {
viewModel.groupModel.setPositionPlaying(viewModel.groupModel.positionValue)
viewModel.groupModel.getTVListModelNotFilter(viewModel.groupModel.positionValue)?.let {
it.setPositionPlaying(it.positionValue)
}

val currentGroup = TVList.groupModel.positionValue
val currentGroup = viewModel.groupModel.positionValue
if (currentGroup != prevGroup) {
menuFragment.updateList(currentGroup)
}
}

fun next() {
val prevGroup = TVList.groupModel.positionValue
val prevGroup = viewModel.groupModel.positionValue
val tvModel =
if (SP.defaultLike && TVList.groupModel.isInLikeMode && TVList.groupModel.getTVListModel(
if (SP.defaultLike && viewModel.groupModel.isInLikeMode && viewModel.groupModel.getTVListModel(
0
) != null
) {
TVList.groupModel.getNext(true)
viewModel.groupModel.getNext(true)
} else {
TVList.groupModel.getNext()
viewModel.groupModel.getNext()
}

tvModel!!.setReady()
Log.i(TAG, "tv ${tvModel.tv.name}")
TVList.groupModel.setPositionPlaying(TVList.groupModel.positionValue)
TVList.groupModel.getTVListModelNotFilter(TVList.groupModel.positionValue)?.let {
viewModel.groupModel.setPositionPlaying(viewModel.groupModel.positionValue)
viewModel.groupModel.getTVListModelNotFilter(viewModel.groupModel.positionValue)?.let {
it.setPositionPlaying(it.positionValue)
}

val currentGroup = TVList.groupModel.positionValue
val currentGroup = viewModel.groupModel.positionValue
if (currentGroup != prevGroup) {
menuFragment.updateList(currentGroup)
}
Expand Down
Loading

0 comments on commit 131c189

Please sign in to comment.