diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt index efa6bb2cb..fd6a168b5 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt @@ -26,16 +26,14 @@ import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.backgroundColor import io.legado.app.ui.book.source.debug.BookSourceDebugActivity 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.utils.GSON import io.legado.app.utils.applyTint import io.legado.app.utils.getViewModel import io.legado.app.utils.shareWithQr import kotlinx.android.synthetic.main.activity_book_source_edit.* -import org.jetbrains.anko.displayMetrics -import org.jetbrains.anko.share -import org.jetbrains.anko.startActivity -import org.jetbrains.anko.toast +import org.jetbrains.anko.* import kotlin.math.abs class BookSourceEditActivity : @@ -44,6 +42,7 @@ class BookSourceEditActivity : override val viewModel: BookSourceEditViewModel get() = getViewModel(BookSourceEditViewModel::class.java) + private val qrRequestCode = 101 private val adapter = BookSourceEditAdapter() private val sourceEntities: ArrayList = ArrayList() private val searchEntities: ArrayList = ArrayList() @@ -88,6 +87,7 @@ class BookSourceEditActivity : } } R.id.menu_paste_source -> viewModel.pasteSource { upRecyclerView(it) } + R.id.menu_qr_code_camera -> startActivityForResult(qrRequestCode) R.id.menu_share_str -> GSON.toJson(getSource())?.let { share(it) } R.id.menu_share_qr -> GSON.toJson(getSource())?.let { sourceStr -> shareWithQr(getString(R.string.share_book_source), sourceStr) @@ -392,6 +392,19 @@ class BookSourceEditActivity : 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 { override fun onGlobalLayout() { val rect = Rect() 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 82b50e536..81f3d22f0 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 @@ -8,6 +8,8 @@ import io.legado.app.App import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.BookSource import io.legado.app.help.storage.OldRule +import io.legado.app.utils.GSON +import io.legado.app.utils.fromJsonObject import kotlinx.coroutines.Dispatchers 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(text1)?.let { + finally.invoke(it) + } + }.onError { + toast(it.localizedMessage ?: "Error") + } + } } \ No newline at end of file