Skip to content

Commit

Permalink
optimize the remote configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Oct 29, 2024
1 parent 7df1559 commit b02c038
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 77 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.8.0

* 優化遠程配置

### v1.3.7.20

* 解決一個可能引起閃退的問題
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* 遙控器返回鍵關閉視頻列表/配置
* 在聚焦視頻標題的時候,右鍵收藏/取消收藏
* 打開配置后,選擇遠程配置,掃描二維碼可以配置視頻源等。也可以直接遠程配置地址 http://0.0.0.0:34567
* 如果視頻源地址已配置,並且打開了“應用啟動后自動更新視頻源”后,應用啟動后會自動更新視頻源
* 如果視頻源地址已配置,並且打開了“應用啟動后更新視頻源”后,應用啟動后會自動更新視頻源
* 默認遙控器下鍵/觸屏下滑切換到下一個視頻。換台反轉打開後,邏輯相反

注意:
Expand Down
90 changes: 55 additions & 35 deletions app/src/main/java/com/lizongying/mytv0/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class MainViewModel : ViewModel() {
private lateinit var appDirectory: File
var listModel: List<TVModel> = listOf()
val groupModel = TVGroupModel()

private var cacheFile: File? = null
private var cacheConfig = ""
private var initialized = false

private val _channelsOk = MutableLiveData<Boolean>()
val channelsOk: LiveData<Boolean>
Expand Down Expand Up @@ -71,27 +73,43 @@ class MainViewModel : ViewModel() {
}
}

private fun getCache(): String {
return if (cacheFile!!.exists()) {
cacheFile!!.readText()
} else {
""
}
}

