pull/1327/head
gedoor 3 years ago
parent eab7528e70
commit 919bce4430
  1. 10
      app/src/main/java/io/legado/app/lib/dialogs/SelectItem.kt
  2. 2
      app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt
  3. 22
      app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt
  4. 3
      app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt
  5. 15
      app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt
  6. 5
      app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt
  7. 2
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt
  8. 21
      app/src/main/java/io/legado/app/utils/IntentExtensions.kt

@ -1,14 +1,10 @@
package io.legado.app.lib.dialogs package io.legado.app.lib.dialogs
import android.os.Parcelable
import kotlinx.parcelize.Parcelize
@Suppress("unused") @Suppress("unused")
@Parcelize data class SelectItem<T>(
data class SelectItem(
val title: String, val title: String,
val id: Int val value: T
) : Parcelable { ) {
override fun toString(): String { override fun toString(): String {
return title return title

@ -244,7 +244,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
} }
private fun selectExportFolder() { private fun selectExportFolder() {
val default = arrayListOf<SelectItem>() val default = arrayListOf<SelectItem<Int>>()
val path = ACache.get(this@CacheActivity).getAsString(exportBookPathKey) val path = ACache.get(this@CacheActivity).getAsString(exportBookPathKey)
if (!path.isNullOrEmpty()) { if (!path.isNullOrEmpty()) {
default.add(SelectItem(path, -1)) default.add(SelectItem(path, -1))

@ -22,6 +22,7 @@ import io.legado.app.databinding.DialogHttpTtsEditBinding
import io.legado.app.databinding.DialogRecyclerViewBinding import io.legado.app.databinding.DialogRecyclerViewBinding
import io.legado.app.databinding.ItemHttpTtsBinding import io.legado.app.databinding.ItemHttpTtsBinding
import io.legado.app.help.DirectLinkUpload import io.legado.app.help.DirectLinkUpload
import io.legado.app.lib.dialogs.SelectItem
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.primaryColor import io.legado.app.lib.theme.primaryColor
@ -95,8 +96,7 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener
tvFooterLeft.setText(R.string.system_tts) tvFooterLeft.setText(R.string.system_tts)
tvFooterLeft.visible() tvFooterLeft.visible()
tvFooterLeft.setOnClickListener { tvFooterLeft.setOnClickListener {
removePref(PreferKey.speakEngine) selectSysTts()
dismissAllowingStateLoss()
} }
tvOk.visible() tvOk.visible()
tvOk.setOnClickListener { tvOk.setOnClickListener {
@ -144,6 +144,14 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener
return true return true
} }
private fun selectSysTts() {
val ttsItems = viewModel.tts.engines.map {
SelectItem(it.label, 0)
}
removePref(PreferKey.speakEngine)
dismissAllowingStateLoss()
}
private fun importAlert() { private fun importAlert() {
val aCache = ACache.get(requireContext(), cacheDir = false) val aCache = ACache.get(requireContext(), cacheDir = false)
val cacheUrls: MutableList<String> = aCache val cacheUrls: MutableList<String> = aCache
@ -218,18 +226,18 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener
} }
override fun registerListener(holder: ItemViewHolder, binding: ItemHttpTtsBinding) { override fun registerListener(holder: ItemViewHolder, binding: ItemHttpTtsBinding) {
binding.apply { binding.run {
cbName.setOnClickListener { cbName.setOnClickListener {
getItem(holder.layoutPosition)?.let { httpTTS -> getItemByLayoutPosition(holder.layoutPosition)?.let { httpTTS ->
engineId = httpTTS.id engineId = httpTTS.id
notifyItemRangeChanged(0, itemCount) notifyItemRangeChanged(getHeaderCount(), itemCount)
} }
} }
ivEdit.setOnClickListener { ivEdit.setOnClickListener {
editHttpTTS(getItem(holder.layoutPosition)) editHttpTTS(getItemByLayoutPosition(holder.layoutPosition))
} }
ivMenuDelete.setOnClickListener { ivMenuDelete.setOnClickListener {
getItem(holder.layoutPosition)?.let { httpTTS -> getItemByLayoutPosition(holder.layoutPosition)?.let { httpTTS ->
appDb.httpTTSDao.delete(httpTTS) appDb.httpTTSDao.delete(httpTTS)
} }
} }

@ -2,6 +2,7 @@ package io.legado.app.ui.book.read.config
import android.app.Application import android.app.Application
import android.net.Uri import android.net.Uri
import android.speech.tts.TextToSpeech
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.HttpTTS import io.legado.app.data.entities.HttpTTS
@ -13,6 +14,8 @@ import io.legado.app.utils.*
class SpeakEngineViewModel(application: Application) : BaseViewModel(application) { class SpeakEngineViewModel(application: Application) : BaseViewModel(application) {
val tts = TextToSpeech(context, null)
fun importDefault() { fun importDefault() {
execute { execute {
DefaultData.importDefaultHttpTTS() DefaultData.importDefaultHttpTTS()

@ -16,6 +16,7 @@ import io.legado.app.lib.dialogs.SelectItem
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.permission.Permissions import io.legado.app.lib.permission.Permissions
import io.legado.app.lib.permission.PermissionsCompat import io.legado.app.lib.permission.PermissionsCompat
import io.legado.app.utils.getJsonArray
import io.legado.app.utils.isContentScheme import io.legado.app.utils.isContentScheme
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
@ -56,7 +57,7 @@ class HandleFileActivity :
finish() finish()
} }
val allowExtensions = intent.getStringArrayExtra("allowExtensions") val allowExtensions = intent.getStringArrayExtra("allowExtensions")
val selectList = when (mode) { val selectList: ArrayList<SelectItem<Int>> = when (mode) {
HandleFileContract.DIR -> getDirActions() HandleFileContract.DIR -> getDirActions()
HandleFileContract.FILE -> getFileActions() HandleFileContract.FILE -> getFileActions()
HandleFileContract.EXPORT -> arrayListOf( HandleFileContract.EXPORT -> arrayListOf(
@ -66,7 +67,7 @@ class HandleFileActivity :
} }
else -> arrayListOf() else -> arrayListOf()
} }
intent.getParcelableArrayListExtra<SelectItem>("otherActions")?.let { intent.getJsonArray<SelectItem<Int>>("otherActions")?.let {
selectList.addAll(it) selectList.addAll(it)
} }
val title = intent.getStringExtra("title") ?: let { val title = intent.getStringExtra("title") ?: let {
@ -78,7 +79,7 @@ class HandleFileActivity :
} }
alert(title) { alert(title) {
items(selectList) { _, item, _ -> items(selectList) { _, item, _ ->
when (item.id) { when (item.value) {
HandleFileContract.DIR -> selectDocTree.launch(null) HandleFileContract.DIR -> selectDocTree.launch(null)
HandleFileContract.FILE -> selectDoc.launch(typesOfExtensions(allowExtensions)) HandleFileContract.FILE -> selectDoc.launch(typesOfExtensions(allowExtensions))
10 -> checkPermissions { 10 -> checkPermissions {
@ -104,9 +105,9 @@ class HandleFileActivity :
else -> { else -> {
val path = item.title val path = item.title
val uri = if (path.isContentScheme()) { val uri = if (path.isContentScheme()) {
Uri.fromFile(File(path))
} else {
Uri.parse(path) Uri.parse(path)
} else {
Uri.fromFile(File(path))
} }
onResult(Intent().setData(uri)) onResult(Intent().setData(uri))
} }
@ -130,7 +131,7 @@ class HandleFileActivity :
return null return null
} }
private fun getDirActions(): ArrayList<SelectItem> { private fun getDirActions(): ArrayList<SelectItem<Int>> {
return if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { return if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) {
arrayListOf( arrayListOf(
SelectItem(getString(R.string.sys_folder_picker), HandleFileContract.DIR), SelectItem(getString(R.string.sys_folder_picker), HandleFileContract.DIR),
@ -141,7 +142,7 @@ class HandleFileActivity :
} }
} }
private fun getFileActions(): ArrayList<SelectItem> { private fun getFileActions(): ArrayList<SelectItem<Int>> {
return if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { return if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) {
arrayListOf( arrayListOf(
SelectItem(getString(R.string.sys_file_picker), HandleFileContract.FILE), SelectItem(getString(R.string.sys_file_picker), HandleFileContract.FILE),

@ -7,6 +7,7 @@ import android.net.Uri
import androidx.activity.result.contract.ActivityResultContract import androidx.activity.result.contract.ActivityResultContract
import io.legado.app.help.IntentDataHelp import io.legado.app.help.IntentDataHelp
import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.dialogs.SelectItem
import io.legado.app.utils.putJson
@Suppress("unused") @Suppress("unused")
class HandleFileContract : class HandleFileContract :
@ -28,7 +29,7 @@ class HandleFileContract :
intent.putExtra("mode", it.mode) intent.putExtra("mode", it.mode)
intent.putExtra("title", it.title) intent.putExtra("title", it.title)
intent.putExtra("allowExtensions", it.allowExtensions) intent.putExtra("allowExtensions", it.allowExtensions)
intent.putExtra("otherActions", it.otherActions) intent.putJson("otherActions", it.otherActions)
it.fileData?.let { fileData -> it.fileData?.let { fileData ->
intent.putExtra("fileName", fileData.first) intent.putExtra("fileName", fileData.first)
intent.putExtra("fileKey", IntentDataHelp.putData(fileData.second)) intent.putExtra("fileKey", IntentDataHelp.putData(fileData.second))
@ -50,7 +51,7 @@ class HandleFileContract :
var mode: Int = DIR, var mode: Int = DIR,
var title: String? = null, var title: String? = null,
var allowExtensions: Array<String> = arrayOf(), var allowExtensions: Array<String> = arrayOf(),
var otherActions: ArrayList<SelectItem>? = null, var otherActions: ArrayList<SelectItem<Int>>? = null,
var fileData: Triple<String, ByteArray, String>? = null var fileData: Triple<String, ByteArray, String>? = null
) )

@ -174,7 +174,7 @@ class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>
} }
private fun selectSaveFolder() { private fun selectSaveFolder() {
val default = arrayListOf<SelectItem>() val default = arrayListOf<SelectItem<Int>>()
val path = ACache.get(this@ReadRssActivity).getAsString(imagePathKey) val path = ACache.get(this@ReadRssActivity).getAsString(imagePathKey)
if (!path.isNullOrEmpty()) { if (!path.isNullOrEmpty()) {
default.add(SelectItem(path, -1)) default.add(SelectItem(path, -1))

@ -0,0 +1,21 @@
@file:Suppress("unused")
package io.legado.app.utils
import android.content.Intent
fun Intent.putJson(key: String, any: Any?) {
any?.let {
putExtra(key, GSON.toJson(any))
}
}
inline fun <reified T> Intent.getJsonObject(key: String): T? {
val value = getStringExtra(key)
return GSON.fromJsonObject<T>(value)
}
inline fun <reified T> Intent.getJsonArray(key: String): List<T>? {
val value = getStringExtra(key)
return GSON.fromJsonArray(value)
}
Loading…
Cancel
Save