feat: SettingsActivity 使用 SourceRegistry 驱动源切换
This commit is contained in:
@@ -11,7 +11,8 @@ import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.videoapp.tv.data.ConfigRepository
|
||||
import com.videoapp.tv.data.SiteConfig
|
||||
import com.videoapp.tv.data.SitePreset
|
||||
import com.videoapp.tv.engine.SourceHandler
|
||||
import com.videoapp.tv.engine.SourceRegistry
|
||||
|
||||
class SettingsActivity : AppCompatActivity() {
|
||||
|
||||
@@ -53,44 +54,40 @@ class SettingsActivity : AppCompatActivity() {
|
||||
btnSave = findViewById(R.id.btn_save)
|
||||
btnRestore = findViewById(R.id.btn_restore)
|
||||
|
||||
setupPresetSpinner()
|
||||
setupSourceSpinner()
|
||||
loadConfig()
|
||||
|
||||
btnSave.setOnClickListener { saveConfig() }
|
||||
btnRestore.setOnClickListener {
|
||||
configRepo.restoreDefault()
|
||||
ignoringSpinner = true
|
||||
spinnerPreset.setSelection(0)
|
||||
ignoringSpinner = false
|
||||
loadConfig()
|
||||
Toast.makeText(this, R.string.config_restored, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupPresetSpinner() {
|
||||
val presets = configRepo.getPresets()
|
||||
val presetNames = presets.map { it.name }
|
||||
private fun setupSourceSpinner() {
|
||||
val handlers = SourceRegistry.getAll()
|
||||
val names = handlers.map { it.displayName }
|
||||
val adapter = ArrayAdapter(
|
||||
this,
|
||||
android.R.layout.simple_spinner_item,
|
||||
presetNames
|
||||
names
|
||||
)
|
||||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
|
||||
spinnerPreset.adapter = adapter
|
||||
|
||||
val currentId = configRepo.getCurrentSourceId()
|
||||
val currentIndex = handlers.indexOfFirst { it.id == currentId }
|
||||
if (currentIndex >= 0) {
|
||||
spinnerPreset.setSelection(currentIndex)
|
||||
}
|
||||
|
||||
spinnerPreset.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
||||
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
|
||||
if (ignoringSpinner) return
|
||||
val preset = presets.getOrNull(position) ?: return
|
||||
configRepo.applyPreset(preset)
|
||||
loadConfigFromPreset(preset.config)
|
||||
Toast.makeText(
|
||||
this@SettingsActivity,
|
||||
"已切换到 ${preset.name}",
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
val handler = handlers[position]
|
||||
configRepo.setCurrentSourceId(handler.id)
|
||||
}
|
||||
|
||||
override fun onNothingSelected(parent: AdapterView<*>?) {}
|
||||
}
|
||||
}
|
||||
@@ -115,10 +112,9 @@ class SettingsActivity : AppCompatActivity() {
|
||||
val config = configRepo.getConfig()
|
||||
loadConfigFromPreset(config)
|
||||
|
||||
val presets = configRepo.getPresets()
|
||||
val matchedIndex = presets.indexOfFirst {
|
||||
it.config.baseUrl == config.baseUrl
|
||||
}
|
||||
val handlers = SourceRegistry.getAll()
|
||||
val currentId = configRepo.getCurrentSourceId()
|
||||
val matchedIndex = handlers.indexOfFirst { it.id == currentId }
|
||||
if (matchedIndex >= 0) {
|
||||
ignoringSpinner = true
|
||||
spinnerPreset.setSelection(matchedIndex)
|
||||
|
||||
Reference in New Issue
Block a user