From 4066608c268f78a9587eb00d479f2bf66fbad2ad Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 29 Aug 2021 22:57:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/lib/dialogs/AndroidDialogs.kt | 60 ++++++++++++++ .../app/lib/dialogs/AndroidSelectors.kt | 22 ++++++ .../java/io/legado/app/lib/dialogs/Dialogs.kt | 67 ---------------- .../io/legado/app/lib/dialogs/SelectItem.kt | 17 ++++ .../io/legado/app/lib/dialogs/Selectors.kt | 29 ------- .../legado/app/ui/book/cache/CacheActivity.kt | 7 +- .../ui/book/read/config/BgTextConfigDialog.kt | 3 +- .../app/ui/document/FilePickerDialog.kt | 6 +- .../app/ui/document/HandleFileActivity.kt | 79 +++++++++++-------- .../app/ui/document/HandleFileContract.kt | 8 +- .../legado/app/ui/rss/read/ReadRssActivity.kt | 7 +- .../app/ui/widget/font/FontSelectDialog.kt | 3 +- 12 files changed, 164 insertions(+), 144 deletions(-) delete mode 100644 app/src/main/java/io/legado/app/lib/dialogs/Dialogs.kt create mode 100644 app/src/main/java/io/legado/app/lib/dialogs/SelectItem.kt delete mode 100644 app/src/main/java/io/legado/app/lib/dialogs/Selectors.kt diff --git a/app/src/main/java/io/legado/app/lib/dialogs/AndroidDialogs.kt b/app/src/main/java/io/legado/app/lib/dialogs/AndroidDialogs.kt index 44ebed762..646f6fa1f 100644 --- a/app/src/main/java/io/legado/app/lib/dialogs/AndroidDialogs.kt +++ b/app/src/main/java/io/legado/app/lib/dialogs/AndroidDialogs.kt @@ -123,3 +123,63 @@ private fun Context.progressDialog( if (init != null) init() show() } + +typealias AlertBuilderFactory = (Context) -> AlertBuilder + +inline fun Fragment.alert( + noinline factory: AlertBuilderFactory, + title: String? = null, + message: String? = null, + noinline init: (AlertBuilder.() -> Unit)? = null +) = activity?.alert(factory, title, message, init) + +fun Context.alert( + factory: AlertBuilderFactory, + title: String? = null, + message: String? = null, + init: (AlertBuilder.() -> Unit)? = null +): AlertBuilder { + return factory(this).apply { + if (title != null) { + this.setTitle(title) + } + if (message != null) { + this.setMessage(message) + } + if (init != null) init() + } +} + +inline fun Fragment.alert( + noinline factory: AlertBuilderFactory, + titleResource: Int? = null, + messageResource: Int? = null, + noinline init: (AlertBuilder.() -> Unit)? = null +) = requireActivity().alert(factory, titleResource, messageResource, init) + +fun Context.alert( + factory: AlertBuilderFactory, + titleResource: Int? = null, + messageResource: Int? = null, + init: (AlertBuilder.() -> Unit)? = null +): AlertBuilder { + return factory(this).apply { + if (titleResource != null) { + this.setTitle(titleResource) + } + if (messageResource != null) { + this.setMessage(messageResource) + } + if (init != null) init() + } +} + +inline fun Fragment.alert( + noinline factory: AlertBuilderFactory, + noinline init: AlertBuilder.() -> Unit +) = requireActivity().alert(factory, init) + +fun Context.alert( + factory: AlertBuilderFactory, + init: AlertBuilder.() -> Unit +): AlertBuilder = factory(this).apply { init() } diff --git a/app/src/main/java/io/legado/app/lib/dialogs/AndroidSelectors.kt b/app/src/main/java/io/legado/app/lib/dialogs/AndroidSelectors.kt index 6033879de..8084b9706 100644 --- a/app/src/main/java/io/legado/app/lib/dialogs/AndroidSelectors.kt +++ b/app/src/main/java/io/legado/app/lib/dialogs/AndroidSelectors.kt @@ -55,3 +55,25 @@ fun Context.selector( show() } } + +inline fun Fragment.selector( + noinline factory: AlertBuilderFactory, + title: CharSequence? = null, + items: List, + noinline onClick: (DialogInterface, CharSequence, Int) -> Unit +) = requireActivity().selector(factory, title, items, onClick) + +fun Context.selector( + factory: AlertBuilderFactory, + title: CharSequence? = null, + items: List, + onClick: (DialogInterface, CharSequence, Int) -> Unit +) { + with(factory(this)) { + if (title != null) { + this.setTitle(title) + } + items(items, onClick) + show() + } +} diff --git a/app/src/main/java/io/legado/app/lib/dialogs/Dialogs.kt b/app/src/main/java/io/legado/app/lib/dialogs/Dialogs.kt deleted file mode 100644 index 0abcc36c0..000000000 --- a/app/src/main/java/io/legado/app/lib/dialogs/Dialogs.kt +++ /dev/null @@ -1,67 +0,0 @@ -@file:Suppress("NOTHING_TO_INLINE", "unused") - -package io.legado.app.lib.dialogs - -import android.content.Context -import android.content.DialogInterface -import androidx.fragment.app.Fragment - -typealias AlertBuilderFactory = (Context) -> AlertBuilder - -inline fun Fragment.alert( - noinline factory: AlertBuilderFactory, - title: String? = null, - message: String? = null, - noinline init: (AlertBuilder.() -> Unit)? = null -) = activity?.alert(factory, title, message, init) - -fun Context.alert( - factory: AlertBuilderFactory, - title: String? = null, - message: String? = null, - init: (AlertBuilder.() -> Unit)? = null -): AlertBuilder { - return factory(this).apply { - if (title != null) { - this.setTitle(title) - } - if (message != null) { - this.setMessage(message) - } - if (init != null) init() - } -} - -inline fun Fragment.alert( - noinline factory: AlertBuilderFactory, - titleResource: Int? = null, - messageResource: Int? = null, - noinline init: (AlertBuilder.() -> Unit)? = null -) = requireActivity().alert(factory, titleResource, messageResource, init) - -fun Context.alert( - factory: AlertBuilderFactory, - titleResource: Int? = null, - messageResource: Int? = null, - init: (AlertBuilder.() -> Unit)? = null -): AlertBuilder { - return factory(this).apply { - if (titleResource != null) { - this.setTitle(titleResource) - } - if (messageResource != null) { - this.setMessage(messageResource) - } - if (init != null) init() - } -} - -inline fun Fragment.alert( - noinline factory: AlertBuilderFactory, - noinline init: AlertBuilder.() -> Unit -) = requireActivity().alert(factory, init) - -fun Context.alert( - factory: AlertBuilderFactory, - init: AlertBuilder.() -> Unit -): AlertBuilder = factory(this).apply { init() } diff --git a/app/src/main/java/io/legado/app/lib/dialogs/SelectItem.kt b/app/src/main/java/io/legado/app/lib/dialogs/SelectItem.kt new file mode 100644 index 000000000..dcee51afa --- /dev/null +++ b/app/src/main/java/io/legado/app/lib/dialogs/SelectItem.kt @@ -0,0 +1,17 @@ +package io.legado.app.lib.dialogs + +import android.os.Parcelable +import kotlinx.parcelize.Parcelize + +@Suppress("unused") +@Parcelize +data class SelectItem( + val title: String, + val id: Int +) : Parcelable { + + override fun toString(): String { + return title + } + +} diff --git a/app/src/main/java/io/legado/app/lib/dialogs/Selectors.kt b/app/src/main/java/io/legado/app/lib/dialogs/Selectors.kt deleted file mode 100644 index c74535955..000000000 --- a/app/src/main/java/io/legado/app/lib/dialogs/Selectors.kt +++ /dev/null @@ -1,29 +0,0 @@ -@file:Suppress("NOTHING_TO_INLINE", "unused") - -package io.legado.app.lib.dialogs - -import android.content.Context -import android.content.DialogInterface -import androidx.fragment.app.Fragment - -inline fun Fragment.selector( - noinline factory: AlertBuilderFactory, - title: CharSequence? = null, - items: List, - noinline onClick: (DialogInterface, CharSequence, Int) -> Unit -) = requireActivity().selector(factory, title, items, onClick) - -fun Context.selector( - factory: AlertBuilderFactory, - title: CharSequence? = null, - items: List, - onClick: (DialogInterface, CharSequence, Int) -> Unit -) { - with(factory(this)) { - if (title != null) { - this.setTitle(title) - } - items(items, onClick) - show() - } -} diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt index 45ccd2d72..b0e27b01e 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt @@ -21,6 +21,7 @@ import io.legado.app.databinding.ActivityCacheBookBinding import io.legado.app.databinding.DialogEditTextBinding import io.legado.app.help.AppConfig import io.legado.app.help.BookHelp +import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.selector import io.legado.app.service.help.CacheBook @@ -248,13 +249,13 @@ class CacheActivity : VMBaseActivity() } private fun selectExportFolder() { - val default = arrayListOf() + val default = arrayListOf() val path = ACache.get(this@CacheActivity).getAsString(exportBookPathKey) if (!path.isNullOrEmpty()) { - default.add(path) + default.add(SelectItem(path, -1)) } exportDir.launch { - otherActions = default.toTypedArray() + otherActions = default } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index ce80dbaad..d08cee1aa 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt @@ -18,6 +18,7 @@ import io.legado.app.help.DefaultData import io.legado.app.help.ReadBookConfig import io.legado.app.help.http.newCall import io.legado.app.help.http.okHttpClient +import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.selector import io.legado.app.lib.theme.bottomBackground @@ -191,7 +192,7 @@ class BgTextConfigDialog : BaseDialogFragment() { mode = HandleFileContract.FILE title = getString(R.string.import_str) allowExtensions = arrayOf("zip") - otherActions = arrayOf(importFormNet) + otherActions = arrayListOf(SelectItem(importFormNet, -1)) } } binding.ivExport.setOnClickListener { diff --git a/app/src/main/java/io/legado/app/ui/document/FilePickerDialog.kt b/app/src/main/java/io/legado/app/ui/document/FilePickerDialog.kt index e549295ae..1bf1aac41 100644 --- a/app/src/main/java/io/legado/app/ui/document/FilePickerDialog.kt +++ b/app/src/main/java/io/legado/app/ui/document/FilePickerDialog.kt @@ -16,7 +16,7 @@ import androidx.recyclerview.widget.RecyclerView import io.legado.app.R import io.legado.app.databinding.DialogFileChooserBinding import io.legado.app.lib.theme.primaryColor -import io.legado.app.ui.document.HandleFileContract.Companion.DIRECTORY +import io.legado.app.ui.document.HandleFileContract.Companion.DIR import io.legado.app.ui.document.HandleFileContract.Companion.FILE import io.legado.app.ui.document.adapter.FileAdapter import io.legado.app.ui.document.adapter.PathAdapter @@ -63,7 +63,7 @@ class FilePickerDialog : DialogFragment(), private val binding by viewBinding(DialogFileChooserBinding::bind) override var allowExtensions: Array? = null override val isSelectDir: Boolean - get() = mode == DIRECTORY + get() = mode == DIR override var isShowHomeDir: Boolean = false override var isShowUpDir: Boolean = true override var isShowHideDir: Boolean = false @@ -159,7 +159,7 @@ class FilePickerDialog : DialogFragment(), refreshCurrentDirPath(fileItem.path) } else { fileItem?.path?.let { path -> - if (mode == DIRECTORY) { + if (mode == DIR) { toastOnUi("这是文件夹选择,不能选择文件,点击右上角的确定选择文件夹") } else if (allowExtensions.isNullOrEmpty() || allowExtensions?.contains(FileUtils.getExtension(path)) == true diff --git a/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt b/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt index cfa6b1354..3785d094d 100644 --- a/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt +++ b/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt @@ -10,6 +10,7 @@ import io.legado.app.R import io.legado.app.base.BaseActivity import io.legado.app.constant.Theme import io.legado.app.databinding.ActivityTranslucenceBinding +import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.dialogs.alert import io.legado.app.lib.permission.Permissions import io.legado.app.lib.permission.PermissionsCompat @@ -46,50 +47,60 @@ class HandleFileActivity : override fun onActivityCreated(savedInstanceState: Bundle?) { val mode = intent.getIntExtra("mode", 0) val allowExtensions = intent.getStringArrayExtra("allowExtensions") - val selectList = if (mode == HandleFileContract.DIRECTORY) { - arrayListOf(getString(R.string.sys_folder_picker)) - } else { - arrayListOf(getString(R.string.sys_file_picker)) - } - if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { - selectList.add(getString(R.string.app_folder_picker)) + val selectList = when (mode) { + HandleFileContract.DIR -> arrayListOf( + SelectItem(getString(R.string.sys_folder_picker), HandleFileContract.DIR) + ).apply { + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { + add(SelectItem(getString(R.string.app_folder_picker), 10)) + } + } + HandleFileContract.FILE -> arrayListOf( + SelectItem(getString(R.string.sys_file_picker), HandleFileContract.FILE) + ).apply { + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { + add(SelectItem(getString(R.string.app_folder_picker), 11)) + } + } + HandleFileContract.EXPORT -> arrayListOf( + SelectItem(getString(R.string.upload_url), 111), + SelectItem(getString(R.string.sys_folder_picker), HandleFileContract.DIR) + ) + else -> arrayListOf() } - intent.getStringArrayListExtra("otherActions")?.let { + intent.getParcelableArrayListExtra("otherActions")?.let { selectList.addAll(it) } val title = intent.getStringExtra("title") ?: let { - if (mode == HandleFileContract.DIRECTORY) { - return@let getString(R.string.select_folder) - } else { - return@let getString(R.string.select_file) + when (mode) { + HandleFileContract.DIR -> { + return@let getString(R.string.select_folder) + } + else -> { + return@let getString(R.string.select_file) + } } } alert(title) { - items(selectList) { _, index -> - when (index) { - 0 -> if (mode == HandleFileContract.DIRECTORY) { - selectDocTree.launch(null) - } else { - selectDoc.launch(typesOfExtensions(allowExtensions)) + items(selectList) { _, item, _ -> + when (item.id) { + HandleFileContract.DIR -> selectDocTree.launch(null) + HandleFileContract.FILE -> selectDoc.launch(typesOfExtensions(allowExtensions)) + 10 -> checkPermissions { + FilePickerDialog.show( + supportFragmentManager, + mode = HandleFileContract.DIR + ) } - 1 -> if (mode == HandleFileContract.DIRECTORY) { - checkPermissions { - FilePickerDialog.show( - supportFragmentManager, - mode = HandleFileContract.DIRECTORY - ) - } - } else { - checkPermissions { - FilePickerDialog.show( - supportFragmentManager, - mode = HandleFileContract.FILE, - allowExtensions = allowExtensions - ) - } + 11 -> checkPermissions { + FilePickerDialog.show( + supportFragmentManager, + mode = HandleFileContract.FILE, + allowExtensions = allowExtensions + ) } else -> { - val path = selectList[index] + val path = item.title val uri = if (path.isContentScheme()) { Uri.fromFile(File(path)) } else { diff --git a/app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt b/app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt index 1571ec882..54d5588da 100644 --- a/app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt +++ b/app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt @@ -5,14 +5,16 @@ import android.content.Context import android.content.Intent import android.net.Uri import androidx.activity.result.contract.ActivityResultContract +import io.legado.app.lib.dialogs.SelectItem @Suppress("unused") class HandleFileContract : ActivityResultContract Unit, Uri?>() { companion object { - const val DIRECTORY = 0 + const val DIR = 0 const val FILE = 1 + const val EXPORT = 3 } override fun createIntent(context: Context, input: (HandleFileParam.() -> Unit)?): Intent { @@ -39,10 +41,10 @@ class HandleFileContract : @Suppress("ArrayInDataClass") data class HandleFileParam( - var mode: Int = DIRECTORY, + var mode: Int = DIR, var title: String? = null, var allowExtensions: Array = arrayOf(), - var otherActions: Array? = null, + var otherActions: ArrayList? = null, ) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt index 9ac7aba98..c189bc585 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt @@ -18,6 +18,7 @@ import io.legado.app.base.VMBaseActivity import io.legado.app.constant.AppConst import io.legado.app.databinding.ActivityRssReadBinding import io.legado.app.help.AppConfig +import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.theme.DrawableUtils import io.legado.app.lib.theme.primaryTextColor import io.legado.app.service.help.Download @@ -164,13 +165,13 @@ class ReadRssActivity : VMBaseActivity } private fun selectSaveFolder() { - val default = arrayListOf() + val default = arrayListOf() val path = ACache.get(this@ReadRssActivity).getAsString(imagePathKey) if (!path.isNullOrEmpty()) { - default.add(path) + default.add(SelectItem(path, -1)) } saveImage.launch { - otherActions = default.toTypedArray() + otherActions = default } } diff --git a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt index 002aa5388..cdc5f1f9d 100644 --- a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt +++ b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt @@ -15,6 +15,7 @@ import io.legado.app.base.BaseDialogFragment import io.legado.app.constant.PreferKey import io.legado.app.databinding.DialogFontSelectBinding import io.legado.app.help.AppConfig +import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.dialogs.alert import io.legado.app.lib.permission.Permissions import io.legado.app.lib.permission.PermissionsCompat @@ -128,7 +129,7 @@ class FontSelectDialog : BaseDialogFragment(), launch(Main) { val defaultPath = "SD${File.separator}Fonts" selectFontDir.launch { - otherActions = arrayOf(defaultPath) + otherActions = arrayListOf(SelectItem(defaultPath, -1)) } } }