Skip to content

Commit 70954dc

Browse files
committed
[Update]
- Marked AbstractFileType.kt as deprecated, using AbstractFileDetector.kt instead. - Modify some fields and functions visibility of FilePickerConfig.kt which would leak in Java caller. [Add] - Custom file type demo.
1 parent 37963ea commit 70954dc

14 files changed

Lines changed: 159 additions & 31 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package me.rosuh.filepicker.config
2+
3+
import me.rosuh.filepicker.bean.FileItemBeanImpl
4+
import me.rosuh.filepicker.filetype.FileType
5+
6+
/**
7+
*
8+
* @author rosu
9+
* @date 2020/06/25
10+
* 这个类用于注册你自己的文件类型检测方法。您需要遵循下列步骤:
11+
* 1. 实现您自己的文件类型[FileType],也就是其中的[FileType.verify]方法
12+
* 2. 构建此类的一个子类,并在[AbstractFileDetector.fillFileType] 中,检测文件类型,并赋值给[FileItemBeanImpl.fileType]属性
13+
* ===================================================================================================================
14+
* This class is used to register your own file type detection methods. You need to follow the following steps:
15+
* 1. implement your own file type [FileType], which is the [FileType.verify] method of the [FileType].
16+
* 2. Construct a subclass of this class and, in [AbstractFileDetector.fillFileType], detect the file type and assign it to [FileItemBeanImpl.fileType] property.
17+
*
18+
*/
19+
abstract class AbstractFileDetector {
20+
/**
21+
* 自定义文件类型识别方法,传入 @param itemBeanImpl 条目数据对象,
22+
* 由实现者来实现文件类型的甄别,返回填充了 fileType 的方法
23+
*/
24+
abstract fun fillFileType(itemBeanImpl: FileItemBeanImpl): FileItemBeanImpl
25+
}

