-
-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: crash on showing toast in LogActivity on some custom ROMs
ToastUtils of AndroidUtilCode may cause this issue, so I adapt the toast utils from fcitx5-android. Ref: https://github.com/fcitx5-android/fcitx5-android/blob/59558c5b624359455911082b10750f4dcbd10fe8/app/src/main/java/org/fcitx/fcitx5/android/utils/Toast.kt
- Loading branch information
1 parent
2bc4f2f
commit 7bb35d3
Showing
3 changed files
with
46 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
// Adapted from https://github.com/fcitx5-android/fcitx5-android/blob/59558c5b624359455911082b10750f4dcbd10fe8/app/src/main/java/org/fcitx/fcitx5/android/utils/Toast.kt | ||
package com.osfans.trime.util | ||
|
||
import android.content.Context | ||
import android.widget.Toast | ||
import androidx.annotation.StringRes | ||
import com.osfans.trime.R | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
fun Context.toast( | ||
string: String, | ||
duration: Int = Toast.LENGTH_SHORT, | ||
) { | ||
Toast.makeText(this, string, duration).show() | ||
} | ||
|
||
fun Context.toast( | ||
@StringRes resId: Int, | ||
duration: Int = Toast.LENGTH_SHORT, | ||
) { | ||
Toast.makeText(this, resId, duration).show() | ||
} | ||
|
||
fun Context.toast( | ||
t: Throwable, | ||
duration: Int = Toast.LENGTH_SHORT, | ||
) { | ||
toast(t.localizedMessage ?: t.stackTraceToString(), duration) | ||
} | ||
|
||
suspend fun <T> Context.toast( | ||
result: Result<T>, | ||
duration: Int = Toast.LENGTH_SHORT, | ||
) { | ||
withContext(Dispatchers.Main.immediate) { | ||
result | ||
.onSuccess { toast(R.string.setup__done, duration) } | ||
.onFailure { toast(it, duration) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters