diff --git a/app/src/main/java/io/legado/app/api/controller/BookSourceController.kt b/app/src/main/java/io/legado/app/api/controller/BookSourceController.kt index c705dd2a8..417db03e8 100644 --- a/app/src/main/java/io/legado/app/api/controller/BookSourceController.kt +++ b/app/src/main/java/io/legado/app/api/controller/BookSourceController.kt @@ -5,7 +5,6 @@ import android.text.TextUtils import io.legado.app.api.ReturnData import io.legado.app.data.appDb import io.legado.app.data.entities.BookSource -import io.legado.app.help.SourceAnalyzer import io.legado.app.utils.GSON import io.legado.app.utils.fromJsonArray import io.legado.app.utils.msg @@ -25,7 +24,7 @@ object BookSourceController { val returnData = ReturnData() postData ?: return returnData.setErrorMsg("数据不能为空") kotlin.runCatching { - val bookSource = SourceAnalyzer.jsonToBookSource(postData) + val bookSource = BookSource.fromJson(postData) if (bookSource != null) { if (TextUtils.isEmpty(bookSource.bookSourceName) || TextUtils.isEmpty(bookSource.bookSourceUrl)) { returnData.setErrorMsg("源名称和URL不能为空") @@ -45,7 +44,7 @@ object BookSourceController { fun saveSources(postData: String?): ReturnData { postData ?: return ReturnData().setErrorMsg("数据为空") val okSources = arrayListOf() - val bookSources = SourceAnalyzer.jsonToBookSources(postData) + val bookSources = BookSource.fromJsonArray(postData) if (bookSources.isNotEmpty()) { bookSources.forEach { bookSource -> if (bookSource.bookSourceName.isNotBlank() diff --git a/app/src/main/java/io/legado/app/data/entities/BookSource.kt b/app/src/main/java/io/legado/app/data/entities/BookSource.kt index b97e1ab54..01fdf3fe5 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookSource.kt @@ -5,6 +5,7 @@ import android.text.TextUtils import androidx.room.* import io.legado.app.constant.BookType import io.legado.app.data.entities.rule.* +import io.legado.app.help.SourceAnalyzer import io.legado.app.utils.* import kotlinx.parcelize.IgnoredOnParcel import kotlinx.parcelize.Parcelize @@ -154,6 +155,17 @@ data class BookSource( private fun equal(a: String?, b: String?) = a == b || (a.isNullOrEmpty() && b.isNullOrEmpty()) + companion object { + + fun fromJson(json: String): BookSource? { + return SourceAnalyzer.jsonToBookSource(json) + } + + fun fromJsonArray(json: String): List { + return SourceAnalyzer.jsonToBookSources(json) + } + } + class Converters { @TypeConverter diff --git a/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt b/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt index 0c79c280d..b2709cc35 100644 --- a/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt +++ b/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt @@ -4,7 +4,7 @@ import android.content.Context import android.net.Uri import androidx.documentfile.provider.DocumentFile import io.legado.app.data.appDb -import io.legado.app.help.SourceAnalyzer +import io.legado.app.data.entities.BookSource import io.legado.app.utils.* import java.io.File @@ -89,7 +89,7 @@ object ImportOldData { } fun importOldSource(json: String): Int { - val bookSources = SourceAnalyzer.jsonToBookSources(json) + val bookSources = BookSource.fromJsonArray(json) appDb.bookSourceDao.insert(*bookSources.toTypedArray()) return bookSources.size } diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt index 24e1e98d1..42c8146ea 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt @@ -35,7 +35,8 @@ import splitties.views.onClick * 导入书源弹出窗口 */ class ImportBookSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_view), - Toolbar.OnMenuItemClickListener { + Toolbar.OnMenuItemClickListener, + CodeDialog.Callback { constructor(source: String, finishOnDismiss: Boolean = false) : this() { arguments = Bundle().apply { @@ -189,6 +190,15 @@ class ImportBookSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_vie } } + override fun onCodeSave(code: String, requestId: String?) { + requestId?.toInt()?.let { + BookSource.fromJson(code)?.let { source -> + viewModel.allSources[it] = source + adapter.notifyItemChanged(it) + } + } + } + inner class SourcesAdapter(context: Context) : RecyclerAdapter(context) { @@ -229,7 +239,12 @@ class ImportBookSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_vie } tvOpen.setOnClickListener { val source = viewModel.allSources[holder.layoutPosition] - showDialogFragment(CodeDialog(GSON.toJson(source))) + showDialogFragment( + CodeDialog( + GSON.toJson(source), + requestId = holder.layoutPosition.toString() + ) + ) } } } diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt index 9c1c7b83f..e11eae675 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt @@ -10,7 +10,6 @@ import io.legado.app.data.appDb import io.legado.app.data.entities.BookSource import io.legado.app.help.AppConfig import io.legado.app.help.ContentProcessor -import io.legado.app.help.SourceAnalyzer import io.legado.app.help.SourceHelp import io.legado.app.help.http.newCallResponseBody import io.legado.app.help.http.okHttpClient @@ -99,13 +98,13 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { importSourceUrl(it) } } else { - SourceAnalyzer.jsonToBookSource(mText)?.let { + BookSource.fromJson(mText)?.let { allSources.add(it) } } } mText.isJsonArray() -> { - val items = SourceAnalyzer.jsonToBookSources(mText) + val items = BookSource.fromJsonArray(mText) allSources.addAll(items) } mText.isAbsUrl() -> { @@ -130,13 +129,13 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { val items: List> = jsonPath.parse(body).read("$") for (item in items) { val jsonItem = jsonPath.parse(item) - SourceAnalyzer.jsonToBookSource(jsonItem.jsonString())?.let { source -> + BookSource.fromJson(jsonItem.jsonString())?.let { source -> allSources.add(source) } } } body.isJsonObject() -> { - SourceAnalyzer.jsonToBookSource(body)?.let { + BookSource.fromJson(body)?.let { allSources.add(it) } } diff --git a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt index 94635bf12..ce174a793 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt @@ -30,7 +30,8 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding import splitties.views.onClick class ImportReplaceRuleDialog() : BaseDialogFragment(R.layout.dialog_recycler_view), - Toolbar.OnMenuItemClickListener { + Toolbar.OnMenuItemClickListener, + CodeDialog.Callback { constructor(source: String, finishOnDismiss: Boolean = false) : this() { arguments = Bundle().apply { @@ -181,6 +182,15 @@ class ImportReplaceRuleDialog() : BaseDialogFragment(R.layout.dialog_recycler_vi } } + override fun onCodeSave(code: String, requestId: String?) { + requestId?.toInt()?.let { + GSON.fromJsonObject(code)?.let { rule -> + viewModel.allRules[it] = rule + adapter.notifyItemChanged(it) + } + } + } + inner class SourcesAdapter(context: Context) : RecyclerAdapter(context) { @@ -224,7 +234,12 @@ class ImportReplaceRuleDialog() : BaseDialogFragment(R.layout.dialog_recycler_vi } tvOpen.setOnClickListener { val source = viewModel.allRules[holder.layoutPosition] - showDialogFragment(CodeDialog(GSON.toJson(source))) + showDialogFragment( + CodeDialog( + GSON.toJson(source), + requestId = holder.layoutPosition.toString() + ) + ) } } } diff --git a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt index aa3d0114d..1477c5005 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt @@ -34,7 +34,8 @@ import splitties.views.onClick * 导入rss源弹出窗口 */ class ImportRssSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_view), - Toolbar.OnMenuItemClickListener { + Toolbar.OnMenuItemClickListener, + CodeDialog.Callback { constructor(source: String, finishOnDismiss: Boolean = false) : this() { arguments = Bundle().apply { @@ -188,6 +189,15 @@ class ImportRssSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_view } } + override fun onCodeSave(code: String, requestId: String?) { + requestId?.toInt()?.let { + RssSource.fromJson(code)?.let { source -> + viewModel.allSources[it] = source + adapter.notifyItemChanged(it) + } + } + } + inner class SourcesAdapter(context: Context) : RecyclerAdapter(context) { @@ -227,7 +237,12 @@ class ImportRssSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_view } tvOpen.setOnClickListener { val source = viewModel.allSources[holder.layoutPosition] - showDialogFragment(CodeDialog(GSON.toJson(source))) + showDialogFragment( + CodeDialog( + GSON.toJson(source), + requestId = holder.layoutPosition.toString() + ) + ) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt index 0038ed4db..02ed8b4a2 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt @@ -5,7 +5,6 @@ import android.content.Intent import io.legado.app.base.BaseViewModel import io.legado.app.data.appDb import io.legado.app.data.entities.BookSource -import io.legado.app.help.SourceAnalyzer import io.legado.app.help.http.newCallStrResponse import io.legado.app.help.http.okHttpClient import io.legado.app.model.NoStackTraceException @@ -86,10 +85,10 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat text.isJsonArray() -> { val items: List> = jsonPath.parse(text).read("$") val jsonItem = jsonPath.parse(items[0]) - SourceAnalyzer.jsonToBookSource(jsonItem.jsonString()) + BookSource.fromJson(jsonItem.jsonString()) } text.isJsonObject() -> { - SourceAnalyzer.jsonToBookSource(text) + BookSource.fromJson(text) } else -> { null diff --git a/app/src/main/java/io/legado/app/ui/widget/dialog/CodeDialog.kt b/app/src/main/java/io/legado/app/ui/widget/dialog/CodeDialog.kt index c51c9ed7f..279e5369c 100644 --- a/app/src/main/java/io/legado/app/ui/widget/dialog/CodeDialog.kt +++ b/app/src/main/java/io/legado/app/ui/widget/dialog/CodeDialog.kt @@ -15,10 +15,11 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding class CodeDialog() : BaseDialogFragment(R.layout.dialog_code_view) { - constructor(code: String, disableEdit: Boolean = true) : this() { + constructor(code: String, disableEdit: Boolean = true, requestId: String? = null) : this() { arguments = Bundle().apply { putBoolean("disableEdit", disableEdit) putString("code", code) + putString("requestId", requestId) } } @@ -50,8 +51,9 @@ class CodeDialog() : BaseDialogFragment(R.layout.dialog_code_view) { binding.toolBar.setOnMenuItemClickListener { when (it.itemId) { R.id.menu_save -> binding.codeView.text?.toString()?.let { code -> - (parentFragment as? Callback)?.saveCode(code) - ?: (activity as? Callback)?.saveCode(code) + val requestId = arguments?.getString("requestId") + (parentFragment as? Callback)?.onCodeSave(code, requestId) + ?: (activity as? Callback)?.onCodeSave(code, requestId) } } return@setOnMenuItemClickListener true @@ -61,7 +63,7 @@ class CodeDialog() : BaseDialogFragment(R.layout.dialog_code_view) { interface Callback { - fun saveCode(code: String) + fun onCodeSave(code: String, requestId: String?) }