Skip to content

Commit 8f2d46f

Browse files
committed
Merge branch 'dev'
2 parents 7687193 + b698017 commit 8f2d46f

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

filepicker/src/main/java/me/rosuh/filepicker/FilePickerActivity.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,19 @@ class FilePickerActivity : AppCompatActivity(), View.OnClickListener,
7070

7171
private val loadFileRunnable: Runnable by lazy {
7272
Runnable {
73-
val rootFile = if (navDataSource.isEmpty()) {
74-
FileUtils.getRootFile()
75-
} else {
76-
File(navDataSource.last().dirPath)
73+
val rootFile = when{
74+
navDataSource.isEmpty() && pickerConfig.isSkipDir -> {
75+
FileUtils.getRootFile()
76+
}
77+
navDataSource.isEmpty() && !pickerConfig.isSkipDir -> {
78+
// 如果是文件夹作为可选项时,需要让根目录也作为 item 被点击,故而取根目录上级作为 rootFiles
79+
FileUtils.getRootFile().parentFile
80+
}
81+
else -> {
82+
File(navDataSource.last().dirPath)
83+
}
7784
}
85+
7886
val listData = FileUtils.produceListDataSource(rootFile, this@FilePickerActivity)
7987
// 导航栏数据集
8088
navDataSource = FileUtils.produceNavDataSource(

filepicker/src/main/java/me/rosuh/filepicker/utils/FileUtils.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ class FileUtils {
5151
): ArrayList<FileItemBeanImpl> {
5252
val listData: ArrayList<FileItemBeanImpl> = ArrayList()
5353
var isDetected = false
54+
// 查看是否在根目录上级
55+
val realRoot = getRootFile()
56+
val isInRootParent = rootFile.list() == null
57+
&& !config.isSkipDir
58+
&& rootFile.path == realRoot.parentFile.path
59+
if (isInRootParent) {
60+
// 如果是文件夹作为可选项时,需要让根目录也作为 item 被点击
61+
listData.add(
62+
FileItemBeanImpl(
63+
realRoot.name,
64+
realRoot.path,
65+
false,
66+
null,
67+
isDir = true,
68+
isHide = false,
69+
beanSubscriber = beanSubscriber
70+
)
71+
)
72+
return config.selfFilter?.doFilter(listData) ?: listData
73+
}
5474
for (file in rootFile.listFiles()) {
5575
//以符号 . 开头的视为隐藏文件或隐藏文件夹,后面进行过滤
5676
val isHiddenFile = file.name.startsWith(".")
@@ -84,7 +104,7 @@ class FileUtils {
84104
// 如果调用者没有实现文件类型甄别器,则使用的默认甄别器
85105
config.customDetector?.fillFileType(itemBean)
86106
?: config.defaultFileDetector.fillFileType(itemBean)
87-
isDetected = itemBean.fileType != null
107+
isDetected = itemBean.fileType != null
88108
if (config.defaultFileDetector.enableCustomTypes
89109
&& config.isAutoFilter
90110
&& !isDetected
@@ -94,6 +114,7 @@ class FileUtils {
94114
}
95115
listData.add(itemBean)
96116
}
117+
97118
// 默认字典排序
98119
// Default sort by alphabet
99120
listData.sortWith(compareBy({ !it.isDir }, { it.fileName.toUpperCase() }))

0 commit comments

Comments
 (0)