diff options
Diffstat (limited to 'core/src/main/kotlin/ca/allanwang/kau/utils')
20 files changed, 646 insertions, 287 deletions
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 eab9536..a655e5b 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ @file:Suppress("NOTHING_TO_INLINE") package ca.allanwang.kau.utils @@ -36,19 +51,22 @@ annotation class KauActivity */ @Suppress("DEPRECATION") inline fun <reified T : Activity> Activity.startActivityForResult( - requestCode: Int, - bundleBuilder: Bundle.() -> Unit = {}, - intentBuilder: Intent.() -> Unit = {} + requestCode: Int, + bundleBuilder: Bundle.() -> Unit = {}, + intentBuilder: Intent.() -> Unit = {} ) = startActivityForResult(T::class.java, requestCode, bundleBuilder, intentBuilder) -@Deprecated("Use reified generic instead of passing class", - ReplaceWith("startActivityForResult<T>(requestCode, bundleBuilder, intentBuilder)"), - DeprecationLevel.WARNING) +@Deprecated( + "Use reified generic instead of passing class", + ReplaceWith("startActivityForResult<T>(requestCode, bundleBuilder, intentBuilder)"), + DeprecationLevel.WARNING +) inline fun <T : Activity> Activity.startActivityForResult( - clazz: Class<T>, - requestCode: Int, - bundleBuilder: Bundle.() -> Unit = {}, - intentBuilder: Intent.() -> Unit = {}) { + clazz: Class<T>, + requestCode: Int, + bundleBuilder: Bundle.() -> Unit = {}, + intentBuilder: Intent.() -> Unit = {} +) { val intent = Intent(this, clazz) intent.intentBuilder() val bundle = Bundle() @@ -72,7 +90,6 @@ inline fun Activity.restart(intentBuilder: Intent.() -> Unit = {}) { overridePendingTransition(R.anim.kau_fade_in, R.anim.kau_fade_out) } - /** * Force restart an entire application */ @@ -119,8 +136,8 @@ inline var Activity.statusBarLight: Boolean 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() + if (value) flags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR + else flags and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv() } } @@ -143,6 +160,14 @@ inline fun Activity.showKeyboard() { currentFocus?.showKeyboard() } -inline fun Activity.snackbar(text: String, duration: Int = Snackbar.LENGTH_LONG, noinline builder: Snackbar.() -> Unit = {}) = contentView!!.snackbar(text, duration, builder) - -inline fun Activity.snackbar(@StringRes textId: Int, duration: Int = Snackbar.LENGTH_LONG, noinline builder: Snackbar.() -> Unit = {}) = contentView!!.snackbar(textId, duration, builder)
\ No newline at end of file +inline fun Activity.snackbar( + text: String, + duration: Int = Snackbar.LENGTH_LONG, + noinline builder: Snackbar.() -> Unit = {} +) = contentView!!.snackbar(text, duration, builder) + +inline fun Activity.snackbar( + @StringRes textId: Int, + duration: Int = Snackbar.LENGTH_LONG, + noinline builder: Snackbar.() -> Unit = {} +) = contentView!!.snackbar(textId, duration, builder) diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/AnimHolder.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/AnimHolder.kt index b988085..0062361 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/AnimHolder.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/AnimHolder.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.utils import android.os.Build @@ -14,5 +29,4 @@ object AnimHolder { @RequiresApi(Build.VERSION_CODES.LOLLIPOP) val fastOutSlowInInterpolator = lazyInterpolator(android.R.interpolator.fast_out_linear_in) val decelerateInterpolator = lazyInterpolator(android.R.interpolator.decelerate_cubic) - -}
\ No newline at end of file +} 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 d8e4681..0a548ce 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/AnimUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/AnimUtils.kt @@ -1,15 +1,30 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.utils import android.animation.Animator import android.animation.AnimatorListenerAdapter import android.annotation.SuppressLint -import androidx.annotation.StringRes import android.view.View import android.view.ViewAnimationUtils import android.view.ViewPropertyAnimator import android.view.animation.Animation import android.view.animation.AnimationUtils import android.widget.TextView +import androidx.annotation.StringRes /** * Created by Allan Wang on 2017-06-01. @@ -19,7 +34,15 @@ import android.widget.TextView @SuppressLint("NewApi") @KauUtils -fun View.circularReveal(x: Int = 0, y: Int = 0, offset: Long = 0L, radius: Float = -1.0f, duration: Long = 500L, onStart: (() -> Unit)? = null, onFinish: (() -> Unit)? = null) { +fun View.circularReveal( + x: Int = 0, + y: Int = 0, + offset: Long = 0L, + radius: Float = -1.0f, + duration: Long = 500L, + onStart: (() -> Unit)? = null, + onFinish: (() -> Unit)? = null +) { if (!isAttachedToWindow) { onStart?.invoke() visible() @@ -29,7 +52,10 @@ fun View.circularReveal(x: Int = 0, y: Int = 0, offset: Long = 0L, radius: Float if (!buildIsLollipopAndUp) return fadeIn(offset, duration, onStart, onFinish) val r = if (radius >= 0) radius - else Math.max(Math.hypot(x.toDouble(), y.toDouble()), Math.hypot((width - x.toDouble()), (height - y.toDouble()))).toFloat() + else Math.max( + Math.hypot(x.toDouble(), y.toDouble()), + Math.hypot((width - x.toDouble()), (height - y.toDouble())) + ).toFloat() val anim = ViewAnimationUtils.createCircularReveal(this, x, y, 0f, r).setDuration(duration) anim.startDelay = offset @@ -47,7 +73,15 @@ fun View.circularReveal(x: Int = 0, y: Int = 0, offset: Long = 0L, radius: Float @SuppressLint("NewApi") @KauUtils -fun View.circularHide(x: Int = 0, y: Int = 0, offset: Long = 0L, radius: Float = -1.0f, duration: Long = 500L, onStart: (() -> Unit)? = null, onFinish: (() -> Unit)? = null) { +fun View.circularHide( + x: Int = 0, + y: Int = 0, + offset: Long = 0L, + radius: Float = -1.0f, + duration: Long = 500L, + onStart: (() -> Unit)? = null, + onFinish: (() -> Unit)? = null +) { if (!isAttachedToWindow) { onStart?.invoke() invisible() @@ -57,7 +91,10 @@ fun View.circularHide(x: Int = 0, y: Int = 0, offset: Long = 0L, radius: Float = if (!buildIsLollipopAndUp) return fadeOut(offset, duration, onStart, onFinish) val r = if (radius >= 0) radius - else Math.max(Math.hypot(x.toDouble(), y.toDouble()), Math.hypot((width - x.toDouble()), (height - y.toDouble()))).toFloat() + else Math.max( + Math.hypot(x.toDouble(), y.toDouble()), + Math.hypot((width - x.toDouble()), (height - y.toDouble())) + ).toFloat() val anim = ViewAnimationUtils.createCircularReveal(this, x, y, r, 0f).setDuration(duration) anim.startDelay = offset @@ -75,7 +112,12 @@ fun View.circularHide(x: Int = 0, y: Int = 0, offset: Long = 0L, radius: Float = } @KauUtils -fun View.fadeIn(offset: Long = 0L, duration: Long = 200L, onStart: (() -> Unit)? = null, onFinish: (() -> Unit)? = null) { +fun View.fadeIn( + offset: Long = 0L, + duration: Long = 200L, + onStart: (() -> Unit)? = null, + onFinish: (() -> Unit)? = null +) { if (!isAttachedToWindow) { onStart?.invoke() visible() @@ -97,7 +139,12 @@ fun View.fadeIn(offset: Long = 0L, duration: Long = 200L, onStart: (() -> Unit)? } @KauUtils -fun View.fadeOut(offset: Long = 0L, duration: Long = 200L, onStart: (() -> Unit)? = null, onFinish: (() -> Unit)? = null) { +fun View.fadeOut( + offset: Long = 0L, + duration: Long = 200L, + onStart: (() -> Unit)? = null, + onFinish: (() -> Unit)? = null +) { if (!isAttachedToWindow) { onStart?.invoke() invisible() @@ -130,7 +177,8 @@ fun TextView.setTextWithFade(text: String, duration: Long = 200, onFinish: (() - } @KauUtils -fun TextView.setTextWithFade(@StringRes textId: Int, duration: Long = 200, onFinish: (() -> Unit)? = null) = setTextWithFade(context.getString(textId), duration, onFinish) +fun TextView.setTextWithFade(@StringRes textId: Int, duration: Long = 200, onFinish: (() -> Unit)? = null) = + setTextWithFade(context.getString(textId), duration, onFinish) @KauUtils -fun ViewPropertyAnimator.scaleXY(value: Float) = scaleX(value).scaleY(value)
\ No newline at end of file +fun ViewPropertyAnimator.scaleXY(value: Float) = scaleX(value).scaleY(value) diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/BundleUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/BundleUtils.kt index d628214..314ca60 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/BundleUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/BundleUtils.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.utils import android.annotation.SuppressLint @@ -5,9 +20,9 @@ import android.app.Activity import android.app.ActivityOptions import android.content.Context import android.os.Bundle -import androidx.annotation.AnimRes import android.util.Pair import android.view.View +import androidx.annotation.AnimRes import ca.allanwang.kau.R /** @@ -36,9 +51,9 @@ fun Bundle.withSceneTransitionAnimation(context: Context) { * create a scene transition animation */ fun Bundle.withSceneTransitionAnimation(parent: View, data: Map<Int, String>) = - withSceneTransitionAnimation(parent.context, data.mapKeys { (id, _) -> - parent.findViewById<View>(id) - }) + withSceneTransitionAnimation(parent.context, data.mapKeys { (id, _) -> + parent.findViewById<View>(id) + }) /** * Given a mapping of views to tags, @@ -48,22 +63,33 @@ fun Bundle.withSceneTransitionAnimation(parent: View, data: Map<Int, String>) = fun Bundle.withSceneTransitionAnimation(context: Context, data: Map<View, String>) { if (context !is Activity || !buildIsLollipopAndUp) return val options = ActivityOptions.makeSceneTransitionAnimation(context, - *data.map { (view, tag) -> Pair(view, tag) }.toTypedArray()) + *data.map { (view, tag) -> Pair(view, tag) }.toTypedArray() + ) putAll(options.toBundle()) } -fun Bundle.withCustomAnimation(context: Context, - @AnimRes enterResId: Int, - @AnimRes exitResId: Int) { - this with ActivityOptions.makeCustomAnimation(context, - enterResId, exitResId).toBundle() +fun Bundle.withCustomAnimation( + context: Context, + @AnimRes enterResId: Int, + @AnimRes exitResId: Int +) { + this with ActivityOptions.makeCustomAnimation( + context, + enterResId, exitResId + ).toBundle() } -fun Bundle.withSlideIn(context: Context) = withCustomAnimation(context, - R.anim.kau_slide_in_right, R.anim.kau_fade_out) +fun Bundle.withSlideIn(context: Context) = withCustomAnimation( + context, + R.anim.kau_slide_in_right, R.anim.kau_fade_out +) -fun Bundle.withSlideOut(context: Context) = withCustomAnimation(context, - R.anim.kau_fade_in, R.anim.kau_slide_out_right_top) +fun Bundle.withSlideOut(context: Context) = withCustomAnimation( + context, + R.anim.kau_fade_in, R.anim.kau_slide_out_right_top +) -fun Bundle.withFade(context: Context) = withCustomAnimation(context, - android.R.anim.fade_in, android.R.anim.fade_out)
\ No newline at end of file +fun Bundle.withFade(context: Context) = withCustomAnimation( + context, + android.R.anim.fade_in, android.R.anim.fade_out +) 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 f010c6f..9e1832f 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.utils import android.annotation.SuppressLint @@ -7,15 +22,21 @@ import android.graphics.Color import android.graphics.PorterDuff import android.graphics.drawable.Drawable import android.os.Build +import android.widget.CheckBox +import android.widget.EditText +import android.widget.ImageButton +import android.widget.ProgressBar +import android.widget.RadioButton +import android.widget.SeekBar +import android.widget.TextView import androidx.annotation.ColorInt import androidx.annotation.FloatRange import androidx.annotation.IntRange -import androidx.core.graphics.drawable.DrawableCompat import androidx.appcompat.widget.AppCompatEditText import androidx.appcompat.widget.Toolbar -import android.widget.* +import androidx.core.graphics.drawable.DrawableCompat import com.afollestad.materialdialogs.R -import java.util.* +import java.util.Random /** * Created by Allan Wang on 2017-06-08. @@ -38,7 +59,7 @@ 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 + ((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) @@ -46,7 +67,8 @@ fun Int.toHexString(withAlpha: Boolean = false, withHexPrefix: Boolean = true): return if (withHexPrefix) hex else hex.substring(1) } -fun Int.toRgbaString(): String = "rgba(${Color.red(this)}, ${Color.green(this)}, ${Color.blue(this)}, ${(Color.alpha(this) / 255f).round(3)})" +fun Int.toRgbaString(): String = + "rgba(${Color.red(this)}, ${Color.green(this)}, ${Color.blue(this)}, ${(Color.alpha(this) / 255f).round(3)})" fun Int.toHSV(): FloatArray { val hsv = FloatArray(3) @@ -59,13 +81,15 @@ inline val Int.isColorOpaque: Boolean fun FloatArray.toColor(): Int = Color.HSVToColor(this) -fun Int.isColorVisibleOn(@ColorInt color: Int, @IntRange(from = 0L, to = 255L) delta: Int = 25, - @IntRange(from = 0L, to = 255L) minAlpha: Int = 50): Boolean = - if (Color.alpha(this) < minAlpha) false - else !(Math.abs(Color.red(this) - Color.red(color)) < delta - && Math.abs(Color.green(this) - Color.green(color)) < delta - && Math.abs(Color.blue(this) - Color.blue(color)) < delta) - +fun Int.isColorVisibleOn( + @ColorInt color: Int, + @IntRange(from = 0L, to = 255L) delta: Int = 25, + @IntRange(from = 0L, to = 255L) minAlpha: Int = 50 +): Boolean = + if (Color.alpha(this) < minAlpha) false + else !(Math.abs(Color.red(this) - Color.red(color)) < delta && + Math.abs(Color.green(this) - Color.green(color)) < delta && + Math.abs(Color.blue(this) - Color.blue(color)) < delta) @ColorInt fun Context.getDisabledColor(): Int { @@ -94,30 +118,34 @@ fun Int.blendWith(@ColorInt color: Int, @FloatRange(from = 0.0, to = 1.0) ratio: } @ColorInt -fun Int.withAlpha(@IntRange(from = 0L, to = 255L) alpha: Int): Int = Color.argb(alpha, Color.red(this), Color.green(this), Color.blue(this)) +fun Int.withAlpha(@IntRange(from = 0L, to = 255L) alpha: Int): Int = + Color.argb(alpha, Color.red(this), Color.green(this), Color.blue(this)) @ColorInt -fun Int.withMinAlpha(@IntRange(from = 0L, to = 255L) alpha: Int): Int = Color.argb(Math.max(alpha, Color.alpha(this)), Color.red(this), Color.green(this), Color.blue(this)) +fun Int.withMinAlpha(@IntRange(from = 0L, to = 255L) alpha: Int): Int = + Color.argb(Math.max(alpha, Color.alpha(this)), Color.red(this), Color.green(this), Color.blue(this)) @ColorInt fun Int.lighten(@FloatRange(from = 0.0, to = 1.0) factor: Float = 0.1f): Int { val (red, green, blue) = intArrayOf(Color.red(this), Color.green(this), Color.blue(this)) - .map { (it * (1f - factor) + 255f * factor).toInt() } + .map { (it * (1f - factor) + 255f * factor).toInt() } return Color.argb(Color.alpha(this), red, green, blue) } @ColorInt fun Int.darken(@FloatRange(from = 0.0, to = 1.0) factor: Float = 0.1f): Int { val (red, green, blue) = intArrayOf(Color.red(this), Color.green(this), Color.blue(this)) - .map { (it * (1f - factor)).toInt() } + .map { (it * (1f - factor)).toInt() } return Color.argb(Color.alpha(this), red, green, blue) } @ColorInt -fun Int.colorToBackground(@FloatRange(from = 0.0, to = 1.0) factor: Float = 0.1f): Int = if (isColorDark) darken(factor) else lighten(factor) +fun Int.colorToBackground(@FloatRange(from = 0.0, to = 1.0) factor: Float = 0.1f): Int = + if (isColorDark) darken(factor) else lighten(factor) @ColorInt -fun Int.colorToForeground(@FloatRange(from = 0.0, to = 1.0) factor: Float = 0.1f): Int = if (isColorDark) lighten(factor) else darken(factor) +fun Int.colorToForeground(@FloatRange(from = 0.0, to = 1.0) factor: Float = 0.1f): Int = + if (isColorDark) lighten(factor) else darken(factor) @Throws(IllegalArgumentException::class) fun String.toColor(): Int { @@ -132,11 +160,15 @@ fun String.toColor(): Int { //Get ColorStateList fun Context.colorStateList(@ColorInt color: Int): ColorStateList { val disabledColor = color.adjustAlpha(0.3f) - return ColorStateList(arrayOf(intArrayOf(android.R.attr.state_enabled, -android.R.attr.state_checked), + return ColorStateList( + arrayOf( + intArrayOf(android.R.attr.state_enabled, -android.R.attr.state_checked), intArrayOf(android.R.attr.state_enabled, android.R.attr.state_checked), intArrayOf(-android.R.attr.state_enabled, -android.R.attr.state_checked), - intArrayOf(-android.R.attr.state_enabled, android.R.attr.state_checked)), - intArrayOf(color.adjustAlpha(0.8f), color, disabledColor, disabledColor)) + intArrayOf(-android.R.attr.state_enabled, android.R.attr.state_checked) + ), + intArrayOf(color.adjustAlpha(0.8f), color, disabledColor, disabledColor) + ) } /* @@ -203,14 +235,14 @@ fun ProgressBar.tint(@ColorInt color: Int, skipIndeterminate: Boolean = false) { fun Context.textColorStateList(@ColorInt color: Int): ColorStateList { val states = arrayOf( - intArrayOf(-android.R.attr.state_enabled), - intArrayOf(-android.R.attr.state_pressed, -android.R.attr.state_focused), - intArrayOf() + intArrayOf(-android.R.attr.state_enabled), + intArrayOf(-android.R.attr.state_pressed, -android.R.attr.state_focused), + intArrayOf() ) val colors = intArrayOf( - resolveColor(R.attr.colorControlNormal), - resolveColor(R.attr.colorControlNormal), - color + resolveColor(R.attr.colorControlNormal), + resolveColor(R.attr.colorControlNormal), + color ) return ColorStateList(states, colors) } @@ -254,4 +286,4 @@ fun Toolbar.tint(@ColorInt color: Int, tintTitle: Boolean = true) { setSubtitleTextColor(color) } (0 until childCount).asSequence().forEach { (getChildAt(it) as? ImageButton)?.setColorFilter(color) } -}
\ No newline at end of file +} 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 eb09093..9dca16c 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/Const.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/Const.kt @@ -1,8 +1,22 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.utils import androidx.customview.widget.ViewDragHelper - /** * Created by Allan Wang on 2017-06-08. */ @@ -21,4 +35,4 @@ const val KAU_COLLAPSING = 1 const val KAU_EXPANDING = 2 const val KAU_EXPANDED = 3 -const val KAU_ELLIPSIS = '\u2026'
\ No newline at end of file +const val KAU_ELLIPSIS = '\u2026' diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt index f6e9ac7..134126d 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ @file:Suppress("NOTHING_TO_INLINE") package ca.allanwang.kau.utils @@ -11,17 +26,26 @@ import android.content.pm.PackageManager import android.graphics.drawable.Drawable import android.net.Uri import android.os.Bundle -import androidx.annotation.* -import androidx.core.content.ContextCompat import android.util.TypedValue import android.view.View import android.view.animation.AnimationUtils import android.widget.Toast +import androidx.annotation.AnimRes +import androidx.annotation.AttrRes +import androidx.annotation.BoolRes +import androidx.annotation.ColorInt +import androidx.annotation.ColorRes +import androidx.annotation.DimenRes +import androidx.annotation.DrawableRes +import androidx.annotation.IntegerRes +import androidx.annotation.InterpolatorRes +import androidx.annotation.PluralsRes +import androidx.annotation.StringRes +import androidx.core.content.ContextCompat import ca.allanwang.kau.R import ca.allanwang.kau.logging.KL import com.afollestad.materialdialogs.MaterialDialog - /** * Created by Allan Wang on 2017-06-03. */ @@ -33,19 +57,22 @@ import com.afollestad.materialdialogs.MaterialDialog */ @Suppress("DEPRECATION") inline fun <reified T : Activity> Context.startActivity( - clearStack: Boolean = false, - bundleBuilder: Bundle.() -> Unit = {}, - intentBuilder: Intent.() -> Unit = {} + clearStack: Boolean = false, + bundleBuilder: Bundle.() -> Unit = {}, + intentBuilder: Intent.() -> Unit = {} ) = startActivity(T::class.java, clearStack, bundleBuilder, intentBuilder) -@Deprecated("Use reified generic instead of passing class", - ReplaceWith("startActivity<T>(clearStack, bundleBuilder, intentBuilder)"), - DeprecationLevel.WARNING) +@Deprecated( + "Use reified generic instead of passing class", + ReplaceWith("startActivity<T>(clearStack, bundleBuilder, intentBuilder)"), + DeprecationLevel.WARNING +) inline fun <T : Activity> Context.startActivity( - clazz: Class<T>, - clearStack: Boolean = false, - bundleBuilder: Bundle.() -> Unit = {}, - intentBuilder: Intent.() -> Unit = {}) { + clazz: Class<T>, + clearStack: Boolean = false, + bundleBuilder: Bundle.() -> Unit = {}, + intentBuilder: Intent.() -> Unit = {} +) { val intent = Intent(this, clazz) if (clearStack) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK) intent.intentBuilder() @@ -55,7 +82,6 @@ inline fun <T : Activity> Context.startActivity( if (clearStack && this is Activity) finish() } - fun Context.startPlayStoreLink(@StringRes packageIdRes: Int) = startPlayStoreLink(string(packageIdRes)) fun Context.startPlayStoreLink(packageId: String) { @@ -82,11 +108,14 @@ fun Context.startLink(vararg url: String?) { fun Context.startLink(@StringRes url: Int) = startLink(string(url)) //Toast helpers -inline fun View.toast(@StringRes id: Int, duration: Int = Toast.LENGTH_LONG, log: Boolean = false) = context.toast(id, duration, log) +inline fun View.toast(@StringRes id: Int, duration: Int = Toast.LENGTH_LONG, log: Boolean = false) = + context.toast(id, duration, log) -inline fun Context.toast(@StringRes id: Int, duration: Int = Toast.LENGTH_LONG, log: Boolean = false) = toast(this.string(id), duration, log) +inline fun Context.toast(@StringRes id: Int, duration: Int = Toast.LENGTH_LONG, log: Boolean = false) = + toast(this.string(id), duration, log) -inline fun View.toast(text: String, duration: Int = Toast.LENGTH_LONG, log: Boolean = false) = context.toast(text, duration, log) +inline fun View.toast(text: String, duration: Int = Toast.LENGTH_LONG, log: Boolean = false) = + context.toast(text, duration, log) inline fun Context.toast(text: String, duration: Int = Toast.LENGTH_LONG, log: Boolean = false) { Toast.makeText(this, text, duration).show() @@ -98,24 +127,33 @@ const val INVALID_ID = 0 //Resource retrievers inline fun Context.string(@StringRes id: Int): String = getString(id) -inline fun Context.string(@StringRes id: Int, fallback: String?): String? = if (id != INVALID_ID) string(id) else fallback -inline fun Context.string(@StringRes id: Int, fallback: () -> String?): String? = if (id != INVALID_ID) string(id) else fallback() +inline fun Context.string(@StringRes id: Int, fallback: String?): String? = + if (id != INVALID_ID) string(id) else fallback + +inline fun Context.string(@StringRes id: Int, fallback: () -> String?): String? = + if (id != INVALID_ID) string(id) else fallback() + inline fun Context.color(@ColorRes id: Int): Int = ContextCompat.getColor(this, id) inline fun Context.boolean(@BoolRes id: Int): Boolean = resources.getBoolean(id) inline fun Context.integer(@IntegerRes id: Int): Int = resources.getInteger(id) inline fun Context.dimen(@DimenRes id: Int): Float = resources.getDimension(id) inline fun Context.dimenPixelSize(@DimenRes id: Int): Int = resources.getDimensionPixelSize(id) inline fun Context.drawable(@DrawableRes id: Int): Drawable = ContextCompat.getDrawable(this, id) - ?: throw KauException("Drawable with id $id not found") + ?: throw KauException("Drawable with id $id not found") + +inline fun Context.drawable(@DrawableRes id: Int, fallback: Drawable?): Drawable? = + if (id != INVALID_ID) drawable(id) else fallback + +inline fun Context.drawable(@DrawableRes id: Int, fallback: () -> Drawable?): Drawable? = + if (id != INVALID_ID) drawable(id) else fallback() -inline fun Context.drawable(@DrawableRes id: Int, fallback: Drawable?): Drawable? = if (id != INVALID_ID) drawable(id) else fallback -inline fun Context.drawable(@DrawableRes id: Int, fallback: () -> Drawable?): Drawable? = if (id != INVALID_ID) drawable(id) else fallback() inline fun Context.interpolator(@InterpolatorRes id: Int) = AnimationUtils.loadInterpolator(this, id)!! inline fun Context.animation(@AnimRes id: Int) = AnimationUtils.loadAnimation(this, id)!! /** * Returns plural form of res. The quantity is also passed to the formatter as an int */ -inline fun Context.plural(@PluralsRes id: Int, quantity: Number) = resources.getQuantityString(id, quantity.toInt(), quantity.toInt())!! +inline fun Context.plural(@PluralsRes id: Int, quantity: Number) = + resources.getQuantityString(id, quantity.toInt(), quantity.toInt())!! //Attr retrievers fun Context.resolveColor(@AttrRes attr: Int, @ColorInt fallback: Int = 0): Int { @@ -164,7 +202,8 @@ inline fun Context.materialDialog(action: MaterialDialog.Builder.() -> Unit): Ma return builder.show() } -fun Context.getDip(value: Float): Float = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, resources.displayMetrics) +fun Context.getDip(value: Float): Float = + TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, resources.displayMetrics) inline val Context.isRtl: Boolean get() = resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL @@ -181,7 +220,10 @@ inline val Context.isNavBarOnBottom: Boolean return !canMove || dm.widthPixels < dm.heightPixels } -fun Context.hasPermission(permissions: String) = !buildIsMarshmallowAndUp || ContextCompat.checkSelfPermission(this, permissions) == PackageManager.PERMISSION_GRANTED +fun Context.hasPermission(permissions: String) = !buildIsMarshmallowAndUp || ContextCompat.checkSelfPermission( + this, + permissions +) == PackageManager.PERMISSION_GRANTED fun Context.copyToClipboard(text: String?, label: String = "Copied Text", showToast: Boolean = true) { val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager @@ -207,4 +249,4 @@ fun Context.shareText(text: String?) { * As of now, it is only checked when tied to an activity */ inline val Context.isFinishing: Boolean - get() = (this as? Activity)?.isFinishing ?: false
\ No newline at end of file + get() = (this as? Activity)?.isFinishing ?: false diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/DrawableUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/DrawableUtils.kt index 59e684a..6950f2f 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/DrawableUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/DrawableUtils.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.utils import android.content.res.ColorStateList @@ -17,4 +32,4 @@ 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/FileUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/FileUtils.kt index bfbc009..1d27bfc 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/FileUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/FileUtils.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.utils import java.io.File @@ -6,4 +21,5 @@ import java.io.InputStream /** * Created by Allan Wang on 2017-08-04. */ -fun File.copyFromInputStream(inputStream: InputStream) = inputStream.use { input -> outputStream().use { output -> input.copyTo(output) } }
\ No newline at end of file +fun File.copyFromInputStream(inputStream: InputStream) = + inputStream.use { input -> outputStream().use { output -> input.copyTo(output) } } diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/FontUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/FontUtils.kt index 1db7694..064d70b 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/FontUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/FontUtils.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.utils import android.content.Context @@ -14,17 +29,17 @@ object FontUtils { synchronized(sTypefaceCache) { if (!sTypefaceCache.containsKey(font)) { val tf = Typeface.createFromAsset( - context.applicationContext.assets, "fonts/$font.ttf") + context.applicationContext.assets, "fonts/$font.ttf" + ) sTypefaceCache.put(font, tf) } return sTypefaceCache.get(font) - ?: throw IllegalArgumentException("Font error; typeface does not exist at assets/fonts$font.ttf") + ?: throw IllegalArgumentException("Font error; typeface does not exist at assets/fonts$font.ttf") } } fun getName(typeface: Typeface): String? = sTypefaceCache.entries.firstOrNull { it.value == typeface }?.key - } fun Context.getFont(font: String) = FontUtils.get(this, font) -fun Context.getFontName(typeface: Typeface) = FontUtils.getName(typeface)
\ No newline at end of file +fun Context.getFontName(typeface: Typeface) = FontUtils.getName(typeface) diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/FragmentUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/FragmentUtils.kt index f99b342..1c97900 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/FragmentUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/FragmentUtils.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.utils import androidx.fragment.app.Fragment @@ -9,4 +24,4 @@ import org.jetbrains.anko.bundleOf fun <T : Fragment> T.withArguments(vararg params: Pair<String, Any>): T { arguments = bundleOf(*params) return this -}
\ No newline at end of file +} diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/IIconUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/IIconUtils.kt index 66c56f8..8b40352 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/IIconUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/IIconUtils.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.utils import android.content.Context @@ -12,10 +27,15 @@ import com.mikepenz.iconics.typeface.IIcon * Created by Allan Wang on 2017-05-29. */ @KauUtils -fun IIcon.toDrawable(c: Context, sizeDp: Int = 24, @ColorInt color: Int = Color.WHITE, builder: IconicsDrawable.() -> Unit = {}): Drawable { +fun IIcon.toDrawable( + c: Context, + sizeDp: Int = 24, + @ColorInt color: Int = Color.WHITE, + builder: IconicsDrawable.() -> Unit = {} +): Drawable { val state = ColorStateList.valueOf(color) val icon = IconicsDrawable(c).icon(this).color(state) if (sizeDp > 0) icon.sizeDp(sizeDp) icon.builder() return icon -}
\ No newline at end of file +} diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt index 3cbd93d..1c22f62 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ @file:Suppress("UNCHECKED_CAST", "DEPRECATION") package ca.allanwang.kau.utils @@ -21,107 +36,76 @@ import android.app.Fragment import android.view.View import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView.ViewHolder -import java.util.* +import java.util.Collections +import java.util.WeakHashMap import kotlin.properties.ReadOnlyProperty import kotlin.reflect.KProperty import androidx.fragment.app.DialogFragment as SupportDialogFragment import androidx.fragment.app.Fragment as SupportFragment -fun <V : View> View.bindView(id: Int) - : ReadOnlyProperty<View, V> = required(id, viewFinder) +fun <V : View> View.bindView(id: Int): ReadOnlyProperty<View, V> = required(id, viewFinder) -fun <V : View> Activity.bindView(id: Int) - : ReadOnlyProperty<Activity, V> = required(id, viewFinder) +fun <V : View> Activity.bindView(id: Int): ReadOnlyProperty<Activity, V> = required(id, viewFinder) -fun <V : View> Dialog.bindView(id: Int) - : ReadOnlyProperty<Dialog, V> = required(id, viewFinder) +fun <V : View> Dialog.bindView(id: Int): ReadOnlyProperty<Dialog, V> = required(id, viewFinder) -fun <V : View> DialogFragment.bindView(id: Int) - : ReadOnlyProperty<DialogFragment, V> = required(id, viewFinder) +fun <V : View> DialogFragment.bindView(id: Int): ReadOnlyProperty<DialogFragment, V> = required(id, viewFinder) -fun <V : View> SupportDialogFragment.bindView(id: Int) - : ReadOnlyProperty<SupportDialogFragment, V> = required(id, viewFinder) +fun <V : View> SupportDialogFragment.bindView(id: Int): ReadOnlyProperty<SupportDialogFragment, V> = required(id, viewFinder) -fun <V : View> Fragment.bindView(id: Int) - : ReadOnlyProperty<Fragment, V> = required(id, viewFinder) +fun <V : View> Fragment.bindView(id: Int): ReadOnlyProperty<Fragment, V> = required(id, viewFinder) -fun <V : View> SupportFragment.bindView(id: Int) - : ReadOnlyProperty<SupportFragment, V> = required(id, viewFinder) +fun <V : View> SupportFragment.bindView(id: Int): ReadOnlyProperty<SupportFragment, V> = required(id, viewFinder) -fun <V : View> RecyclerView.ViewHolder.bindView(id: Int) - : ReadOnlyProperty<RecyclerView.ViewHolder, V> = required(id, viewFinder) +fun <V : View> RecyclerView.ViewHolder.bindView(id: Int): ReadOnlyProperty<RecyclerView.ViewHolder, V> = required(id, viewFinder) -fun <V : View> View.bindOptionalView(id: Int) - : ReadOnlyProperty<View, V?> = optional(id, viewFinder) +fun <V : View> View.bindOptionalView(id: Int): ReadOnlyProperty<View, V?> = optional(id, viewFinder) -fun <V : View> Activity.bindOptionalView(id: Int) - : ReadOnlyProperty<Activity, V?> = optional(id, viewFinder) +fun <V : View> Activity.bindOptionalView(id: Int): ReadOnlyProperty<Activity, V?> = optional(id, viewFinder) -fun <V : View> Dialog.bindOptionalView(id: Int) - : ReadOnlyProperty<Dialog, V?> = optional(id, viewFinder) +fun <V : View> Dialog.bindOptionalView(id: Int): ReadOnlyProperty<Dialog, V?> = optional(id, viewFinder) -fun <V : View> DialogFragment.bindOptionalView(id: Int) - : ReadOnlyProperty<DialogFragment, V?> = optional(id, viewFinder) +fun <V : View> DialogFragment.bindOptionalView(id: Int): ReadOnlyProperty<DialogFragment, V?> = optional(id, viewFinder) -fun <V : View> SupportDialogFragment.bindOptionalView(id: Int) - : ReadOnlyProperty<SupportDialogFragment, V?> = optional(id, viewFinder) +fun <V : View> SupportDialogFragment.bindOptionalView(id: Int): ReadOnlyProperty<SupportDialogFragment, V?> = optional(id, viewFinder) -fun <V : View> Fragment.bindOptionalView(id: Int) - : ReadOnlyProperty<Fragment, V?> = optional(id, viewFinder) +fun <V : View> Fragment.bindOptionalView(id: Int): ReadOnlyProperty<Fragment, V?> = optional(id, viewFinder) -fun <V : View> SupportFragment.bindOptionalView(id: Int) - : ReadOnlyProperty<SupportFragment, V?> = optional(id, viewFinder) +fun <V : View> SupportFragment.bindOptionalView(id: Int): ReadOnlyProperty<SupportFragment, V?> = optional(id, viewFinder) -fun <V : View> RecyclerView.ViewHolder.bindOptionalView(id: Int) - : ReadOnlyProperty<RecyclerView.ViewHolder, V?> = optional(id, viewFinder) +fun <V : View> RecyclerView.ViewHolder.bindOptionalView(id: Int): ReadOnlyProperty<RecyclerView.ViewHolder, V?> = optional(id, viewFinder) -fun <V : View> View.bindViews(vararg ids: Int) - : ReadOnlyProperty<View, List<V>> = required(ids, viewFinder) +fun <V : View> View.bindViews(vararg ids: Int): ReadOnlyProperty<View, List<V>> = required(ids, viewFinder) -fun <V : View> Activity.bindViews(vararg ids: Int) - : ReadOnlyProperty<Activity, List<V>> = required(ids, viewFinder) +fun <V : View> Activity.bindViews(vararg ids: Int): ReadOnlyProperty<Activity, List<V>> = required(ids, viewFinder) -fun <V : View> Dialog.bindViews(vararg ids: Int) - : ReadOnlyProperty<Dialog, List<V>> = required(ids, viewFinder) +fun <V : View> Dialog.bindViews(vararg ids: Int): ReadOnlyProperty<Dialog, List<V>> = required(ids, viewFinder) -fun <V : View> DialogFragment.bindViews(vararg ids: Int) - : ReadOnlyProperty<DialogFragment, List<V>> = required(ids, viewFinder) +fun <V : View> DialogFragment.bindViews(vararg ids: Int): ReadOnlyProperty<DialogFragment, List<V>> = required(ids, viewFinder) -fun <V : View> SupportDialogFragment.bindViews(vararg ids: Int) - : ReadOnlyProperty<SupportDialogFragment, List<V>> = required(ids, viewFinder) +fun <V : View> SupportDialogFragment.bindViews(vararg ids: Int): ReadOnlyProperty<SupportDialogFragment, List<V>> = required(ids, viewFinder) -fun <V : View> Fragment.bindViews(vararg ids: Int) - : ReadOnlyProperty<Fragment, List<V>> = required(ids, viewFinder) +fun <V : View> Fragment.bindViews(vararg ids: Int): ReadOnlyProperty<Fragment, List<V>> = required(ids, viewFinder) -fun <V : View> SupportFragment.bindViews(vararg ids: Int) - : ReadOnlyProperty<SupportFragment, List<V>> = required(ids, viewFinder) +fun <V : View> SupportFragment.bindViews(vararg ids: Int): ReadOnlyProperty<SupportFragment, List<V>> = required(ids, viewFinder) -fun <V : View> ViewHolder.bindViews(vararg ids: Int) - : ReadOnlyProperty<ViewHolder, List<V>> = required(ids, viewFinder) +fun <V : View> ViewHolder.bindViews(vararg ids: Int): ReadOnlyProperty<ViewHolder, List<V>> = required(ids, viewFinder) -fun <V : View> View.bindOptionalViews(vararg ids: Int) - : ReadOnlyProperty<View, List<V>> = optional(ids, viewFinder) +fun <V : View> View.bindOptionalViews(vararg ids: Int): ReadOnlyProperty<View, List<V>> = optional(ids, viewFinder) -fun <V : View> Activity.bindOptionalViews(vararg ids: Int) - : ReadOnlyProperty<Activity, List<V>> = optional(ids, viewFinder) +fun <V : View> Activity.bindOptionalViews(vararg ids: Int): ReadOnlyProperty<Activity, List<V>> = optional(ids, viewFinder) -fun <V : View> Dialog.bindOptionalViews(vararg ids: Int) - : ReadOnlyProperty<Dialog, List<V>> = optional(ids, viewFinder) +fun <V : View> Dialog.bindOptionalViews(vararg ids: Int): ReadOnlyProperty<Dialog, List<V>> = optional(ids, viewFinder) -fun <V : View> DialogFragment.bindOptionalViews(vararg ids: Int) - : ReadOnlyProperty<DialogFragment, List<V>> = optional(ids, viewFinder) +fun <V : View> DialogFragment.bindOptionalViews(vararg ids: Int): ReadOnlyProperty<DialogFragment, List<V>> = optional(ids, viewFinder) -fun <V : View> SupportDialogFragment.bindOptionalViews(vararg ids: Int) - : ReadOnlyProperty<SupportDialogFragment, List<V>> = optional(ids, viewFinder) +fun <V : View> SupportDialogFragment.bindOptionalViews(vararg ids: Int): ReadOnlyProperty<SupportDialogFragment, List<V>> = optional(ids, viewFinder) -fun <V : View> Fragment.bindOptionalViews(vararg ids: Int) - : ReadOnlyProperty<Fragment, List<V>> = optional(ids, viewFinder) +fun <V : View> Fragment.bindOptionalViews(vararg ids: Int): ReadOnlyProperty<Fragment, List<V>> = optional(ids, viewFinder) -fun <V : View> SupportFragment.bindOptionalViews(vararg ids: Int) - : ReadOnlyProperty<SupportFragment, List<V>> = optional(ids, viewFinder) +fun <V : View> SupportFragment.bindOptionalViews(vararg ids: Int): ReadOnlyProperty<SupportFragment, List<V>> = optional(ids, viewFinder) -fun <V : View> ViewHolder.bindOptionalViews(vararg ids: Int) - : ReadOnlyProperty<ViewHolder, List<V>> = optional(ids, viewFinder) +fun <V : View> ViewHolder.bindOptionalViews(vararg ids: Int): ReadOnlyProperty<ViewHolder, List<V>> = optional(ids, viewFinder) private inline val View.viewFinder: View.(Int) -> View? get() = { findViewById(it) } @@ -141,7 +125,7 @@ private inline val ViewHolder.viewFinder: ViewHolder.(Int) -> View? get() = { itemView.findViewById(it) } private fun viewNotFound(id: Int, desc: KProperty<*>): Nothing = - throw IllegalStateException("View ID $id for '${desc.name}' not found.") + throw IllegalStateException("View ID $id for '${desc.name}' not found.") private fun <T, V : View> required(id: Int, finder: T.(Int) -> View?) = Lazy { t: T, desc -> (t.finder(id) as V?)?.apply { } ?: viewNotFound(id, desc) @@ -155,19 +139,20 @@ private fun <T, V : View> required(ids: IntArray, finder: T.(Int) -> View?) = La } } -private fun <T, V : View> optional(ids: IntArray, finder: T.(Int) -> View?) = Lazy { t: T, _ -> ids.map { t.finder(it) as V? }.filterNotNull() } +private fun <T, V : View> optional(ids: IntArray, finder: T.(Int) -> View?) = + Lazy { t: T, _ -> ids.map { t.finder(it) as V? }.filterNotNull() } // Like Kotlin's lazy delegate but the initializer gets the target and metadata passed to it private open class Lazy<in T, out V>(private val initializer: (T, KProperty<*>) -> V) : ReadOnlyProperty<T, V> { protected object EMPTY -protected var value: Any? = EMPTY + protected var value: Any? = EMPTY -override fun getValue(thisRef: T, property: KProperty<*>): V { + override fun getValue(thisRef: T, property: KProperty<*>): V { if (value == EMPTY) value = initializer(thisRef, property) -return value as V + return value as V } } @@ -181,107 +166,76 @@ return value as V * Credits to <a href="https://github.com/MichaelRocks">MichaelRocks</a> */ -fun <V : View> View.bindViewResettable(id: Int) - : ReadOnlyProperty<View, V> = requiredResettable(id, viewFinder) +fun <V : View> View.bindViewResettable(id: Int): ReadOnlyProperty<View, V> = requiredResettable(id, viewFinder) -fun <V : View> Activity.bindViewResettable(id: Int) - : ReadOnlyProperty<Activity, V> = requiredResettable(id, viewFinder) +fun <V : View> Activity.bindViewResettable(id: Int): ReadOnlyProperty<Activity, V> = requiredResettable(id, viewFinder) -fun <V : View> Dialog.bindViewResettable(id: Int) - : ReadOnlyProperty<Dialog, V> = requiredResettable(id, viewFinder) +fun <V : View> Dialog.bindViewResettable(id: Int): ReadOnlyProperty<Dialog, V> = requiredResettable(id, viewFinder) -fun <V : View> DialogFragment.bindViewResettable(id: Int) - : ReadOnlyProperty<DialogFragment, V> = requiredResettable(id, viewFinder) +fun <V : View> DialogFragment.bindViewResettable(id: Int): ReadOnlyProperty<DialogFragment, V> = requiredResettable(id, viewFinder) -fun <V : View> SupportDialogFragment.bindViewResettable(id: Int) - : ReadOnlyProperty<SupportDialogFragment, V> = requiredResettable(id, viewFinder) +fun <V : View> SupportDialogFragment.bindViewResettable(id: Int): ReadOnlyProperty<SupportDialogFragment, V> = requiredResettable(id, viewFinder) -fun <V : View> Fragment.bindViewResettable(id: Int) - : ReadOnlyProperty<Fragment, V> = requiredResettable(id, viewFinder) +fun <V : View> Fragment.bindViewResettable(id: Int): ReadOnlyProperty<Fragment, V> = requiredResettable(id, viewFinder) -fun <V : View> SupportFragment.bindViewResettable(id: Int) - : ReadOnlyProperty<SupportFragment, V> = requiredResettable(id, viewFinder) +fun <V : View> SupportFragment.bindViewResettable(id: Int): ReadOnlyProperty<SupportFragment, V> = requiredResettable(id, viewFinder) -fun <V : View> ViewHolder.bindViewResettable(id: Int) - : ReadOnlyProperty<ViewHolder, V> = requiredResettable(id, viewFinder) +fun <V : View> ViewHolder.bindViewResettable(id: Int): ReadOnlyProperty<ViewHolder, V> = requiredResettable(id, viewFinder) -fun <V : View> View.bindOptionalViewResettable(id: Int) - : ReadOnlyProperty<View, V?> = optionalResettable(id, viewFinder) +fun <V : View> View.bindOptionalViewResettable(id: Int): ReadOnlyProperty<View, V?> = optionalResettable(id, viewFinder) -fun <V : View> Activity.bindOptionalViewResettable(id: Int) - : ReadOnlyProperty<Activity, V?> = optionalResettable(id, viewFinder) +fun <V : View> Activity.bindOptionalViewResettable(id: Int): ReadOnlyProperty<Activity, V?> = optionalResettable(id, viewFinder) -fun <V : View> Dialog.bindOptionalViewResettable(id: Int) - : ReadOnlyProperty<Dialog, V?> = optionalResettable(id, viewFinder) +fun <V : View> Dialog.bindOptionalViewResettable(id: Int): ReadOnlyProperty<Dialog, V?> = optionalResettable(id, viewFinder) -fun <V : View> DialogFragment.bindOptionalViewResettable(id: Int) - : ReadOnlyProperty<DialogFragment, V?> = optionalResettable(id, viewFinder) +fun <V : View> DialogFragment.bindOptionalViewResettable(id: Int): ReadOnlyProperty<DialogFragment, V?> = optionalResettable(id, viewFinder) -fun <V : View> SupportDialogFragment.bindOptionalViewResettable(id: Int) - : ReadOnlyProperty<SupportDialogFragment, V?> = optionalResettable(id, viewFinder) +fun <V : View> SupportDialogFragment.bindOptionalViewResettable(id: Int): ReadOnlyProperty<SupportDialogFragment, V?> = optionalResettable(id, viewFinder) -fun <V : View> Fragment.bindOptionalViewResettable(id: Int) - : ReadOnlyProperty<Fragment, V?> = optionalResettable(id, viewFinder) +fun <V : View> Fragment.bindOptionalViewResettable(id: Int): ReadOnlyProperty<Fragment, V?> = optionalResettable(id, viewFinder) -fun <V : View> SupportFragment.bindOptionalViewResettable(id: Int) - : ReadOnlyProperty<SupportFragment, V?> = optionalResettable(id, viewFinder) +fun <V : View> SupportFragment.bindOptionalViewResettable(id: Int): ReadOnlyProperty<SupportFragment, V?> = optionalResettable(id, viewFinder) -fun <V : View> ViewHolder.bindOptionalViewResettable(id: Int) - : ReadOnlyProperty<ViewHolder, V?> = optionalResettable(id, viewFinder) +fun <V : View> ViewHolder.bindOptionalViewResettable(id: Int): ReadOnlyProperty<ViewHolder, V?> = optionalResettable(id, viewFinder) -fun <V : View> View.bindViewsResettable(vararg ids: Int) - : ReadOnlyProperty<View, List<V>> = requiredResettable(ids, viewFinder) +fun <V : View> View.bindViewsResettable(vararg ids: Int): ReadOnlyProperty<View, List<V>> = requiredResettable(ids, viewFinder) -fun <V : View> Activity.bindViewsResettable(vararg ids: Int) - : ReadOnlyProperty<Activity, List<V>> = requiredResettable(ids, viewFinder) +fun <V : View> Activity.bindViewsResettable(vararg ids: Int): ReadOnlyProperty<Activity, List<V>> = requiredResettable(ids, viewFinder) -fun <V : View> Dialog.bindViewsResettable(vararg ids: Int) - : ReadOnlyProperty<Dialog, List<V>> = requiredResettable(ids, viewFinder) +fun <V : View> Dialog.bindViewsResettable(vararg ids: Int): ReadOnlyProperty<Dialog, List<V>> = requiredResettable(ids, viewFinder) -fun <V : View> DialogFragment.bindViewsResettable(vararg ids: Int) - : ReadOnlyProperty<DialogFragment, List<V>> = requiredResettable(ids, viewFinder) +fun <V : View> DialogFragment.bindViewsResettable(vararg ids: Int): ReadOnlyProperty<DialogFragment, List<V>> = requiredResettable(ids, viewFinder) -fun <V : View> SupportDialogFragment.bindViewsResettable(vararg ids: Int) - : ReadOnlyProperty<SupportDialogFragment, List<V>> = requiredResettable(ids, viewFinder) +fun <V : View> SupportDialogFragment.bindViewsResettable(vararg ids: Int): ReadOnlyProperty<SupportDialogFragment, List<V>> = requiredResettable(ids, viewFinder) -fun <V : View> Fragment.bindViewsResettable(vararg ids: Int) - : ReadOnlyProperty<Fragment, List<V>> = requiredResettable(ids, viewFinder) +fun <V : View> Fragment.bindViewsResettable(vararg ids: Int): ReadOnlyProperty<Fragment, List<V>> = requiredResettable(ids, viewFinder) -fun <V : View> SupportFragment.bindViewsResettable(vararg ids: Int) - : ReadOnlyProperty<SupportFragment, List<V>> = requiredResettable(ids, viewFinder) +fun <V : View> SupportFragment.bindViewsResettable(vararg ids: Int): ReadOnlyProperty<SupportFragment, List<V>> = requiredResettable(ids, viewFinder) -fun <V : View> ViewHolder.bindViewsResettable(vararg ids: Int) - : ReadOnlyProperty<ViewHolder, List<V>> = requiredResettable(ids, viewFinder) +fun <V : View> ViewHolder.bindViewsResettable(vararg ids: Int): ReadOnlyProperty<ViewHolder, List<V>> = requiredResettable(ids, viewFinder) -fun <V : View> View.bindOptionalViewsResettable(vararg ids: Int) - : ReadOnlyProperty<View, List<V>> = optionalResettable(ids, viewFinder) +fun <V : View> View.bindOptionalViewsResettable(vararg ids: Int): ReadOnlyProperty<View, List<V>> = optionalResettable(ids, viewFinder) -fun <V : View> Activity.bindOptionalViewsResettable(vararg ids: Int) - : ReadOnlyProperty<Activity, List<V>> = optionalResettable(ids, viewFinder) +fun <V : View> Activity.bindOptionalViewsResettable(vararg ids: Int): ReadOnlyProperty<Activity, List<V>> = optionalResettable(ids, viewFinder) -fun <V : View> Dialog.bindOptionalViewsResettable(vararg ids: Int) - : ReadOnlyProperty<Dialog, List<V>> = optionalResettable(ids, viewFinder) +fun <V : View> Dialog.bindOptionalViewsResettable(vararg ids: Int): ReadOnlyProperty<Dialog, List<V>> = optionalResettable(ids, viewFinder) -fun <V : View> DialogFragment.bindOptionalViewsResettable(vararg ids: Int) - : ReadOnlyProperty<DialogFragment, List<V>> = optionalResettable(ids, viewFinder) +fun <V : View> DialogFragment.bindOptionalViewsResettable(vararg ids: Int): ReadOnlyProperty<DialogFragment, List<V>> = optionalResettable(ids, viewFinder) -fun <V : View> SupportDialogFragment.bindOptionalViewsResettable(vararg ids: Int) - : ReadOnlyProperty<SupportDialogFragment, List<V>> = optionalResettable(ids, viewFinder) +fun <V : View> SupportDialogFragment.bindOptionalViewsResettable(vararg ids: Int): ReadOnlyProperty<SupportDialogFragment, List<V>> = optionalResettable(ids, viewFinder) -fun <V : View> Fragment.bindOptionalViewsResettable(vararg ids: Int) - : ReadOnlyProperty<Fragment, List<V>> = optionalResettable(ids, viewFinder) +fun <V : View> Fragment.bindOptionalViewsResettable(vararg ids: Int): ReadOnlyProperty<Fragment, List<V>> = optionalResettable(ids, viewFinder) -fun <V : View> SupportFragment.bindOptionalViewsResettable(vararg ids: Int) - : ReadOnlyProperty<SupportFragment, List<V>> = optionalResettable(ids, viewFinder) +fun <V : View> SupportFragment.bindOptionalViewsResettable(vararg ids: Int): ReadOnlyProperty<SupportFragment, List<V>> = optionalResettable(ids, viewFinder) -fun <V : View> ViewHolder.bindOptionalViewsResettable(vararg ids: Int) - : ReadOnlyProperty<RecyclerView.ViewHolder, List<V>> = optionalResettable(ids, viewFinder) +fun <V : View> ViewHolder.bindOptionalViewsResettable(vararg ids: Int): ReadOnlyProperty<RecyclerView.ViewHolder, List<V>> = optionalResettable(ids, viewFinder) private fun <T, V : View> requiredResettable(id: Int, finder: T.(Int) -> View?) = LazyResettable { t: T, desc -> (t.finder(id) as V?)?.apply { } ?: viewNotFound(id, desc) } -private fun <T, V : View> optionalResettable(id: Int, finder: T.(Int) -> View?) = LazyResettable { t: T, _ -> t.finder(id) as V? } +private fun <T, V : View> optionalResettable(id: Int, finder: T.(Int) -> View?) = + LazyResettable { t: T, _ -> t.finder(id) as V? } private fun <T, V : View> requiredResettable(ids: IntArray, finder: T.(Int) -> View?) = LazyResettable { t: T, desc -> ids.map { @@ -289,7 +243,8 @@ private fun <T, V : View> requiredResettable(ids: IntArray, finder: T.(Int) -> V } } -private fun <T, V : View> optionalResettable(ids: IntArray, finder: T.(Int) -> View?) = LazyResettable { t: T, _ -> ids.map { t.finder(it) as V? }.filterNotNull() } +private fun <T, V : View> optionalResettable(ids: IntArray, finder: T.(Int) -> View?) = + LazyResettable { t: T, _ -> ids.map { t.finder(it) as V? }.filterNotNull() } //Like Kotterknife's lazy delegate but is resettable private class LazyResettable<in T, out V>(initializer: (T, KProperty<*>) -> V) : Lazy<T, V>(initializer) { @@ -298,7 +253,7 @@ private class LazyResettable<in T, out V>(initializer: (T, KProperty<*>) -> V) : return super.getValue(thisRef, property) } -fun reset() { + fun reset() { value = EMPTY } } @@ -312,7 +267,8 @@ object Kotterknife { private object KotterknifeRegistry { private val lazyMap = WeakHashMap<Any, MutableCollection<LazyResettable<*, *>>>() -fun register(target: Any, lazy: LazyResettable<*, *>) = lazyMap.getOrPut(target, { Collections.newSetFromMap(WeakHashMap()) }).add(lazy) + fun register(target: Any, lazy: LazyResettable<*, *>) = + lazyMap.getOrPut(target, { Collections.newSetFromMap(WeakHashMap()) }).add(lazy) -fun reset(target: Any) = lazyMap[target]?.forEach(LazyResettable<*, *>::reset) -}
\ No newline at end of file + fun reset(target: Any) = lazyMap[target]?.forEach(LazyResettable<*, *>::reset) +} diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/NetworkUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/NetworkUtils.kt index 2271c16..260e90f 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/NetworkUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/NetworkUtils.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.utils import android.annotation.SuppressLint @@ -29,4 +44,4 @@ inline val Context.isMobileDataConnected: Boolean val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val activeNetworkInfo = connectivityManager.activeNetworkInfo return (activeNetworkInfo?.type ?: -1) == ConnectivityManager.TYPE_MOBILE - }
\ No newline at end of file + } diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/NotificationUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/NotificationUtils.kt index 016f3d2..126b133 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/NotificationUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/NotificationUtils.kt @@ -1,10 +1,24 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.utils import android.content.Context import androidx.core.app.NotificationManagerCompat - /** * Created by Allan Wang on 2017-08-04. */ -fun Context.cancelNotification(notifId: Int) = NotificationManagerCompat.from(this).cancel(notifId)
\ No newline at end of file +fun Context.cancelNotification(notifId: Int) = NotificationManagerCompat.from(this).cancel(notifId) 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 77750d3..4055847 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.utils import android.content.ActivityNotFoundException @@ -70,4 +85,4 @@ inline val Context.isFromGooglePlay: Boolean get() { val installer = installerPackageName return arrayOf(INSTALLER_GOOGLE_PLAY_FEEDBACK, INSTALLER_GOOGLE_PLAY_VENDING).any { it == installer } - }
\ No newline at end of file + } diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/RecyclerUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/RecyclerUtils.kt index 1c336f9..11494b3 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/RecyclerUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/RecyclerUtils.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.utils import android.graphics.Rect @@ -22,4 +37,4 @@ class MarginItemDecoration(sizeDp: Int, val edgeFlags: Int) : RecyclerView.ItemD if (edgeFlags and KAU_RIGHT > 0) outRect.right += sizePx if (edgeFlags and KAU_BOTTOM > 0) outRect.bottom += sizePx } -}
\ No newline at end of file +} diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/TransitionUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/TransitionUtils.kt index 1d12fd3..523a586 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/TransitionUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/TransitionUtils.kt @@ -1,13 +1,28 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.utils import android.os.Build +import android.transition.Transition +import android.view.ViewGroup import androidx.annotation.RequiresApi import androidx.annotation.TransitionRes import androidx.transition.AutoTransition import androidx.transition.TransitionInflater import androidx.transition.TransitionManager -import android.transition.Transition -import android.view.ViewGroup import androidx.transition.Transition as SupportTransition /** @@ -29,7 +44,8 @@ fun Transition.addEndListener(onEnd: (transition: Transition) -> Unit) { } @RequiresApi(Build.VERSION_CODES.LOLLIPOP) -class SupportTransitionEndListener(val onEnd: (transition: SupportTransition) -> Unit) : SupportTransition.TransitionListener { +class SupportTransitionEndListener(val onEnd: (transition: SupportTransition) -> Unit) : + SupportTransition.TransitionListener { override fun onTransitionEnd(transition: SupportTransition) = onEnd(transition) override fun onTransitionResume(transition: SupportTransition) {} override fun onTransitionPause(transition: SupportTransition) {} @@ -57,4 +73,4 @@ fun ViewGroup.transitionDelayed(@TransitionRes id: Int, builder: androidx.transi val transition = TransitionInflater.from(context).inflateTransition(id) transition.builder() TransitionManager.beginDelayedTransition(this, transition) -}
\ No newline at end of file +} diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt index c8d5d2a..da4feb8 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.utils import android.content.Context @@ -13,7 +28,6 @@ import ca.allanwang.kau.R import java.math.RoundingMode import java.text.DecimalFormat - /** * Created by Allan Wang on 2017-05-28. */ @@ -132,5 +146,5 @@ inline val kauIsMainThread: Boolean class KauException(message: String) : RuntimeException(message) fun String.withMaxLength(n: Int): String = - if (length <= n) this - else substring(0, n - 1) + KAU_ELLIPSIS
\ No newline at end of file + if (length <= n) this + else substring(0, n - 1) + KAU_ELLIPSIS 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 956df48..186d125 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ @file:Suppress("NOTHING_TO_INLINE") package ca.allanwang.kau.utils @@ -7,10 +22,6 @@ import android.annotation.SuppressLint import android.content.Context import android.graphics.Color import android.os.Build -import androidx.annotation.ColorInt -import androidx.annotation.ColorRes -import androidx.annotation.RequiresApi -import androidx.annotation.StringRes import android.view.LayoutInflater import android.view.MotionEvent import android.view.View @@ -18,6 +29,10 @@ import android.view.ViewGroup import android.view.inputmethod.InputMethodManager import android.widget.EditText import android.widget.ImageView +import androidx.annotation.ColorInt +import androidx.annotation.ColorRes +import androidx.annotation.RequiresApi +import androidx.annotation.StringRes import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import ca.allanwang.kau.ui.createSimpleRippleDrawable @@ -27,7 +42,6 @@ import com.google.android.material.textfield.TextInputEditText import com.mikepenz.iconics.IconicsDrawable import com.mikepenz.iconics.typeface.IIcon - /** * Created by Allan Wang on 2017-05-31. */ @@ -81,10 +95,16 @@ fun View.snackbar(text: String, duration: Int = Snackbar.LENGTH_LONG, builder: S return snackbar } -fun View.snackbar(@StringRes textId: Int, duration: Int = Snackbar.LENGTH_LONG, builder: Snackbar.() -> Unit = {}) = snackbar(context.string(textId), duration, builder) +fun View.snackbar(@StringRes textId: Int, duration: Int = Snackbar.LENGTH_LONG, builder: Snackbar.() -> Unit = {}) = + snackbar(context.string(textId), duration, builder) @KauUtils -fun ImageView.setIcon(icon: IIcon?, sizeDp: Int = 24, @ColorInt color: Int = Color.WHITE, builder: IconicsDrawable.() -> Unit = {}) { +fun ImageView.setIcon( + icon: IIcon?, + sizeDp: Int = 24, + @ColorInt color: Int = Color.WHITE, + builder: IconicsDrawable.() -> Unit = {} +) { if (icon == null) return setImageDrawable(icon.toDrawable(context, sizeDp = sizeDp, color = color, builder = builder)) } @@ -98,7 +118,8 @@ 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) +fun ViewGroup.inflate(layoutId: Int, attachToRoot: Boolean = false): View = + LayoutInflater.from(context).inflate(layoutId, this, attachToRoot) /** * Set left margin to a value in px @@ -150,10 +171,10 @@ fun View.setMargin(margin: Int) = setMargins(margin, KAU_ALL) private fun View.setMargins(margin: Int, flag: Int): Boolean { val p = (layoutParams as? ViewGroup.MarginLayoutParams) ?: return false p.setMargins( - if (flag and KAU_LEFT > 0) margin else p.leftMargin, - if (flag and KAU_TOP > 0) margin else p.topMargin, - if (flag and KAU_RIGHT > 0) margin else p.rightMargin, - if (flag and KAU_BOTTOM > 0) margin else p.bottomMargin + if (flag and KAU_LEFT > 0) margin else p.leftMargin, + if (flag and KAU_TOP > 0) margin else p.topMargin, + if (flag and KAU_RIGHT > 0) margin else p.rightMargin, + if (flag and KAU_BOTTOM > 0) margin else p.bottomMargin ) return true } @@ -206,26 +227,31 @@ fun View.setPadding(padding: Int) = setPadding(padding, KAU_ALL) @KauUtils private fun View.setPadding(padding: Int, flag: Int) { setPadding( - if (flag and KAU_LEFT > 0) padding else paddingLeft, - if (flag and KAU_TOP > 0) padding else paddingTop, - if (flag and KAU_RIGHT > 0) padding else paddingRight, - if (flag and KAU_BOTTOM > 0) padding else paddingBottom + if (flag and KAU_LEFT > 0) padding else paddingLeft, + if (flag and KAU_TOP > 0) padding else paddingTop, + if (flag and KAU_RIGHT > 0) padding else paddingRight, + if (flag and KAU_BOTTOM > 0) padding else paddingBottom ) } @KauUtils fun View.hideKeyboard() { clearFocus() - (context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(windowToken, 0) + (context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow( + windowToken, + 0 + ) } @KauUtils fun View.showKeyboard() { requestFocus() - (context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).showSoftInput(this, InputMethodManager.SHOW_IMPLICIT) + (context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).showSoftInput( + this, + InputMethodManager.SHOW_IMPLICIT + ) } - @RequiresApi(Build.VERSION_CODES.LOLLIPOP) @KauUtils fun View.setRippleBackground(@ColorInt foregroundColor: Int, @ColorInt backgroundColor: Int) { @@ -243,12 +269,14 @@ inline val TextInputEditText.value: String get() = text.toString().trim() /** * Generates a recycler view with match parent and a linearlayoutmanager, since it's so commonly used */ -fun Context.fullLinearRecycler(rvAdapter: RecyclerView.Adapter<*>? = null, configs: RecyclerView.() -> Unit = {}) = RecyclerView(this).apply { - layoutManager = LinearLayoutManager(this@fullLinearRecycler) - layoutParams = RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.MATCH_PARENT) - if (rvAdapter != null) adapter = rvAdapter - configs() -} +fun Context.fullLinearRecycler(rvAdapter: RecyclerView.Adapter<*>? = null, configs: RecyclerView.() -> Unit = {}) = + RecyclerView(this).apply { + layoutManager = LinearLayoutManager(this@fullLinearRecycler) + layoutParams = + RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.MATCH_PARENT) + if (rvAdapter != null) adapter = rvAdapter + configs() + } /** * Sets a linear layout manager along with an adapter @@ -263,7 +291,11 @@ fun RecyclerView.withLinearAdapter(rvAdapter: RecyclerView.Adapter<*>) = apply { * If it is not shown, the action will be invoked directly and no further actions will be made * If it is already shown, scaling and alpha animations will be added to the action */ -inline fun <T : ImageView> T.fadeScaleTransition(duration: Long = 500L, minScale: Float = 0.7f, crossinline action: T.() -> Unit) { +inline fun <T : ImageView> T.fadeScaleTransition( + duration: Long = 500L, + minScale: Float = 0.7f, + crossinline action: T.() -> Unit +) { if (!isVisible) action() else { var transitioned = false @@ -324,4 +356,4 @@ inline fun View.setOnSingleTapListener(crossinline onSingleTap: (v: View, event: else -> false } } -}
\ No newline at end of file +} |