导入本地添加智能扫描,菜单-智能扫描,扫描当前文件夹包括子文件夹下所有文件

pull/457/head
gedoor 4 years ago
parent b623b4d3b6
commit dc719ac462
  1. 20
      app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt
  2. 24
      app/src/main/java/io/legado/app/ui/book/local/ImportBookViewModel.kt
  3. 8
      app/src/main/res/layout/activity_import_book.xml

@ -235,13 +235,15 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
* 扫描当前文件夹
*/
private fun scanFolder() {
launch(IO) {
rootDoc?.let { doc ->
adapter.clearItems()
val lastDoc = subDocs.lastOrNull() ?: doc
viewModel.scanDoc(lastDoc) {
refresh_progress_bar.isAutoLoading = true
launch(IO) {
viewModel.scanDoc(lastDoc, true, find) {
launch {
adapter.addItem(it)
refresh_progress_bar.isAutoLoading = false
}
}
}
} ?: let {
@ -251,9 +253,11 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
} else {
adapter.clearItems()
val file = File(path)
viewModel.scanFile(file) {
refresh_progress_bar.isAutoLoading = true
launch(IO) {
viewModel.scanFile(file, true, find) {
launch {
adapter.addItem(it)
refresh_progress_bar.isAutoLoading = false
}
}
}
@ -261,6 +265,12 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
}
}
private val find: (docItem: DocItem) -> Unit = {
launch {
adapter.addItem(it)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {

@ -33,12 +33,17 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
}
}
fun scanDoc(documentFile: DocumentFile, find: (docItem: DocItem) -> Unit) {
fun scanDoc(
documentFile: DocumentFile,
isRoot: Boolean,
find: (docItem: DocItem) -> Unit,
finally: (() -> Unit)? = null
) {
val docList = DocumentUtils.listFiles(context, documentFile.uri)
docList.forEach { docItem ->
if (docItem.isDir) {
DocumentFile.fromSingleUri(context, docItem.uri)?.let {
scanDoc(it, find)
scanDoc(it, false, find)
}
} else if (docItem.name.endsWith(".txt", true)
|| docItem.name.endsWith(".epub", true)
@ -46,12 +51,20 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
find(docItem)
}
}
if (isRoot) {
finally?.invoke()
}
}
fun scanFile(file: File, find: (docItem: DocItem) -> Unit) {
fun scanFile(
file: File,
isRoot: Boolean,
find: (docItem: DocItem) -> Unit,
finally: (() -> Unit)? = null
) {
file.listFiles()?.forEach {
if (it.isDirectory) {
scanFile(it, find)
scanFile(it, false, find)
} else if (it.name.endsWith(".txt", true)
|| it.name.endsWith(".epub", true)
) {
@ -66,6 +79,9 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
)
}
}
if (isRoot) {
finally?.invoke()
}
}
}

@ -53,11 +53,17 @@
tools:ignore="UnusedAttribute" />
</LinearLayout>
<io.legado.app.ui.widget.anima.RefreshProgressBar
android:id="@+id/refresh_progress_bar"
android:layout_width="match_parent"
android:layout_height="2dp"
app:layout_constraintTop_toBottomOf="@id/lay_top" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:layout_constraintTop_toBottomOf="@id/lay_top"
app:layout_constraintTop_toBottomOf="@id/refresh_progress_bar"
app:layout_constraintBottom_toTopOf="@id/select_action_bar">
<io.legado.app.ui.widget.recycler.scroller.FastScrollRecyclerView

Loading…
Cancel
Save