diff --git a/HISTORY.md b/HISTORY.md index 56bd8ed0..eeee3c4f 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,6 @@ ## 更新日誌 -### v1.3.8.13-kitkat +### v1.3.8.14-kitkat * 優化樣式 diff --git a/app/src/main/java/com/lizongying/mytv0/MainViewModel.kt b/app/src/main/java/com/lizongying/mytv0/MainViewModel.kt index 627378fb..7161f8f7 100644 --- a/app/src/main/java/com/lizongying/mytv0/MainViewModel.kt +++ b/app/src/main/java/com/lizongying/mytv0/MainViewModel.kt @@ -13,6 +13,8 @@ import com.lizongying.mytv0.Utils.getDateFormat import com.lizongying.mytv0.Utils.getUrls import com.lizongying.mytv0.bodyAlias import com.lizongying.mytv0.codeAlias +import com.lizongying.mytv0.data.Global.gson +import com.lizongying.mytv0.data.Global.typeTvList import com.lizongying.mytv0.data.Source import com.lizongying.mytv0.data.SourceType import com.lizongying.mytv0.data.TV @@ -312,8 +314,7 @@ class MainViewModel : ViewModel() { when (string[0]) { '[' -> { try { - val type = object : com.google.gson.reflect.TypeToken>() {}.type - list = com.google.gson.Gson().fromJson(string, type) + list = gson.fromJson(string, typeTvList) Log.i(TAG, "导入频道 ${list.size}") } catch (e: Exception) { Log.i(TAG, "parse error $string") diff --git a/app/src/main/java/com/lizongying/mytv0/SP.kt b/app/src/main/java/com/lizongying/mytv0/SP.kt index 1d869f31..26d5104d 100644 --- a/app/src/main/java/com/lizongying/mytv0/SP.kt +++ b/app/src/main/java/com/lizongying/mytv0/SP.kt @@ -4,8 +4,8 @@ package com.lizongying.mytv0 import android.content.Context import android.content.SharedPreferences import android.util.Log -import com.google.gson.Gson -import com.google.gson.reflect.TypeToken +import com.lizongying.mytv0.data.Global.gson +import com.lizongying.mytv0.data.Global.typeSourceList import com.lizongying.mytv0.data.Source import io.github.lizongying.Gua @@ -93,11 +93,12 @@ object SP { .use { val str = it.readText() if (str.isNotEmpty()) { - DEFAULT_SOURCES = Gson().toJson(Gua().decode(str).trim().split("\n").map { i -> - Source( - uri = i - ) - }, object : TypeToken>() {}.type + DEFAULT_SOURCES = gson.toJson( + Gua().decode(str).trim().split("\n").map { i -> + Source( + uri = i + ) + }, typeSourceList ) ?: "" } } diff --git a/app/src/main/java/com/lizongying/mytv0/SimpleServer.kt b/app/src/main/java/com/lizongying/mytv0/SimpleServer.kt index f02bfdd4..ea31e1db 100644 --- a/app/src/main/java/com/lizongying/mytv0/SimpleServer.kt +++ b/app/src/main/java/com/lizongying/mytv0/SimpleServer.kt @@ -9,9 +9,9 @@ import android.net.Uri import android.os.Handler import android.os.Looper import android.util.Log -import com.google.gson.Gson -import com.google.gson.reflect.TypeToken import com.lizongying.mytv0.Utils.getUrls +import com.lizongying.mytv0.data.Global.gson +import com.lizongying.mytv0.data.Global.typeSourceList import com.lizongying.mytv0.data.ReqSettings import com.lizongying.mytv0.data.ReqSourceAdd import com.lizongying.mytv0.data.ReqSources @@ -30,7 +30,6 @@ import java.nio.charset.StandardCharsets class SimpleServer(private val context: Context, private val viewModel: MainViewModel) : NanoHTTPD(PORT) { private val handler = Handler(Looper.getMainLooper()) - private val gson = Gson() init { try { @@ -72,8 +71,7 @@ class SimpleServer(private val context: Context, private val viewModel: MainView if (!SP.sources.isNullOrEmpty()) { try { - val type = object : TypeToken>() {}.type - val sources: List = gson.fromJson(SP.sources!!, type) + val sources: List = gson.fromJson(SP.sources!!, typeSourceList) history = sources.toMutableList() } catch (e: Exception) { e.printStackTrace() diff --git a/app/src/main/java/com/lizongying/mytv0/Utils.kt b/app/src/main/java/com/lizongying/mytv0/Utils.kt index d021c485..27748cc1 100644 --- a/app/src/main/java/com/lizongying/mytv0/Utils.kt +++ b/app/src/main/java/com/lizongying/mytv0/Utils.kt @@ -5,11 +5,11 @@ import android.util.Log import android.util.TypedValue import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData -import com.google.gson.Gson import com.lizongying.mytv0.ISP.CHINA_MOBILE import com.lizongying.mytv0.ISP.CHINA_TELECOM import com.lizongying.mytv0.ISP.CHINA_UNICOM import com.lizongying.mytv0.ISP.UNKNOWN +import com.lizongying.mytv0.data.Global.gson import com.lizongying.mytv0.requests.HttpClient import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -111,7 +111,7 @@ object Utils { HttpClient.okHttpClient.newCall(request).execute().use { response -> if (!response.isSuccessful) return@withContext UNKNOWN val string = response.bodyAlias()?.string() - val isp = Gson().fromJson(string, IpInfo::class.java).location.isp_domain + val isp = gson.fromJson(string, IpInfo::class.java).location.isp_domain when (isp) { "ChinaMobile" -> CHINA_MOBILE "ChinaUnicom" -> CHINA_UNICOM diff --git a/app/src/main/java/com/lizongying/mytv0/data/Global.kt b/app/src/main/java/com/lizongying/mytv0/data/Global.kt new file mode 100644 index 00000000..b7b30358 --- /dev/null +++ b/app/src/main/java/com/lizongying/mytv0/data/Global.kt @@ -0,0 +1,12 @@ +package com.lizongying.mytv0.data + +import com.google.gson.Gson +import com.google.gson.reflect.TypeToken + +object Global { + val gson = Gson() + + val typeTvList = object : TypeToken>() {}.type + + val typeSourceList = object : TypeToken>() {}.type +} \ No newline at end of file diff --git a/app/src/main/java/com/lizongying/mytv0/models/Sources.kt b/app/src/main/java/com/lizongying/mytv0/models/Sources.kt index cdc2f619..5cfd6658 100644 --- a/app/src/main/java/com/lizongying/mytv0/models/Sources.kt +++ b/app/src/main/java/com/lizongying/mytv0/models/Sources.kt @@ -3,14 +3,12 @@ package com.lizongying.mytv0.models import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData -import com.google.gson.Gson -import com.google.gson.reflect.TypeToken import com.lizongying.mytv0.SP +import com.lizongying.mytv0.data.Global.gson +import com.lizongying.mytv0.data.Global.typeSourceList import com.lizongying.mytv0.data.Source class Sources { - private val type = object : TypeToken>() {}.type - private val gson = Gson() var version = 0 private val _removed = MutableLiveData>() @@ -59,7 +57,7 @@ class Sources { private fun setSources(sources: List) { _sources.value = sources - SP.sources = gson.toJson(sources, type) ?: "" + SP.sources = gson.toJson(sources, typeSourceList) ?: "" } fun addSource(source: Source) { @@ -73,7 +71,7 @@ class Sources { _checked.value = 0 setSourceChecked(checkedValue, true) - SP.sources = gson.toJson(sourcesValue, type) ?: "" + SP.sources = gson.toJson(sourcesValue, typeSourceList) ?: "" _changed.value = version version++ @@ -91,7 +89,7 @@ class Sources { _sources.value = sourcesValue.toMutableList().apply { removeAt(index) } - SP.sources = gson.toJson(sourcesValue, type) ?: "" + SP.sources = gson.toJson(sourcesValue, typeSourceList) ?: "" _removed.value = Pair(index, version) version++ @@ -117,7 +115,7 @@ class Sources { fun init() { if (!SP.sources.isNullOrEmpty()) { try { - val sources: List = gson.fromJson(SP.sources!!, type) + val sources: List = gson.fromJson(SP.sources!!, typeSourceList) setSources(sources.map { it.apply { checked = false } }) } catch (e: Exception) { e.printStackTrace() diff --git a/version.json b/version.json index 530481fa..cb7aabbd 100644 --- a/version.json +++ b/version.json @@ -1 +1 @@ -{"version_code": 16975885, "version_name": "v1.3.8.13-kitkat"} +{"version_code": 16975886, "version_name": "v1.3.8.14-kitkat"}