aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca
diff options
context:
space:
mode:
authorJahir Fiquitiva <jahir.fiquitiva@gmail.com>2017-07-10 01:22:52 -0500
committerAllan Wang <me@allanwang.ca>2017-07-09 23:22:52 -0700
commit3028d35c24da883dad34dbc4f6eb36d1df1838fa (patch)
tree3bf16d3f0e98c570731469a6969cc73b403efc3c /core/src/main/kotlin/ca
parentf1660aab8a25c93aebdb7993e4bfbc3bb7e65ee5 (diff)
downloadkau-3028d35c24da883dad34dbc4f6eb36d1df1838fa.tar.gz
kau-3028d35c24da883dad34dbc4f6eb36d1df1838fa.tar.bz2
kau-3028d35c24da883dad34dbc4f6eb36d1df1838fa.zip
Added some extensions (#4)
* Add a couple extra extensions. * Add more extensions * Make oneline fun * Remove duplicated line from modules.xml * Revert kprefsingle and fix minor docs (cherry picked from commit 76d0de9) * Clean up ColorUtils * Clean up ActivityUtils * Clean up DrawableUtils * Remove MenuUtils * Clean PackageUtils * Clean up ViewUtils * Clean up ViewUtils 2
Diffstat (limited to 'core/src/main/kotlin/ca')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt5
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefSingleDelegate.kt5
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt24
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/AnimUtils.kt2
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt17
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/Const.kt7
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/DrawableUtils.kt20
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt9
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt70
9 files changed, 119 insertions, 40 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt
index 74792e1..f742078 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt
@@ -2,9 +2,6 @@ package ca.allanwang.kau.kpref
import ca.allanwang.kau.kotlin.ILazyResettable
-/**
- * Created by Allan Wang on 2017-06-07.
- */
object UNINITIALIZED
fun KPref.kpref(key: String, fallback: Boolean, postSetter: (value: Boolean) -> Unit = {}) = KPrefDelegate(key, fallback, this, postSetter)
@@ -18,6 +15,8 @@ fun KPref.kpref(key: String, fallback: String, postSetter: (value: String) -> Un
class StringSet(set: Collection<String>) : LinkedHashSet<String>(set)
/**
+ * Created by Allan Wang on 2017-06-07.
+ *
* Implementation of a kpref data item
* Contains a unique key for the shared preference as well as a nonnull fallback item
* Also contains an optional mutable postSetter that will be called every time a new value is given
diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefSingleDelegate.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefSingleDelegate.kt
index 6c0b608..af5c59b 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefSingleDelegate.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefSingleDelegate.kt
@@ -2,12 +2,11 @@ package ca.allanwang.kau.kpref
import ca.allanwang.kau.kotlin.ILazyResettable
-/**
- * Created by Allan Wang on 2017-06-07.
- */
fun KPref.kprefSingle(key: String) = KPrefSingleDelegate(key, this)
/**
+ * Created by Allan Wang on 2017-06-07.
+ *
* Singular KPref Delegate for booleans
* When the shared pref is not initialized, it will return true then set the pref to false
* All subsequent retrievals will be false
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt
index 4f95b2c..3b99c46 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt
@@ -1,12 +1,16 @@
package ca.allanwang.kau.utils
+import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.graphics.Color
+import android.os.Build
import android.support.annotation.ColorInt
+import android.support.annotation.RequiresApi
import android.support.annotation.StringRes
import android.support.design.widget.Snackbar
import android.view.Menu
+import android.view.View
import ca.allanwang.kau.R
import com.mikepenz.iconics.typeface.IIcon
import org.jetbrains.anko.contentView
@@ -16,18 +20,17 @@ import org.jetbrains.anko.contentView
*/
/**
- * Restarts an activity from itself without animations
+ * Restarts an activity from itself with a fade animation
* Keeps its existing extra bundles and has a builder to accept other parameters
*/
fun Activity.restart(builder: Intent.() -> Unit = {}) {
val i = Intent(this, this::class.java)
- i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
i.putExtras(intent.extras)
i.builder()
startActivity(i)
- overridePendingTransition(0, 0) //No transitions
+ overridePendingTransition(R.anim.kau_fade_in, R.anim.kau_fade_out) //No transitions
finish()
- overridePendingTransition(0, 0)
+ overridePendingTransition(R.anim.kau_fade_in, R.anim.kau_fade_out)
}
fun Activity.finishSlideOut() {
@@ -47,6 +50,19 @@ var Activity.statusBarColor: Int
if (buildIsLollipopAndUp) window.statusBarColor = value
}
+var Activity.statusBarLight: Boolean
+ @SuppressLint("InlinedApi")
+ get() = if (buildIsMarshmallowAndUp) window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR > 0 else false
+ @SuppressLint("InlinedApi")
+ set(value) {
+ if (buildIsMarshmallowAndUp) {
+ val flags = window.decorView.systemUiVisibility
+ window.decorView.systemUiVisibility =
+ if (value) flags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
+ else flags and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
+ }
+ }
+
/**
* Themes the base menu icons and adds iicons programmatically based on ids
*
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/AnimUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/AnimUtils.kt
index 2704387..bbde077 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/AnimUtils.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/AnimUtils.kt
@@ -13,7 +13,7 @@ import android.widget.TextView
/**
* Created by Allan Wang on 2017-06-01.
*
- * Animation extension @KauUtils functions for Views
+ * Animation extension functions for Views
*/
@KauUtils fun View.rootCircularReveal(x: Int = 0, y: Int = 0, duration: Long = 500L, onStart: (() -> Unit)? = null, onFinish: (() -> Unit)? = null) {
this.addOnLayoutChangeListener(object : View.OnLayoutChangeListener {
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt
index 84a7c06..81bf0d9 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt
@@ -19,8 +19,11 @@ import com.afollestad.materialdialogs.R
/**
* Created by Allan Wang on 2017-06-08.
*/
-val Int.isColorDark: Boolean
- get() = (0.299 * Color.red(this) + 0.587 * Color.green(this) + 0.114 * Color.blue(this)) / 255.0 < 0.5
+inline val Int.isColorDark: Boolean
+ get() = isColorDark(0.5f)
+
+fun Int.isColorDark(minDarkness: Float): Boolean =
+ ((0.299 * Color.red(this) + 0.587 * Color.green(this) + 0.114 * Color.blue(this)) / 255.0) < minDarkness
fun Int.toHexString(withAlpha: Boolean = false, withHexPrefix: Boolean = true): String {
val hex = if (withAlpha) String.format("#%08X", this)
@@ -66,6 +69,16 @@ val Int.isColorTransparent: Boolean
get() = Color.alpha(this) != 255
@ColorInt
+fun Int.blendWith(@ColorInt color: Int, @FloatRange(from = 0.0, to = 1.0) ratio: Float): Int {
+ val inverseRatio = 1f - ratio
+ val a = Color.alpha(this) * inverseRatio + Color.alpha(color) * ratio
+ val r = Color.red(this) * inverseRatio + Color.red(color) * ratio
+ val g = Color.green(this) * inverseRatio + Color.green(color) * ratio
+ val b = Color.blue(this) * inverseRatio + Color.blue(color) * ratio
+ return Color.argb(a.toInt(), r.toInt(), g.toInt(), b.toInt())
+}
+
+@ColorInt
fun Int.withAlpha(@IntRange(from = 0L, to = 255L) alpha: Int): Int
= Color.argb(alpha, Color.red(this), Color.green(this), Color.blue(this))
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/Const.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/Const.kt
index 944caa4..9fcc7e7 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/Const.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/Const.kt
@@ -3,4 +3,9 @@ package ca.allanwang.kau.utils
/**
* Created by Allan Wang on 2017-06-08.
*/
-const val ANDROID_NAMESPACE = "http://schemas.android.com/apk/res/android" \ No newline at end of file
+const val ANDROID_NAMESPACE = "http://schemas.android.com/apk/res/android"
+
+const val KAU_LEFT = 1
+const val KAU_TOP = 2
+const val KAU_RIGHT = 4
+const val KAU_BOTTOM = 8 \ No newline at end of file
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/DrawableUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/DrawableUtils.kt
new file mode 100644
index 0000000..dae3bff
--- /dev/null
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/DrawableUtils.kt
@@ -0,0 +1,20 @@
+package ca.allanwang.kau.utils
+
+import android.content.res.ColorStateList
+import android.graphics.drawable.Drawable
+import android.support.annotation.ColorInt
+import android.support.v4.graphics.drawable.DrawableCompat
+
+/**
+ * Wrap the color into a state and tint the drawable
+ */
+fun Drawable.tint(@ColorInt color: Int): Drawable = tint(ColorStateList.valueOf(color))
+
+/**
+ * Tint the drawable with a given color state list
+ */
+fun Drawable.tint(state: ColorStateList): Drawable {
+ val drawable = DrawableCompat.wrap(mutate())
+ DrawableCompat.setTintList(drawable, state)
+ return drawable
+} \ No newline at end of file
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt
index 08e945c..d04538c 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt
@@ -14,15 +14,12 @@ import android.os.Build
* @return true if installed with activity, false otherwise
*/
@KauUtils fun Context.isAppInstalled(packageName: String): Boolean {
- val pm = packageManager
- var installed: Boolean
try {
- pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
- installed = true
+ packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
+ return true
} catch (e: PackageManager.NameNotFoundException) {
- installed = false
+ return false
}
- return installed
}
val buildIsLollipopAndUp: Boolean
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt
index 48a048b..53d6d05 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt
@@ -2,25 +2,26 @@ package ca.allanwang.kau.utils
import android.content.Context
import android.graphics.Color
-import android.graphics.Outline
import android.graphics.Rect
import android.support.annotation.ColorInt
import android.support.annotation.StringRes
import android.support.annotation.TransitionRes
+import android.support.design.widget.FloatingActionButton
import android.support.design.widget.Snackbar
+import android.support.design.widget.TextInputEditText
import android.support.transition.AutoTransition
import android.support.transition.Transition
import android.support.transition.TransitionInflater
import android.support.transition.TransitionManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
+import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
-import android.view.ViewOutlineProvider
import android.view.inputmethod.InputMethodManager
+import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
-import ca.allanwang.kau.logging.KL
import ca.allanwang.kau.ui.createSimpleRippleDrawable
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.typeface.IIcon
@@ -29,24 +30,33 @@ import com.mikepenz.iconics.typeface.IIcon
/**
* Created by Allan Wang on 2017-05-31.
*/
-@KauUtils fun <T : View> T.visible(): T {
+
+@KauUtils inline fun <T : View> T.visible(): T {
visibility = View.VISIBLE
return this
}
-@KauUtils fun <T : View> T.invisible(): T {
+@KauUtils inline fun <T : View> T.invisible(): T {
visibility = View.INVISIBLE
return this
}
-@KauUtils fun <T : View> T.gone(): T {
+@KauUtils inline fun <T : View> T.gone(): T {
visibility = View.GONE
return this
}
-@KauUtils fun View.isVisible(): Boolean = visibility == View.VISIBLE
-@KauUtils fun View.isInvisible(): Boolean = visibility == View.INVISIBLE
-@KauUtils fun View.isGone(): Boolean = visibility == View.GONE
+@KauUtils inline fun <T : View> T.invisibleIf(invisible: Boolean): T = if (invisible) invisible() else visible()
+
+@KauUtils inline fun <T : View> T.visibleIf(visible: Boolean): T = if (visible) visible() else gone()
+
+@KauUtils inline fun <T : View> T.goneIf(gone: Boolean): T = visibleIf(!gone)
+
+@KauUtils inline val View.isVisible: Boolean get() = visibility == View.VISIBLE
+
+@KauUtils inline val View.isInvisible: Boolean get() = visibility == View.INVISIBLE
+
+@KauUtils inline val View.isGone: Boolean get() = visibility == View.GONE
fun View.snackbar(text: String, duration: Int = Snackbar.LENGTH_LONG, builder: Snackbar.() -> Unit = {}): Snackbar {
val snackbar = Snackbar.make(this, text, duration)
@@ -67,6 +77,33 @@ fun View.snackbar(@StringRes textId: Int, duration: Int = Snackbar.LENGTH_LONG,
setImageDrawable(icon.toDrawable(context, sizeDp = sizeDp, color = color, builder = builder))
}
+@KauUtils inline val FloatingActionButton.isHidden get() = !isShown
+
+fun FloatingActionButton.showIf(show: Boolean) = if (show) show() else hide()
+
+fun FloatingActionButton.hideIf(hide: Boolean) = if (hide) hide() else show()
+
+@KauUtils fun ViewGroup.inflate(layoutId: Int, attachToRoot: Boolean = false): View = LayoutInflater.from(context).inflate(layoutId, this, attachToRoot)
+
+@KauUtils fun View.updateLeftMargin(margin: Int) = updateMargins(margin, KAU_LEFT)
+
+@KauUtils fun View.updateTopMargin(margin: Int) = updateMargins(margin, KAU_TOP)
+
+@KauUtils fun View.updateRightMargin(margin: Int) = updateMargins(margin, KAU_RIGHT)
+
+@KauUtils fun View.updateBottomMargin(margin: Int) = updateMargins(margin, KAU_BOTTOM)
+
+@KauUtils private fun View.updateMargins(margin: Int, flag: Int) {
+ val p = (layoutParams as? ViewGroup.MarginLayoutParams) ?: return
+ p.setMargins(
+ if (flag == KAU_LEFT) margin else p.leftMargin,
+ if (flag == KAU_TOP) margin else p.topMargin,
+ if (flag == KAU_RIGHT) margin else p.rightMargin,
+ if (flag == KAU_BOTTOM) margin else p.bottomMargin
+ )
+ requestLayout()
+}
+
@KauUtils fun View.hideKeyboard() {
clearFocus()
(context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(windowToken, 0)
@@ -93,8 +130,7 @@ fun View.snackbar(@StringRes textId: Int, duration: Int = Snackbar.LENGTH_LONG,
background = createSimpleRippleDrawable(foregroundColor, backgroundColor)
}
-@KauUtils val View.parentViewGroup: ViewGroup
- get() = parent as ViewGroup
+@KauUtils val View.parentViewGroup: ViewGroup get() = parent as ViewGroup
@KauUtils val View.parentVisibleHeight: Int
get() {
@@ -103,15 +139,9 @@ fun View.snackbar(@StringRes textId: Int, duration: Int = Snackbar.LENGTH_LONG,
return r.height()
}
-val CIRCULAR_OUTLINE: ViewOutlineProvider = object : ViewOutlineProvider() {
- override fun getOutline(view: View, outline: Outline) {
- KL.d("CIRCULAR OUTLINE")
- outline.setOval(view.paddingLeft,
- view.paddingTop,
- view.width - view.paddingRight,
- view.height - view.paddingBottom)
- }
-}
+val EditText.value: String get() = text.toString().trim()
+
+val TextInputEditText.value: String get() = text.toString().trim()
/**
* Generates a recycler view with match parent and a linearlayoutmanager, since it's so commonly used