fun init(context: Context) {
groupModel.addTVListModel(TVListModel("我的收藏", 0))
groupModel.addTVListModel(TVListModel("全部頻道", 1))

appDirectory = context.filesDir
val file = File(appDirectory, FILE_NAME)
val str = if (file.exists()) {
file.readText()
} else {
context.resources.openRawResource(R.raw.channels).bufferedReader()
cacheFile = File(appDirectory, FILE_NAME)
if (!cacheFile!!.exists()) {
cacheFile!!.createNewFile()
}

cacheConfig = getCache()
Log.i(TAG, "cacheConfig $cacheConfig")

if (cacheConfig.isEmpty()) {
cacheConfig = context.resources.openRawResource(R.raw.channels).bufferedReader()
.use { it.readText() }
Log.i(TAG, "cacheConfig $cacheConfig")
}

try {
str2List(str)
str2List(cacheConfig)
} catch (e: Exception) {
e.printStackTrace()
file.deleteOnExit()
cacheFile!!.deleteOnExit()
R.string.channel_read_error.showToast()
}

initialized = true

_channelsOk.value = true
}

Expand Down Expand Up @@ -128,20 +146,9 @@ class MainViewModel : ViewModel() {
val response = HttpClient.okHttpClient.newCall(request).execute()

if (response.isSuccessful) {
val file = File(appDirectory, FILE_NAME)
if (!file.exists()) {
file.createNewFile()
}
val str = response.body!!.string()
val str = response.body?.string() ?: ""
withContext(Dispatchers.Main) {
if (str2List(str)) {
file.writeText(str)
SP.config = serverUrl
_channelsOk.value = true
R.string.channel_import_success.showToast()
} else {
R.string.channel_import_error.showToast()
}
tryStr2List(str, null, serverUrl)
}
} else {
Log.e(TAG, "Request status ${response.code}")
Expand Down Expand Up @@ -186,32 +193,45 @@ class MainViewModel : ViewModel() {
return
}

try {
if (str2List(str)) {
SP.config = uri.toString()
R.string.channel_import_success.showToast()
} else {
R.string.channel_import_error.showToast()
}
} catch (e: Exception) {
e.printStackTrace()
file.deleteOnExit()
R.string.channel_read_error.showToast()
}
tryStr2List(str, file, uri.toString())
} else {
CoroutineScope(Dispatchers.IO).launch {
update(uri.toString())
}
}
}

fun str2List(str: String): Boolean {
fun tryStr2List(str: String, file: File?, url: String) {
try {
if (str2List(str)) {
cacheFile!!.writeText(str)
cacheConfig = str
SP.config = url
_channelsOk.value = true
R.string.channel_import_success.showToast()
} else {
R.string.channel_import_error.showToast()
}
} catch (e: Exception) {
e.printStackTrace()
file?.deleteOnExit()
R.string.channel_read_error.showToast()
}
}

private fun str2List(str: String): Boolean {
var string = str
if (initialized && string == cacheConfig) {
return false
}
val g = Gua()
if (g.verify(str)) {
string = g.decode(str)
}
if (string.isBlank()) {
if (string.isEmpty()) {
return false
}
if (initialized && string == cacheConfig) {
return false
}

Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/com/lizongying/mytv0/ModalFragment.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.lizongying.mytv0

import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.os.Looper
Expand Down Expand Up @@ -47,8 +49,14 @@ class ModalFragment : DialogFragment() {
Glide.with(requireContext())
.load(bitmap)
.into(binding.modalImage)
binding.modalText.text = arguments?.getString(KEY_TEXT)
val text = arguments?.getString(KEY_TEXT)
binding.modalText.text = text
binding.modalText.visibility = View.VISIBLE
binding.modal.setOnClickListener {
val url = "http://$text"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
}
} else {
Glide.with(requireContext())
.load(arguments?.getInt(KEY_DRAWABLE_ID))
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/com/lizongying/mytv0/SettingFragment.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lizongying.mytv0

import MainViewModel
import MainViewModel.Companion.FILE_NAME
import android.Manifest
import android.content.pm.PackageManager
import android.graphics.drawable.ColorDrawable
Expand All @@ -21,6 +22,7 @@ import androidx.lifecycle.ViewModelProvider
import com.lizongying.mytv0.ModalFragment.Companion.KEY_BITMAP
import com.lizongying.mytv0.ModalFragment.Companion.KEY_TEXT
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 @@ -119,7 +121,7 @@ class SettingFragment : Fragment() {
binding.qrcode.setOnClickListener {
val imageModalFragment = ModalFragment()
val size = Utils.dpToPx(200)
val img = QrCodeUtil().createQRCodeBitmap(server, size, size)
val img = QrCodeUtil().createQRCodeBitmap("$server?${getDateTimestamp()}", size, size)
val args = Bundle()
args.putString(KEY_TEXT, server.removePrefix("http://"))
args.putParcelable(KEY_BITMAP, img)
Expand Down Expand Up @@ -292,15 +294,14 @@ class SettingFragment : Fragment() {

SP.config = SP.DEFAULT_CONFIG_URL
Log.i(TAG, "config url: ${SP.config}")
context.deleteFile(FILE_NAME)
viewModel.reset(context)
confirmConfig()

SP.channel = SP.DEFAULT_CHANNEL
Log.i(TAG, "default channel: ${SP.channel}")
confirmChannel()

// context.deleteFile(FILE_NAME)

SP.deleteLike()
Log.i(TAG, "clear like")

Expand Down
19 changes: 12 additions & 7 deletions app/src/main/java/com/lizongying/mytv0/SimpleServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,19 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
private fun handleSettings(): Response {
val response: String
try {
val file = File(context.filesDir, FILE_NAME)
var str = if (file.exists()) {
file.readText()
} else {
""
}
if (str.isEmpty()) {
str = context.resources.openRawResource(R.raw.channels).bufferedReader()
.use { it.readText() }
}
val respSettings = RespSettings(
channelUri = SP.config ?: "",
channelText = str,
channelDefault = SP.channel,
proxy = SP.proxy ?: "",
epg = SP.epg ?: "",
Expand All @@ -68,13 +79,7 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
try {
readBody(session)?.let {
handler.post {
if (viewModel.str2List(it)) {
File(context.filesDir, FILE_NAME).writeText(it)
SP.config = "file://"
R.string.channel_import_success.showToast()
} else {
R.string.channel_import_error.showToast()
}
viewModel.tryStr2List(it, null, "")
}
}
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.lizongying.mytv0.data

data class RespSettings(
val channelUri: String,
val channelText: String,
val channelDefault: Int,
val proxy: String,
val epg: String,
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/modal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/modal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
Expand Down
Loading

0 comments on commit b02c038

Please sign in to comment.