Merge pull request #1232 from GeorgCantor/dev

Some fixes
This commit is contained in:
Prathamesh More 2022-01-30 23:33:14 +05:30 committed by GitHub
commit b2e7a4ed7e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,7 +14,6 @@ import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.list.listItems import com.afollestad.materialdialogs.list.listItems
import com.afollestad.materialdialogs.list.updateListItems import com.afollestad.materialdialogs.list.updateListItems
import java.io.File import java.io.File
import java.util.*
class BlacklistFolderChooserDialog : DialogFragment() { class BlacklistFolderChooserDialog : DialogFragment() {
private var initialPath: String = Environment.getExternalStorageDirectory().absolutePath private var initialPath: String = Environment.getExternalStorageDirectory().absolutePath
@ -34,22 +33,16 @@ class BlacklistFolderChooserDialog : DialogFragment() {
results[0] = ".." results[0] = ".."
} }
for (i in parentContents!!.indices) { for (i in parentContents!!.indices) {
results[if (canGoUp) i + 1 else i] = parentContents!![i].name results[if (canGoUp) i + 1 else i] = parentContents?.getOrNull(i)?.name
} }
return results return results
} }
private fun listFiles(): Array<File>? { private fun listFiles(): Array<File>? {
val contents = parentFolder!!.listFiles() val results = mutableListOf<File>()
val results: MutableList<File> = ArrayList() parentFolder?.listFiles()?.let { files ->
if (contents != null) { files.forEach { file -> if (file.isDirectory) results.add(file) }
for (fi in contents) { return results.sortedBy { it.name }.toTypedArray()
if (fi.isDirectory) {
results.add(fi)
}
}
Collections.sort(results, FolderSorter())
return results.toTypedArray()
} }
return null return null
} }
@ -78,7 +71,7 @@ class BlacklistFolderChooserDialog : DialogFragment() {
checkIfCanGoUp() checkIfCanGoUp()
parentContents = listFiles() parentContents = listFiles()
return materialDialog() return materialDialog()
.title(text = parentFolder!!.absolutePath) .title(text = parentFolder?.absolutePath)
.listItems( .listItems(
items = contentsArray.toCharSequence(), items = contentsArray.toCharSequence(),
waitForPositiveButton = false waitForPositiveButton = false
@ -87,7 +80,7 @@ class BlacklistFolderChooserDialog : DialogFragment() {
} }
.noAutoDismiss() .noAutoDismiss()
.positiveButton(res = R.string.add_action) { .positiveButton(res = R.string.add_action) {
callback!!.onFolderSelection(this@BlacklistFolderChooserDialog, parentFolder!!) callback?.onFolderSelection(this@BlacklistFolderChooserDialog, parentFolder!!)
dismiss() dismiss()
} }
.negativeButton(res = android.R.string.cancel) { dismiss() } .negativeButton(res = android.R.string.cancel) { dismiss() }
@ -95,15 +88,15 @@ class BlacklistFolderChooserDialog : DialogFragment() {
private fun onSelection(i: Int) { private fun onSelection(i: Int) {
if (canGoUp && i == 0) { if (canGoUp && i == 0) {
parentFolder = parentFolder!!.parentFile parentFolder = parentFolder?.parentFile
if (parentFolder!!.absolutePath == "/storage/emulated") { if (parentFolder?.absolutePath == "/storage/emulated") {
parentFolder = parentFolder!!.parentFile parentFolder = parentFolder?.parentFile
} }
checkIfCanGoUp() checkIfCanGoUp()
} else { } else {
parentFolder = parentContents!![if (canGoUp) i - 1 else i] parentFolder = parentContents?.getOrNull(if (canGoUp) i - 1 else i)
canGoUp = true canGoUp = true
if (parentFolder!!.absolutePath == "/storage/emulated") { if (parentFolder?.absolutePath == "/storage/emulated") {
parentFolder = Environment.getExternalStorageDirectory() parentFolder = Environment.getExternalStorageDirectory()
} }
} }
@ -111,14 +104,14 @@ class BlacklistFolderChooserDialog : DialogFragment() {
} }
private fun checkIfCanGoUp() { private fun checkIfCanGoUp() {
canGoUp = parentFolder!!.parent != null canGoUp = parentFolder?.parent != null
} }
private fun reload() { private fun reload() {
parentContents = listFiles() parentContents = listFiles()
val dialog = dialog as MaterialDialog? val dialog = dialog as MaterialDialog?
dialog!!.setTitle(parentFolder!!.absolutePath) dialog?.setTitle(parentFolder?.absolutePath)
dialog.updateListItems(items = contentsArray.toCharSequence()) dialog?.updateListItems(items = contentsArray.toCharSequence())
} }
private fun Array<String?>.toCharSequence(): List<CharSequence> { private fun Array<String?>.toCharSequence(): List<CharSequence> {
@ -127,7 +120,7 @@ class BlacklistFolderChooserDialog : DialogFragment() {
override fun onSaveInstanceState(outState: Bundle) { override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState) super.onSaveInstanceState(outState)
outState.putString("current_path", parentFolder!!.absolutePath) outState.putString("current_path", parentFolder?.absolutePath)
} }
fun setCallback(callback: FolderCallback?) { fun setCallback(callback: FolderCallback?) {
@ -138,12 +131,6 @@ class BlacklistFolderChooserDialog : DialogFragment() {
fun onFolderSelection(dialog: BlacklistFolderChooserDialog, folder: File) fun onFolderSelection(dialog: BlacklistFolderChooserDialog, folder: File)
} }
private class FolderSorter : Comparator<File> {
override fun compare(lhs: File, rhs: File): Int {
return lhs.name.compareTo(rhs.name)
}
}
companion object { companion object {
fun create(): BlacklistFolderChooserDialog { fun create(): BlacklistFolderChooserDialog {
return BlacklistFolderChooserDialog() return BlacklistFolderChooserDialog()