filepicker/src/main/java/me/rosuh/filepicker/config/AbstractFileType.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import me.rosuh.filepicker.bean.FileItemBeanImpl
77
* @author rosu
88
* @date 2018/11/27
99
*/
10+
@Deprecated(
11+
"Because the class name was confused.",
12+
ReplaceWith("AbstractFileDetector", "me.rosuh.filepicker.config")
13+
)
1014
abstract class AbstractFileType {
1115
/**
1216
* 自定义文件类型识别方法,传入 @param itemBeanImpl 条目数据对象,

filepicker/src/main/java/me/rosuh/filepicker/config/DefaultFileType.kt renamed to filepicker/src/main/java/me/rosuh/filepicker/config/DefaultFileDetector.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import me.rosuh.filepicker.filetype.*
88
* @author rosu
99
* @date 2018/11/27
1010
*/
11-
class DefaultFileType : AbstractFileType() {
11+
class DefaultFileDetector : AbstractFileDetector() {
1212

1313
private val allDefaultFileType: ArrayList<FileType> by lazy {
1414
val fileTypes = ArrayList<FileType>()

filepicker/src/main/java/me/rosuh/filepicker/config/FilePickerConfig.kt

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,69 +19,98 @@ class FilePickerConfig(private val pickerManager: FilePickerManager) {
1919
* 是否显示隐藏文件,默认隐藏
2020
* 以符号 . 开头的文件或文件夹视为隐藏
2121
*/
22-
internal var isShowHiddenFiles = false
22+
var isShowHiddenFiles = false
23+
private set
24+
2325
/**
2426
* 是否显示选中框,默认显示
2527
*/
26-
internal var isShowingCheckBox = true
28+
var isShowingCheckBox = true
29+
private set
30+
2731
/**
2832
* 在选中时是否忽略文件夹
2933
*/
30-
internal var isSkipDir = true
34+
var isSkipDir = true
35+
private set
36+
3137
/**
3238
* 是否是单选
3339
* 如果是单选,则隐藏顶部【全选/取消全选按钮】
3440
*/
35-
internal var singleChoice = false
41+
var singleChoice = false
42+
private set
43+
3644
/**
3745
* 最大可被选中数量
3846
*/
39-
internal var maxSelectable = Int.MAX_VALUE
47+
var maxSelectable = Int.MAX_VALUE
48+
private set
49+
4050
/**
4151
* 存储类型
4252
*/
43-
internal var mediaStorageName = contextRes.getString(R.string.file_picker_tv_sd_card)
53+
var mediaStorageName = contextRes.getString(R.string.file_picker_tv_sd_card)
54+
private set
4455

4556
/**
4657
* 自定义存储类型,根据此返回根目录
4758
*/
4859
@get:StorageMediaType
4960
@set:StorageMediaType
50-
internal var mediaStorageType: String = STORAGE_EXTERNAL_STORAGE
61+
var mediaStorageType: String = STORAGE_EXTERNAL_STORAGE
62+
private set
63+
5164
/**
5265
* 自定义根目录路径,需要先设置 [mediaStorageType] 为 [STORAGE_CUSTOM_ROOT_PATH]
5366
*/
54-
internal var customRootPath: String = ""
67+
var customRootPath: String = ""
68+
private set
69+
5570
/**
5671
* 自定义过滤器
5772
*/
58-
internal var selfFilter: AbstractFileFilter? = null
73+
var selfFilter: AbstractFileFilter? = null
74+
private set
75+
5976
/**
6077
* 自定文件类型甄别器和默认类型甄别器
6178
*/
62-
internal var selfFileType: AbstractFileType? = null
63-
internal val defaultFileType: DefaultFileType by lazy { DefaultFileType() }
79+
var customDetector: AbstractFileDetector? = null
80+
private set
81+
val defaultFileDetector: DefaultFileDetector by lazy { DefaultFileDetector() }
82+
6483
/**
6584
* 点击操作接口,采用默认实现
6685
*/
67-
internal var fileItemOnClickListener: FileItemOnClickListener? = null
86+
var fileItemOnClickListener: FileItemOnClickListener? = null
87+
private set
88+
6889
/**
6990
* 主题
7091
*/
71-
internal var themeId: Int = R.style.FilePickerThemeRail
92+
var themeId: Int = R.style.FilePickerThemeRail
93+
private set
94+
7295
/**
7396
* 全选文字,取消全选文字,返回文字,已选择文字,确认按钮,选择限制提示语,空列表提示
7497
*/
75-
internal var selectAllText: String = contextRes.getString(R.string.file_picker_tv_select_all)
98+
var selectAllText: String = contextRes.getString(R.string.file_picker_tv_select_all)
99+
private set
100+
var deSelectAllText: String = contextRes.getString(R.string.file_picker_tv_deselect_all)
101+
private set
76102

77-
internal var deSelectAllText: String =
78-
contextRes.getString(R.string.file_picker_tv_deselect_all)
79103
@StringRes
80-
internal var hadSelectedText: Int = R.string.file_picker_selected_count
81-
internal var confirmText: String = contextRes.getString(R.string.file_picker_tv_select_done)
104+
var hadSelectedText: Int = R.string.file_picker_selected_count
105+
private set
106+
var confirmText: String = contextRes.getString(R.string.file_picker_tv_select_done)
107+
private set
108+
82109
@StringRes
83-
internal var maxSelectCountTips: Int = R.string.max_select_count_tips
84-
internal var emptyListTips: String = contextRes.getString(R.string.empty_list_tips_file_picker)
110+
var maxSelectCountTips: Int = R.string.max_select_count_tips
111+
private set
112+
var emptyListTips: String = contextRes.getString(R.string.empty_list_tips_file_picker)
113+
private set
85114

86115
fun showHiddenFiles(isShow: Boolean): FilePickerConfig {
87116
isShowHiddenFiles = isShow
@@ -120,8 +149,12 @@ class FilePickerConfig(private val pickerManager: FilePickerManager) {
120149
return this
121150
}
122151

123-
fun fileType(fileType: AbstractFileType): FilePickerConfig {
124-
selfFileType = fileType
152+
/**
153+
* 实现 [AbstractFileDetector] 以自定义您自己的文件类型检测器
154+
* Custom your file detector by implementing [AbstractFileDetector]
155+
*/
156+
fun customDetector(detector: AbstractFileDetector): FilePickerConfig {
157+
customDetector = detector
125158
return this
126159
}
127160

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ class FileUtils {
7777
beanSubscriber
7878
)
7979
// 如果调用者没有实现文件类型甄别器,则使用的默认甄别器
80-
FilePickerManager.config.selfFileType?.fillFileType(itemBean)
81-
?: FilePickerManager.config.defaultFileType.fillFileType(itemBean)
80+
FilePickerManager.config.customDetector?.fillFileType(itemBean)
81+
?: FilePickerManager.config.defaultFileDetector.fillFileType(itemBean)
8282
listData.add(itemBean)
8383
}
8484
listData.run {

filepicker/src/main/res/values-zh-rCN/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
<string name="too_many_files_tips">当前文件过多\n正在载入中&#8230;</string>
1212
<string name="max_select_count_tips">最多只能选择 %d 项</string>
1313
<string name="empty_list_tips_file_picker">空空如也~</string>
14+
<string name="custom_file_type">自定义文件类型</string>
1415

1516
</resources>

filepicker/src/main/res/values-zh-rHK/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
<string name="too_many_files_tips">檔案太多\n載入中&#8230;</string>
1212
<string name="max_select_count_tips">最多隻能選擇 %d 项</string>
1313
<string name="empty_list_tips_file_picker">空空如也~</string>
14+
<string name="custom_file_type">自定義文件類型</string>
1415

1516
</resources>

filepicker/src/main/res/values-zh-rTW/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
<string name="too_many_files_tips">檔案太多\n載入中&#8230;</string>
1212
<string name="max_select_count_tips">最多隻能選擇 %d 项</string>
1313
<string name="empty_list_tips_file_picker">空空如也~</string>
14+
<string name="custom_file_type">自定義文件類型</string>
1415

1516
</resources>

filepicker/src/main/res/values-zh/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
<string name="too_many_files_tips">当前文件过多\n正在载入中&#8230;</string>
1212
<string name="max_select_count_tips">最多只能选择 %d 项</string>
1313
<string name="empty_list_tips_file_picker">空空如也~</string>
14+
<string name="custom_file_type">自定义文件类型</string>
1415

1516
</resources>

filepicker/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
<string name="too_many_files_tips">Too many current files, loading…</string>
1111
<string name="max_select_count_tips">You can only choose %d items.</string>
1212
<string name="empty_list_tips_file_picker">Nothing Here~</string>
13+
<string name="custom_file_type">Custom file type</string>
1314

1415
</resources>

0 commit comments

Comments
 (0)