Skip to content

Commit

Permalink
android: Set duration for MiuiStringToast
Browse files Browse the repository at this point in the history
  • Loading branch information
YuKongA committed Jul 15, 2024
1 parent 7594fce commit 29aecb3
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 8 deletions.
6 changes: 3 additions & 3 deletions composeApp/src/androidMain/kotlin/Toast.android.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import top.yukonga.updater.kmp.misc.miuiStringToast.MiuiStringToast
private var lastToast: Toast? = null

actual fun useToast(): Boolean = true
actual fun showToast(message: String, duration: Int) {
actual fun showToast(message: String, duration: Long) {
val context = AndroidAppContext.getApplicationContext()
if ((!isMiPad() && isLandscape()) || !atLeast(Build.VERSION_CODES.TIRAMISU) || !isHyperOS()) {
lastToast?.cancel()
lastToast = Toast.makeText(context, message, duration).apply { show() }
lastToast = Toast.makeText(context, message, duration.toInt()).apply { show() }
} else {
MiuiStringToast.showStringToast(context!!, message)
MiuiStringToast.showStringToast(context!!, message, duration)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import top.yukonga.updater.kmp.misc.miuiStringToast.data.TextParams
object MiuiStringToast {
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
@SuppressLint("WrongConstant")
fun showStringToast(context: Context, text: String) {
fun showStringToast(context: Context, text: String, duration: Long) {
val textParams = TextParams(text, Color.parseColor("#FFFFFFFF"))
val left = Left(textParams = textParams)
val iconParams = IconParams(Category.DRAWABLE, FileType.SVG, "ic_app_icon", 1)
Expand All @@ -26,6 +26,7 @@ object MiuiStringToast {
val bundle = StringToastBundle.Builder()
.setPackageName("top.yukonga.updater.kmp")
.setStrongToastCategory(StrongToastCategory.TEXT_BITMAP)
.setDuration(duration)
.setParam(jsonStr)
.onCreate()
val service = context.getSystemService(Context.STATUS_BAR_SERVICE)
Expand Down
2 changes: 1 addition & 1 deletion composeApp/src/commonMain/kotlin/Toast.kt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
expect fun useToast(): Boolean
expect fun showToast(message: String, duration: Int)
expect fun showToast(message: String, duration: Long)
2 changes: 1 addition & 1 deletion composeApp/src/commonMain/kotlin/misc/MessageUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MessageUtils {

fun showMessage(message: String, duration: Long = 1000L) {
if (useToast()) {
showToast(message, duration.toInt())
showToast(message, duration)
} else {
snackbarCoroutineJob?.cancel()
snackbarMessage.value = message
Expand Down
2 changes: 1 addition & 1 deletion composeApp/src/desktopMain/kotlin/Toast.desktop.kt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
actual fun useToast(): Boolean = false
actual fun showToast(message: String, duration: Int) {}
actual fun showToast(message: String, duration: Long) {}
2 changes: 1 addition & 1 deletion composeApp/src/iosMain/kotlin/Toast.ios.kt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
actual fun useToast(): Boolean = false
actual fun showToast(message: String, duration: Int) {}
actual fun showToast(message: String, duration: Long) {}

0 comments on commit 29aecb3

Please sign in to comment.