优化
This commit is contained in:
@@ -103,20 +103,14 @@ class BookSourceEditActivity :
|
|||||||
|
|
||||||
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
|
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
when (item.itemId) {
|
when (item.itemId) {
|
||||||
R.id.menu_save -> getSource().let { source ->
|
R.id.menu_save -> viewModel.save(getSource()) {
|
||||||
if (!source.equal(viewModel.bookSource ?: BookSource())) {
|
setResult(Activity.RESULT_OK)
|
||||||
source.lastUpdateTime = System.currentTimeMillis()
|
finish()
|
||||||
}
|
|
||||||
if (checkSource(source)) {
|
|
||||||
viewModel.save(source) { setResult(Activity.RESULT_OK); finish() }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
R.id.menu_debug_source -> getSource().let { source ->
|
R.id.menu_debug_source -> viewModel.save(getSource()) { source ->
|
||||||
if (checkSource(source)) {
|
viewModel.save(source) {
|
||||||
viewModel.save(source) {
|
startActivity<BookSourceDebugActivity> {
|
||||||
startActivity<BookSourceDebugActivity> {
|
putExtra("key", source.bookSourceUrl)
|
||||||
putExtra("key", source.bookSourceUrl)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -132,14 +126,10 @@ class BookSourceEditActivity :
|
|||||||
ErrorCorrectionLevel.L
|
ErrorCorrectionLevel.L
|
||||||
)
|
)
|
||||||
R.id.menu_help -> showHelp("ruleHelp")
|
R.id.menu_help -> showHelp("ruleHelp")
|
||||||
R.id.menu_login -> getSource().let { source ->
|
R.id.menu_login -> viewModel.save(getSource()) { source ->
|
||||||
if (checkSource(source)) {
|
startActivity<SourceLoginActivity> {
|
||||||
viewModel.save(source) {
|
putExtra("type", "bookSource")
|
||||||
startActivity<SourceLoginActivity> {
|
putExtra("key", source.bookSourceUrl)
|
||||||
putExtra("type", "bookSource")
|
|
||||||
putExtra("key", source.bookSourceUrl)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
R.id.menu_set_source_variable -> setSourceVariable()
|
R.id.menu_set_source_variable -> setSourceVariable()
|
||||||
@@ -515,14 +505,6 @@ class BookSourceEditActivity :
|
|||||||
return source
|
return source
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkSource(source: BookSource): Boolean {
|
|
||||||
if (source.bookSourceUrl.isBlank() || source.bookSourceName.isBlank()) {
|
|
||||||
toastOnUi(R.string.non_null_name_url)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun alertGroups() {
|
private fun alertGroups() {
|
||||||
launch {
|
launch {
|
||||||
val groups = withContext(IO) {
|
val groups = withContext(IO) {
|
||||||
@@ -594,22 +576,19 @@ class BookSourceEditActivity :
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun setSourceVariable() {
|
private fun setSourceVariable() {
|
||||||
launch {
|
viewModel.save(getSource()) { source ->
|
||||||
val source = viewModel.bookSource
|
launch {
|
||||||
if (source == null) {
|
val comment = source.getDisplayVariableComment("源变量可在js中通过source.getVariable()获取")
|
||||||
toastOnUi("先保存书源")
|
val variable = withContext(IO) { source.getVariable() }
|
||||||
return@launch
|
showDialogFragment(
|
||||||
}
|
VariableDialog(
|
||||||
val comment = source.getDisplayVariableComment("源变量可在js中通过source.getVariable()获取")
|
getString(R.string.set_source_variable),
|
||||||
val variable = withContext(IO) { source.getVariable() }
|
source.getKey(),
|
||||||
showDialogFragment(
|
variable,
|
||||||
VariableDialog(
|
comment
|
||||||
getString(R.string.set_source_variable),
|
)
|
||||||
source.getKey(),
|
|
||||||
variable,
|
|
||||||
comment
|
|
||||||
)
|
)
|
||||||
)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package io.legado.app.ui.book.source.edit
|
|||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import io.legado.app.R
|
||||||
import io.legado.app.base.BaseViewModel
|
import io.legado.app.base.BaseViewModel
|
||||||
import io.legado.app.data.appDb
|
import io.legado.app.data.appDb
|
||||||
import io.legado.app.data.entities.BookSource
|
import io.legado.app.data.entities.BookSource
|
||||||
@@ -34,8 +35,14 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun save(source: BookSource, success: (() -> Unit)? = null) {
|
fun save(source: BookSource, success: ((BookSource) -> Unit)? = null) {
|
||||||
execute {
|
execute {
|
||||||
|
if (source.bookSourceUrl.isBlank() || source.bookSourceName.isBlank()) {
|
||||||
|
throw NoStackTraceException(context.getString(R.string.non_null_name_url))
|
||||||
|
}
|
||||||
|
if (!source.equal(bookSource ?: BookSource())) {
|
||||||
|
source.lastUpdateTime = System.currentTimeMillis()
|
||||||
|
}
|
||||||
bookSource?.let {
|
bookSource?.let {
|
||||||
appDb.bookSourceDao.delete(it)
|
appDb.bookSourceDao.delete(it)
|
||||||
SourceConfig.removeSource(it.bookSourceUrl)
|
SourceConfig.removeSource(it.bookSourceUrl)
|
||||||
@@ -43,8 +50,9 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
|
|||||||
source.lastUpdateTime = System.currentTimeMillis()
|
source.lastUpdateTime = System.currentTimeMillis()
|
||||||
appDb.bookSourceDao.insert(source)
|
appDb.bookSourceDao.insert(source)
|
||||||
bookSource = source
|
bookSource = source
|
||||||
|
source
|
||||||
}.onSuccess {
|
}.onSuccess {
|
||||||
success?.invoke()
|
success?.invoke(it)
|
||||||
}.onError {
|
}.onError {
|
||||||
context.toastOnUi(it.localizedMessage)
|
context.toastOnUi(it.localizedMessage)
|
||||||
it.printOnDebug()
|
it.printOnDebug()
|
||||||
|
|||||||
@@ -24,14 +24,19 @@ import io.legado.app.ui.qrcode.QrCodeResult
|
|||||||
import io.legado.app.ui.rss.source.debug.RssSourceDebugActivity
|
import io.legado.app.ui.rss.source.debug.RssSourceDebugActivity
|
||||||
import io.legado.app.ui.widget.dialog.TextDialog
|
import io.legado.app.ui.widget.dialog.TextDialog
|
||||||
import io.legado.app.ui.widget.dialog.UrlOptionDialog
|
import io.legado.app.ui.widget.dialog.UrlOptionDialog
|
||||||
|
import io.legado.app.ui.widget.dialog.VariableDialog
|
||||||
import io.legado.app.ui.widget.keyboard.KeyboardToolPop
|
import io.legado.app.ui.widget.keyboard.KeyboardToolPop
|
||||||
import io.legado.app.ui.widget.text.EditEntity
|
import io.legado.app.ui.widget.text.EditEntity
|
||||||
import io.legado.app.utils.*
|
import io.legado.app.utils.*
|
||||||
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
class RssSourceEditActivity :
|
class RssSourceEditActivity :
|
||||||
VMBaseActivity<ActivityRssSourceEditBinding, RssSourceEditViewModel>(false),
|
VMBaseActivity<ActivityRssSourceEditBinding, RssSourceEditViewModel>(false),
|
||||||
KeyboardToolPop.CallBack {
|
KeyboardToolPop.CallBack,
|
||||||
|
VariableDialog.Callback {
|
||||||
|
|
||||||
override val binding by viewBinding(ActivityRssSourceEditBinding::inflate)
|
override val binding by viewBinding(ActivityRssSourceEditBinding::inflate)
|
||||||
override val viewModel by viewModels<RssSourceEditViewModel>()
|
override val viewModel by viewModels<RssSourceEditViewModel>()
|
||||||
@@ -107,35 +112,22 @@ class RssSourceEditActivity :
|
|||||||
|
|
||||||
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
|
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
when (item.itemId) {
|
when (item.itemId) {
|
||||||
R.id.menu_save -> {
|
R.id.menu_save -> viewModel.save(getRssSource()) {
|
||||||
val source = getRssSource()
|
setResult(Activity.RESULT_OK)
|
||||||
if (checkSource(source)) {
|
finish()
|
||||||
viewModel.save(source) {
|
}
|
||||||
setResult(Activity.RESULT_OK)
|
R.id.menu_debug_source -> viewModel.save(getRssSource()) { source ->
|
||||||
finish()
|
startActivity<RssSourceDebugActivity> {
|
||||||
}
|
putExtra("key", source.sourceUrl)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
R.id.menu_debug_source -> {
|
R.id.menu_login -> viewModel.save(getRssSource()) {
|
||||||
val source = getRssSource()
|
startActivity<SourceLoginActivity> {
|
||||||
if (checkSource(source)) {
|
putExtra("type", "rssSource")
|
||||||
viewModel.save(source) {
|
putExtra("key", it.sourceUrl)
|
||||||
startActivity<RssSourceDebugActivity> {
|
|
||||||
putExtra("key", source.sourceUrl)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
R.id.menu_login -> getRssSource().let {
|
|
||||||
if (checkSource(it)) {
|
|
||||||
viewModel.save(it) {
|
|
||||||
startActivity<SourceLoginActivity> {
|
|
||||||
putExtra("type", "rssSource")
|
|
||||||
putExtra("key", it.sourceUrl)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
R.id.menu_set_source_variable -> setSourceVariable()
|
||||||
R.id.menu_clear_cookie -> viewModel.clearCookie(getRssSource().sourceUrl)
|
R.id.menu_clear_cookie -> viewModel.clearCookie(getRssSource().sourceUrl)
|
||||||
R.id.menu_auto_complete -> viewModel.autoComplete = !viewModel.autoComplete
|
R.id.menu_auto_complete -> viewModel.autoComplete = !viewModel.autoComplete
|
||||||
R.id.menu_copy_source -> sendToClip(GSON.toJson(getRssSource()))
|
R.id.menu_copy_source -> sendToClip(GSON.toJson(getRssSource()))
|
||||||
@@ -305,12 +297,25 @@ class RssSourceEditActivity :
|
|||||||
return source
|
return source
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkSource(source: RssSource): Boolean {
|
private fun setSourceVariable() {
|
||||||
if (source.sourceName.isBlank() || source.sourceName.isBlank()) {
|
viewModel.save(getRssSource()) { source ->
|
||||||
toastOnUi("名称或url不能为空")
|
launch {
|
||||||
return false
|
val comment = source.getDisplayVariableComment("源变量可在js中通过source.getVariable()获取")
|
||||||
|
val variable = withContext(Dispatchers.IO) { source.getVariable() }
|
||||||
|
showDialogFragment(
|
||||||
|
VariableDialog(
|
||||||
|
getString(R.string.set_source_variable),
|
||||||
|
source.getKey(),
|
||||||
|
variable,
|
||||||
|
comment
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true
|
}
|
||||||
|
|
||||||
|
override fun setVariable(key: String, variable: String?) {
|
||||||
|
viewModel.rssSource?.setVariable(variable)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun helpActions(): List<SelectItem<String>> {
|
override fun helpActions(): List<SelectItem<String>> {
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ package io.legado.app.ui.rss.source.edit
|
|||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import io.legado.app.R
|
||||||
import io.legado.app.base.BaseViewModel
|
import io.legado.app.base.BaseViewModel
|
||||||
import io.legado.app.data.appDb
|
import io.legado.app.data.appDb
|
||||||
import io.legado.app.data.entities.RssSource
|
import io.legado.app.data.entities.RssSource
|
||||||
|
import io.legado.app.exception.NoStackTraceException
|
||||||
import io.legado.app.help.RuleComplete
|
import io.legado.app.help.RuleComplete
|
||||||
import io.legado.app.help.http.CookieStore
|
import io.legado.app.help.http.CookieStore
|
||||||
import io.legado.app.utils.getClipText
|
import io.legado.app.utils.getClipText
|
||||||
@@ -32,15 +34,19 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun save(source: RssSource, success: (() -> Unit)) {
|
fun save(source: RssSource, success: ((RssSource) -> Unit)) {
|
||||||
execute {
|
execute {
|
||||||
|
if (source.sourceName.isBlank() || source.sourceName.isBlank()) {
|
||||||
|
throw NoStackTraceException(context.getString(R.string.non_null_name_url))
|
||||||
|
}
|
||||||
rssSource?.let {
|
rssSource?.let {
|
||||||
appDb.rssSourceDao.delete(it)
|
appDb.rssSourceDao.delete(it)
|
||||||
}
|
}
|
||||||
appDb.rssSourceDao.insert(source)
|
appDb.rssSourceDao.insert(source)
|
||||||
rssSource = source
|
rssSource = source
|
||||||
|
source
|
||||||
}.onSuccess {
|
}.onSuccess {
|
||||||
success()
|
success(it)
|
||||||
}.onError {
|
}.onError {
|
||||||
context.toastOnUi(it.localizedMessage)
|
context.toastOnUi(it.localizedMessage)
|
||||||
it.printOnDebug()
|
it.printOnDebug()
|
||||||
|
|||||||
Reference in New Issue
Block a user