pull/42/head
kunfei 5 years ago
parent c3ebf84eb3
commit 56117c22f9
  1. 10
      app/src/main/java/io/legado/app/ui/filechooser/FileChooserDialog.kt
  2. 89
      app/src/main/java/io/legado/app/ui/filechooser/adapter/FileAdapter.kt

@ -36,6 +36,12 @@ class FileChooserDialog : DialogFragment(),
} }
} }
override var allowExtensions: Array<String?>? = null
override var isOnlyListDir: Boolean = false
override var isShowHomeDir: Boolean = false
override var isShowUpDir: Boolean = true
override var isShowHideDir: Boolean = false
private var initPath = FileUtils.getSdCardPath() private var initPath = FileUtils.getSdCardPath()
private var mode: Int = FILE private var mode: Int = FILE
private lateinit var fileAdapter: FileAdapter private lateinit var fileAdapter: FileAdapter
@ -100,10 +106,10 @@ class FileChooserDialog : DialogFragment(),
} }
fileAdapter.loadData(currentPath) fileAdapter.loadData(currentPath)
var adapterCount = fileAdapter.itemCount var adapterCount = fileAdapter.itemCount
if (fileAdapter.isShowHomeDir) { if (isShowHomeDir) {
adapterCount-- adapterCount--
} }
if (fileAdapter.isShowUpDir) { if (isShowUpDir) {
adapterCount-- adapterCount--
} }
if (adapterCount < 1) { if (adapterCount < 1) {

@ -21,56 +21,11 @@ class FileAdapter(context: Context, val callBack: CallBack) :
private var rootPath: String? = null private var rootPath: String? = null
var currentPath: String? = null var currentPath: String? = null
private set private set
private var allowExtensions: Array<String?>? = null//允许的扩展名
/**
* 是否仅仅读取目录
*/
var isOnlyListDir = false//是否仅仅读取目录
/**
* 是否显示返回主目录
*/
var isShowHomeDir = false//是否显示返回主目录
/**
* 是否显示返回上一级
*/
var isShowUpDir = true//是否显示返回上一级
/**
* 是否显示隐藏的目录.开头
*/
var isShowHideDir = true//是否显示隐藏的目录(以“.”开头)
private var itemHeight = 40// dp
private var homeIcon: Drawable? = null private var homeIcon: Drawable? = null
private var upIcon: Drawable? = null private var upIcon: Drawable? = null
private var folderIcon: Drawable? = null private var folderIcon: Drawable? = null
private var fileIcon: Drawable? = null private var fileIcon: Drawable? = null
fun setFileIcon(fileIcon: Drawable) {
this.fileIcon = fileIcon
}
fun setFolderIcon(folderIcon: Drawable) {
this.folderIcon = folderIcon
}
fun setHomeIcon(homeIcon: Drawable) {
this.homeIcon = homeIcon
}
fun setUpIcon(upIcon: Drawable) {
this.upIcon = upIcon
}
/**
* 允许的扩展名
*/
fun setAllowExtensions(allowExtensions: Array<String?>) {
this.allowExtensions = allowExtensions
}
fun setItemHeight(itemHeight: Int) {
this.itemHeight = itemHeight
}
fun loadData(path: String?) { fun loadData(path: String?) {
if (path == null) { if (path == null) {
return return
@ -87,12 +42,12 @@ class FileAdapter(context: Context, val callBack: CallBack) :
if (fileIcon == null) { if (fileIcon == null) {
fileIcon = ConvertUtils.toDrawable(FilePickerIcon.getFILE()) fileIcon = ConvertUtils.toDrawable(FilePickerIcon.getFILE())
} }
val datas = ArrayList<FileItem>() val data = ArrayList<FileItem>()
if (rootPath == null) { if (rootPath == null) {
rootPath = path rootPath = path
} }
currentPath = path currentPath = path
if (isShowHomeDir) { if (callBack.isShowHomeDir) {
//添加“返回主目录” //添加“返回主目录”
val fileRoot = FileItem() val fileRoot = FileItem()
fileRoot.isDirectory = true fileRoot.isDirectory = true
@ -100,9 +55,9 @@ class FileAdapter(context: Context, val callBack: CallBack) :
fileRoot.name = DIR_ROOT fileRoot.name = DIR_ROOT
fileRoot.size = 0 fileRoot.size = 0
fileRoot.path = rootPath ?: path fileRoot.path = rootPath ?: path
datas.add(fileRoot) data.add(fileRoot)
} }
if (isShowUpDir && path != "/") { if (callBack.isShowUpDir && path != "/") {
//添加“返回上一级目录” //添加“返回上一级目录”
val fileParent = FileItem() val fileParent = FileItem()
fileParent.isDirectory = true fileParent.isDirectory = true
@ -110,17 +65,17 @@ class FileAdapter(context: Context, val callBack: CallBack) :
fileParent.name = DIR_PARENT fileParent.name = DIR_PARENT
fileParent.size = 0 fileParent.size = 0
fileParent.path = File(path).parent fileParent.path = File(path).parent
datas.add(fileParent) data.add(fileParent)
} }
currentPath?.let { currentPath -> currentPath?.let { currentPath ->
val files: Array<File?>? = allowExtensions?.let { allowExtensions -> val files: Array<File?>? = callBack.allowExtensions?.let { allowExtensions ->
if (isOnlyListDir) { if (callBack.isOnlyListDir) {
FileUtils.listDirs(currentPath, allowExtensions) FileUtils.listDirs(currentPath, allowExtensions)
} else { } else {
FileUtils.listDirsAndFiles(currentPath, allowExtensions) FileUtils.listDirsAndFiles(currentPath, allowExtensions)
} }
} ?: let { } ?: let {
if (isOnlyListDir) { if (callBack.isOnlyListDir) {
FileUtils.listDirs(currentPath) FileUtils.listDirs(currentPath)
} else { } else {
FileUtils.listDirsAndFiles(currentPath) FileUtils.listDirsAndFiles(currentPath)
@ -128,7 +83,7 @@ class FileAdapter(context: Context, val callBack: CallBack) :
} }
if (files != null) { if (files != null) {
for (file in files) { for (file in files) {
if (file == null || (!isShowHideDir && file.name.startsWith("."))) { if (file == null || (!callBack.isShowHideDir && file.name.startsWith("."))) {
continue continue
} }
val fileItem = FileItem() val fileItem = FileItem()
@ -143,10 +98,10 @@ class FileAdapter(context: Context, val callBack: CallBack) :
} }
fileItem.name = file.name fileItem.name = file.name
fileItem.path = file.absolutePath fileItem.path = file.absolutePath
datas.add(fileItem) data.add(fileItem)
} }
} }
setItems(datas) setItems(data)
} }
} }
@ -163,11 +118,29 @@ class FileAdapter(context: Context, val callBack: CallBack) :
interface CallBack { interface CallBack {
fun onFileClick(position: Int) fun onFileClick(position: Int)
//允许的扩展名
var allowExtensions: Array<String?>?
/**
* 是否仅仅读取目录
*/
var isOnlyListDir: Boolean
/**
* 是否显示返回主目录
*/
var isShowHomeDir: Boolean
/**
* 是否显示返回上一级
*/
var isShowUpDir: Boolean
/**
* 是否显示隐藏的目录.开头
*/
var isShowHideDir: Boolean
} }
companion object { companion object {
val DIR_ROOT = "." const val DIR_ROOT = "."
val DIR_PARENT = ".." const val DIR_PARENT = ".."
} }
} }

Loading…
Cancel
Save