From ae0e3253a46559dfb9cd4d47cd3aa64356e478a8 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 11 Aug 2020 15:22:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/model/localBook/LocalBook.kt | 3 +- .../ui/association/FileAssociationActivity.kt | 19 +++++---- .../association/FileAssociationViewModel.kt | 39 +++++++++++-------- 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt index a935131a1..d4eeee61c 100644 --- a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt +++ b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt @@ -31,7 +31,7 @@ object LocalBook { } } - fun importFile(path: String) { + fun importFile(path: String): Book { val fileName = if (path.isContentPath()) { val doc = DocumentFile.fromSingleUri(App.INSTANCE, Uri.parse(path)) doc?.name ?: "" @@ -67,6 +67,7 @@ object LocalBook { ) ) App.db.bookDao().insert(book) + return book } fun deleteBook(book: Book, deleteOriginal: Boolean) { diff --git a/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt b/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt index befd9a4aa..c3de3d832 100644 --- a/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt +++ b/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt @@ -2,11 +2,13 @@ package io.legado.app.ui.association import android.content.Intent import android.os.Bundle +import androidx.lifecycle.Observer import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Theme import io.legado.app.ui.main.MainActivity import io.legado.app.utils.getViewModel +import org.jetbrains.anko.toast class FileAssociationActivity : VMBaseActivity( @@ -18,20 +20,21 @@ class FileAssociationActivity : VMBaseActivity( get() = getViewModel(FileAssociationViewModel::class.java) override fun onActivityCreated(savedInstanceState: Bundle?) { + viewModel.errorLiveData.observe(this, Observer { + toast(it) + gotoMainActivity() + }) + viewModel.successLiveData.observe(this, Observer { + startActivity(it) + finish() + }) intent.data?.let { data -> - val newIntent = viewModel.dispatchIndent(data) - if (newIntent != null) { - this.startActivityForResult(newIntent, 100) - finish() - } else { - gotoMainActivity() - } + viewModel.dispatchIndent(data) } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) - //返回后直接跳转到主页面 gotoMainActivity() } diff --git a/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt b/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt index d1e0c0dc5..24743fa46 100644 --- a/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt @@ -5,8 +5,10 @@ import android.content.Intent import android.net.Uri import android.text.TextUtils import androidx.documentfile.provider.DocumentFile +import androidx.lifecycle.MutableLiveData import io.legado.app.base.BaseViewModel import io.legado.app.model.localBook.LocalBook +import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.utils.isJsonArray import io.legado.app.utils.isJsonObject import io.legado.app.utils.readText @@ -14,8 +16,11 @@ import java.io.File class FileAssociationViewModel(application: Application) : BaseViewModel(application) { - fun dispatchIndent(uri: Uri): Intent? { - try { + val successLiveData = MutableLiveData() + val errorLiveData = MutableLiveData() + + fun dispatchIndent(uri: Uri) { + execute { val url: String //如果是普通的url,需要根据返回的内容判断是什么 if (uri.scheme == "file" || uri.scheme == "content") { @@ -46,19 +51,19 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica } } if (TextUtils.isEmpty(scheme)) { - execute { - if (uri.scheme == "content") { - LocalBook.importFile(uri.toString()) - } else { - LocalBook.importFile(uri.path.toString()) - } - toast("添加本地文件成功${uri.path}") + val book = if (uri.scheme == "content") { + LocalBook.importFile(uri.toString()) + } else { + LocalBook.importFile(uri.path.toString()) } - return null + val intent = Intent(context, ReadBookActivity::class.java) + intent.putExtra("bookUrl", book.bookUrl) + successLiveData.postValue(intent) + return@execute } } else { - toast("文件不存在") - return null + errorLiveData.postValue("文件不存在") + return@execute } // content模式下,需要传递完整的路径,方便后续解析 url = if (uri.scheme == "content") { @@ -75,11 +80,11 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica val data = Uri.parse(url) val newIndent = Intent(Intent.ACTION_VIEW) newIndent.data = data - return newIndent - } catch (e: Exception) { - e.printStackTrace() - toast(e.localizedMessage) - return null + successLiveData.postValue(newIndent) + return@execute + }.onError { + it.printStackTrace() + toast(it.localizedMessage) } } } \ No newline at end of file