|
|
|
@ -5,6 +5,10 @@ import android.net.Uri |
|
|
|
|
import androidx.documentfile.provider.DocumentFile |
|
|
|
|
import io.legado.app.base.BaseViewModel |
|
|
|
|
import io.legado.app.model.localBook.LocalBook |
|
|
|
|
import io.legado.app.utils.DocItem |
|
|
|
|
import io.legado.app.utils.DocumentUtils |
|
|
|
|
import java.io.File |
|
|
|
|
import java.util.* |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ImportBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
@ -29,4 +33,43 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun scanDoc(documentFile: DocumentFile, find: (docItem: DocItem) -> Unit) { |
|
|
|
|
execute { |
|
|
|
|
val docList = DocumentUtils.listFiles(context, documentFile.uri) |
|
|
|
|
docList.forEach { docItem -> |
|
|
|
|
if (docItem.isDir) { |
|
|
|
|
DocumentFile.fromSingleUri(context, docItem.uri)?.let { |
|
|
|
|
scanDoc(it, find) |
|
|
|
|
} |
|
|
|
|
} else if (docItem.name.endsWith(".txt", true) |
|
|
|
|
|| docItem.name.endsWith(".epub", true) |
|
|
|
|
) { |
|
|
|
|
find(docItem) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun scanFile(file: File, find: (docItem: DocItem) -> Unit) { |
|
|
|
|
execute { |
|
|
|
|
file.listFiles()?.forEach { |
|
|
|
|
if (it.isDirectory) { |
|
|
|
|
scanFile(it, find) |
|
|
|
|
} else if (it.name.endsWith(".txt", true) |
|
|
|
|
|| it.name.endsWith(".epub", true) |
|
|
|
|
) { |
|
|
|
|
find( |
|
|
|
|
DocItem( |
|
|
|
|
it.name, |
|
|
|
|
it.extension, |
|
|
|
|
it.length(), |
|
|
|
|
Date(it.lastModified()), |
|
|
|
|
Uri.parse(it.absolutePath) |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |