Merge and remove util classes

This commit is contained in:
len
2017-01-08 18:50:51 +01:00
parent dd56d7c0bb
commit 07cae4d684
15 changed files with 47 additions and 75 deletions
@@ -4,6 +4,7 @@ import android.app.Notification
import android.app.NotificationManager
import android.content.Context
import android.content.pm.PackageManager
import android.content.res.Resources
import android.net.ConnectivityManager
import android.os.PowerManager
import android.support.annotation.StringRes
@@ -13,6 +14,7 @@ import android.widget.Toast
/**
* Display a toast in this context.
*
* @param resource the text resource.
* @param duration the duration of the toast. Defaults to short.
*/
@@ -22,6 +24,7 @@ fun Context.toast(@StringRes resource: Int, duration: Int = Toast.LENGTH_SHORT)
/**
* Display a toast in this context.
*
* @param text the text to display.
* @param duration the duration of the toast. Defaults to short.
*/
@@ -31,6 +34,7 @@ fun Context.toast(text: String?, duration: Int = Toast.LENGTH_SHORT) {
/**
* Helper method to create a notification.
*
* @param func the function that will execute inside the builder.
* @return a notification to be displayed or updated.
*/
@@ -42,12 +46,37 @@ inline fun Context.notification(func: NotificationCompat.Builder.() -> Unit): No
/**
* Checks if the give permission is granted.
*
* @param permission the permission to check.
* @return true if it has permissions.
*/
fun Context.hasPermission(permission: String)
= ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED
/**
* Returns the color for the given attribute.
*
* @param resource the attribute.
*/
fun Context.getResourceColor(@StringRes resource: Int): Int {
val typedArray = obtainStyledAttributes(intArrayOf(resource))
val attrValue = typedArray.getColor(0, 0)
typedArray.recycle()
return attrValue
}
/**
* Converts to dp.
*/
val Int.pxToDp: Int
get() = (this / Resources.getSystem().displayMetrics.density).toInt()
/**
* Converts to px.
*/
val Int.dpToPx: Int
get() = (this * Resources.getSystem().displayMetrics.density).toInt()
/**
* Property to get the notification manager from the context.
*/
@@ -64,4 +93,5 @@ val Context.connectivityManager: ConnectivityManager
* Property to get the power manager from the context.
*/
val Context.powerManager: PowerManager
get() = getSystemService(Context.POWER_SERVICE) as PowerManager
get() = getSystemService(Context.POWER_SERVICE) as PowerManager