优化文件选择

pull/363/head
gedoor 4 years ago
parent ac8ea32a06
commit cda68d51b3
  1. 11
      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. 18
      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. 11
      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.Dispatchers.IO
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.jetbrains.anko.toast
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CopyOnWriteArraySet import java.util.concurrent.CopyOnWriteArraySet
@ -191,13 +190,13 @@ class DownloadActivity : VMBaseActivity<DownloadViewModel>(R.layout.activity_dow
override fun export(position: Int) { override fun export(position: Int) {
exportPosition = position exportPosition = position
FilePicker.selectFolder(this, exportRequestCode) { val default = arrayListOf<String>()
val path = ACache.get(this@DownloadActivity).getAsString(exportBookPathKey) val path = ACache.get(this@DownloadActivity).getAsString(exportBookPathKey)
if (path.isNullOrEmpty()) { if (!path.isNullOrEmpty()) {
toast(R.string.no_default_path) default.add(path)
} else {
startExport(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 -> R.id.menu_group_manage ->
GroupManageDialog().show(supportFragmentManager, "groupManage") GroupManageDialog().show(supportFragmentManager, "groupManage")
R.id.menu_import_source_local -> FilePicker R.id.menu_import_source_local -> FilePicker
.selectFile( .selectFile(this, importRequestCode, allowExtensions = arrayOf("txt", "json"))
this,
importRequestCode,
allowExtensions = arrayOf("txt", "json")
)
R.id.menu_import_source_onLine -> showImportDialog() R.id.menu_import_source_onLine -> showImportDialog()
R.id.menu_sort_manual -> { R.id.menu_sort_manual -> {
item.isChecked = true item.isChecked = true

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

@ -19,7 +19,6 @@ import kotlinx.android.synthetic.main.activity_rss_read.*
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.apache.commons.text.StringEscapeUtils import org.apache.commons.text.StringEscapeUtils
import org.jetbrains.anko.share import org.jetbrains.anko.share
import org.jetbrains.anko.toast
import org.jsoup.Jsoup import org.jsoup.Jsoup
@ -142,13 +141,18 @@ class ReadRssActivity : VMBaseActivity<ReadRssViewModel>(R.layout.activity_rss_r
} }
private fun saveImage() { private fun saveImage() {
FilePicker.selectFolder(this, savePathRequestCode, getString(R.string.save_image)) { val default = arrayListOf<String>()
val path = ACache.get(this).getAsString(imagePathKey) val path = ACache.get(this).getAsString(imagePathKey)
if (path.isNullOrEmpty()) { if (!path.isNullOrEmpty()) {
toast(R.string.no_default_path) default.add(path)
} else { }
viewModel.saveImage(webPic, 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) { when (item.itemId) {
R.id.menu_add -> startActivity<RssSourceEditActivity>() R.id.menu_add -> startActivity<RssSourceEditActivity>()
R.id.menu_import_source_local -> FilePicker R.id.menu_import_source_local -> FilePicker
.selectFile( .selectFile(this, importRequestCode, allowExtensions = arrayOf("txt", "json"))
this,
importRequestCode,
allowExtensions = arrayOf("txt", "json")
)
R.id.menu_import_source_onLine -> showImportDialog() R.id.menu_import_source_onLine -> showImportDialog()
R.id.menu_import_source_qr -> startActivityForResult<QrCodeActivity>(qrRequestCode) R.id.menu_import_source_qr -> startActivityForResult<QrCodeActivity>(qrRequestCode)
R.id.menu_group_manage -> GroupManageDialog() R.id.menu_group_manage -> GroupManageDialog()

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

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

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

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

Loading…
Cancel
Save