Skip to content

Commit

Permalink
menu auto close
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Jan 10, 2024
1 parent d0c7627 commit 61f6212
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 53 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

## 更新日志

### v1.3.3

* 部分错误会提示用户
* 菜单3秒钟后自动关闭

### v1.3.2

* 增加重试,减少因网络问题导致的播放失败
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/lizongying/mytv/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class MainActivity : FragmentActivity() {

private lateinit var gestureDetector: GestureDetector

private val handler = Handler()
private val delay: Long = 3000

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand Down Expand Up @@ -86,13 +89,23 @@ class MainActivity : FragmentActivity() {

if (mainFragment.isHidden) {
transaction.show(mainFragment)
keepRunnable()
} else {
transaction.hide(mainFragment)
}

transaction.commit()
}

fun keepRunnable() {
handler.removeCallbacks(hideRunnable)
handler.postDelayed(hideRunnable, delay)
}

private val hideRunnable = Runnable {
supportFragmentManager.beginTransaction().hide(mainFragment).commit()
}

private fun mainFragmentIsHidden(): Boolean {
return mainFragment.isHidden
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/lizongying/mytv/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Toast
import androidx.leanback.app.BrowseSupportFragment
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.HeaderItem
Expand Down Expand Up @@ -63,6 +64,13 @@ class MainFragment : BrowseSupportFragment() {
handler.post(mUpdateProgramRunnable)

tvListViewModel.getTVListViewModel().value?.forEach { tvViewModel ->
tvViewModel.errInfo.observe(viewLifecycleOwner) { _ ->
if (tvViewModel.errInfo.value != null
&& tvViewModel.id.value == itemPosition
) {
Toast.makeText(context, tvViewModel.errInfo.value, Toast.LENGTH_SHORT).show()
}
}
tvViewModel.ready.observe(viewLifecycleOwner) { _ ->

// not first time && channel not change
Expand Down Expand Up @@ -305,6 +313,7 @@ class MainFragment : BrowseSupportFragment() {
) {
if (item is TVViewModel) {
tvListViewModel.setItemPositionCurrent(item.id.value!!)
(activity as MainActivity).keepRunnable()
}
}
}
Expand Down
95 changes: 42 additions & 53 deletions app/src/main/java/com/lizongying/mytv/Request.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ class Request {
"海南卫视" to "海南卫视",
)

private var token: String? = null

fun initYSP(context: Context) {
ysp = YSP(context)
}
Expand Down Expand Up @@ -135,21 +133,26 @@ class Request {
Log.e(TAG, "$title key error")
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
tvModel.retryTimes++
fetchData(tvModel)
fetchVideo(tvModel, cookie)
}
}
} else {
Log.e(TAG, "$title url error $request")
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
tvModel.retryTimes++
fetchData(tvModel)
if (liveInfo?.data?.errinfo != null && liveInfo.data.errinfo != "success!") {
Log.e(TAG, "$title url error ${liveInfo.data.errinfo}")
tvModel.setErrInfo(liveInfo.data.errinfo)
} else {
Log.e(TAG, "$title url error $request $liveInfo")
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
tvModel.retryTimes++
fetchVideo(tvModel, cookie)
}
}
}
} else {
Log.e(TAG, "$title status error")
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
tvModel.retryTimes++
fetchData(tvModel)
fetchVideo(tvModel, cookie)
}
}
}
Expand All @@ -158,61 +161,47 @@ class Request {
Log.e(TAG, "$title request error")
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
tvModel.retryTimes++
fetchData(tvModel)
fetchVideo(tvModel, cookie)
}
}
})
}

