Skip to content

Commit

Permalink
optimize style
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Dec 23, 2024
1 parent db04909 commit 7738b88
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 26 deletions.
2 changes: 1 addition & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 更新日誌

### v1.3.8.13-kitkat
### v1.3.8.14-kitkat

* 優化樣式

Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/lizongying/mytv0/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -312,8 +314,7 @@ class MainViewModel : ViewModel() {
when (string[0]) {
'[' -> {
try {
val type = object : com.google.gson.reflect.TypeToken<List<TV>>() {}.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")
Expand Down
15 changes: 8 additions & 7 deletions app/src/main/java/com/lizongying/mytv0/SP.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<List<Source>>() {}.type
DEFAULT_SOURCES = gson.toJson(
Gua().decode(str).trim().split("\n").map { i ->
Source(
uri = i
)
}, typeSourceList
) ?: ""
}
}
Expand Down
8 changes: 3 additions & 5 deletions app/src/main/java/com/lizongying/mytv0/SimpleServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -72,8 +71,7 @@ class SimpleServer(private val context: Context, private val viewModel: MainView

if (!SP.sources.isNullOrEmpty()) {
try {
val type = object : TypeToken<List<Source>>() {}.type
val sources: List<Source> = gson.fromJson(SP.sources!!, type)
val sources: List<Source> = gson.fromJson(SP.sources!!, typeSourceList)
history = sources.toMutableList()
} catch (e: Exception) {
e.printStackTrace()
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/lizongying/mytv0/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/lizongying/mytv0/data/Global.kt
Original file line number Diff line number Diff line change
@@ -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<List<TV>>() {}.type

val typeSourceList = object : TypeToken<List<Source>>() {}.type
}
14 changes: 6 additions & 8 deletions app/src/main/java/com/lizongying/mytv0/models/Sources.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Source>>() {}.type
private val gson = Gson()
var version = 0

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

private fun setSources(sources: List<Source>) {
_sources.value = sources
SP.sources = gson.toJson(sources, type) ?: ""
SP.sources = gson.toJson(sources, typeSourceList) ?: ""
}

fun addSource(source: Source) {
Expand All @@ -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++
Expand All @@ -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++
Expand All @@ -117,7 +115,7 @@ class Sources {
fun init() {
if (!SP.sources.isNullOrEmpty()) {
try {
val sources: List<Source> = gson.fromJson(SP.sources!!, type)
val sources: List<Source> = gson.fromJson(SP.sources!!, typeSourceList)
setSources(sources.map { it.apply { checked = false } })
} catch (e: Exception) {
e.printStackTrace()
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version_code": 16975885, "version_name": "v1.3.8.13-kitkat"}
{"version_code": 16975886, "version_name": "v1.3.8.14-kitkat"}

0 comments on commit 7738b88

Please sign in to comment.