pull/84/head
kunfei 5 years ago
parent abc1f597a6
commit 228289b675
  1. 30
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  2. 21
      app/src/main/java/io/legado/app/ui/importbook/ImportBookViewModel.kt

@ -0,0 +1,30 @@
package io.legado.app.model.localBook
import androidx.documentfile.provider.DocumentFile
import io.legado.app.App
import io.legado.app.data.entities.Book
object LocalBook {
fun importFile(doc: DocumentFile) {
doc.name?.let { fileName ->
val str = fileName.substringBeforeLast(".")
var name = str.substringBefore("作者")
val author = str.substringAfter("作者")
val smhStart = name.indexOf("")
val smhEnd = name.indexOf("")
if (smhStart != -1 && smhEnd != -1) {
name = (name.substring(smhStart + 1, smhEnd))
}
val book = Book(
bookUrl = doc.uri.toString(),
name = name,
author = author,
originName = fileName
)
App.db.bookDao().insert(book)
}
}
}

@ -3,9 +3,8 @@ package io.legado.app.ui.importbook
import android.app.Application
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import io.legado.app.App
import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.Book
import io.legado.app.model.localBook.LocalBook
class ImportBookViewModel(application: Application) : BaseViewModel(application) {
@ -14,23 +13,7 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
execute {
uriList.forEach { uriStr ->
DocumentFile.fromSingleUri(context, Uri.parse(uriStr))?.let { doc ->
doc.name?.let { fileName ->
val str = fileName.substringBeforeLast(".")
var name = str.substringBefore("作者")
val author = str.substringAfter("作者")
val smhStart = name.indexOf("")
val smhEnd = name.indexOf("")
if (smhStart != -1 && smhEnd != -1) {
name = (name.substring(smhStart + 1, smhEnd))
}
val book = Book(
bookUrl = uriStr,
name = name,
author = author,
originName = fileName
)
App.db.bookDao().insert(book)
}
LocalBook.importFile(doc)
}
}
}.onFinally {

Loading…
Cancel
Save