Skip to content

Instantly share code, notes, and snippets.

@CompileConnected
Created September 13, 2020 17:26
Show Gist options
  • Select an option

  • Save CompileConnected/58a10368490892ddbbe6b0873206f8eb to your computer and use it in GitHub Desktop.

Select an option

Save CompileConnected/58a10368490892ddbbe6b0873206f8eb to your computer and use it in GitHub Desktop.
Android Extenstions
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import androidx.core.content.ContextCompat
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.appcompat.content.res.AppCompatResources
fun <T> Context.getSystemServiceCompat(serviceClass: Class<T>):T =
ContextCompat.getSystemService(this, serviceClass)!!
fun Context.getColorCompat(@ColorRes id: Int): Int = getColorStateListCompat(id).defaultColor
fun Context.getColorStateListCompat(@ColorRes id: Int): ColorStateList =
AppCompatResources.getColorStateList(this, id)!!
fun Context.getDrawableCompat(@DrawableRes id: Int): Drawable =
AppCompatResources.getDrawable(this, id)!!
import android.content.SharedPreferences
import android.os.Bundle
inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) {
val editor = this.edit()
operation(editor)
editor.apply()
}
operator fun SharedPreferences.set(key: String, value: Any?) {
when (value) {
is String? -> edit { it.putString(key, value) }
is Int -> edit { it.putInt(key, value) }
is Boolean -> edit { it.putBoolean(key, value) }
is Float -> edit { it.putFloat(key, value) }
is Long -> edit { it.putLong(key, value) }
else -> throw UnsupportedOperationException("Not yet implemented")
}
}
inline operator fun <reified T : Any> SharedPreferences.get(
key: String,
defaultValue: T? = null
): T? {
return when (T::class) {
String::class -> getString(key, defaultValue as? String) as T?
Int::class -> getInt(key, defaultValue as? Int ?: -1) as T?
Boolean::class -> getBoolean(key, defaultValue as? Boolean ?: false) as T?
Float::class -> getFloat(key, defaultValue as? Float ?: -1f) as T?
Long::class -> getLong(key, defaultValue as? Long ?: -1) as T?
else -> throw UnsupportedOperationException("Not yet implemented")
}
}
import android.content.Context
import android.widget.Toast
import androidx.fragment.app.Fragment
fun Context.showToast(message: String?, duration: Int? = null, ellipsesAt: Int? = null) {
this.createToast(message, duration, ellipsesAt).show()
}
fun Context.createToast(message: String?, duration: Int? = null, ellipsesAt: Int? = 40): Toast {
return Toast.makeText(
this,
(message ?: "").ellipsesAt(ellipsesAt!!),
duration ?: Toast.LENGTH_SHORT
)
}
fun Fragment.showToast(message: String?, duration: Int? = null) {
this.createToast(message, duration)?.show()
}
fun Fragment.createToast(message: String?, duration: Int? = null): Toast? {
return this.activity?.createToast(message, duration)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment