Skip to content

Commit

Permalink
Add DocumentFile.findFiles()
Browse files Browse the repository at this point in the history
  • Loading branch information
anggrayudi committed Sep 9, 2020
1 parent 57ef1f9 commit 38904bd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ Adding Simple Storage into your project is simple:
implementation "com.anggrayudi:storage:0.1.0"
```

Snapshots can be found [here](https://oss.sonatype.org/#nexus-search;quick~com.anggrayudi).
To use SNAPSHOT version, you need to add this URL to the root Gradle:

```groovy
allprojects {
repositories {
google()
jcenter()
mavenCentral()
// add this line
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
}
```

## Request Storage Access

Although user has granted read and write permissions during runtime, your app may still does not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,20 +237,29 @@ fun DocumentFile.createBinaryFile(
}
}

fun DocumentFile.findFiles(names: Array<String>, documentType: DocumentFileType = DocumentFileType.ALL): List<DocumentFile> {
val files = listFiles().filter { it.name in names }
return when (documentType) {
DocumentFileType.FILE -> files.filter { it.isFile }
DocumentFileType.FOLDER -> files.filter { it.isDirectory }
else -> files
}
}

/**
* @param recursive walk into sub folders
*/
@WorkerThread
fun DocumentFile.search(
recursive: Boolean = false,
searchMode: FileSearchMode = FileSearchMode.ALL,
documentType: DocumentFileType = DocumentFileType.ALL,
mimeType: String = DocumentFileCompat.MIME_TYPE_UNKNOWN,
name: String = "",
regex: Regex? = null
): List<DocumentFile> {
return when {
!isDirectory -> emptyList()
recursive -> walkFileTree(searchMode, mimeType, name, regex)
recursive -> walkFileTree(documentType, mimeType, name, regex)
else -> {
var sequence = listFiles().asSequence().filter { it.canRead() }
if (name.isNotEmpty()) {
Expand All @@ -263,17 +272,17 @@ fun DocumentFile.search(
sequence = sequence.filter { it.type == mimeType }
}
@Suppress("NON_EXHAUSTIVE_WHEN")
when (searchMode) {
FileSearchMode.FILE_ONLY -> sequence = sequence.filter { it.isFile }
FileSearchMode.FOLDER_ONLY -> sequence = sequence.filter { it.isDirectory }
when (documentType) {
DocumentFileType.FILE -> sequence = sequence.filter { it.isFile }
DocumentFileType.FOLDER -> sequence = sequence.filter { it.isDirectory }
}
sequence.toList()
}
}
}

private fun DocumentFile.walkFileTree(
searchMode: FileSearchMode,
documentType: DocumentFileType,
mimeType: String,
nameFilter: String,
regex: Regex?
Expand All @@ -283,7 +292,7 @@ private fun DocumentFile.walkFileTree(
if (!canRead()) continue

if (file.isFile) {
if (searchMode == FileSearchMode.FOLDER_ONLY) {
if (documentType == DocumentFileType.FOLDER) {
continue
}
val filename = file.name.orEmpty()
Expand All @@ -294,14 +303,13 @@ private fun DocumentFile.walkFileTree(
fileTree.add(file)
}
} else {
val folderName = file.name.orEmpty()
if (searchMode != FileSearchMode.FILE_ONLY
&& (nameFilter.isEmpty() || folderName == nameFilter)
&& (regex == null || regex.matches(folderName))
) {
fileTree.add(file)
if (documentType != DocumentFileType.FILE) {
val folderName = file.name.orEmpty()
if ((nameFilter.isEmpty() || folderName == nameFilter) && (regex == null || regex.matches(folderName))) {
fileTree.add(file)
}
}
fileTree.addAll(file.walkFileTree(searchMode, mimeType, nameFilter, regex))
fileTree.addAll(file.walkFileTree(documentType, mimeType, nameFilter, regex))
}
}
return fileTree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ package com.anggrayudi.storage.file
* Created on 08/09/20
* @author Anggrayudi H
*/
enum class FileSearchMode {
enum class DocumentFileType {
ALL,
FILE_ONLY,
FOLDER_ONLY
FILE,
FOLDER
}

0 comments on commit 38904bd

Please sign in to comment.