Skip to content

Commit

Permalink
optimize remote settings
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Dec 7, 2024
1 parent af13248 commit 36a3e37
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 95 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 更新日誌

### v1.3.8.6

* 優化遠程設置視頻源

### v1.3.8.5

* 修復視頻組名為空的情況
Expand Down
76 changes: 46 additions & 30 deletions app/src/main/java/com/lizongying/mytv0/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,34 +117,45 @@ class MainViewModel : ViewModel() {
}

private suspend fun updateEPG(epg: String) {
try {
withContext(Dispatchers.IO) {
val request = okhttp3.Request.Builder().url(epg).build()
val response = HttpClient.okHttpClient.newCall(request).execute()
var shouldBreak = false
for (i in 0..2) {
try {
withContext(Dispatchers.IO) {
val request = okhttp3.Request.Builder().url(epg).build()
val response = HttpClient.okHttpClient.newCall(request).execute()

if (response.isSuccessful) {
val res = EPGXmlParser().parse(response.body!!.byteStream())
if (response.isSuccessful) {
val res = EPGXmlParser().parse(response.body!!.byteStream())

withContext(Dispatchers.Main) {
for (m in listModel) {
res[m.tv.name]?.let { m.setEpg(it) }
withContext(Dispatchers.Main) {
for (m in listModel) {
res[m.tv.name]?.let { m.setEpg(it) }
}
}

shouldBreak = true
} else {
Log.e(TAG, "EPG ${response.code}")
}
} else {
Log.e(TAG, "EPG ${response.code}")
R.string.epg_status_err.showToast()
}
}
} catch (e: Exception) {
Log.i(TAG, "EPG request error:", e)
} catch (e: Exception) {
Log.i(TAG, "EPG request error:", e)
// R.string.epg_request_err.showToast()
}

if (shouldBreak) {
break
}
}

if (!shouldBreak) {
R.string.epg_status_err.showToast()
}
}

private suspend fun importFromUrl(serverUrl: String) {
Log.i(TAG, "request $serverUrl")
private suspend fun importFromUrl(url: String, id: String = "") {
val urls =
if (serverUrl.startsWith("https://raw.githubusercontent.com") || serverUrl.startsWith("https://github.com")) {
if (url.startsWith("https://raw.githubusercontent.com") || url.startsWith("https://github.com")) {
listOf(
"https://ghp.ci/",
"https://gh.llkk.cc/",
Expand All @@ -156,16 +167,19 @@ class MainViewModel : ViewModel() {
"https://ghproxy.com/",
"https://github.moeyy.cn/",
"https://gh-proxy.llyke.com/",
"https://www.ghproxy.cc/",
"https://cf.ghproxy.cc/"
).map {
Pair("$it$serverUrl", serverUrl)
Pair("$it$url", url)
}
} else {
listOf(Pair(serverUrl, serverUrl))
listOf(Pair(url, url))
}

var err = 0
var shouldBreak = false
for ((a, b) in urls) {
Log.i(TAG, "request $a")
try {
withContext(Dispatchers.IO) {
val request = okhttp3.Request.Builder().url(a).build()
Expand All @@ -174,7 +188,7 @@ class MainViewModel : ViewModel() {
if (response.isSuccessful) {
val str = response.body?.string() ?: ""
withContext(Dispatchers.Main) {
tryStr2Channels(str, null, b)
tryStr2Channels(str, null, b, id)
}
err = 0
shouldBreak = true
Expand Down Expand Up @@ -218,7 +232,7 @@ class MainViewModel : ViewModel() {
}
}

fun importFromUri(uri: Uri) {
fun importFromUri(uri: Uri, id: String = "") {
if (uri.scheme == "file") {
val file = uri.toFile()
Log.i(TAG, "file $file")
Expand All @@ -229,25 +243,27 @@ class MainViewModel : ViewModel() {
return
}

tryStr2Channels(str, file, uri.toString())
tryStr2Channels(str, file, uri.toString(), id)
} else {
viewModelScope.launch {
importFromUrl(uri.toString())
importFromUrl(uri.toString(), id)
}
}
}

fun tryStr2Channels(str: String, file: File?, url: String) {
fun tryStr2Channels(str: String, file: File?, url: String, id: String = "") {
try {
if (str2Channels(str)) {
cacheFile!!.writeText(str)
cacheChannels = str
if (url.isNotEmpty()) {
SP.configUrl = url
val source = Source(
id = id,
uri = url
)
sources.addSource(
Source(
uri = url
)
source
)
}
_channelsOk.value = true
Expand All @@ -266,7 +282,7 @@ class MainViewModel : ViewModel() {
var string = str
if (initialized && string == cacheChannels) {
Log.w(TAG, "same channels")
return false
return true
}

val g = Gua()
Expand All @@ -281,7 +297,7 @@ class MainViewModel : ViewModel() {

if (initialized && string == cacheChannels) {
Log.w(TAG, "same channels")
return false
return true
}

val list: List<TV>
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/lizongying/mytv0/SP.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ object SP {
const val DEFAULT_COMPACT_MENU = true
const val DEFAULT_DISPLAY_SECONDS = false
const val DEFAULT_LOG_TIMES = 10
const val DEFAULT_POSITION_GROUP = 0 // favorite

// 0 favorite; 1 all
const val DEFAULT_POSITION_GROUP = 1
const val DEFAULT_POSITION = 0
const val DEFAULT_REPEAT_INFO = true
const val DEFAULT_CONFIG_AUTO_LOAD = false
Expand Down
11 changes: 7 additions & 4 deletions app/src/main/java/com/lizongying/mytv0/SettingFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,6 @@ class SettingFragment : Fragment() {
binding.clear.setOnClickListener {
SP.channelNum = SP.DEFAULT_CHANNEL_NUM

SP.positionGroup = SP.DEFAULT_POSITION_GROUP
viewModel.groupModel.setPosition(SP.DEFAULT_POSITION_GROUP)
viewModel.groupModel.setPositionPlaying(SP.DEFAULT_POSITION_GROUP)

SP.sources = SP.DEFAULT_SOURCES
SP.channelReversal = SP.DEFAULT_CHANNEL_REVERSAL
SP.time = SP.DEFAULT_TIME
Expand All @@ -316,6 +312,13 @@ class SettingFragment : Fragment() {
SP.deleteLike()
Log.i(TAG, "clear like")

// SP.positionGroup = SP.DEFAULT_POSITION_GROUP
// viewModel.groupModel.setPosition(SP.DEFAULT_POSITION_GROUP)
// viewModel.groupModel.setPositionPlaying(SP.DEFAULT_POSITION_GROUP)

SP.positionGroup = viewModel.groupModel.defaultPosition()
viewModel.groupModel.initPosition()

SP.position = SP.DEFAULT_POSITION
Log.i(TAG, "list position: ${SP.position}")
val tvListModel = viewModel.groupModel.getCurrentList()
Expand Down
33 changes: 13 additions & 20 deletions app/src/main/java/com/lizongying/mytv0/SimpleServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.util.Log
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.lizongying.mytv0.data.ReqSettings
import com.lizongying.mytv0.data.ReqSourceAdd
import com.lizongying.mytv0.data.ReqSources
import com.lizongying.mytv0.data.RespSettings
import com.lizongying.mytv0.data.Source
Expand Down Expand Up @@ -62,20 +63,14 @@ class SimpleServer(private val context: Context, private val viewModel: MainView

var history = mutableListOf<Source>()

SP.sources?.let {
if (it.isEmpty()) {
Log.i(Sources.TAG, "sources is empty")
return@let
}

val type = object : TypeToken<List<Source>>() {}.type
val sources: List<Source> = Gson().fromJson(it, type)
history = sources.toMutableList()
}

if (history.size == 0) {
if (!SP.configUrl.isNullOrEmpty()) {
history.add(Source(uri = SP.configUrl!!))
if (!SP.sources.isNullOrEmpty()) {
try {
val type = object : TypeToken<List<Source>>() {}.type
val sources: List<Source> = Gson().fromJson(SP.sources!!, type)
history = sources.toMutableList()
} catch (e: Exception) {
e.printStackTrace()
SP.sources = SP.DEFAULT_SOURCES
}
}

Expand Down Expand Up @@ -125,12 +120,10 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
val response = ""
try {
readBody(session)?.let {
val req = Gson().fromJson(it, ReqSettings::class.java)
if (req.uri != null) {
val uri = Uri.parse(req.uri)
handler.post {
viewModel.importFromUri(uri)
}
val req = Gson().fromJson(it, ReqSourceAdd::class.java)
val uri = Uri.parse(req.uri)
handler.post {
viewModel.importFromUri(uri, req.id)
}
}
} catch (e: IOException) {
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/lizongying/mytv0/SourcesFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class SourcesFragment : DialogFragment(), SourcesAdapter.ItemListener {

viewModel.sources.removed.observe(this) { items ->
sourcesAdapter.removed(items.first)
checkEmpty()
}

viewModel.sources.added.observe(this) { items ->
Expand All @@ -73,7 +74,10 @@ class SourcesFragment : DialogFragment(), SourcesAdapter.ItemListener {

viewModel.sources.changed.observe(this) { _ ->
sourcesAdapter.changed()
checkEmpty()
}

checkEmpty()
}

private val hideFragment = Runnable {
Expand Down Expand Up @@ -106,6 +110,14 @@ class SourcesFragment : DialogFragment(), SourcesAdapter.ItemListener {
return false
}

private fun checkEmpty() {
if (viewModel.sources.size() == 0) {
binding.content.visibility = View.VISIBLE
} else {
binding.content.visibility = View.GONE
}
}

companion object {
const val TAG = "SourcesFragment"
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/lizongying/mytv0/data/ReqSources.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ package com.lizongying.mytv0.data
data class ReqSources(
var sourceId: String,
)

data class ReqSourceAdd(
val id: String,
var uri: String,
)
9 changes: 1 addition & 8 deletions app/src/main/java/com/lizongying/mytv0/models/Sources.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lizongying.mytv0.models

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
Expand Down Expand Up @@ -116,14 +117,6 @@ class Sources {
}
}

if (!SP.configUrl.isNullOrEmpty()) {
addSource(
Source(
uri = SP.configUrl!!,
)
)
}

if (size() > 0) {
_checked.value = sourcesValue.indexOfFirst { it.uri == SP.configUrl }

Expand Down
9 changes: 3 additions & 6 deletions app/src/main/java/com/lizongying/mytv0/models/TVGroupModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,9 @@ class TVGroupModel : ViewModel() {
}

fun defaultPosition(): Int {
return if (SP.DEFAULT_SHOW_ALL_CHANNELS) {
if (tvGroupValue.size > 2) 2 else 1
} else {
// if (tvGroupValue.size > 2) 1 else 0
if (tvGroupValue.size > 2) 2 else 1
}
// 1 全部
// 2 第一組
return if (tvGroupValue.size > 2) 2 else 1
}

init {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/error.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/error"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -25,7 +26,7 @@
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textColor="@color/white"
android:text="Message"
tools:text="Message"
android:textSize="20sp" />
</LinearLayout>
</FrameLayout>
17 changes: 16 additions & 1 deletion app/src/main/res/layout/sources.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,23 @@
android:id="@+id/sources"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/description_blur"
>

<TextView
android:id="@+id/content"
android:layout_width="600dp"
android:layout_height="400dp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@color/blur"
android:textStyle="bold"
android:textSize="14sp"
tools:text="暂无视频源"
android:visibility="gone"
android:text="@string/no_video_source"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="600dp"
Expand Down
Loading

0 comments on commit 36a3e37

Please sign in to comment.