Skip to content

Commit 8893242

Browse files
committed
optimize logo cache
1 parent ce87970 commit 8893242

File tree

6 files changed

+66
-34
lines changed

6 files changed

+66
-34
lines changed

HISTORY.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
## 更新日誌
22

3+
### v1.3.8.17-kitkat
4+
5+
* 優化LOGO緩存
6+
7+
### v1.3.8.17
8+
9+
* 優化LOGO緩存
10+
311
### v1.3.8.16-kitkat
412

513
* 增加LOGO緩存

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ adb install my-tv-0.apk
8686
* 無效的頻道?
8787
* 如果上次播放頻道不在收藏?
8888
* 當list為空,顯示group/空group不顯示?
89-
* 默認頻道菜單顯示
9089
* 遠程配置使用webView
9190

9291
## 讚賞

app/src/main/java/com/lizongying/mytv0/ImageHelper.kt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@ import com.lizongying.mytv0.requests.HttpClient
1010
import kotlinx.coroutines.Dispatchers
1111
import kotlinx.coroutines.withContext
1212
import java.io.File
13+
import java.util.concurrent.ConcurrentHashMap
1314

1415

1516
class ImageHelper(private val context: Context) {
16-
val cacheDir = context.cacheDir
17-
val files: MutableMap<String, File> = mutableMapOf()
17+
private val cacheDir = context.cacheDir
18+
19+
private var dir: File = File(cacheDir, LOGO)
20+
private val files = ConcurrentHashMap<String, File>()
1821

1922
init {
20-
val dir = File(cacheDir, LOGO)
2123
if (!dir.exists()) {
2224
dir.mkdir()
2325
}
2426
dir.listFiles()?.forEach { file ->
25-
val name = file.name.substringBeforeLast(".")
26-
files[name] = file
27+
files[file.name] = file
2728
}
2829
}
2930

@@ -62,9 +63,9 @@ class ImageHelper(private val context: Context) {
6263
}
6364

6465
for (url in urlList) {
65-
val ext = url.substringBeforeLast("?").substringAfterLast(".")
66-
val file = File(cacheDir, "$LOGO/$key.$ext")
66+
val file = File(cacheDir, "$LOGO/$key")
6767
if (downloadImage(url, file)) {
68+
files[file.name] = file
6869
Log.i(TAG, "image download success ${file.absolutePath}")
6970
break
7071
}
@@ -101,6 +102,13 @@ class ImageHelper(private val context: Context) {
101102
}
102103
}
103104

105+
fun clearImage() {
106+
val dir = File(cacheDir, LOGO)
107+
if (dir.exists()) {
108+
dir.deleteRecursively()
109+
}
110+
}
111+
104112
companion object {
105113
const val TAG = "ImageHelper"
106114
const val LOGO = "logo"

app/src/main/java/com/lizongying/mytv0/MainViewModel.kt

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.lifecycle.MutableLiveData
77
import androidx.lifecycle.ViewModel
88
import androidx.lifecycle.viewModelScope
99
import com.google.gson.JsonSyntaxException
10+
import com.lizongying.mytv0.ImageHelper
1011
import com.lizongying.mytv0.MyTVApplication
1112
import com.lizongying.mytv0.R
1213
import com.lizongying.mytv0.SP
@@ -49,6 +50,8 @@ class MainViewModel : ViewModel() {
4950
private lateinit var cacheEPG: File
5051
private var epgUrl = SP.epg
5152

53+
private lateinit var imageHelper: ImageHelper
54+
5255
val sources = Sources()
5356

5457
private val _channelsOk = MutableLiveData<Boolean>()
@@ -99,6 +102,9 @@ class MainViewModel : ViewModel() {
99102
}
100103

101104
fun init(context: Context) {
105+
val application = context.applicationContext as MyTVApplication
106+
imageHelper = application.imageHelper
107+
102108
groupModel.addTVListModel(TVListModel("我的收藏", 0))
103109
groupModel.addTVListModel(TVListModel("全部頻道", 1))
104110

@@ -121,7 +127,7 @@ class MainViewModel : ViewModel() {
121127
try {
122128
str2Channels(cacheChannels)
123129
} catch (e: Exception) {
124-
e.printStackTrace()
130+
Log.e(TAG, "init", e)
125131
cacheFile!!.deleteOnExit()
126132
R.string.channel_read_error.showToast()
127133
}
@@ -140,34 +146,36 @@ class MainViewModel : ViewModel() {
140146
}
141147
}
142148

143-
val application = context.applicationContext as MyTVApplication
144-
val imageHelper = application.imageHelper
149+
initialized = true
145150

146-
viewModelScope.launch {
147-
for (tvModel in listModel) {
148-
var name = tvModel.tv.name
149-
if (name.isEmpty()) {
150-
name = tvModel.tv.title
151-
}
152-
val url = tvModel.tv.logo
153-
var urls =
154-
listOf(
155-
"https://live.fanmingming.cn/tv/$name.png"
156-
) + getUrls("https://raw.githubusercontent.com/fanmingming/live/main/tv/$name.png")
157-
if (url.isNotEmpty()) {
158-
urls = (getUrls(url) + urls).distinct()
159-
}
151+
_channelsOk.value = true
152+
}
160153

161-
imageHelper.preloadImage(
162-
name,
163-
urls,
164-
)
165-
}
154+
suspend fun preloadLogo() {
155+
if (!this::imageHelper.isInitialized) {
156+
Log.w(TAG, "imageHelper is not initialized")
157+
return
166158
}
167159

168-
initialized = true
160+
for (tvModel in listModel) {
161+
var name = tvModel.tv.name
162+
if (name.isEmpty()) {
163+
name = tvModel.tv.title
164+
}
165+
val url = tvModel.tv.logo
166+
var urls =
167+
listOf(
168+
"https://live.fanmingming.cn/tv/$name.png"
169+
) + getUrls("https://raw.githubusercontent.com/fanmingming/live/main/tv/$name.png")
170+
if (url.isNotEmpty()) {
171+
urls = (getUrls(url) + urls).distinct()
172+
}
169173

170-
_channelsOk.value = true
174+
imageHelper.preloadImage(
175+
name,
176+
urls,
177+
)
178+
}
171179
}
172180

173181
suspend fun readEPG(input: InputStream): Boolean = withContext(Dispatchers.IO) {
@@ -200,7 +208,7 @@ class MainViewModel : ViewModel() {
200208
}
201209
}
202210

203-
suspend fun readEPG(str: String): Boolean = withContext(Dispatchers.IO) {
211+
private suspend fun readEPG(str: String): Boolean = withContext(Dispatchers.IO) {
204212
try {
205213
val res: Map<String, List<EPG>> = gson.fromJson(str, typeEPGMap)
206214

@@ -553,6 +561,10 @@ class MainViewModel : ViewModel() {
553561

554562
groupModel.setChange()
555563

564+
viewModelScope.launch {
565+
preloadLogo()
566+
}
567+
556568
return true
557569
}
558570

app/src/main/java/com/lizongying/mytv0/SettingFragment.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ class SettingFragment : Fragment() {
286286

287287
val context = requireActivity()
288288
val mainActivity = (activity as MainActivity)
289+
val application = context.applicationContext as MyTVApplication
290+
val imageHelper = application.imageHelper
291+
289292
viewModel = ViewModelProvider(context)[MainViewModel::class.java]
290293

291294
binding.switchDisplaySeconds.setOnCheckedChangeListener { _, isChecked ->
@@ -306,6 +309,8 @@ class SettingFragment : Fragment() {
306309
SP.configAutoLoad = SP.DEFAULT_CONFIG_AUTO_LOAD
307310
SP.proxy = SP.DEFAULT_PROXY
308311

312+
imageHelper.clearImage()
313+
309314
// TODO update player
310315
SP.softDecode = SP.DEFAULT_SOFT_DECODE
311316

version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version_code": 16975888, "version_name": "v1.3.8.16-kitkat"}
1+
{"version_code": 16975889, "version_name": "v1.3.8.17-kitkat"}

0 commit comments

Comments
 (0)