优化toast提示重复

pull/1463/head
gedoor 3 years ago
parent 787064dd21
commit 49bfcc1be2
  1. 5
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt
  2. 25
      app/src/main/java/io/legado/app/utils/ContextExtensions.kt
  3. 66
      app/src/main/java/io/legado/app/utils/ToastUtils.kt
  4. 35
      app/src/main/java/io/legado/app/utils/Toasts.kt

@ -8,6 +8,7 @@ import io.legado.app.constant.AppPattern
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.ConcurrentException
import io.legado.app.model.NoStackTraceException import io.legado.app.model.NoStackTraceException
import io.legado.app.model.ReadAloud import io.legado.app.model.ReadAloud
import io.legado.app.model.ReadBook import io.legado.app.model.ReadBook
@ -163,13 +164,15 @@ class HttpReadAloudService : BaseReadAloudService(),
} catch (e: CancellationException) { } catch (e: CancellationException) {
removeSpeakCache(fileName) removeSpeakCache(fileName)
//任务取消,不处理 //任务取消,不处理
} catch (e: ConcurrentException) {
removeSpeakCache(fileName)
downloadAudio()
} catch (e: SocketTimeoutException) { } catch (e: SocketTimeoutException) {
removeSpeakCache(fileName) removeSpeakCache(fileName)
downloadErrorNo++ downloadErrorNo++
if (playErrorNo > 5) { if (playErrorNo > 5) {
createSilentSound(fileName) createSilentSound(fileName)
} else { } else {
toastOnUi("tts接口超时,尝试重新获取")
downloadAudio() downloadAudio()
} }
} catch (e: ConnectException) { } catch (e: ConnectException) {

@ -17,7 +17,6 @@ import android.os.BatteryManager
import android.os.Build import android.os.Build
import android.os.Process import android.os.Process
import android.provider.Settings import android.provider.Settings
import android.widget.Toast
import androidx.annotation.ColorRes import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
@ -79,30 +78,6 @@ inline fun <reified T : BroadcastReceiver> Context.broadcastPendingIntent(
return getBroadcast(this, 0, intent, FLAG_CANCEL_CURRENT) 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 val Context.defaultSharedPreferences: SharedPreferences
get() = PreferenceManager.getDefaultSharedPreferences(this) get() = PreferenceManager.getDefaultSharedPreferences(this)

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

@ -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)
Loading…
Cancel
Save