@@ -7,6 +7,7 @@ import androidx.lifecycle.MutableLiveData
77import androidx.lifecycle.ViewModel
88import androidx.lifecycle.viewModelScope
99import com.google.gson.JsonSyntaxException
10+ import com.lizongying.mytv0.ImageHelper
1011import com.lizongying.mytv0.MyTVApplication
1112import com.lizongying.mytv0.R
1213import 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
0 commit comments