Skip to content

Commit

Permalink
fix: crash on showing toast in LogActivity on some custom ROMs
Browse files Browse the repository at this point in the history
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
WhiredPlanck committed May 3, 2024
1 parent 2bc4f2f commit 7bb35d3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/osfans/trime/ui/main/LogActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class LogActivity : AppCompatActivity() {
it.write(logView.currentLog)
}
}
}?.toast()
}?.let { toast(it) }
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions app/src/main/java/com/osfans/trime/util/Toast.kt
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) }
}
}
27 changes: 0 additions & 27 deletions app/src/main/java/com/osfans/trime/util/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,16 @@ import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.osfans.trime.R
import com.osfans.trime.TrimeApplication
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import splitties.experimental.InternalSplittiesApi
import splitties.resources.withResolvedThemeAttribute
import java.io.Serializable
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.TimeZone
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

val appContext: Context get() = TrimeApplication.getInstance().applicationContext

@OptIn(ExperimentalContracts::class)
inline fun <T : Any, U> Result<T?>.bindOnNotNull(block: (T) -> Result<U>): Result<U>? {
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
}
return when {
isSuccess && getOrThrow() != null -> block(getOrThrow()!!)
isSuccess && getOrThrow() == null -> null
else -> Result.failure(exceptionOrNull()!!)
}
}

suspend fun <T> Result<T>.toast() =
withContext(Dispatchers.Main.immediate) {
onSuccess {
ToastUtils.showShort(R.string.setup__done)
}
onFailure {
ToastUtils.showShort(it.message)
}
}

@OptIn(InternalSplittiesApi::class)
fun Context.styledFloat(
@AttrRes attrRes: Int,
Expand Down

0 comments on commit 7bb35d3

Please sign in to comment.