pull/1282/head
gedoor 3 years ago
parent ac3f2334d0
commit 1d738cf4e2
  1. 33
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  2. 7
      app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt
  3. 14
      app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt

@ -70,23 +70,24 @@ object LocalBook {
path = uri.path!! path = uri.path!!
File(path).name File(path).name
}) })
var book = appDb.bookDao.getBook(path)
val nameAuthor = analyzeNameAuthor(fileName) if (book == null) {
val nameAuthor = analyzeNameAuthor(fileName)
val book = Book( book = Book(
bookUrl = path, bookUrl = path,
name = nameAuthor.first, name = nameAuthor.first,
author = nameAuthor.second, author = nameAuthor.second,
originName = fileName, originName = fileName,
coverUrl = FileUtils.getPath( coverUrl = FileUtils.getPath(
appCtx.externalFiles, appCtx.externalFiles,
"covers", "covers",
"${MD5Utils.md5Encode16(path)}.jpg" "${MD5Utils.md5Encode16(path)}.jpg"
)
) )
) if (book.isEpub()) EpubFile.upBookInfo(book)
if (book.isEpub()) EpubFile.upBookInfo(book) if (book.isUmd()) UmdFile.upBookInfo(book)
if (book.isUmd()) UmdFile.upBookInfo(book) appDb.bookDao.insert(book)
appDb.bookDao.insert(book) }
return book return book
} }

@ -4,6 +4,7 @@ import android.os.Bundle
import androidx.activity.viewModels import androidx.activity.viewModels
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.databinding.ActivityTranslucenceBinding 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.startActivity
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
@ -40,9 +41,11 @@ class FileAssociationActivity :
toastOnUi(it) toastOnUi(it)
finish() finish()
}) })
viewModel.successLiveData.observe(this, { viewModel.openBookLiveData.observe(this, {
binding.rotateLoading.hide() binding.rotateLoading.hide()
startActivity(it) startActivity<ReadBookActivity> {
putExtra("bookUrl", it)
}
finish() finish()
}) })
intent.data?.let { data -> intent.data?.let { data ->

@ -1,13 +1,11 @@
package io.legado.app.ui.association package io.legado.app.ui.association
import android.app.Application import android.app.Application
import android.content.Intent
import android.net.Uri import android.net.Uri
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.model.localBook.LocalBook 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.isJson
import io.legado.app.utils.readText import io.legado.app.utils.readText
import java.io.File import java.io.File
@ -17,7 +15,7 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica
val importBookSourceLive = MutableLiveData<String>() val importBookSourceLive = MutableLiveData<String>()
val importRssSourceLive = MutableLiveData<String>() val importRssSourceLive = MutableLiveData<String>()
val importReplaceRuleLive = MutableLiveData<String>() val importReplaceRuleLive = MutableLiveData<String>()
val successLiveData = MutableLiveData<Intent>() val openBookLiveData = MutableLiveData<String>()
val errorLiveData = MutableLiveData<String>() val errorLiveData = MutableLiveData<String>()
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
@ -48,14 +46,8 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica
} }
} }
} }
val book = if (uri.scheme == "content") { val book = LocalBook.importFile(uri)
LocalBook.importFile(uri) openBookLiveData.postValue(book.bookUrl)
} else {
LocalBook.importFile(uri)
}
val intent = Intent(context, ReadBookActivity::class.java)
intent.putExtra("bookUrl", book.bookUrl)
successLiveData.postValue(intent)
} ?: throw Exception("文件不存在") } ?: throw Exception("文件不存在")
} else { } else {
onLineImportLive.postValue(uri) onLineImportLive.postValue(uri)

Loading…
Cancel
Save