pull/1415/head
gedoor 3 years ago
parent 305cc3badf
commit cbcbcc7d35
  1. 5
      app/src/main/java/io/legado/app/api/controller/BookSourceController.kt
  2. 12
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  3. 4
      app/src/main/java/io/legado/app/help/storage/ImportOldData.kt
  4. 19
      app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt
  5. 9
      app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt
  6. 19
      app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt
  7. 19
      app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt
  8. 5
      app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt
  9. 10
      app/src/main/java/io/legado/app/ui/widget/dialog/CodeDialog.kt

@ -5,7 +5,6 @@ import android.text.TextUtils
import io.legado.app.api.ReturnData import io.legado.app.api.ReturnData
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
import io.legado.app.help.SourceAnalyzer
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonArray import io.legado.app.utils.fromJsonArray
import io.legado.app.utils.msg import io.legado.app.utils.msg
@ -25,7 +24,7 @@ object BookSourceController {
val returnData = ReturnData() val returnData = ReturnData()
postData ?: return returnData.setErrorMsg("数据不能为空") postData ?: return returnData.setErrorMsg("数据不能为空")
kotlin.runCatching { kotlin.runCatching {
val bookSource = SourceAnalyzer.jsonToBookSource(postData) val bookSource = BookSource.fromJson(postData)
if (bookSource != null) { if (bookSource != null) {
if (TextUtils.isEmpty(bookSource.bookSourceName) || TextUtils.isEmpty(bookSource.bookSourceUrl)) { if (TextUtils.isEmpty(bookSource.bookSourceName) || TextUtils.isEmpty(bookSource.bookSourceUrl)) {
returnData.setErrorMsg("源名称和URL不能为空") returnData.setErrorMsg("源名称和URL不能为空")
@ -45,7 +44,7 @@ object BookSourceController {
fun saveSources(postData: String?): ReturnData { fun saveSources(postData: String?): ReturnData {
postData ?: return ReturnData().setErrorMsg("数据为空") postData ?: return ReturnData().setErrorMsg("数据为空")
val okSources = arrayListOf<BookSource>() val okSources = arrayListOf<BookSource>()
val bookSources = SourceAnalyzer.jsonToBookSources(postData) val bookSources = BookSource.fromJsonArray(postData)
if (bookSources.isNotEmpty()) { if (bookSources.isNotEmpty()) {
bookSources.forEach { bookSource -> bookSources.forEach { bookSource ->
if (bookSource.bookSourceName.isNotBlank() if (bookSource.bookSourceName.isNotBlank()

@ -5,6 +5,7 @@ import android.text.TextUtils
import androidx.room.* import androidx.room.*
import io.legado.app.constant.BookType import io.legado.app.constant.BookType
import io.legado.app.data.entities.rule.* import io.legado.app.data.entities.rule.*
import io.legado.app.help.SourceAnalyzer
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.parcelize.IgnoredOnParcel import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize import kotlinx.parcelize.Parcelize
@ -154,6 +155,17 @@ data class BookSource(
private fun equal(a: String?, b: String?) = a == b || (a.isNullOrEmpty() && b.isNullOrEmpty()) 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<BookSource> {
return SourceAnalyzer.jsonToBookSources(json)
}
}
class Converters { class Converters {
@TypeConverter @TypeConverter

@ -4,7 +4,7 @@ import android.content.Context
import android.net.Uri import android.net.Uri
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import io.legado.app.data.appDb 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 io.legado.app.utils.*
import java.io.File import java.io.File
@ -89,7 +89,7 @@ object ImportOldData {
} }
fun importOldSource(json: String): Int { fun importOldSource(json: String): Int {
val bookSources = SourceAnalyzer.jsonToBookSources(json) val bookSources = BookSource.fromJsonArray(json)
appDb.bookSourceDao.insert(*bookSources.toTypedArray()) appDb.bookSourceDao.insert(*bookSources.toTypedArray())
return bookSources.size return bookSources.size
} }

@ -35,7 +35,8 @@ import splitties.views.onClick
* 导入书源弹出窗口 * 导入书源弹出窗口
*/ */
class ImportBookSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_view), class ImportBookSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_view),
Toolbar.OnMenuItemClickListener { Toolbar.OnMenuItemClickListener,
CodeDialog.Callback {
constructor(source: String, finishOnDismiss: Boolean = false) : this() { constructor(source: String, finishOnDismiss: Boolean = false) : this() {
arguments = Bundle().apply { 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) : inner class SourcesAdapter(context: Context) :
RecyclerAdapter<BookSource, ItemSourceImportBinding>(context) { RecyclerAdapter<BookSource, ItemSourceImportBinding>(context) {
@ -229,7 +239,12 @@ class ImportBookSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_vie
} }
tvOpen.setOnClickListener { tvOpen.setOnClickListener {
val source = viewModel.allSources[holder.layoutPosition] val source = viewModel.allSources[holder.layoutPosition]
showDialogFragment(CodeDialog(GSON.toJson(source))) showDialogFragment(
CodeDialog(
GSON.toJson(source),
requestId = holder.layoutPosition.toString()
)
)
} }
} }
} }

@ -10,7 +10,6 @@ import io.legado.app.data.appDb
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.ContentProcessor import io.legado.app.help.ContentProcessor
import io.legado.app.help.SourceAnalyzer
import io.legado.app.help.SourceHelp import io.legado.app.help.SourceHelp
import io.legado.app.help.http.newCallResponseBody import io.legado.app.help.http.newCallResponseBody
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
@ -99,13 +98,13 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
importSourceUrl(it) importSourceUrl(it)
} }
} else { } else {
SourceAnalyzer.jsonToBookSource(mText)?.let { BookSource.fromJson(mText)?.let {
allSources.add(it) allSources.add(it)
} }
} }
} }
mText.isJsonArray() -> { mText.isJsonArray() -> {
val items = SourceAnalyzer.jsonToBookSources(mText) val items = BookSource.fromJsonArray(mText)
allSources.addAll(items) allSources.addAll(items)
} }
mText.isAbsUrl() -> { mText.isAbsUrl() -> {
@ -130,13 +129,13 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
val items: List<Map<String, Any>> = jsonPath.parse(body).read("$") val items: List<Map<String, Any>> = jsonPath.parse(body).read("$")
for (item in items) { for (item in items) {
val jsonItem = jsonPath.parse(item) val jsonItem = jsonPath.parse(item)
SourceAnalyzer.jsonToBookSource(jsonItem.jsonString())?.let { source -> BookSource.fromJson(jsonItem.jsonString())?.let { source ->
allSources.add(source) allSources.add(source)
} }
} }
} }
body.isJsonObject() -> { body.isJsonObject() -> {
SourceAnalyzer.jsonToBookSource(body)?.let { BookSource.fromJson(body)?.let {
allSources.add(it) allSources.add(it)
} }
} }

@ -30,7 +30,8 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding
import splitties.views.onClick import splitties.views.onClick
class ImportReplaceRuleDialog() : BaseDialogFragment(R.layout.dialog_recycler_view), class ImportReplaceRuleDialog() : BaseDialogFragment(R.layout.dialog_recycler_view),
Toolbar.OnMenuItemClickListener { Toolbar.OnMenuItemClickListener,
CodeDialog.Callback {
constructor(source: String, finishOnDismiss: Boolean = false) : this() { constructor(source: String, finishOnDismiss: Boolean = false) : this() {
arguments = Bundle().apply { 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<ReplaceRule>(code)?.let { rule ->
viewModel.allRules[it] = rule
adapter.notifyItemChanged(it)
}
}
}
inner class SourcesAdapter(context: Context) : inner class SourcesAdapter(context: Context) :
RecyclerAdapter<ReplaceRule, ItemSourceImportBinding>(context) { RecyclerAdapter<ReplaceRule, ItemSourceImportBinding>(context) {
@ -224,7 +234,12 @@ class ImportReplaceRuleDialog() : BaseDialogFragment(R.layout.dialog_recycler_vi
} }
tvOpen.setOnClickListener { tvOpen.setOnClickListener {
val source = viewModel.allRules[holder.layoutPosition] val source = viewModel.allRules[holder.layoutPosition]
showDialogFragment(CodeDialog(GSON.toJson(source))) showDialogFragment(
CodeDialog(
GSON.toJson(source),
requestId = holder.layoutPosition.toString()
)
)
} }
} }
} }

@ -34,7 +34,8 @@ import splitties.views.onClick
* 导入rss源弹出窗口 * 导入rss源弹出窗口
*/ */
class ImportRssSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_view), class ImportRssSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_view),
Toolbar.OnMenuItemClickListener { Toolbar.OnMenuItemClickListener,
CodeDialog.Callback {
constructor(source: String, finishOnDismiss: Boolean = false) : this() { constructor(source: String, finishOnDismiss: Boolean = false) : this() {
arguments = Bundle().apply { 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) : inner class SourcesAdapter(context: Context) :
RecyclerAdapter<RssSource, ItemSourceImportBinding>(context) { RecyclerAdapter<RssSource, ItemSourceImportBinding>(context) {
@ -227,7 +237,12 @@ class ImportRssSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_view
} }
tvOpen.setOnClickListener { tvOpen.setOnClickListener {
val source = viewModel.allSources[holder.layoutPosition] val source = viewModel.allSources[holder.layoutPosition]
showDialogFragment(CodeDialog(GSON.toJson(source))) showDialogFragment(
CodeDialog(
GSON.toJson(source),
requestId = holder.layoutPosition.toString()
)
)
} }
} }
} }

@ -5,7 +5,6 @@ import android.content.Intent
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
import io.legado.app.help.SourceAnalyzer
import io.legado.app.help.http.newCallStrResponse import io.legado.app.help.http.newCallStrResponse
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.model.NoStackTraceException import io.legado.app.model.NoStackTraceException
@ -86,10 +85,10 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
text.isJsonArray() -> { text.isJsonArray() -> {
val items: List<Map<String, Any>> = jsonPath.parse(text).read("$") val items: List<Map<String, Any>> = jsonPath.parse(text).read("$")
val jsonItem = jsonPath.parse(items[0]) val jsonItem = jsonPath.parse(items[0])
SourceAnalyzer.jsonToBookSource(jsonItem.jsonString()) BookSource.fromJson(jsonItem.jsonString())
} }
text.isJsonObject() -> { text.isJsonObject() -> {
SourceAnalyzer.jsonToBookSource(text) BookSource.fromJson(text)
} }
else -> { else -> {
null null

@ -15,10 +15,11 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding
class CodeDialog() : BaseDialogFragment(R.layout.dialog_code_view) { 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 { arguments = Bundle().apply {
putBoolean("disableEdit", disableEdit) putBoolean("disableEdit", disableEdit)
putString("code", code) putString("code", code)
putString("requestId", requestId)
} }
} }
@ -50,8 +51,9 @@ class CodeDialog() : BaseDialogFragment(R.layout.dialog_code_view) {
binding.toolBar.setOnMenuItemClickListener { binding.toolBar.setOnMenuItemClickListener {
when (it.itemId) { when (it.itemId) {
R.id.menu_save -> binding.codeView.text?.toString()?.let { code -> R.id.menu_save -> binding.codeView.text?.toString()?.let { code ->
(parentFragment as? Callback)?.saveCode(code) val requestId = arguments?.getString("requestId")
?: (activity as? Callback)?.saveCode(code) (parentFragment as? Callback)?.onCodeSave(code, requestId)
?: (activity as? Callback)?.onCodeSave(code, requestId)
} }
} }
return@setOnMenuItemClickListener true return@setOnMenuItemClickListener true
@ -61,7 +63,7 @@ class CodeDialog() : BaseDialogFragment(R.layout.dialog_code_view) {
interface Callback { interface Callback {
fun saveCode(code: String) fun onCodeSave(code: String, requestId: String?)
} }

Loading…
Cancel
Save