Skip to content

Commit 7738b88

Browse files
committed
optimize style
1 parent db04909 commit 7738b88

File tree

8 files changed

+36
-26
lines changed

8 files changed

+36
-26
lines changed

HISTORY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## 更新日誌
22

3-
### v1.3.8.13-kitkat
3+
### v1.3.8.14-kitkat
44

55
* 優化樣式
66

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import com.lizongying.mytv0.Utils.getDateFormat
1313
import com.lizongying.mytv0.Utils.getUrls
1414
import com.lizongying.mytv0.bodyAlias
1515
import com.lizongying.mytv0.codeAlias
16+
import com.lizongying.mytv0.data.Global.gson
17+
import com.lizongying.mytv0.data.Global.typeTvList
1618
import com.lizongying.mytv0.data.Source
1719
import com.lizongying.mytv0.data.SourceType
1820
import com.lizongying.mytv0.data.TV
@@ -312,8 +314,7 @@ class MainViewModel : ViewModel() {
312314
when (string[0]) {
313315
'[' -> {
314316
try {
315-
val type = object : com.google.gson.reflect.TypeToken<List<TV>>() {}.type
316-
list = com.google.gson.Gson().fromJson(string, type)
317+
list = gson.fromJson(string, typeTvList)
317318
Log.i(TAG, "导入频道 ${list.size}")
318319
} catch (e: Exception) {
319320
Log.i(TAG, "parse error $string")

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ package com.lizongying.mytv0
44
import android.content.Context
55
import android.content.SharedPreferences
66
import android.util.Log
7-
import com.google.gson.Gson
8-
import com.google.gson.reflect.TypeToken
7+
import com.lizongying.mytv0.data.Global.gson
8+
import com.lizongying.mytv0.data.Global.typeSourceList
99
import com.lizongying.mytv0.data.Source
1010
import io.github.lizongying.Gua
1111

@@ -93,11 +93,12 @@ object SP {
9393
.use {
9494
val str = it.readText()
9595
if (str.isNotEmpty()) {
96-
DEFAULT_SOURCES = Gson().toJson(Gua().decode(str).trim().split("\n").map { i ->
97-
Source(
98-
uri = i
99-
)
100-
}, object : TypeToken<List<Source>>() {}.type
96+
DEFAULT_SOURCES = gson.toJson(
97+
Gua().decode(str).trim().split("\n").map { i ->
98+
Source(
99+
uri = i
100+
)
101+
}, typeSourceList
101102
) ?: ""
102103
}
103104
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import android.net.Uri
99
import android.os.Handler
1010
import android.os.Looper
1111
import android.util.Log
12-
import com.google.gson.Gson
13-
import com.google.gson.reflect.TypeToken
1412
import com.lizongying.mytv0.Utils.getUrls
13+
import com.lizongying.mytv0.data.Global.gson
14+
import com.lizongying.mytv0.data.Global.typeSourceList
1515
import com.lizongying.mytv0.data.ReqSettings
1616
import com.lizongying.mytv0.data.ReqSourceAdd
1717
import com.lizongying.mytv0.data.ReqSources
@@ -30,7 +30,6 @@ import java.nio.charset.StandardCharsets
3030
class SimpleServer(private val context: Context, private val viewModel: MainViewModel) :
3131
NanoHTTPD(PORT) {
3232
private val handler = Handler(Looper.getMainLooper())
33-
private val gson = Gson()
3433

3534
init {
3635
try {
@@ -72,8 +71,7 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
7271

7372
if (!SP.sources.isNullOrEmpty()) {
7473
try {
75-
val type = object : TypeToken<List<Source>>() {}.type
76-
val sources: List<Source> = gson.fromJson(SP.sources!!, type)
74+
val sources: List<Source> = gson.fromJson(SP.sources!!, typeSourceList)
7775
history = sources.toMutableList()
7876
} catch (e: Exception) {
7977
e.printStackTrace()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import android.util.Log
55
import android.util.TypedValue
66
import androidx.lifecycle.LiveData
77
import androidx.lifecycle.MutableLiveData
8-
import com.google.gson.Gson
98
import com.lizongying.mytv0.ISP.CHINA_MOBILE
109
import com.lizongying.mytv0.ISP.CHINA_TELECOM
1110
import com.lizongying.mytv0.ISP.CHINA_UNICOM
1211
import com.lizongying.mytv0.ISP.UNKNOWN
12+
import com.lizongying.mytv0.data.Global.gson
1313
import com.lizongying.mytv0.requests.HttpClient
1414
import kotlinx.coroutines.CoroutineScope
1515
import kotlinx.coroutines.Dispatchers
@@ -111,7 +111,7 @@ object Utils {
111111
HttpClient.okHttpClient.newCall(request).execute().use { response ->
112112
if (!response.isSuccessful) return@withContext UNKNOWN
113113
val string = response.bodyAlias()?.string()
114-
val isp = Gson().fromJson(string, IpInfo::class.java).location.isp_domain
114+
val isp = gson.fromJson(string, IpInfo::class.java).location.isp_domain
115115
when (isp) {
116116
"ChinaMobile" -> CHINA_MOBILE
117117
"ChinaUnicom" -> CHINA_UNICOM
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.lizongying.mytv0.data
2+
3+
import com.google.gson.Gson
4+
import com.google.gson.reflect.TypeToken
5+
6+
object Global {
7+
val gson = Gson()
8+
9+
val typeTvList = object : TypeToken<List<TV>>() {}.type
10+
11+
val typeSourceList = object : TypeToken<List<Source>>() {}.type
12+
}

app/src/main/java/com/lizongying/mytv0/models/Sources.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ package com.lizongying.mytv0.models
33
import android.util.Log
44
import androidx.lifecycle.LiveData
55
import androidx.lifecycle.MutableLiveData
6-
import com.google.gson.Gson
7-
import com.google.gson.reflect.TypeToken
86
import com.lizongying.mytv0.SP
7+
import com.lizongying.mytv0.data.Global.gson
8+
import com.lizongying.mytv0.data.Global.typeSourceList
99
import com.lizongying.mytv0.data.Source
1010

1111
class Sources {
12-
private val type = object : TypeToken<List<Source>>() {}.type
13-
private val gson = Gson()
1412
var version = 0
1513

1614
private val _removed = MutableLiveData<Pair<Int, Int>>()
@@ -59,7 +57,7 @@ class Sources {
5957

6058
private fun setSources(sources: List<Source>) {
6159
_sources.value = sources
62-
SP.sources = gson.toJson(sources, type) ?: ""
60+
SP.sources = gson.toJson(sources, typeSourceList) ?: ""
6361
}
6462

6563
fun addSource(source: Source) {
@@ -73,7 +71,7 @@ class Sources {
7371

7472
_checked.value = 0
7573
setSourceChecked(checkedValue, true)
76-
SP.sources = gson.toJson(sourcesValue, type) ?: ""
74+
SP.sources = gson.toJson(sourcesValue, typeSourceList) ?: ""
7775

7876
_changed.value = version
7977
version++
@@ -91,7 +89,7 @@ class Sources {
9189
_sources.value = sourcesValue.toMutableList().apply {
9290
removeAt(index)
9391
}
94-
SP.sources = gson.toJson(sourcesValue, type) ?: ""
92+
SP.sources = gson.toJson(sourcesValue, typeSourceList) ?: ""
9593

9694
_removed.value = Pair(index, version)
9795
version++
@@ -117,7 +115,7 @@ class Sources {
117115
fun init() {
118116
if (!SP.sources.isNullOrEmpty()) {
119117
try {
120-
val sources: List<Source> = gson.fromJson(SP.sources!!, type)
118+
val sources: List<Source> = gson.fromJson(SP.sources!!, typeSourceList)
121119
setSources(sources.map { it.apply { checked = false } })
122120
} catch (e: Exception) {
123121
e.printStackTrace()

version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version_code": 16975885, "version_name": "v1.3.8.13-kitkat"}
1+
{"version_code": 16975886, "version_name": "v1.3.8.14-kitkat"}

0 commit comments

Comments
 (0)