pull/1284/head
gedoor 3 years ago
parent afd1a82259
commit 4066608c26
  1. 60
      app/src/main/java/io/legado/app/lib/dialogs/AndroidDialogs.kt
  2. 22
      app/src/main/java/io/legado/app/lib/dialogs/AndroidSelectors.kt
  3. 67
      app/src/main/java/io/legado/app/lib/dialogs/Dialogs.kt
  4. 17
      app/src/main/java/io/legado/app/lib/dialogs/SelectItem.kt
  5. 29
      app/src/main/java/io/legado/app/lib/dialogs/Selectors.kt
  6. 7
      app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt
  7. 3
      app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt
  8. 6
      app/src/main/java/io/legado/app/ui/document/FilePickerDialog.kt
  9. 79
      app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt
  10. 8
      app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt
  11. 7
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt
  12. 3
      app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt

@ -123,3 +123,63 @@ private fun Context.progressDialog(
if (init != null) init()
show()
}
typealias AlertBuilderFactory<D> = (Context) -> AlertBuilder<D>
inline fun <D : DialogInterface> Fragment.alert(
noinline factory: AlertBuilderFactory<D>,
title: String? = null,
message: String? = null,
noinline init: (AlertBuilder<D>.() -> Unit)? = null
) = activity?.alert(factory, title, message, init)
fun <D : DialogInterface> Context.alert(
factory: AlertBuilderFactory<D>,
title: String? = null,
message: String? = null,
init: (AlertBuilder<D>.() -> Unit)? = null
): AlertBuilder<D> {
return factory(this).apply {
if (title != null) {
this.setTitle(title)
}
if (message != null) {
this.setMessage(message)
}
if (init != null) init()
}
}
inline fun <D : DialogInterface> Fragment.alert(
noinline factory: AlertBuilderFactory<D>,
titleResource: Int? = null,
messageResource: Int? = null,
noinline init: (AlertBuilder<D>.() -> Unit)? = null
) = requireActivity().alert(factory, titleResource, messageResource, init)
fun <D : DialogInterface> Context.alert(
factory: AlertBuilderFactory<D>,
titleResource: Int? = null,
messageResource: Int? = null,
init: (AlertBuilder<D>.() -> Unit)? = null
): AlertBuilder<D> {
return factory(this).apply {
if (titleResource != null) {
this.setTitle(titleResource)
}
if (messageResource != null) {
this.setMessage(messageResource)
}
if (init != null) init()
}
}
inline fun <D : DialogInterface> Fragment.alert(
noinline factory: AlertBuilderFactory<D>,
noinline init: AlertBuilder<D>.() -> Unit
) = requireActivity().alert(factory, init)
fun <D : DialogInterface> Context.alert(
factory: AlertBuilderFactory<D>,
init: AlertBuilder<D>.() -> Unit
): AlertBuilder<D> = factory(this).apply { init() }

@ -55,3 +55,25 @@ fun Context.selector(
show()
}
}
inline fun <D : DialogInterface> Fragment.selector(
noinline factory: AlertBuilderFactory<D>,
title: CharSequence? = null,
items: List<CharSequence>,
noinline onClick: (DialogInterface, CharSequence, Int) -> Unit
) = requireActivity().selector(factory, title, items, onClick)
fun <D : DialogInterface> Context.selector(
factory: AlertBuilderFactory<D>,
title: CharSequence? = null,
items: List<CharSequence>,
onClick: (DialogInterface, CharSequence, Int) -> Unit
) {
with(factory(this)) {
if (title != null) {
this.setTitle(title)
}
items(items, onClick)
show()
}
}

@ -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<D> = (Context) -> AlertBuilder<D>
inline fun <D : DialogInterface> Fragment.alert(
noinline factory: AlertBuilderFactory<D>,
title: String? = null,
message: String? = null,
noinline init: (AlertBuilder<D>.() -> Unit)? = null
) = activity?.alert(factory, title, message, init)
fun <D : DialogInterface> Context.alert(
factory: AlertBuilderFactory<D>,
title: String? = null,
message: String? = null,
init: (AlertBuilder<D>.() -> Unit)? = null
): AlertBuilder<D> {
return factory(this).apply {
if (title != null) {
this.setTitle(title)
}
if (message != null) {
this.setMessage(message)
}
if (init != null) init()
}
}
inline fun <D : DialogInterface> Fragment.alert(
noinline factory: AlertBuilderFactory<D>,
titleResource: Int? = null,
messageResource: Int? = null,
noinline init: (AlertBuilder<D>.() -> Unit)? = null
) = requireActivity().alert(factory, titleResource, messageResource, init)
fun <D : DialogInterface> Context.alert(
factory: AlertBuilderFactory<D>,
titleResource: Int? = null,
messageResource: Int? = null,
init: (AlertBuilder<D>.() -> Unit)? = null
): AlertBuilder<D> {
return factory(this).apply {
if (titleResource != null) {
this.setTitle(titleResource)
}
if (messageResource != null) {
this.setMessage(messageResource)
}
if (init != null) init()
}
}
inline fun <D : DialogInterface> Fragment.alert(
noinline factory: AlertBuilderFactory<D>,
noinline init: AlertBuilder<D>.() -> Unit
) = requireActivity().alert(factory, init)
fun <D : DialogInterface> Context.alert(
factory: AlertBuilderFactory<D>,
init: AlertBuilder<D>.() -> Unit
): AlertBuilder<D> = factory(this).apply { init() }

@ -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
}
}

@ -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 <D : DialogInterface> Fragment.selector(
noinline factory: AlertBuilderFactory<D>,
title: CharSequence? = null,
items: List<CharSequence>,
noinline onClick: (DialogInterface, CharSequence, Int) -> Unit
) = requireActivity().selector(factory, title, items, onClick)
fun <D : DialogInterface> Context.selector(
factory: AlertBuilderFactory<D>,
title: CharSequence? = null,
items: List<CharSequence>,
onClick: (DialogInterface, CharSequence, Int) -> Unit
) {
with(factory(this)) {
if (title != null) {
this.setTitle(title)
}
items(items, onClick)
show()
}
}

@ -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<ActivityCacheBookBinding, CacheViewModel>()
}
private fun selectExportFolder() {
val default = arrayListOf<String>()
val default = arrayListOf<SelectItem>()
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
}
}

@ -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 {

@ -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<String>? = 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

@ -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<SelectItem>("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 {

@ -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<HandleFileContract.HandleFileParam.() -> 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<String> = arrayOf(),
var otherActions: Array<String>? = null,
var otherActions: ArrayList<SelectItem>? = null,
)
}

@ -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<ActivityRssReadBinding, ReadRssViewModel>
}
private fun selectSaveFolder() {
val default = arrayListOf<String>()
val default = arrayListOf<SelectItem>()
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
}
}

@ -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))
}
}
}

Loading…
Cancel
Save