Skip to content

Commit

Permalink
fix some crash
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Dec 9, 2024
1 parent 36a3e37 commit eecdeac
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 188 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## 更新日誌

### v1.3.8.7

* 修復切換頻道不正確的問題
* EPG兼容匹配
* 修復一些閃退問題

### v1.3.8.6

* 優化遠程設置視頻源
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/lizongying/mytv0/GroupAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lizongying.mytv0

import android.content.Context
import android.util.Log
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -214,8 +215,7 @@ class GroupAdapter(
this.listener = listener
}

fun update(tvGroupModel: TVGroupModel) {
this.tvGroupModel = tvGroupModel
fun changed() {
recyclerView.post {
notifyDataSetChanged()
}
Expand Down
16 changes: 8 additions & 8 deletions app/src/main/java/com/lizongying/mytv0/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ class MainActivity : AppCompatActivity() {
Log.i(TAG, "播放上次頻道")
viewModel.groupModel.getCurrent()
}
viewModel.groupModel.setPlaying()
viewModel.groupModel.setPositionPlaying()
viewModel.groupModel.getCurrentList()
?.let {
Log.i(TAG, "當前組 ${it.getName()}")
it.setPlaying()
it.setPositionPlaying()
}
tvModel?.setReady()

Expand Down Expand Up @@ -406,8 +406,8 @@ class MainActivity : AppCompatActivity() {
val tvModel = viewModel.groupModel.getPosition(position)

tvModel?.setReady()
viewModel.groupModel.setPlaying()
viewModel.groupModel.getCurrentList()?.setPlaying()
viewModel.groupModel.setPositionPlaying()
viewModel.groupModel.getCurrentList()?.setPositionPlaying()

val currentGroup = viewModel.groupModel.positionValue
if (currentGroup != prevGroup) {
Expand All @@ -429,8 +429,8 @@ class MainActivity : AppCompatActivity() {
}

tvModel?.setReady()
viewModel.groupModel.setPlaying()
viewModel.groupModel.getCurrentList()?.setPlaying()
viewModel.groupModel.setPositionPlaying()
viewModel.groupModel.getCurrentList()?.setPositionPlaying()

val currentGroup = viewModel.groupModel.positionValue
if (currentGroup != prevGroup) {
Expand All @@ -449,8 +449,8 @@ class MainActivity : AppCompatActivity() {
}

tvModel?.setReady()
viewModel.groupModel.setPlaying()
viewModel.groupModel.getCurrentList()?.setPlaying()
viewModel.groupModel.setPositionPlaying()
viewModel.groupModel.getCurrentList()?.setPositionPlaying()

val currentGroup = viewModel.groupModel.positionValue
if (currentGroup != prevGroup) {
Expand Down
22 changes: 18 additions & 4 deletions app/src/main/java/com/lizongying/mytv0/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,36 @@ class MainViewModel : ViewModel() {

private suspend fun updateEPG(epg: String) {
var shouldBreak = false
val request = okhttp3.Request.Builder().url(epg).build()
for (i in 0..2) {
try {
withContext(Dispatchers.IO) {
val request = okhttp3.Request.Builder().url(epg).build()
val response = HttpClient.okHttpClient.newCall(request).execute()

if (response.isSuccessful) {
val res = EPGXmlParser().parse(response.body!!.byteStream())

withContext(Dispatchers.Main) {
for (m in listModel) {
res[m.tv.name]?.let { m.setEpg(it) }
val name = m.tv.name.ifEmpty { m.tv.title }.lowercase()
if (name.isEmpty()) {
continue
}

for ((a, b) in res) {
if (name.contains(a, ignoreCase = true)) {
m.setEpg(b)
if (m.tv.logo.isEmpty()) {
m.tv.logo = "https://live.fanmingming.com/tv/$a.png"
}
break
}
}
}
}

shouldBreak = true
Log.i(TAG, "EPG success")
} else {
Log.e(TAG, "EPG ${response.code}")
}
Expand All @@ -149,7 +163,7 @@ class MainViewModel : ViewModel() {
}

if (!shouldBreak) {
R.string.epg_status_err.showToast()
// R.string.epg_status_err.showToast()
}
}

Expand Down Expand Up @@ -458,7 +472,7 @@ class MainViewModel : ViewModel() {
listModel = listModelNew

// 全部频道
(groupModel.tvGroup.value as List<TVListModel>)[1].setTVListModel(listModel)
groupModel.tvGroupValue[1].setTVListModel(listModel)

groupModel.initPosition()
groupModel.setChange()
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/lizongying/mytv0/MenuFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList

fun update() {
view?.post {
groupAdapter.update(viewModel.groupModel)
Log.e(TAG, "changed")
groupAdapter.changed()

getList()?.let {
(binding.list.adapter as ListAdapter).update(it)
Expand Down Expand Up @@ -154,10 +155,10 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList
}

override fun onItemClicked(position: Int, type: String) {
viewModel.groupModel.setPlaying()
viewModel.groupModel.setPositionPlaying()
viewModel.groupModel.getCurrentList()?.let {
it.setPosition(position)
it.setPlaying()
it.setPositionPlaying()
it.getCurrent()?.setReady()
}
(activity as MainActivity).hideMenuFragment()
Expand Down Expand Up @@ -200,7 +201,6 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList
groupAdapter.focusable(true)
listAdapter.focusable(false)
listAdapter.clear()
Log.i(TAG, "group toPosition on left")
groupAdapter.scrollToPositionAndSelect(viewModel.groupModel.positionValue)
return true
}
Expand Down
22 changes: 12 additions & 10 deletions app/src/main/java/com/lizongying/mytv0/ModalFragment.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.lizongying.mytv0

import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.os.Handler
Expand Down Expand Up @@ -43,19 +42,23 @@ class ModalFragment : DialogFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

val bitmap: Bitmap? = arguments?.getParcelable(KEY_BITMAP)
val url = arguments?.getString(KEY_URL)
if (!url.isNullOrEmpty()) {
val size = Utils.dpToPx(200)
val img = QrCodeUtil().createQRCodeBitmap(url, size, size)

if (bitmap != null) {
Glide.with(requireContext())
.load(bitmap)
.load(img)
.into(binding.modalImage)
val text = arguments?.getString(KEY_TEXT)
binding.modalText.text = text
binding.modalText.text = url.removePrefix("http://")
binding.modalText.visibility = View.VISIBLE
binding.modal.setOnClickListener {
val url = "http://$text"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
try {
startActivity(intent)
} catch (e: Exception) {
e.printStackTrace()
}
}
} else {
Glide.with(requireContext())
Expand All @@ -81,8 +84,7 @@ class ModalFragment : DialogFragment() {

companion object {
const val KEY_DRAWABLE_ID = "drawable_id"
const val KEY_BITMAP = "bitmap"
const val KEY_TEXT = "text"
const val KEY_URL = "url"
const val TAG = "ModalFragment"
}
}
20 changes: 9 additions & 11 deletions app/src/main/java/com/lizongying/mytv0/SettingFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import androidx.core.view.marginEnd
import androidx.core.view.marginTop
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.lizongying.mytv0.ModalFragment.Companion.KEY_BITMAP
import com.lizongying.mytv0.ModalFragment.Companion.KEY_TEXT
import com.lizongying.mytv0.ModalFragment.Companion.KEY_URL
import com.lizongying.mytv0.SimpleServer.Companion.PORT
import com.lizongying.mytv0.Utils.getDateTimestamp
import com.lizongying.mytv0.databinding.SettingBinding
import kotlin.math.max
import kotlin.math.min
Expand Down Expand Up @@ -120,11 +118,8 @@ class SettingFragment : Fragment() {

binding.qrcode.setOnClickListener {
val imageModalFragment = ModalFragment()
val size = Utils.dpToPx(200)
val img = QrCodeUtil().createQRCodeBitmap("$server?${getDateTimestamp()}", size, size)
val args = Bundle()
args.putString(KEY_TEXT, server.removePrefix("http://"))
args.putParcelable(KEY_BITMAP, img)
args.putString(KEY_URL, server)
imageModalFragment.arguments = args

imageModalFragment.show(requireFragmentManager(), ModalFragment.TAG)
Expand Down Expand Up @@ -292,6 +287,9 @@ class SettingFragment : Fragment() {
SP.channelNum = SP.DEFAULT_CHANNEL_NUM

SP.sources = SP.DEFAULT_SOURCES
Log.i(TAG, "DEFAULT_SOURCES ${SP.DEFAULT_SOURCES}")
viewModel.sources.init()

SP.channelReversal = SP.DEFAULT_CHANNEL_REVERSAL
SP.time = SP.DEFAULT_TIME
SP.bootStartup = SP.DEFAULT_BOOT_STARTUP
Expand Down Expand Up @@ -325,8 +323,8 @@ class SettingFragment : Fragment() {
tvListModel?.setPosition(SP.DEFAULT_POSITION)
tvListModel?.setPositionPlaying(SP.DEFAULT_POSITION)

viewModel.groupModel.setPlaying()
viewModel.groupModel.getCurrentList()?.setPlaying()
viewModel.groupModel.setPositionPlaying()
viewModel.groupModel.getCurrentList()?.setPositionPlaying()
viewModel.groupModel.getCurrent()?.setReady()

SP.showAllChannels = SP.DEFAULT_SHOW_ALL_CHANNELS
Expand All @@ -342,8 +340,8 @@ class SettingFragment : Fragment() {

binding.switchShowAllChannels.setOnCheckedChangeListener { _, isChecked ->
SP.showAllChannels = isChecked
viewModel.groupModel.tvGroup.value?.let { viewModel.groupModel.setTVListModelList(it) }
mainActivity.update()
viewModel.groupModel.setChange()

mainActivity.settingActive()
}
}
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/com/lizongying/mytv0/SimpleServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import com.lizongying.mytv0.data.ReqSourceAdd
import com.lizongying.mytv0.data.ReqSources
import com.lizongying.mytv0.data.RespSettings
import com.lizongying.mytv0.data.Source
import com.lizongying.mytv0.models.Sources
import fi.iki.elonen.NanoHTTPD
import java.io.File
import java.io.IOException
Expand Down Expand Up @@ -220,8 +219,14 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
val req = Gson().fromJson(it, ReqSources::class.java)
Log.i(TAG, "req $req")
if (req.sourceId.isNotEmpty()) {
viewModel.sources.removeSource(req.sourceId)
val res = viewModel.sources.removeSource(req.sourceId)
if (res) {
Log.i(TAG, "remove source success ${req.sourceId}")
} else {
Log.i(TAG, "remove source failure ${req.sourceId}")
}
} else {
Log.i(TAG, "remove source failure, sourceId is empty")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class EPGXmlParser {
parser.next()
}
}
return epg

return epg.toSortedMap { a, b -> b.compareTo(a) }
}
}
15 changes: 8 additions & 7 deletions app/src/main/java/com/lizongying/mytv0/models/Sources.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import com.lizongying.mytv0.data.Source

class Sources {
private val type = object : TypeToken<List<Source>>() {}.type
var version = 0
private val gson = Gson()
var version = 0

private val _removed = MutableLiveData<Pair<Int, Int>>()
val removed: LiveData<Pair<Int, Int>>
Expand Down Expand Up @@ -61,10 +61,6 @@ class Sources {
}

fun addSource(source: Source) {
if (_sources.value == null) {
_sources.value = mutableListOf(source)
}

val index = sourcesValue.indexOfFirst { it.uri == source.uri }
if (index == -1) {
_sources.value = sourcesValue.toMutableList().apply {
Expand All @@ -77,9 +73,10 @@ class Sources {
}
}

fun removeSource(id: String) {
fun removeSource(id: String): Boolean {
if (sourcesValue.isEmpty()) {
return
Log.i(TAG, "sources is empty")
return false
}

val index = sourcesValue.indexOfFirst { it.id == id }
Expand All @@ -91,7 +88,11 @@ class Sources {

_removed.value = Pair(index, version)
version++
return true
}

Log.i(TAG, "sourceId is not exists")
return false
}

fun getSource(idx: Int): Source? {
Expand Down
Loading

0 comments on commit eecdeac

Please sign in to comment.