diff options
author | Allan Wang <me@allanwang.ca> | 2018-12-24 01:17:31 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-24 01:17:31 -0500 |
commit | fb90c4a4ab23f2fd246c29c1dde4d6e155a2e38b (patch) | |
tree | cc6d67461b6caa256043b9482f7646831d528a08 /core-ui/src/main/kotlin/ca/allanwang/kau/ui/widgets | |
parent | a380adea1052d39f23c9c4d432a9380ce347d6c4 (diff) | |
download | kau-fb90c4a4ab23f2fd246c29c1dde4d6e155a2e38b.tar.gz kau-fb90c4a4ab23f2fd246c29c1dde4d6e155a2e38b.tar.bz2 kau-fb90c4a4ab23f2fd246c29c1dde4d6e155a2e38b.zip |
Enhancement/ktlint (#179)
* Add spotless dependency
* Apply ktlint
* Add license headers
Diffstat (limited to 'core-ui/src/main/kotlin/ca/allanwang/kau/ui/widgets')
-rw-r--r-- | core-ui/src/main/kotlin/ca/allanwang/kau/ui/widgets/ElasticDragDismissFrameLayout.kt | 115 | ||||
-rw-r--r-- | core-ui/src/main/kotlin/ca/allanwang/kau/ui/widgets/TextSlider.kt | 50 |
2 files changed, 111 insertions, 54 deletions
diff --git a/core-ui/src/main/kotlin/ca/allanwang/kau/ui/widgets/ElasticDragDismissFrameLayout.kt b/core-ui/src/main/kotlin/ca/allanwang/kau/ui/widgets/ElasticDragDismissFrameLayout.kt index b89373e..f4578f2 100644 --- a/core-ui/src/main/kotlin/ca/allanwang/kau/ui/widgets/ElasticDragDismissFrameLayout.kt +++ b/core-ui/src/main/kotlin/ca/allanwang/kau/ui/widgets/ElasticDragDismissFrameLayout.kt @@ -1,11 +1,11 @@ /* - * Copyright 2015 Google Inc. + * 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 + * 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, @@ -13,22 +13,28 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package ca.allanwang.kau.ui.widgets import android.app.Activity import android.content.Context import android.graphics.Color import android.os.Build -import androidx.annotation.RequiresApi import android.transition.TransitionInflater import android.util.AttributeSet import android.view.MotionEvent import android.view.View import android.widget.FrameLayout +import androidx.annotation.RequiresApi import ca.allanwang.kau.logging.KL import ca.allanwang.kau.ui.R -import ca.allanwang.kau.utils.* +import ca.allanwang.kau.utils.AnimHolder +import ca.allanwang.kau.utils.dimen +import ca.allanwang.kau.utils.dpToPx +import ca.allanwang.kau.utils.isNavBarOnBottom +import ca.allanwang.kau.utils.navigationBarColor +import ca.allanwang.kau.utils.scaleXY +import ca.allanwang.kau.utils.statusBarColor +import ca.allanwang.kau.utils.withAlpha /** * A [FrameLayout] which responds to nested scrolls to create drag-dismissable layouts. @@ -37,7 +43,10 @@ import ca.allanwang.kau.utils.* */ @RequiresApi(Build.VERSION_CODES.LOLLIPOP) class ElasticDragDismissFrameLayout @JvmOverloads constructor( - context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0 + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = 0, + defStyleRes: Int = 0 ) : FrameLayout(context, attrs, defStyleAttr, defStyleRes) { // configurable attribs @@ -62,8 +71,11 @@ class ElasticDragDismissFrameLayout @JvmOverloads constructor( init { if (attrs != null) { val a = getContext().obtainStyledAttributes(attrs, R.styleable.ElasticDragDismissFrameLayout, 0, 0) - dragDismissDistance = a.getDimensionPixelSize(R.styleable.ElasticDragDismissFrameLayout_dragDismissDistance, Int.MAX_VALUE).toFloat() - dragDismissFraction = a.getFloat(R.styleable.ElasticDragDismissFrameLayout_dragDismissFraction, dragDismissFraction) + dragDismissDistance = + a.getDimensionPixelSize(R.styleable.ElasticDragDismissFrameLayout_dragDismissDistance, Int.MAX_VALUE) + .toFloat() + dragDismissFraction = + a.getFloat(R.styleable.ElasticDragDismissFrameLayout_dragDismissFraction, dragDismissFraction) dragDismissScale = a.getFloat(R.styleable.ElasticDragDismissFrameLayout_dragDismissScale, dragDismissScale) dragElacticity = a.getFloat(R.styleable.ElasticDragDismissFrameLayout_dragElasticity, dragElacticity) a.recycle() @@ -74,27 +86,27 @@ class ElasticDragDismissFrameLayout @JvmOverloads constructor( /** * Called for each drag event. - - * @param elasticOffset Indicating the drag offset with elasticity applied i.e. may - * * exceed 1. - * * + * @param elasticOffset Indicating the drag offset with elasticity applied i.e. may exceed 1. + * * @param elasticOffsetPixels The elastically scaled drag distance in pixels. - * * - * @param rawOffset Value from [0, 1] indicating the raw drag offset i.e. - * * without elasticity applied. A value of 1 indicates that the - * * dismiss distance has been reached. - * * - * @param rawOffsetPixels The raw distance the user has dragged + * + * @param rawOffset Value from [0, 1] indicating the raw drag offset i.e. without elasticity applied. + * A value of 1 indicates that the dismiss distance has been reached. + * + * @param rawOffsetPixels The raw distance the user has dragged */ - internal open fun onDrag(elasticOffset: Float, elasticOffsetPixels: Float, - rawOffset: Float, rawOffsetPixels: Float) { + internal open fun onDrag( + elasticOffset: Float, + elasticOffsetPixels: Float, + rawOffset: Float, + rawOffsetPixels: Float + ) { } /** * Called when dragging is released and has exceeded the threshold dismiss distance. */ internal open fun onDragDismissed() {} - } override fun onInterceptTouchEvent(ev: MotionEvent): Boolean { @@ -114,8 +126,13 @@ class ElasticDragDismissFrameLayout @JvmOverloads constructor( } } - override fun onNestedScroll(target: View, dxConsumed: Int, dyConsumed: Int, - dxUnconsumed: Int, dyUnconsumed: Int) { + override fun onNestedScroll( + target: View, + dxConsumed: Int, + dyConsumed: Int, + dxUnconsumed: Int, + dyUnconsumed: Int + ) { dragScale(dyUnconsumed) } @@ -129,12 +146,12 @@ class ElasticDragDismissFrameLayout @JvmOverloads constructor( scaleXY = 1f } else { animate() - .translationY(0f) - .scaleXY(1f) - .setDuration(200L) - .setInterpolator(AnimHolder.fastOutSlowInInterpolator(context)) - .setListener(null) - .start() + .translationY(0f) + .scaleXY(1f) + .setDuration(200L) + .setInterpolator(AnimHolder.fastOutSlowInInterpolator(context)) + .setListener(null) + .start() } totalDrag = 0f @@ -201,15 +218,23 @@ class ElasticDragDismissFrameLayout @JvmOverloads constructor( translationY = 0f scaleXY = 1f } - dispatchDragCallback(dragFraction, dragTo, - Math.min(1f, Math.abs(totalDrag) / dragDismissDistance), totalDrag) + dispatchDragCallback( + dragFraction, dragTo, + Math.min(1f, Math.abs(totalDrag) / dragDismissDistance), totalDrag + ) } - private fun dispatchDragCallback(elasticOffset: Float, elasticOffsetPixels: Float, - rawOffset: Float, rawOffsetPixels: Float) { + private fun dispatchDragCallback( + elasticOffset: Float, + elasticOffsetPixels: Float, + rawOffset: Float, + rawOffsetPixels: Float + ) { callbacks.forEach { - it.onDrag(elasticOffset, elasticOffsetPixels, - rawOffset, rawOffsetPixels) + it.onDrag( + elasticOffset, elasticOffsetPixels, + rawOffset, rawOffsetPixels + ) } } @@ -227,8 +252,12 @@ class ElasticDragDismissFrameLayout @JvmOverloads constructor( private val navBarAlpha: Int = Color.alpha(activity.navigationBarColor) private val fadeNavBar: Boolean = activity.isNavBarOnBottom - public override fun onDrag(elasticOffset: Float, elasticOffsetPixels: Float, - rawOffset: Float, rawOffsetPixels: Float) { + public override fun onDrag( + elasticOffset: Float, + elasticOffsetPixels: Float, + rawOffset: Float, + rawOffsetPixels: Float + ) { if (elasticOffsetPixels > 0) { // dragging downward, fade the status bar in proportion activity.statusBarColor = activity.statusBarColor.withAlpha(((1f - rawOffset) * statusBarAlpha).toInt()) @@ -238,7 +267,8 @@ class ElasticDragDismissFrameLayout @JvmOverloads constructor( activity.navigationBarColor = activity.navigationBarColor.withAlpha(navBarAlpha) } else if (fadeNavBar) { // dragging upward, fade the navigation bar in proportion - activity.navigationBarColor = activity.navigationBarColor.withAlpha(((1f - rawOffset) * navBarAlpha).toInt()) + activity.navigationBarColor = + activity.navigationBarColor.withAlpha(((1f - rawOffset) * navBarAlpha).toInt()) } } @@ -247,15 +277,18 @@ class ElasticDragDismissFrameLayout @JvmOverloads constructor( } } - fun addExitListener(activity: Activity, transitionBottom: Int = R.transition.kau_exit_slide_bottom, transitionTop: Int = R.transition.kau_exit_slide_top) { + fun addExitListener( + activity: Activity, + transitionBottom: Int = R.transition.kau_exit_slide_bottom, + transitionTop: Int = R.transition.kau_exit_slide_top + ) { addListener(object : ElasticDragDismissFrameLayout.SystemChromeFader(activity) { override fun onDragDismissed() { KL.v { "New transition" } activity.window.returnTransition = TransitionInflater.from(activity) - .inflateTransition(if (translationY > 0) transitionBottom else transitionTop) + .inflateTransition(if (translationY > 0) transitionBottom else transitionTop) activity.finishAfterTransition() } }) } - } diff --git a/core-ui/src/main/kotlin/ca/allanwang/kau/ui/widgets/TextSlider.kt b/core-ui/src/main/kotlin/ca/allanwang/kau/ui/widgets/TextSlider.kt index a53ee9d..51f7ab9 100644 --- a/core-ui/src/main/kotlin/ca/allanwang/kau/ui/widgets/TextSlider.kt +++ b/core-ui/src/main/kotlin/ca/allanwang/kau/ui/widgets/TextSlider.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.ui.widgets import android.content.Context import android.graphics.Color -import androidx.core.widget.TextViewCompat import android.text.TextUtils import android.util.AttributeSet import android.view.Gravity @@ -10,8 +24,10 @@ import android.view.animation.Animation import android.view.animation.AnimationUtils import android.widget.TextSwitcher import android.widget.TextView +import androidx.core.widget.TextViewCompat import ca.allanwang.kau.ui.R -import java.util.* +import java.util.EmptyStackException +import java.util.Stack /** * Created by Allan Wang on 2017-06-21. @@ -19,7 +35,9 @@ import java.util.* * Text switcher with global text color and embedded sliding animations * Also has a stack to keep track of title changes */ -class TextSlider @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null +class TextSlider @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null ) : TextSwitcher(context, attrs) { val titleStack: Stack<CharSequence?> = Stack() @@ -28,20 +46,26 @@ class TextSlider @JvmOverloads constructor(context: Context, attrs: AttributeSet * Holds a mapping of animation types to their respective animations */ val animationMap = mapOf( - ANIMATION_NONE to null, - ANIMATION_SLIDE_HORIZONTAL to AnimationBundle( - R.anim.kau_slide_in_right, R.anim.kau_slide_out_left, - R.anim.kau_slide_in_left, R.anim.kau_slide_out_right), - ANIMATION_SLIDE_VERTICAL to AnimationBundle( - R.anim.kau_slide_in_bottom, R.anim.kau_slide_out_top, - R.anim.kau_slide_in_top, R.anim.kau_slide_out_bottom - ) + ANIMATION_NONE to null, + ANIMATION_SLIDE_HORIZONTAL to AnimationBundle( + R.anim.kau_slide_in_right, R.anim.kau_slide_out_left, + R.anim.kau_slide_in_left, R.anim.kau_slide_out_right + ), + ANIMATION_SLIDE_VERTICAL to AnimationBundle( + R.anim.kau_slide_in_bottom, R.anim.kau_slide_out_top, + R.anim.kau_slide_in_top, R.anim.kau_slide_out_bottom + ) ) /** * Holds lazy instances of the animations */ - inner class AnimationBundle(private val nextIn: Int, private val nextOut: Int, private val prevIn: Int, private val prevOut: Int) { + inner class AnimationBundle( + private val nextIn: Int, + private val nextOut: Int, + private val prevIn: Int, + private val prevOut: Int + ) { val NEXT_IN: Animation by lazy { AnimationUtils.loadAnimation(context, nextIn) } val NEXT_OUT: Animation by lazy { AnimationUtils.loadAnimation(context, nextOut) } val PREV_IN: Animation by lazy { AnimationUtils.loadAnimation(context, prevIn) } @@ -122,4 +146,4 @@ class TextSlider @JvmOverloads constructor(context: Context, attrs: AttributeSet } } } -}
\ No newline at end of file +} |