From 8893242b91d7c8568544d03f8f4764507cec2eaa Mon Sep 17 00:00:00 2001 From: Li ZongYing Date: Fri, 24 Jan 2025 09:53:58 +0800 Subject: [PATCH] optimize logo cache --- HISTORY.md | 8 +++ README.md | 1 - .../java/com/lizongying/mytv0/ImageHelper.kt | 22 ++++--- .../com/lizongying/mytv0/MainViewModel.kt | 62 +++++++++++-------- .../com/lizongying/mytv0/SettingFragment.kt | 5 ++ version.json | 2 +- 6 files changed, 66 insertions(+), 34 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 797525d..afebd47 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,13 @@ ## 更新日誌 +### v1.3.8.17-kitkat + +* 優化LOGO緩存 + +### v1.3.8.17 + +* 優化LOGO緩存 + ### v1.3.8.16-kitkat * 增加LOGO緩存 diff --git a/README.md b/README.md index ce392b6..adc69cd 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,6 @@ adb install my-tv-0.apk * 無效的頻道? * 如果上次播放頻道不在收藏? * 當list為空,顯示group/空group不顯示? -* 默認頻道菜單顯示 * 遠程配置使用webView ## 讚賞 diff --git a/app/src/main/java/com/lizongying/mytv0/ImageHelper.kt b/app/src/main/java/com/lizongying/mytv0/ImageHelper.kt index 9d355ad..b077e38 100644 --- a/app/src/main/java/com/lizongying/mytv0/ImageHelper.kt +++ b/app/src/main/java/com/lizongying/mytv0/ImageHelper.kt @@ -10,20 +10,21 @@ import com.lizongying.mytv0.requests.HttpClient import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import java.io.File +import java.util.concurrent.ConcurrentHashMap class ImageHelper(private val context: Context) { - val cacheDir = context.cacheDir - val files: MutableMap = mutableMapOf() + private val cacheDir = context.cacheDir + + private var dir: File = File(cacheDir, LOGO) + private val files = ConcurrentHashMap() init { - val dir = File(cacheDir, LOGO) if (!dir.exists()) { dir.mkdir() } dir.listFiles()?.forEach { file -> - val name = file.name.substringBeforeLast(".") - files[name] = file + files[file.name] = file } } @@ -62,9 +63,9 @@ class ImageHelper(private val context: Context) { } for (url in urlList) { - val ext = url.substringBeforeLast("?").substringAfterLast(".") - val file = File(cacheDir, "$LOGO/$key.$ext") + val file = File(cacheDir, "$LOGO/$key") if (downloadImage(url, file)) { + files[file.name] = file Log.i(TAG, "image download success ${file.absolutePath}") break } @@ -101,6 +102,13 @@ class ImageHelper(private val context: Context) { } } + fun clearImage() { + val dir = File(cacheDir, LOGO) + if (dir.exists()) { + dir.deleteRecursively() + } + } + companion object { const val TAG = "ImageHelper" const val LOGO = "logo" diff --git a/app/src/main/java/com/lizongying/mytv0/MainViewModel.kt b/app/src/main/java/com/lizongying/mytv0/MainViewModel.kt index c097781..bd8383e 100644 --- a/app/src/main/java/com/lizongying/mytv0/MainViewModel.kt +++ b/app/src/main/java/com/lizongying/mytv0/MainViewModel.kt @@ -7,6 +7,7 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.google.gson.JsonSyntaxException +import com.lizongying.mytv0.ImageHelper import com.lizongying.mytv0.MyTVApplication import com.lizongying.mytv0.R import com.lizongying.mytv0.SP @@ -49,6 +50,8 @@ class MainViewModel : ViewModel() { private lateinit var cacheEPG: File private var epgUrl = SP.epg + private lateinit var imageHelper: ImageHelper + val sources = Sources() private val _channelsOk = MutableLiveData() @@ -99,6 +102,9 @@ class MainViewModel : ViewModel() { } fun init(context: Context) { + val application = context.applicationContext as MyTVApplication + imageHelper = application.imageHelper + groupModel.addTVListModel(TVListModel("我的收藏", 0)) groupModel.addTVListModel(TVListModel("全部頻道", 1)) @@ -121,7 +127,7 @@ class MainViewModel : ViewModel() { try { str2Channels(cacheChannels) } catch (e: Exception) { - e.printStackTrace() + Log.e(TAG, "init", e) cacheFile!!.deleteOnExit() R.string.channel_read_error.showToast() } @@ -140,34 +146,36 @@ class MainViewModel : ViewModel() { } } - val application = context.applicationContext as MyTVApplication - val imageHelper = application.imageHelper + initialized = true - viewModelScope.launch { - for (tvModel in listModel) { - var name = tvModel.tv.name - if (name.isEmpty()) { - name = tvModel.tv.title - } - val url = tvModel.tv.logo - var urls = - listOf( - "https://live.fanmingming.cn/tv/$name.png" - ) + getUrls("https://raw.githubusercontent.com/fanmingming/live/main/tv/$name.png") - if (url.isNotEmpty()) { - urls = (getUrls(url) + urls).distinct() - } + _channelsOk.value = true + } - imageHelper.preloadImage( - name, - urls, - ) - } + suspend fun preloadLogo() { + if (!this::imageHelper.isInitialized) { + Log.w(TAG, "imageHelper is not initialized") + return } - initialized = true + for (tvModel in listModel) { + var name = tvModel.tv.name + if (name.isEmpty()) { + name = tvModel.tv.title + } + val url = tvModel.tv.logo + var urls = + listOf( + "https://live.fanmingming.cn/tv/$name.png" + ) + getUrls("https://raw.githubusercontent.com/fanmingming/live/main/tv/$name.png") + if (url.isNotEmpty()) { + urls = (getUrls(url) + urls).distinct() + } - _channelsOk.value = true + imageHelper.preloadImage( + name, + urls, + ) + } } suspend fun readEPG(input: InputStream): Boolean = withContext(Dispatchers.IO) { @@ -200,7 +208,7 @@ class MainViewModel : ViewModel() { } } - suspend fun readEPG(str: String): Boolean = withContext(Dispatchers.IO) { + private suspend fun readEPG(str: String): Boolean = withContext(Dispatchers.IO) { try { val res: Map> = gson.fromJson(str, typeEPGMap) @@ -553,6 +561,10 @@ class MainViewModel : ViewModel() { groupModel.setChange() + viewModelScope.launch { + preloadLogo() + } + return true } diff --git a/app/src/main/java/com/lizongying/mytv0/SettingFragment.kt b/app/src/main/java/com/lizongying/mytv0/SettingFragment.kt index f177b88..1e4b35a 100644 --- a/app/src/main/java/com/lizongying/mytv0/SettingFragment.kt +++ b/app/src/main/java/com/lizongying/mytv0/SettingFragment.kt @@ -286,6 +286,9 @@ class SettingFragment : Fragment() { val context = requireActivity() val mainActivity = (activity as MainActivity) + val application = context.applicationContext as MyTVApplication + val imageHelper = application.imageHelper + viewModel = ViewModelProvider(context)[MainViewModel::class.java] binding.switchDisplaySeconds.setOnCheckedChangeListener { _, isChecked -> @@ -306,6 +309,8 @@ class SettingFragment : Fragment() { SP.configAutoLoad = SP.DEFAULT_CONFIG_AUTO_LOAD SP.proxy = SP.DEFAULT_PROXY + imageHelper.clearImage() + // TODO update player SP.softDecode = SP.DEFAULT_SOFT_DECODE diff --git a/version.json b/version.json index 3569c55..36d54ab 100644 --- a/version.json +++ b/version.json @@ -1 +1 @@ -{"version_code": 16975888, "version_name": "v1.3.8.16-kitkat"} +{"version_code": 16975889, "version_name": "v1.3.8.17-kitkat"}