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 new file mode 100644 index 000000000..e17c08603 --- /dev/null +++ b/app/src/main/java/io/legado/app/model/localBook/LocalBook.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) + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/importbook/ImportBookViewModel.kt b/app/src/main/java/io/legado/app/ui/importbook/ImportBookViewModel.kt index 75d2fc5e0..1250da682 100644 --- a/app/src/main/java/io/legado/app/ui/importbook/ImportBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/importbook/ImportBookViewModel.kt @@ -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 {