优化导入

pull/306/head
gedoor 4 years ago
parent 92b9a6e1cd
commit 0e460d89d6
  1. 21
      app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt
  2. 13
      app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt

@ -26,16 +26,14 @@ import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.backgroundColor import io.legado.app.lib.theme.backgroundColor
import io.legado.app.ui.book.source.debug.BookSourceDebugActivity import io.legado.app.ui.book.source.debug.BookSourceDebugActivity
import io.legado.app.ui.login.SourceLogin import io.legado.app.ui.login.SourceLogin
import io.legado.app.ui.qrcode.QrCodeActivity
import io.legado.app.ui.widget.KeyboardToolPop import io.legado.app.ui.widget.KeyboardToolPop
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.applyTint import io.legado.app.utils.applyTint
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import io.legado.app.utils.shareWithQr import io.legado.app.utils.shareWithQr
import kotlinx.android.synthetic.main.activity_book_source_edit.* import kotlinx.android.synthetic.main.activity_book_source_edit.*
import org.jetbrains.anko.displayMetrics import org.jetbrains.anko.*
import org.jetbrains.anko.share
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import kotlin.math.abs import kotlin.math.abs
class BookSourceEditActivity : class BookSourceEditActivity :
@ -44,6 +42,7 @@ class BookSourceEditActivity :
override val viewModel: BookSourceEditViewModel override val viewModel: BookSourceEditViewModel
get() = getViewModel(BookSourceEditViewModel::class.java) get() = getViewModel(BookSourceEditViewModel::class.java)
private val qrRequestCode = 101
private val adapter = BookSourceEditAdapter() private val adapter = BookSourceEditAdapter()
private val sourceEntities: ArrayList<EditEntity> = ArrayList() private val sourceEntities: ArrayList<EditEntity> = ArrayList()
private val searchEntities: ArrayList<EditEntity> = ArrayList() private val searchEntities: ArrayList<EditEntity> = ArrayList()
@ -88,6 +87,7 @@ class BookSourceEditActivity :
} }
} }
R.id.menu_paste_source -> viewModel.pasteSource { upRecyclerView(it) } R.id.menu_paste_source -> viewModel.pasteSource { upRecyclerView(it) }
R.id.menu_qr_code_camera -> startActivityForResult<QrCodeActivity>(qrRequestCode)
R.id.menu_share_str -> GSON.toJson(getSource())?.let { share(it) } R.id.menu_share_str -> GSON.toJson(getSource())?.let { share(it) }
R.id.menu_share_qr -> GSON.toJson(getSource())?.let { sourceStr -> R.id.menu_share_qr -> GSON.toJson(getSource())?.let { sourceStr ->
shareWithQr(getString(R.string.share_book_source), sourceStr) shareWithQr(getString(R.string.share_book_source), sourceStr)
@ -392,6 +392,19 @@ class BookSourceEditActivity :
mSoftKeyboardTool?.dismiss() mSoftKeyboardTool?.dismiss()
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
qrRequestCode -> if (resultCode == RESULT_OK) {
data?.getStringExtra("result")?.let {
viewModel.importSource(it) { source ->
upRecyclerView(source)
}
}
}
}
}
private inner class KeyboardOnGlobalChangeListener : ViewTreeObserver.OnGlobalLayoutListener { private inner class KeyboardOnGlobalChangeListener : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() { override fun onGlobalLayout() {
val rect = Rect() val rect = Rect()

@ -8,6 +8,8 @@ import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.storage.OldRule import io.legado.app.help.storage.OldRule
import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonObject
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
class BookSourceEditViewModel(application: Application) : BaseViewModel(application) { class BookSourceEditViewModel(application: Application) : BaseViewModel(application) {
@ -71,4 +73,15 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
} }
} }
} }
fun importSource(text: String, finally: (source: BookSource) -> Unit) {
execute {
val text1 = text.trim()
GSON.fromJsonObject<BookSource>(text1)?.let {
finally.invoke(it)
}
}.onError {
toast(it.localizedMessage ?: "Error")
}
}
} }
Loading…
Cancel
Save