优化文件选择

pull/363/head
gedoor 4 years ago
parent ac8ea32a06
commit cda68d51b3
  1. 15
      app/src/main/java/io/legado/app/ui/book/download/DownloadActivity.kt
  2. 6
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt
  3. 60
      app/src/main/java/io/legado/app/ui/filechooser/FilePicker.kt
  4. 6
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt
  5. 20
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt
  6. 6
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt
  7. 17
      app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt
  8. 1
      app/src/main/res/values-zh-rTW/arrays.xml
  9. 1
      app/src/main/res/values-zh/arrays.xml
  10. 1
      app/src/main/res/values/arrays.xml

@ -28,7 +28,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jetbrains.anko.toast
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CopyOnWriteArraySet
@ -191,13 +190,13 @@ class DownloadActivity : VMBaseActivity<DownloadViewModel>(R.layout.activity_dow
override fun export(position: Int) {
exportPosition = position
FilePicker.selectFolder(this, exportRequestCode) {
val path = ACache.get(this@DownloadActivity).getAsString(exportBookPathKey)
if (path.isNullOrEmpty()) {
toast(R.string.no_default_path)
} else {
startExport(path)
}
val default = arrayListOf<String>()
val path = ACache.get(this@DownloadActivity).getAsString(exportBookPathKey)
if (!path.isNullOrEmpty()) {
default.add(path)
}
FilePicker.selectFolder(this, exportRequestCode, otherActions = default) {
startExport(it)
}
}

@ -89,11 +89,7 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
R.id.menu_group_manage ->
GroupManageDialog().show(supportFragmentManager, "groupManage")
R.id.menu_import_source_local -> FilePicker
.selectFile(
this,
importRequestCode,
allowExtensions = arrayOf("txt", "json")
)
.selectFile(this, importRequestCode, allowExtensions = arrayOf("txt", "json"))
R.id.menu_import_source_onLine -> showImportDialog()
R.id.menu_sort_manual -> {
item.isChecked = true

@ -19,18 +19,18 @@ object FilePicker {
activity: AppCompatActivity,
requestCode: Int,
title: String = activity.getString(R.string.select_folder),
default: (() -> Unit)? = null
otherActions: List<String>? = null,
otherFun: ((action: String) -> Unit)? = null
) {
activity.alert(title = title) {
val selectList =
activity.resources.getStringArray(R.array.select_folder).toMutableList()
default ?: let {
selectList.removeAt(0)
otherActions?.let {
selectList.addAll(otherActions)
}
items(selectList) { _, index ->
when (if (default == null) index + 1 else index) {
0 -> default?.invoke()
1 -> {
when (index) {
0 -> {
try {
val intent = createSelectDirIntent()
activity.startActivityForResult(intent, requestCode)
@ -39,13 +39,14 @@ object FilePicker {
activity.toast(e.localizedMessage ?: "ERROR")
}
}
2 -> checkPermissions(activity) {
1 -> checkPermissions(activity) {
FileChooserDialog.show(
activity.supportFragmentManager,
requestCode,
mode = FileChooserDialog.DIRECTORY
)
}
else -> otherFun?.invoke(selectList[index])
}
}
}.show().applyTint()
@ -55,19 +56,19 @@ object FilePicker {
fragment: Fragment,
requestCode: Int,
title: String = fragment.getString(R.string.select_folder),
default: (() -> Unit)? = null
otherActions: List<String>? = null,
otherFun: ((action: String) -> Unit)? = null
) {
fragment.requireContext()
.alert(title = title) {
val selectList =
fragment.resources.getStringArray(R.array.select_folder).toMutableList()
default ?: let {
selectList.removeAt(0)
otherActions?.let {
selectList.addAll(otherActions)
}
items(selectList) { _, index ->
when (if (default == null) index + 1 else index) {
0 -> default?.invoke()
1 -> {
when (index) {
0 -> {
try {
val intent = createSelectDirIntent()
fragment.startActivityForResult(intent, requestCode)
@ -76,13 +77,14 @@ object FilePicker {
fragment.toast(e.localizedMessage ?: "ERROR")
}
}
2 -> checkPermissions(fragment) {
1 -> checkPermissions(fragment) {
FileChooserDialog.show(
fragment.childFragmentManager,
requestCode,
mode = FileChooserDialog.DIRECTORY
)
}
else -> otherFun?.invoke(selectList[index])
}
}
}.show().applyTint()
@ -93,18 +95,18 @@ object FilePicker {
requestCode: Int,
title: String = activity.getString(R.string.select_file),
allowExtensions: Array<String>,
default: (() -> Unit)? = null
otherActions: List<String>? = null,
otherFun: ((action: String) -> Unit)? = null
) {
activity.alert(title = title) {
val selectList =
activity.resources.getStringArray(R.array.select_folder).toMutableList()
default ?: let {
selectList.removeAt(0)
otherActions?.let {
selectList.addAll(otherActions)
}
items(selectList) { _, index ->
when (if (default == null) index + 1 else index) {
0 -> default?.invoke()
1 -> {
when (index) {
0 -> {
try {
val intent = createSelectFileIntent()
intent.putExtra(
@ -117,7 +119,7 @@ object FilePicker {
activity.toast(e.localizedMessage ?: "ERROR")
}
}
2 -> checkPermissions(activity) {
1 -> checkPermissions(activity) {
FileChooserDialog.show(
activity.supportFragmentManager,
requestCode,
@ -125,6 +127,7 @@ object FilePicker {
allowExtensions = allowExtensions
)
}
else -> otherFun?.invoke(selectList[index])
}
}
}.show().applyTint()
@ -135,19 +138,19 @@ object FilePicker {
requestCode: Int,
title: String = fragment.getString(R.string.select_file),
allowExtensions: Array<String>,
default: (() -> Unit)? = null
otherActions: List<String>? = null,
otherFun: ((action: String) -> Unit)? = null
) {
fragment.requireContext()
.alert(title = title) {
val selectList =
fragment.resources.getStringArray(R.array.select_folder).toMutableList()
default ?: let {
selectList.removeAt(0)
otherActions?.let {
selectList.addAll(otherActions)
}
items(selectList) { _, index ->
when (if (default == null) index + 1 else index) {
0 -> default?.invoke()
1 -> {
when (index) {
0 -> {
try {
val intent = createSelectFileIntent()
intent.putExtra(
@ -160,7 +163,7 @@ object FilePicker {
fragment.toast(e.localizedMessage ?: "ERROR")
}
}
2 -> checkPermissions(fragment) {
1 -> checkPermissions(fragment) {
FileChooserDialog.show(
fragment.childFragmentManager,
requestCode,
@ -168,6 +171,7 @@ object FilePicker {
allowExtensions = allowExtensions
)
}
else -> otherFun?.invoke(selectList[index])
}
}
}.show().applyTint()

@ -165,11 +165,7 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
R.id.menu_del_selection -> viewModel.delSelection(adapter.getSelection())
R.id.menu_import_source_onLine -> showImportDialog()
R.id.menu_import_source_local -> FilePicker
.selectFile(
this,
importRequestCode,
allowExtensions = arrayOf("txt", "json")
)
.selectFile(this, importRequestCode, allowExtensions = arrayOf("txt", "json"))
else -> if (item.groupId == R.id.replace_group) {
search_view.setQuery(item.title, true)
}

@ -19,7 +19,6 @@ import kotlinx.android.synthetic.main.activity_rss_read.*
import kotlinx.coroutines.launch
import org.apache.commons.text.StringEscapeUtils
import org.jetbrains.anko.share
import org.jetbrains.anko.toast
import org.jsoup.Jsoup
@ -142,13 +141,18 @@ class ReadRssActivity : VMBaseActivity<ReadRssViewModel>(R.layout.activity_rss_r
}
private fun saveImage() {
FilePicker.selectFolder(this, savePathRequestCode, getString(R.string.save_image)) {
val path = ACache.get(this).getAsString(imagePathKey)
if (path.isNullOrEmpty()) {
toast(R.string.no_default_path)
} else {
viewModel.saveImage(webPic, path)
}
val default = arrayListOf<String>()
val path = ACache.get(this).getAsString(imagePathKey)
if (!path.isNullOrEmpty()) {
default.add(path)
}
FilePicker.selectFolder(
this,
savePathRequestCode,
getString(R.string.save_image),
default
) {
viewModel.saveImage(webPic, it)
}
}

@ -83,11 +83,7 @@ class RssSourceActivity : VMBaseActivity<RssSourceViewModel>(R.layout.activity_r
when (item.itemId) {
R.id.menu_add -> startActivity<RssSourceEditActivity>()
R.id.menu_import_source_local -> FilePicker
.selectFile(
this,
importRequestCode,
allowExtensions = arrayOf("txt", "json")
)
.selectFile(this, importRequestCode, allowExtensions = arrayOf("txt", "json"))
R.id.menu_import_source_onLine -> showImportDialog()
R.id.menu_import_source_qr -> startActivityForResult<QrCodeActivity>(qrRequestCode)
R.id.menu_group_manage -> GroupManageDialog()

@ -106,10 +106,19 @@ class FontSelectDialog : BaseDialogFragment(),
private fun openFolder() {
launch(Main) {
FilePicker.selectFolder(this@FontSelectDialog, fontFolderRequestCode) {
val path = "${FileUtils.getSdCardPath()}${File.separator}Fonts"
putPrefString(PreferKey.fontFolder, path)
loadFontFilesByPermission(path)
val defaultPath = "SD${File.separator}Fonts"
FilePicker.selectFolder(
this@FontSelectDialog,
fontFolderRequestCode,
otherActions = arrayListOf(defaultPath)
) {
when (it) {
defaultPath -> {
val path = "${FileUtils.getSdCardPath()}${File.separator}Fonts"
putPrefString(PreferKey.fontFolder, path)
loadFontFilesByPermission(path)
}
}
}
}
}

@ -48,7 +48,6 @@
</string-array>
<string-array name="select_folder">
<item>@string/default_path</item>
<item>@string/sys_folder_picker</item>
<item>@string/app_folder_picker</item>
</string-array>

@ -48,7 +48,6 @@
</string-array>
<string-array name="select_folder">
<item>@string/default_path</item>
<item>@string/sys_folder_picker</item>
<item>@string/app_folder_picker</item>
</string-array>

@ -48,7 +48,6 @@
</string-array>
<string-array name="select_folder">
<item>@string/default_path</item>
<item>@string/sys_folder_picker</item>
<item>@string/app_folder_picker</item>
</string-array>

Loading…
Cancel
Save