Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8813c0d5 authored by tibbi's avatar tibbi
Browse files

ensure the activity is alive when trying to show a toast

parent 246a83de
Loading
Loading
Loading
Loading
+16 −4
Original line number Diff line number Diff line
@@ -41,24 +41,36 @@ import java.util.*

fun Activity.toast(id: Int, length: Int = Toast.LENGTH_SHORT) {
    if (isOnMainThread()) {
        Toast.makeText(this, id, length).show()
        showToast(this, id, length)
    } else {
        runOnUiThread {
            Toast.makeText(this, id, length).show()
            showToast(this, id, length)
        }
    }
}

fun Activity.toast(msg: String, length: Int = Toast.LENGTH_SHORT) {
    if (isOnMainThread()) {
        Toast.makeText(this, msg, length).show()
        showToast(this, msg, length)
    } else {
        runOnUiThread {
            Toast.makeText(this, msg, length).show()
            showToast(this, msg, length)
        }
    }
}

private fun showToast(activity: Activity, messageId: Int, length: Int) {
    if (!activity.isActivityDestroyed()) {
        Toast.makeText(activity, messageId, length).show()
    }
}

private fun showToast(activity: Activity, message: String, length: Int) {
    if (!activity.isActivityDestroyed()) {
        Toast.makeText(activity, message, length).show()
    }
}

fun Activity.showErrorToast(msg: String, length: Int = Toast.LENGTH_LONG) {
    toast(String.format(getString(R.string.an_error_occurred), msg), length)
}