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

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() { private fun scanFolder() {
launch(IO) {
rootDoc?.let { doc -> rootDoc?.let { doc ->
adapter.clearItems() adapter.clearItems()
val lastDoc = subDocs.lastOrNull() ?: doc val lastDoc = subDocs.lastOrNull() ?: doc
viewModel.scanDoc(lastDoc) { refresh_progress_bar.isAutoLoading = true
launch(IO) {
viewModel.scanDoc(lastDoc, true, find) {
launch { launch {
adapter.addItem(it) refresh_progress_bar.isAutoLoading = false
}
} }
} }
} ?: let { } ?: let {
@ -251,9 +253,11 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
} else { } else {
adapter.clearItems() adapter.clearItems()
val file = File(path) val file = File(path)
viewModel.scanFile(file) { refresh_progress_bar.isAutoLoading = true
launch(IO) {
viewModel.scanFile(file, true, find) {
launch { 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?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data) super.onActivityResult(requestCode, resultCode, data)
when (requestCode) { 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) val docList = DocumentUtils.listFiles(context, documentFile.uri)
docList.forEach { docItem -> docList.forEach { docItem ->
if (docItem.isDir) { if (docItem.isDir) {
DocumentFile.fromSingleUri(context, docItem.uri)?.let { DocumentFile.fromSingleUri(context, docItem.uri)?.let {
scanDoc(it, find) scanDoc(it, false, find)
} }
} else if (docItem.name.endsWith(".txt", true) } else if (docItem.name.endsWith(".txt", true)
|| docItem.name.endsWith(".epub", true) || docItem.name.endsWith(".epub", true)
@ -46,12 +51,20 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
find(docItem) 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 { file.listFiles()?.forEach {
if (it.isDirectory) { if (it.isDirectory) {
scanFile(it, find) scanFile(it, false, find)
} else if (it.name.endsWith(".txt", true) } else if (it.name.endsWith(".txt", true)
|| it.name.endsWith(".epub", 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" /> tools:ignore="UnusedAttribute" />
</LinearLayout> </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 <FrameLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="1" 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"> app:layout_constraintBottom_toTopOf="@id/select_action_bar">
<io.legado.app.ui.widget.recycler.scroller.FastScrollRecyclerView <io.legado.app.ui.widget.recycler.scroller.FastScrollRecyclerView

Loading…
Cancel
Save