diff --git a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt index 0516531a2..eda18ee5c 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -8,6 +8,7 @@ import io.legado.app.constant.AppPattern import io.legado.app.constant.EventBus import io.legado.app.help.AppConfig import io.legado.app.help.coroutine.Coroutine +import io.legado.app.model.ConcurrentException import io.legado.app.model.NoStackTraceException import io.legado.app.model.ReadAloud import io.legado.app.model.ReadBook @@ -163,13 +164,15 @@ class HttpReadAloudService : BaseReadAloudService(), } catch (e: CancellationException) { removeSpeakCache(fileName) //任务取消,不处理 + } catch (e: ConcurrentException) { + removeSpeakCache(fileName) + downloadAudio() } catch (e: SocketTimeoutException) { removeSpeakCache(fileName) downloadErrorNo++ if (playErrorNo > 5) { createSilentSound(fileName) } else { - toastOnUi("tts接口超时,尝试重新获取") downloadAudio() } } catch (e: ConnectException) { diff --git a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt index 3e2985c9e..7d90fff72 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -17,7 +17,6 @@ import android.os.BatteryManager import android.os.Build import android.os.Process import android.provider.Settings -import android.widget.Toast import androidx.annotation.ColorRes import androidx.annotation.DrawableRes import androidx.core.content.ContextCompat @@ -79,30 +78,6 @@ inline fun Context.broadcastPendingIntent( return getBroadcast(this, 0, intent, FLAG_CANCEL_CURRENT) } -fun Context.toastOnUi(message: Int) { - runOnUI { - Toast.makeText(this, message, Toast.LENGTH_SHORT).show() - } -} - -fun Context.toastOnUi(message: CharSequence?) { - runOnUI { - Toast.makeText(this, message, Toast.LENGTH_SHORT).show() - } -} - -fun Context.longToastOnUi(message: Int) { - runOnUI { - Toast.makeText(this, message, Toast.LENGTH_LONG).show() - } -} - -fun Context.longToastOnUi(message: CharSequence?) { - runOnUI { - Toast.makeText(this, message, Toast.LENGTH_LONG).show() - } -} - val Context.defaultSharedPreferences: SharedPreferences get() = PreferenceManager.getDefaultSharedPreferences(this) diff --git a/app/src/main/java/io/legado/app/utils/ToastUtils.kt b/app/src/main/java/io/legado/app/utils/ToastUtils.kt new file mode 100644 index 000000000..7a5f2cc83 --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/ToastUtils.kt @@ -0,0 +1,66 @@ +@file:Suppress("unused") + +package io.legado.app.utils + +import android.content.Context +import android.widget.Toast +import androidx.fragment.app.Fragment + +private var toast: Toast? = null + +fun Context.toastOnUi(message: Int) { + runOnUI { + if (toast == null) { + toast = Toast.makeText(this, message, Toast.LENGTH_SHORT) + } else { + toast?.setText(message) + toast?.duration = Toast.LENGTH_SHORT + } + toast?.show() + } +} + +fun Context.toastOnUi(message: CharSequence?) { + runOnUI { + if (toast == null) { + toast = Toast.makeText(this, message, Toast.LENGTH_SHORT) + } else { + toast?.setText(message) + toast?.duration = Toast.LENGTH_SHORT + } + toast?.show() + } +} + +fun Context.longToastOnUi(message: Int) { + runOnUI { + if (toast == null) { + toast = Toast.makeText(this, message, Toast.LENGTH_LONG) + } else { + toast?.setText(message) + toast?.duration = Toast.LENGTH_LONG + } + toast?.show() + } +} + +fun Context.longToastOnUi(message: CharSequence?) { + runOnUI { + if (toast == null) { + toast = Toast.makeText(this, message, Toast.LENGTH_LONG) + } else { + toast?.setText(message) + toast?.duration = Toast.LENGTH_LONG + } + toast?.show() + } +} + + +fun Fragment.toastOnUi(message: Int) = requireActivity().toastOnUi(message) + +fun Fragment.toastOnUi(message: CharSequence) = requireActivity().toastOnUi(message) + +fun Fragment.longToast(message: Int) = requireContext().longToastOnUi(message) + +fun Fragment.longToast(message: CharSequence) = requireContext().longToastOnUi(message) diff --git a/app/src/main/java/io/legado/app/utils/Toasts.kt b/app/src/main/java/io/legado/app/utils/Toasts.kt deleted file mode 100644 index c98f3561c..000000000 --- a/app/src/main/java/io/legado/app/utils/Toasts.kt +++ /dev/null @@ -1,35 +0,0 @@ -@file:Suppress("unused") - -package io.legado.app.utils - -import android.widget.Toast -import androidx.fragment.app.Fragment - - -/** - * Display the simple Toast message with the [Toast.LENGTH_SHORT] duration. - * - * @param message the message text resource. - */ -fun Fragment.toastOnUi(message: Int) = requireActivity().toastOnUi(message) - -/** - * Display the simple Toast message with the [Toast.LENGTH_SHORT] duration. - * - * @param message the message text. - */ -fun Fragment.toastOnUi(message: CharSequence) = requireActivity().toastOnUi(message) - -/** - * Display the simple Toast message with the [Toast.LENGTH_LONG] duration. - * - * @param message the message text resource. - */ -fun Fragment.longToast(message: Int) = requireContext().longToastOnUi(message) - -/** - * Display the simple Toast message with the [Toast.LENGTH_LONG] duration. - * - * @param message the message text. - */ -fun Fragment.longToast(message: CharSequence) = requireContext().longToastOnUi(message)