diff options
author | Allan Wang <me@allanwang.ca> | 2017-06-10 17:44:45 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2017-06-10 17:44:45 -0700 |
commit | b0860dafa6ebaa2d1e8a8087058573db914a5182 (patch) | |
tree | 9b4b85510a50913f734c59051787c4d4829da847 | |
parent | 8b7f8a75a0a44f3d60e8432fe9f857e6c03a017b (diff) | |
parent | 4c81c599b0c1c8893af25dd8cd51b6428389e910 (diff) | |
download | kau-b0860dafa6ebaa2d1e8a8087058573db914a5182.tar.gz kau-b0860dafa6ebaa2d1e8a8087058573db914a5182.tar.bz2 kau-b0860dafa6ebaa2d1e8a8087058573db914a5182.zip |
Merge
-rw-r--r-- | README.md | 124 | ||||
-rw-r--r-- | library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerPreference.kt | 72 |
2 files changed, 119 insertions, 77 deletions
@@ -4,16 +4,130 @@ Kotlin Android Utils This library contains small helper functions used throughout almost all of my other projects. The goal is to make common interactions executable in a single line. -## Features +# Features +* [KPrefs](#kprefs) +* [Changelog XML](#changelog) +* [Ripple Canvas](#ripple-canvas) +* [Timber Logger](#timber-logger) +* [Extensions](#extensions) +<a name="kprefs"></a> +## KPrefs + +A typical SharedPreference contains items that look like so: + +```Java +class MyPrefs { + public static final String TEXT_COLOR = "TEXT_COLOR"; + + private static SharedPreference prefs = ... + + public static void setTextColor(int color) { + prefs.edit().putInt(TEXT_COLOR, color).apply(); + } + + public static int getTextColor() { + prefs.getInt(TEXT_COLOR, Color.WHITE); + } +} +``` + +KPrefs greatly simplifies it by using Kotlin's object pattern and delegates to achieve the following: + +```Kotlin +object MyPrefs : KPref() { + var textColor: Int by kpref("TEXT_COLOR", Color.WHITE) + var bgColor: Int by kpref("BG_COLOR", Color.BLACK) + var isFirstLaunch: Boolean by kpref("IS_FIRST_LAUNCH", true) + ... +} +``` + +By using `KPrefSample.textColor = Color.RED` or `textView.setTextColor(KPrefSample.textColor)` we can effectively set and save the value. + +The values are retrieved lazily and are only done so once; future retrievals will be done with a local value, and updating a preference will both save it in the SharedPreference and update it locally. + +The object inherits the initializer method `fun initialize(c: Context, preferenceName: String)`, which must be invoked (preferably in the Application class) before using a preference. + +There is also a `reset()` method to clear the local values and have them retrieve from the SharedPreference again + +<a name="changelog"></a> +## Changelog XML + +Create an xml resource with the following structure: + +```xml +<?xml version="1.0" encoding="utf-8"?> +<resources> + <version title="v0.1" /> + <item text="Initial Changelog" /> + <item text="Bullet point here" /> + <item text="More points" /> + <item text="" /> <!-- this one is empty and therefore ignored --> +</resources> +``` + +And show it with `context.showChangelog(@XmlRes xmlRes: Int)` + +As mentioned, blank items will be ignored, so feel free to create a bunch of empty lines to facilitate updating the items in the future. + +<a name="ripple-canvas"></a> +## Ripple Canvas + +Ripple canvas provides a way to create simultaneous ripples against a background color. They can be used as transitions, or as a toolbar background to replicate the look for [Google Calendar](https://stackoverflow.com/questions/27872324/how-can-i-animate-the-color-change-of-the-statusbar-and-toolbar-like-the-new-ca) + +<a name="timber-logger"></a> +## Timber Logger + +[Timber](https://github.com/JakeWharton/timber)'s DebugTree uses the tag to specify the current class that is being logged. To add the tag directly in the message, create an object that extends the TimberLogger class with the tag name as the argument. + +<a name="extensions"></a> ## Extension Functions -<sup><sub>"[Extensions](https://kotlinlang.org/docs/reference/extensions.html) provides the ability to extend a class with new functionality without having to inherit from the class"</sub></sup> +> "[Extensions](https://kotlinlang.org/docs/reference/extensions.html) provide the ability to extend a class with new functionality without having to inherit from the class" -### AnimUtils - Extensions for Views -Extends Views +### AnimUtils +> Extends View * Fade In/Fade Out/Circle Reveal with callbacks * Switch texts in a TextView with a fade transition -### ColorUtils - Extensions for Int +### ColorUtils +> Extends Int +* Check if color is dark or light +* Check if color is visible against another color +* Convert color to HSV (float[]) or hex (String) +* Get the disabled color of a theme +* Adjust alpha +> Extends String +* Parses color; adds onto the original parser by supporting #AAA values +> Extends View +* Various tinting for different views; taken from [MDTintHelper](https://github.com/afollestad/material-dialogs/blob/master/core/src/main/java/com/afollestad/materialdialogs/internal/MDTintHelper.java) + +### ContextUtils +> Extends Activity +* Restart an activity +> Extends Context +* Create a toast directly +* Get resource values through `.color(id)`, `.dimen(id)`, `.drawable(id)`, `.integer(id)`, `.string(id)` +* Get attribute values through resolve methods +* Show a Changelog by parsing an xml resource +* Check if network is available + +### FragmentUtils +> Extends Fragment +* Directly put extras into a fragment; if a bundle does not exist, it will be created + +### IIconUtils +> Extends [IIcon](https://github.com/mikepenz/Android-Iconics) +* toDrawable method that only requires a context; defaults to a white icon of size 24dp and uses a ColorStateList to allow for dimming + +### Utils [Misc] +> Extends Int +* dpToPx & pxToDp conversions +### ViewUtils +> Extends View +* `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
\ No newline at end of file diff --git a/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerPreference.kt b/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerPreference.kt deleted file mode 100644 index 043e287..0000000 --- a/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerPreference.kt +++ /dev/null @@ -1,72 +0,0 @@ -package ca.allanwang.kau.dialogs.color - -import android.annotation.SuppressLint -import android.content.Context -import android.graphics.Color -import android.os.Build -import android.os.Bundle -import android.os.Parcelable -import android.support.annotation.ColorInt -import android.support.annotation.StringRes -import android.support.v4.content.res.ResourcesCompat -import android.support.v7.app.AppCompatActivity -import android.support.v7.preference.Preference -import android.text.Editable -import android.text.TextWatcher -import android.util.AttributeSet -import android.view.View -import android.view.ViewGroup -import android.widget.* -import ca.allanwang.kau.R -import ca.allanwang.kau.utils.ANDROID_NAMESPACE -import ca.allanwang.kau.utils.integer -import ca.allanwang.kau.utils.resolveColor -import ca.allanwang.kau.utils.toColor -import com.afollestad.materialdialogs.DialogAction -import com.afollestad.materialdialogs.MaterialDialog -import com.afollestad.materialdialogs.color.CircleView -import com.afollestad.materialdialogs.color.ColorChooserDialog -import com.afollestad.materialdialogs.internal.MDTintHelper -import com.afollestad.materialdialogs.util.DialogUtils -import java.util.* - -/** - * Created by Allan Wang on 2017-06-08. - */ -class ColorPickerPreference @JvmOverloads constructor( - context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0 -) : Preference(context, attrs, defStyleAttr, defStyleRes), Preference.OnPreferenceClickListener { - - var defaultColor: Int = Color.BLACK - var currentColor: Int - var accentMode = false - var dialogTitle: Int = 0 - - init { - onPreferenceClickListener = this - if (attrs != null) { - val defaultValue = attrs.getAttributeValue(ANDROID_NAMESPACE, "defaultValue") - if (defaultValue?.startsWith("#") ?: false) { - try { - defaultColor = defaultValue.toColor() - } catch (e: IllegalArgumentException) { - throw IllegalArgumentException("ColorPickerPreference $key has a default value that is not a color resource: $defaultValue") - } - } else { - val resourceId = attrs.getAttributeResourceValue(ANDROID_NAMESPACE, "defaultValue", 0) - if (resourceId != 0) - defaultColor = context.integer(resourceId) - else - throw IllegalArgumentException("ColorPickerPreference $key has a default value that is not a color resource: $defaultValue") - } - } - currentColor = getPersistedInt(defaultColor) - } - - override fun onPreferenceClick(preference: Preference): Boolean { - context.colorPickerDialog { - - }.show() - return true - } -}
\ No newline at end of file |