Skip to content

Commit

Permalink
Feature: added the new resource ktx and removed the resolution functi…
Browse files Browse the repository at this point in the history
…ons.
  • Loading branch information
pokk committed Mar 5, 2019
1 parent 71a26fd commit 9bbc35b
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 72 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.android.tools.build:gradle:3.3.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
12 changes: 5 additions & 7 deletions kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Bitmap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ import androidx.annotation.DrawableRes
import androidx.palette.graphics.Palette
import java.io.ByteArrayOutputStream

/**
* @author jieyi
* @since 2018/01/12
*/

fun Resources.createBitmap(drawableResId: Int, opts: BitmapFactory.Options? = null, rect: Rect? = null): Bitmap? {
fun Resources.createBitmap(
drawableResId: Int, opts: BitmapFactory.Options? = null,
rect: Rect? = null
): Bitmap? {
var bitmap: Bitmap? = null
openRawResource(drawableResId).use {
bitmap = BitmapFactory.decodeStream(it, rect, opts)
Expand Down Expand Up @@ -65,7 +63,7 @@ fun Bitmap.scale(widthRatio: Float, heightRatio: Float): Bitmap {
fun Bitmap.scale(ratio: Float) = scale(ratio, ratio)

fun Resources.createCompressedBitmap(
drawableResId: Int,
@DrawableRes drawableResId: Int,
simpleSize: Int = 1,
bitmapConf: Bitmap.Config? = null
): Bitmap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fun isShowingSoftKeyboard(rootView: View): Boolean {

val heightDiff = bottom - r.bottom

heightDiff > softKeyboardHeight * resources.displayMetrics.density
heightDiff > softKeyboardHeight * displayMetrics().density
}
}

Expand Down
36 changes: 0 additions & 36 deletions kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Resolution.kt

This file was deleted.

178 changes: 178 additions & 0 deletions kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Resource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package com.devrapid.kotlinknifer

import android.app.Activity
import android.content.Context
import android.content.res.Resources.Theme
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Rect
import android.os.Build
import android.view.View
import androidx.annotation.AnimRes
import androidx.annotation.AnimatorRes
import androidx.annotation.DimenRes
import androidx.annotation.DrawableRes
import androidx.annotation.FontRes
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment

//region Context
inline fun Context.getDimen(@DimenRes id: Int) = resources.getDimension(id)

inline fun Context.getDimenPixelSize(@DimenRes id: Int) = resources.getDimensionPixelSize(id)

inline fun Context.getAnimation(@AnimatorRes @AnimRes id: Int) = resources.getAnimation(id)

inline fun Context.getIdentifier(name: String, defType: String, defPackage: String) =
resources.getIdentifier(name, defType, defPackage)

inline fun Context.getDrawable(@DrawableRes id: Int, theme: Theme? = null) = resources.getDrawable(id, theme)

@RequiresApi(Build.VERSION_CODES.O)
inline fun Context.getFont(@FontRes id: Int) = resources.getFont(id)

inline fun Context.displayMetrics() = resources.displayMetrics

inline fun Context.createBitmap(@DrawableRes id: Int, opts: BitmapFactory.Options? = null, rect: Rect? = null) =
resources.createBitmap(id, opts, rect)

inline fun Context.obtainBitmapSize(@DrawableRes id: Int) = resources.obtainBitmapSize(id)

inline fun Context.createCompressedBitmap(
@DrawableRes id: Int, simpleSize: Int = 1,
bitmapConf: Bitmap.Config? = null
) = resources.createCompressedBitmap(id, simpleSize, bitmapConf)

inline fun Context.createScaledBitmap(@DrawableRes id: Int, width: Int, height: Int) =
resources.createScaledBitmap(id, width, height)

inline fun Context.createScaledBitmap(@DrawableRes id: Int, widthRatio: Float, heightRatio: Float) =
resources.createScaledBitmap(id, widthRatio, heightRatio)

inline fun Context.createScaledBitmap(@DrawableRes id: Int, ratio: Float) = resources.createScaledBitmap(id, ratio)

inline fun Context.createRegionBitmap(
@DrawableRes id: Int, rect: Rect,
opts: BitmapFactory.Options = BitmapFactory.Options()
) = resources.createRegionBitmap(id, rect, opts)
//endregion

//region Activity
inline fun Activity.getDimen(@DimenRes id: Int) = resources.getDimension(id)

inline fun Activity.getDimenPixelSize(@DimenRes id: Int) = resources.getDimensionPixelSize(id)

inline fun Activity.getAnimation(@AnimatorRes @AnimRes id: Int) = resources.getAnimation(id)

inline fun Activity.getIdentifier(name: String, defType: String, defPackage: String) =
resources.getIdentifier(name, defType, defPackage)

inline fun Activity.getDrawable(@DrawableRes id: Int, theme: Theme? = null) = resources.getDrawable(id, theme)
@RequiresApi(Build.VERSION_CODES.O)

inline fun Activity.getFont(@FontRes id: Int) = resources.getFont(id)

inline fun Activity.displayMetrics() = resources.displayMetrics

inline fun Activity.createBitmap(@DrawableRes id: Int, opts: BitmapFactory.Options? = null, rect: Rect? = null) =
resources.createBitmap(id, opts, rect)

inline fun Activity.obtainBitmapSize(@DrawableRes id: Int) = resources.obtainBitmapSize(id)

inline fun Activity.createCompressedBitmap(
@DrawableRes id: Int, simpleSize: Int = 1,
bitmapConf: Bitmap.Config? = null
) = resources.createCompressedBitmap(id, simpleSize, bitmapConf)

inline fun Activity.createScaledBitmap(@DrawableRes id: Int, width: Int, height: Int) =
resources.createScaledBitmap(id, width, height)

inline fun Activity.createScaledBitmap(@DrawableRes id: Int, widthRatio: Float, heightRatio: Float) =
resources.createScaledBitmap(id, widthRatio, heightRatio)

inline fun Activity.createScaledBitmap(@DrawableRes id: Int, ratio: Float) = resources.createScaledBitmap(id, ratio)

inline fun Activity.createRegionBitmap(
@DrawableRes id: Int, rect: Rect,
opts: BitmapFactory.Options = BitmapFactory.Options()
) = resources.createRegionBitmap(id, rect, opts)
//endregion

//region Fragment
inline fun Fragment.getDimen(@DimenRes id: Int) = resources.getDimension(id)

inline fun Fragment.getDimenPixelSize(@DimenRes id: Int) = resources.getDimensionPixelSize(id)

inline fun Fragment.getAnimation(@AnimatorRes @AnimRes id: Int) = resources.getAnimation(id)

inline fun Fragment.getIdentifier(name: String, defType: String, defPackage: String) =
resources.getIdentifier(name, defType, defPackage)

inline fun Fragment.getDrawable(@DrawableRes id: Int, theme: Theme? = null) = resources.getDrawable(id, theme)

@RequiresApi(Build.VERSION_CODES.O)
inline fun Fragment.getFont(@FontRes id: Int) = resources.getFont(id)

inline fun Fragment.displayMetrics() = resources.displayMetrics

inline fun Fragment.createBitmap(@DrawableRes id: Int, opts: BitmapFactory.Options? = null, rect: Rect? = null) =
resources.createBitmap(id, opts, rect)

inline fun Fragment.obtainBitmapSize(@DrawableRes id: Int) = resources.obtainBitmapSize(id)

inline fun Fragment.createCompressedBitmap(
@DrawableRes id: Int, simpleSize: Int = 1,
bitmapConf: Bitmap.Config? = null
) = resources.createCompressedBitmap(id, simpleSize, bitmapConf)

inline fun Fragment.createScaledBitmap(@DrawableRes id: Int, width: Int, height: Int) =
resources.createScaledBitmap(id, width, height)

inline fun Fragment.createScaledBitmap(@DrawableRes id: Int, widthRatio: Float, heightRatio: Float) =
resources.createScaledBitmap(id, widthRatio, heightRatio)

inline fun Fragment.createScaledBitmap(@DrawableRes id: Int, ratio: Float) = resources.createScaledBitmap(id, ratio)
inline fun Fragment.createRegionBitmap(
@DrawableRes id: Int, rect: Rect,
opts: BitmapFactory.Options = BitmapFactory.Options()
) = resources.createRegionBitmap(id, rect, opts)
//endregion

//region View
inline fun View.getDimen(@DimenRes id: Int) = resources.getDimension(id)

inline fun View.getDimenPixelSize(@DimenRes id: Int) = resources.getDimensionPixelSize(id)

inline fun View.getAnimation(@AnimatorRes @AnimRes id: Int) = resources.getAnimation(id)

inline fun View.getIdentifier(name: String, defType: String, defPackage: String) =
resources.getIdentifier(name, defType, defPackage)

inline fun View.getDrawable(@DrawableRes id: Int, theme: Theme? = null) = resources.getDrawable(id, theme)

@RequiresApi(Build.VERSION_CODES.O)
inline fun View.getFont(@FontRes id: Int) = resources.getFont(id)

inline fun View.displayMetrics() = resources.displayMetrics

inline fun View.createBitmap(@DrawableRes id: Int, opts: BitmapFactory.Options? = null, rect: Rect? = null) =
resources.createBitmap(id, opts, rect)

inline fun View.obtainBitmapSize(@DrawableRes id: Int) = resources.obtainBitmapSize(id)

inline fun View.createCompressedBitmap(@DrawableRes id: Int, simpleSize: Int = 1, bitmapConf: Bitmap.Config? = null) =
resources.createCompressedBitmap(id, simpleSize, bitmapConf)

inline fun View.createScaledBitmap(@DrawableRes id: Int, width: Int, height: Int) =
resources.createScaledBitmap(id, width, height)

inline fun View.createScaledBitmap(@DrawableRes id: Int, widthRatio: Float, heightRatio: Float) =
resources.createScaledBitmap(id, widthRatio, heightRatio)

inline fun View.createScaledBitmap(@DrawableRes id: Int, ratio: Float) = resources.createScaledBitmap(id, ratio)

inline fun View.createRegionBitmap(
@DrawableRes id: Int, rect: Rect,
opts: BitmapFactory.Options = BitmapFactory.Options()
) = resources.createRegionBitmap(id, rect, opts)
//endregion
16 changes: 7 additions & 9 deletions kotlinknifer/src/main/java/com/devrapid/kotlinknifer/View.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,13 @@ fun Context.alert(message: Int, title: Int? = null, init: (AlertDialog.Builder.(
init?.let { init() }
}

inline fun Context.navigationBarHeiht() =
resources.getIdentifier("navigation_bar_height", "dimen", "android")
.takeIf { 0 < it }
?.let { resources.getDimensionPixelSize(it) } ?: 0

inline fun Context.statusBarHeight() =
resources.getIdentifier("status_bar_height", "dimen", "android")
.takeIf { 0 < it }
?.let { resources.getDimensionPixelSize(it) } ?: 0
inline fun Context.navigationBarHeiht() = getIdentifier("navigation_bar_height", "dimen", "android")
.takeIf { 0 < it }
?.let { getDimenPixelSize(it) } ?: 0

inline fun Context.statusBarHeight() = getIdentifier("status_bar_height", "dimen", "android")
.takeIf { 0 < it }
?.let { getDimenPixelSize(it) } ?: 0

inline fun Activity.statusBarHeight() = Rect()
.apply { window.decorView.getWindowVisibleDisplayFrame(this) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

/**
* The item decorator for grid type recycler view.
*
* @author jieyi
* @since 11/11/17
*/
class GridSpacingItemDecorator(private val spanCount: Int, private val spacing: Int, private val includeEdge: Boolean) :
RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

/**
* The item decorator for horizontal recycler view.
*
* @author jieyi
* @since 11/11/17
*/
class HorizontalItemDecorator(private val space: Int) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
val position: Int = parent.getChildAdapterPosition(view)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

/**
* The item decorator for vertical type recycler view.
*
* @author jieyi
* @since 11/11/17
*/
class VerticalItemDecorator(
private val topBottom: Int,
private val leftRight: Int = topBottom
Expand Down

0 comments on commit 9bbc35b

Please sign in to comment.