diff options
-rw-r--r-- | .idea/kannotator.xml | 6 | ||||
-rw-r--r-- | .idea/misc.xml | 2 | ||||
-rw-r--r-- | colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerView.kt | 6 | ||||
-rw-r--r-- | core/README.md | 2 | ||||
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt | 5 | ||||
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefSingleDelegate.kt | 5 | ||||
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt | 24 | ||||
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/utils/AnimUtils.kt | 2 | ||||
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt | 17 | ||||
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/utils/Const.kt | 7 | ||||
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/utils/DrawableUtils.kt | 20 | ||||
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt | 9 | ||||
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt | 70 | ||||
-rw-r--r-- | docs/Changelog.md | 2 | ||||
-rw-r--r-- | sample/src/main/res/xml/changelog.xml | 11 | ||||
-rw-r--r-- | searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt | 2 |
16 files changed, 141 insertions, 49 deletions
diff --git a/.idea/kannotator.xml b/.idea/kannotator.xml new file mode 100644 index 0000000..9b05217 --- /dev/null +++ b/.idea/kannotator.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="KannotatorSettings"> + <option name="dismissedInVersion" value="0.2.1205" /> + </component> +</project>
\ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 085136f..7319f02 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -53,7 +53,7 @@ <ConfirmationsSetting value="0" id="Add" /> <ConfirmationsSetting value="0" id="Remove" /> </component> - <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> + <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <output url="file://$PROJECT_DIR$/build/classes" /> </component> <component name="ProjectType"> diff --git a/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerView.kt b/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerView.kt index 04345a1..e9da763 100644 --- a/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerView.kt +++ b/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerView.kt @@ -133,7 +133,7 @@ internal class ColorPickerView @JvmOverloads constructor( } customColorIndicator.setBackgroundColor(selectedColor) - if (alphaSeekbar.isVisible()) { + if (alphaSeekbar.isVisible) { val alpha = Color.alpha(selectedColor) alphaSeekbar.progress = alpha alphaValue.text = String.format(Locale.CANADA, "%d", alpha) @@ -178,9 +178,9 @@ internal class ColorPickerView @JvmOverloads constructor( redSeekbar.setOnSeekBarChangeListener(customRgbListener) greenSeekbar.setOnSeekBarChangeListener(customRgbListener) blueSeekbar.setOnSeekBarChangeListener(customRgbListener) - if (alphaSeekbar.isVisible()) + if (alphaSeekbar.isVisible) alphaSeekbar.setOnSeekBarChangeListener(customRgbListener) - hexInput.setText(selectedColor.toHexString(alphaSeekbar.isVisible(), false)) + hexInput.setText(selectedColor.toHexString(alphaSeekbar.isVisible, false)) gridView.fadeOut(onFinish = { gridView.gone() }) customFrame.fadeIn() } else { diff --git a/core/README.md b/core/README.md index 16ba5a0..42d82c5 100644 --- a/core/README.md +++ b/core/README.md @@ -165,7 +165,7 @@ Note that since KAU depends on [ANKO](https://github.com/Kotlin/anko), all of th ### ViewUtils > Extends View -* `visible()`, `invisible()`, `gone()`, `isVisible()`, `isInvisible()`, `isGone()` methods +* `visible()`, `invisible()`, `gone()`, `isVisible`, `isInvisible()`, `isGone()` methods * matchParent method to set the layout params to match_parent * Create snackbar directly * Set IIcon into ImageView directly 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 diff --git a/docs/Changelog.md b/docs/Changelog.md index d458c9d..75fc1d0 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -1,5 +1,7 @@ # Changelog +## v2.1 + ## v2.0 * Huge refactoring to separate functions to their own submodules * Huge Docs update diff --git a/sample/src/main/res/xml/changelog.xml b/sample/src/main/res/xml/changelog.xml index f460ac1..d9bebfc 100644 --- a/sample/src/main/res/xml/changelog.xml +++ b/sample/src/main/res/xml/changelog.xml @@ -6,6 +6,14 @@ <item text="" /> --> + <version title="v2.1"/> + <item text="" /> + <item text="" /> + <item text="" /> + <item text="" /> + <item text="" /> + <item text="" /> + <version title="v2.0"/> <item text="Huge refactoring to separate functions to their own submodules" /> <item text="Huge Docs update" /> @@ -14,9 +22,6 @@ <item text="Open all kpref item binders so they may be extended" /> <item text="Fix scrolling issue on about dismiss" /> <item text="Make rClass optional in about activity" /> - <item text="" /> - <item text="" /> - <item text="" /> <version title="v1.5"/> <item text="Change snackbar builder" /> diff --git a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt index 2178806..0399883 100644 --- a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt +++ b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt @@ -218,7 +218,7 @@ class SearchView @JvmOverloads constructor( val adapter = FastItemAdapter<SearchItem>() var menuItem: MenuItem? = null val isOpen: Boolean - get() = card.isVisible() + get() = card.isVisible /* * Ripple start points and search view offset |