fun fetchData(tvModel: TVViewModel) {
var cookie = "guid=1; vplatform=109"
val channels = arrayOf(
"CCTV3 综艺",
"CCTV6 电影",
"CCTV8 电视剧",
"风云剧场",
"第一剧场",
"怀旧剧场",
"世界地理",
"风云音乐",
"兵器科技",
"风云足球",
"高尔夫网球",
"女性时尚",
"央视文化精品",
"央视台球",
"电视指南",
"卫生健康",
)
if (tvModel.title.value in channels) {
yspTokenService.getInfo()
.enqueue(object : Callback<Info> {
override fun onResponse(call: Call<Info>, response: Response<Info>) {
if (response.isSuccessful) {
val info = response.body()
token = info?.data?.token
Log.i(TAG, "info success $token")
cookie =
"guid=1; vplatform=109; yspopenid=vu0-8lgGV2LW9QjDeuBFsX8yMnzs37Q3_HZF6XyVDpGR_I; vusession=$token"
fetchVideo(tvModel, cookie)
} else {
Log.e(TAG, "info status error")
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
tvModel.retryTimes++
fetchData(tvModel)
}
}
}

override fun onFailure(call: Call<Info>, t: Throwable) {
Log.e(TAG, "info request error $t")
fun fetchVideo(tvModel: TVViewModel) {
yspTokenService.getInfo()
.enqueue(object : Callback<Info> {
override fun onResponse(call: Call<Info>, response: Response<Info>) {
if (response.isSuccessful) {
val info = response.body()
val token = info?.data?.token
Log.i(TAG, "info success $token")
val cookie =
"guid=1; vplatform=109; yspopenid=vu0-8lgGV2LW9QjDeuBFsX8yMnzs37Q3_HZF6XyVDpGR_I; vusession=$token"
fetchVideo(tvModel, cookie)
} else {
Log.e(TAG, "info status error")
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
tvModel.retryTimes++
fetchData(tvModel)
fetchVideo(tvModel)
}
}
})
}

override fun onFailure(call: Call<Info>, t: Throwable) {
Log.e(TAG, "info request error $t")
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
tvModel.retryTimes++
fetchVideo(tvModel)
}
}
})
}

fun fetchData(tvModel: TVViewModel) {
if (tvModel.needToken) {
fetchVideo(tvModel)
} else {
val cookie = "guid=1; vplatform=109"
fetchVideo(tvModel, cookie)
}
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/lizongying/mytv/api/LiveInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ data class LiveInfo(
data class LiveInfoData(
val chanll: String,
val playurl: String,
val errinfo: String,
)

data class LiveInfoRequest(
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/com/lizongying/mytv/models/TVViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class TVViewModel(private var tv: TV) : ViewModel() {
var retryMaxTimes: Int = 3
var programUpdateTime: Long = 0

private val _errInfo = MutableLiveData<String>()
val errInfo: LiveData<String>
get() = _errInfo

private val _programId = MutableLiveData<String>()
val programId: LiveData<String>
get() = _programId
Expand Down Expand Up @@ -71,6 +75,27 @@ class TVViewModel(private var tv: TV) : ViewModel() {

var seq = 0

var needToken = false

private val channelsNeedToken = arrayOf(
"CCTV3 综艺",
"CCTV6 电影",
"CCTV8 电视剧",
"风云剧场",
"第一剧场",
"怀旧剧场",
"世界地理",
"风云音乐",
"兵器科技",
"风云足球",
"高尔夫网球",
"女性时尚",
"央视文化精品",
"央视台球",
"电视指南",
"卫生健康",
)

fun addVideoUrl(url: String) {
if (_videoUrl.value?.isNotEmpty() == true) {
if (_videoUrl.value!!.last().contains("cctv.cn")) {
Expand Down Expand Up @@ -121,6 +146,10 @@ class TVViewModel(private var tv: TV) : ViewModel() {
_pid.value = tv.pid
_sid.value = tv.sid
_program.value = mutableListOf()

if (tv.title in channelsNeedToken) {
needToken = true
}
}

fun getRowPosition(): Int {
Expand All @@ -139,6 +168,10 @@ class TVViewModel(private var tv: TV) : ViewModel() {
itemPosition = position
}

fun setErrInfo(info: String) {
_errInfo.value = info
}

fun update(t: TV) {
tv = t
}
Expand Down

0 comments on commit 61f6212

Please sign in to comment.