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

Commit bf4fd80f authored by tibbi's avatar tibbi
Browse files

moving toast showing from Activity to Context extension

use Handler(Looper.getMainLooper()).post instead of
activity.runOnMainThread()
parent f99be9af
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ buildscript {
        propMinSdkVersion = 21
        propTargetSdkVersion = propCompileSdkVersion
        propVersionCode = 1
        propVersionName = '5.4.8'
        propVersionName = '5.5.0'
        kotlin_version = '1.3.10'
    }

+0 −41
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.documentfile.provider.DocumentFile
@@ -34,46 +33,6 @@ import kotlinx.android.synthetic.main.dialog_title.view.*
import java.io.*
import java.util.*

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

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

private fun showToast(activity: Activity, messageId: Int, length: Int) {
    if (!activity.isDestroyed) {
        showToast(activity, activity.getString(messageId), length)
    }
}

private fun showToast(activity: Activity, message: String, length: Int) {
    if (!activity.isDestroyed) {
        activity.applicationContext.toast(message, length)
    }
}

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

fun Activity.showErrorToast(exception: Exception, length: Int = Toast.LENGTH_LONG) {
    showErrorToast(exception.toString(), length)
}

fun AppCompatActivity.updateActionBarTitle(text: String, color: Int = baseConfig.primaryColor) {
    supportActionBar?.title = Html.fromHtml("<font color='${color.getContrastColor().toHex()}'>$text</font>")
}
+17 −1
Original line number Diff line number Diff line
@@ -13,6 +13,8 @@ import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.os.Handler
import android.os.Looper
import android.provider.BaseColumns
import android.provider.DocumentsContract
import android.provider.MediaStore
@@ -89,7 +91,13 @@ fun Context.isBlackAndWhiteTheme() = baseConfig.textColor == Color.WHITE && base
fun Context.getAdjustedPrimaryColor() = if (isBlackAndWhiteTheme()) Color.WHITE else baseConfig.primaryColor

fun Context.toast(id: Int, length: Int = Toast.LENGTH_SHORT) {
    if (isOnMainThread()) {
        toast(getString(id), length)
    } else {
        Handler(Looper.getMainLooper()).post {
            toast(getString(id), length)
        }
    }
}

fun Context.toast(msg: String, length: Int = Toast.LENGTH_SHORT) {
@@ -99,6 +107,14 @@ fun Context.toast(msg: String, length: Int = Toast.LENGTH_SHORT) {
    }
}

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

fun Context.showErrorToast(exception: Exception, length: Int = Toast.LENGTH_LONG) {
    showErrorToast(exception.toString(), length)
}

val Context.baseConfig: BaseConfig get() = BaseConfig.newInstance(this)
val Context.sdCardPath: String get() = baseConfig.sdCardPath
val Context.internalStoragePath: String get() = baseConfig.internalStoragePath