Skip to content

Commit

Permalink
optimize logo cache
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Jan 24, 2025
1 parent ce87970 commit 8893242
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 34 deletions.
8 changes: 8 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
## 更新日誌

### v1.3.8.17-kitkat

* 優化LOGO緩存

### v1.3.8.17

* 優化LOGO緩存

### v1.3.8.16-kitkat

* 增加LOGO緩存
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ adb install my-tv-0.apk
* 無效的頻道?
* 如果上次播放頻道不在收藏?
* 當list為空,顯示group/空group不顯示?
* 默認頻道菜單顯示
* 遠程配置使用webView

## 讚賞
Expand Down
22 changes: 15 additions & 7 deletions app/src/main/java/com/lizongying/mytv0/ImageHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, File> = mutableMapOf()
private val cacheDir = context.cacheDir

private var dir: File = File(cacheDir, LOGO)
private val files = ConcurrentHashMap<String, File>()

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
}
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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"
Expand Down
62 changes: 37 additions & 25 deletions app/src/main/java/com/lizongying/mytv0/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Boolean>()
Expand Down Expand Up @@ -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))

Expand All @@ -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()
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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<String, List<EPG>> = gson.fromJson(str, typeEPGMap)

Expand Down Expand Up @@ -553,6 +561,10 @@ class MainViewModel : ViewModel() {

groupModel.setChange()

viewModelScope.launch {
preloadLogo()
}

return true
}

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/lizongying/mytv0/SettingFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version_code": 16975888, "version_name": "v1.3.8.16-kitkat"}
{"version_code": 16975889, "version_name": "v1.3.8.17-kitkat"}

0 comments on commit 8893242

Please sign in to comment.