From 1d738cf4e292326c703be0ab46261f50c6ff6772 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 27 Aug 2021 19:36:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/model/localBook/LocalBook.kt | 33 ++++++++++--------- .../ui/association/FileAssociationActivity.kt | 7 ++-- .../association/FileAssociationViewModel.kt | 14 ++------ 3 files changed, 25 insertions(+), 29 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 5bf94834a..7739c3e52 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 @@ -70,23 +70,24 @@ object LocalBook { path = uri.path!! File(path).name }) - - val nameAuthor = analyzeNameAuthor(fileName) - - val book = Book( - bookUrl = path, - name = nameAuthor.first, - author = nameAuthor.second, - originName = fileName, - coverUrl = FileUtils.getPath( - appCtx.externalFiles, - "covers", - "${MD5Utils.md5Encode16(path)}.jpg" + var book = appDb.bookDao.getBook(path) + if (book == null) { + val nameAuthor = analyzeNameAuthor(fileName) + book = Book( + bookUrl = path, + name = nameAuthor.first, + author = nameAuthor.second, + originName = fileName, + coverUrl = FileUtils.getPath( + appCtx.externalFiles, + "covers", + "${MD5Utils.md5Encode16(path)}.jpg" + ) ) - ) - if (book.isEpub()) EpubFile.upBookInfo(book) - if (book.isUmd()) UmdFile.upBookInfo(book) - appDb.bookDao.insert(book) + if (book.isEpub()) EpubFile.upBookInfo(book) + if (book.isUmd()) UmdFile.upBookInfo(book) + appDb.bookDao.insert(book) + } return book } 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 e58f2ade2..970b65670 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 @@ -4,6 +4,7 @@ import android.os.Bundle import androidx.activity.viewModels import io.legado.app.base.VMBaseActivity import io.legado.app.databinding.ActivityTranslucenceBinding +import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.utils.startActivity import io.legado.app.utils.toastOnUi import io.legado.app.utils.viewbindingdelegate.viewBinding @@ -40,9 +41,11 @@ class FileAssociationActivity : toastOnUi(it) finish() }) - viewModel.successLiveData.observe(this, { + viewModel.openBookLiveData.observe(this, { binding.rotateLoading.hide() - startActivity(it) + startActivity { + putExtra("bookUrl", it) + } finish() }) intent.data?.let { data -> 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 4d009981b..b445d62bf 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 @@ -1,13 +1,11 @@ package io.legado.app.ui.association import android.app.Application -import android.content.Intent import android.net.Uri 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.isJson import io.legado.app.utils.readText import java.io.File @@ -17,7 +15,7 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica val importBookSourceLive = MutableLiveData() val importRssSourceLive = MutableLiveData() val importReplaceRuleLive = MutableLiveData() - val successLiveData = MutableLiveData() + val openBookLiveData = MutableLiveData() val errorLiveData = MutableLiveData() @Suppress("BlockingMethodInNonBlockingContext") @@ -48,14 +46,8 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica } } } - val book = if (uri.scheme == "content") { - LocalBook.importFile(uri) - } else { - LocalBook.importFile(uri) - } - val intent = Intent(context, ReadBookActivity::class.java) - intent.putExtra("bookUrl", book.bookUrl) - successLiveData.postValue(intent) + val book = LocalBook.importFile(uri) + openBookLiveData.postValue(book.bookUrl) } ?: throw Exception("文件不存在") } else { onLineImportLive.postValue(uri)