diff options
author | Allan Wang <me@allanwang.ca> | 2019-10-19 22:38:45 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-19 22:38:45 -0700 |
commit | cd068b144ef533bccafc20b8b25ff72b56a93e23 (patch) | |
tree | f1136e879ef1e92b7f0390c23ea52f34a8e96e1e | |
parent | 8d42b8ec4049b62424ec3ea9cd80487409e381d9 (diff) | |
parent | 7007e61879bf33c09115b5d8bc16f27af2731045 (diff) | |
download | kau-cd068b144ef533bccafc20b8b25ff72b56a93e23.tar.gz kau-cd068b144ef533bccafc20b8b25ff72b56a93e23.tar.bz2 kau-cd068b144ef533bccafc20b8b25ff72b56a93e23.zip |
Merge pull request #233 from AllanWang/kotlin-math
Convert java math to kotlin math
13 files changed, 47 insertions, 31 deletions
diff --git a/adapter/src/main/kotlin/ca/allanwang/kau/animators/KauAnimator.kt b/adapter/src/main/kotlin/ca/allanwang/kau/animators/KauAnimator.kt index b9df946..935c484 100644 --- a/adapter/src/main/kotlin/ca/allanwang/kau/animators/KauAnimator.kt +++ b/adapter/src/main/kotlin/ca/allanwang/kau/animators/KauAnimator.kt @@ -19,6 +19,7 @@ import android.view.ViewPropertyAnimator import androidx.recyclerview.widget.RecyclerView import ca.allanwang.kau.utils.KAU_BOTTOM import ca.allanwang.kau.utils.KAU_RIGHT +import kotlin.math.max /** * Created by Allan Wang on 2017-06-27. @@ -30,7 +31,7 @@ open class KauAnimator( ) : BaseItemAnimator() { open fun startDelay(holder: RecyclerView.ViewHolder, duration: Long, factor: Float) = - Math.max(0L, (holder.adapterPosition * duration * factor).toLong()) + max(0L, (holder.adapterPosition * duration * factor).toLong()) override fun removeAnimation(holder: RecyclerView.ViewHolder): ViewPropertyAnimator { return holder.itemView.animate().apply { diff --git a/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/CircleView.kt b/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/CircleView.kt index 29257d8..39fb1cf 100644 --- a/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/CircleView.kt +++ b/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/CircleView.kt @@ -41,6 +41,7 @@ import ca.allanwang.kau.utils.getDip import ca.allanwang.kau.utils.setBackgroundColorRes import ca.allanwang.kau.utils.toColor import ca.allanwang.kau.utils.toHSV +import kotlin.math.roundToInt /** * Created by Allan Wang on 2017-06-10. @@ -214,7 +215,7 @@ class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet @ColorInt fun translucentColor(color: Int): Int { val factor = 0.7f - val alpha = Math.round(Color.alpha(color) * factor) + val alpha = (Color.alpha(color) * factor).roundToInt() val red = Color.red(color) val green = Color.green(color) val blue = Color.blue(color) diff --git a/core-ui/src/main/kotlin/ca/allanwang/kau/ui/views/CutoutView.kt b/core-ui/src/main/kotlin/ca/allanwang/kau/ui/views/CutoutView.kt index 474d40a..7669a2a 100644 --- a/core-ui/src/main/kotlin/ca/allanwang/kau/ui/views/CutoutView.kt +++ b/core-ui/src/main/kotlin/ca/allanwang/kau/ui/views/CutoutView.kt @@ -33,6 +33,8 @@ import ca.allanwang.kau.utils.dimenPixelSize import ca.allanwang.kau.utils.getFont import ca.allanwang.kau.utils.parentViewGroup import ca.allanwang.kau.utils.toBitmap +import kotlin.math.max +import kotlin.math.min /** * A view which punches out some text from an opaque color block, allowing you to see through it. @@ -133,7 +135,7 @@ class CutoutView @JvmOverloads constructor( if (drawable!!.intrinsicHeight <= 0 || drawable!!.intrinsicWidth <= 0) throw IllegalArgumentException("Drawable's intrinsic size cannot be less than 0") val targetWidth = width / PHI val targetHeight = height / PHI - bitmapScaling = Math.min(targetHeight / drawable!!.intrinsicHeight, targetWidth / drawable!!.intrinsicWidth) + bitmapScaling = min(targetHeight / drawable!!.intrinsicHeight, targetWidth / drawable!!.intrinsicWidth) cutoutX = (width - drawable!!.intrinsicWidth * bitmapScaling) / 2 cutoutY = (height - drawable!!.intrinsicHeight * bitmapScaling) / 2 } @@ -143,9 +145,9 @@ class CutoutView @JvmOverloads constructor( */ override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { parentViewGroup.getWindowVisibleDisplayFrame(parentFrame) - val minHeight = Math.max(minHeight, heightPercentage * parentFrame.height()) + val minHeight = max(minHeight, heightPercentage * parentFrame.height()) val trueHeightMeasureSpec = if (minHeight > 0) - MeasureSpec.makeMeasureSpec(Math.max(minHeight.toInt(), measuredHeight), MeasureSpec.EXACTLY) + MeasureSpec.makeMeasureSpec(max(minHeight.toInt(), measuredHeight), MeasureSpec.EXACTLY) else heightMeasureSpec super.onMeasure(widthMeasureSpec, trueHeightMeasureSpec) } 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 6095a32..ab0e464 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 @@ -34,6 +34,9 @@ import ca.allanwang.kau.utils.navigationBarColor import ca.allanwang.kau.utils.scaleXY import ca.allanwang.kau.utils.statusBarColor import ca.allanwang.kau.utils.withAlpha +import kotlin.math.abs +import kotlin.math.log10 +import kotlin.math.min /** * A [FrameLayout] which responds to nested scrolls to create drag-dismissable layouts. @@ -136,7 +139,7 @@ class ElasticDragDismissFrameLayout @JvmOverloads constructor( } override fun onStopNestedScroll(child: View) { - if (Math.abs(totalDrag) >= dragDismissDistance) { + if (abs(totalDrag) >= dragDismissDistance) { dispatchDismissCallback() } else { // settle back to natural position @@ -192,7 +195,7 @@ class ElasticDragDismissFrameLayout @JvmOverloads constructor( } // how far have we dragged relative to the distance to perform a dismiss // (0–1 where 1 = dismiss distance). Decreasing logarithmically as we approach the limit - var dragFraction = Math.log10((1 + Math.abs(totalDrag) / dragDismissDistance).toDouble()).toFloat() + var dragFraction = log10((1 + abs(totalDrag) / dragDismissDistance).toDouble()).toFloat() // calculate the desired translation given the drag fraction var dragTo = dragFraction * dragDismissDistance * dragElacticity @@ -221,7 +224,7 @@ class ElasticDragDismissFrameLayout @JvmOverloads constructor( } dispatchDragCallback( dragFraction, dragTo, - Math.min(1f, Math.abs(totalDrag) / dragDismissDistance), totalDrag + min(1f, abs(totalDrag) / dragDismissDistance), totalDrag ) } diff --git a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt index b549523..dfe98cb 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt @@ -27,6 +27,7 @@ import ca.allanwang.kau.utils.buildIsMarshmallowAndUp import ca.allanwang.kau.utils.hasPermission import ca.allanwang.kau.utils.toast import java.lang.ref.WeakReference +import kotlin.math.min /** * Created by Allan Wang on 2017-07-03. @@ -97,7 +98,7 @@ internal object PermissionManager { */ fun onRequestPermissionsResult(context: Context, permissions: Array<out String>, grantResults: IntArray) { KL.i { "On permission result: pending ${pendingResults.size}" } - val count = Math.min(permissions.size, grantResults.size) + val count = min(permissions.size, grantResults.size) pendingResults.kauRemoveIf { val action = it.get() action == null || (0 until count).any { i -> action.onResult(permissions[i], grantResults[i]) } diff --git a/core/src/main/kotlin/ca/allanwang/kau/swipe/RelativeSlider.kt b/core/src/main/kotlin/ca/allanwang/kau/swipe/RelativeSlider.kt index 9f23b63..e3baa94 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/RelativeSlider.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/RelativeSlider.kt @@ -15,6 +15,9 @@ */ package ca.allanwang.kau.swipe +import kotlin.math.max +import kotlin.math.min + /** * Created by Mr.Jude on 2015/8/26. * @@ -46,13 +49,13 @@ internal class RelativeSlider(var curPage: SwipeBackPage) : SwipeListener { } when (edgeFlag) { SWIPE_EDGE_LEFT -> page.swipeBackLayout.x = - Math.min(-offset * Math.max(1 - percent, 0f) + DEFAULT_OFFSET, 0f) + min(-offset * max(1 - percent, 0f) + DEFAULT_OFFSET, 0f) SWIPE_EDGE_RIGHT -> page.swipeBackLayout.x = - Math.min(offset * Math.max(1 - percent, 0f) - DEFAULT_OFFSET, 0f) + min(offset * max(1 - percent, 0f) - DEFAULT_OFFSET, 0f) SWIPE_EDGE_TOP -> page.swipeBackLayout.y = - Math.min(-offset * Math.max(1 - percent, 0f) + DEFAULT_OFFSET, 0f) + min(-offset * max(1 - percent, 0f) + DEFAULT_OFFSET, 0f) SWIPE_EDGE_BOTTOM -> page.swipeBackLayout.y = - Math.min(offset * Math.max(1 - percent, 0f) - DEFAULT_OFFSET, 0f) + min(offset * max(1 - percent, 0f) - DEFAULT_OFFSET, 0f) } } diff --git a/core/src/main/kotlin/ca/allanwang/kau/ui/views/CollapsibleViewDelegate.kt b/core/src/main/kotlin/ca/allanwang/kau/ui/views/CollapsibleViewDelegate.kt index 2a057cb..4e6326e 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/ui/views/CollapsibleViewDelegate.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/ui/views/CollapsibleViewDelegate.kt @@ -23,6 +23,7 @@ import ca.allanwang.kau.utils.KAU_EXPANDED import ca.allanwang.kau.utils.KAU_EXPANDING import ca.allanwang.kau.utils.goneIf import java.lang.ref.WeakReference +import kotlin.math.roundToInt /** * Created by Allan Wang on 2017-08-03. @@ -107,7 +108,7 @@ class CollapsibleViewDelegate : CollapsibleView { val v = view ?: return Pair(0, 0) val size = v.measuredHeight v.goneIf(expansion == 0f && size == 0) - return Pair(v.measuredWidth, Math.round(size * expansion)) + return Pair(v.measuredWidth, (size * expansion).roundToInt()) } private fun animateSize(target: Float) { diff --git a/core/src/main/kotlin/ca/allanwang/kau/ui/views/RippleCanvas.kt b/core/src/main/kotlin/ca/allanwang/kau/ui/views/RippleCanvas.kt index a972447..bd8f689 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/ui/views/RippleCanvas.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/ui/views/RippleCanvas.kt @@ -27,6 +27,8 @@ import android.graphics.PorterDuff import android.graphics.PorterDuffXfermode import android.util.AttributeSet import android.view.View +import kotlin.math.hypot +import kotlin.math.max /** * Created by Allan Wang on 2016-11-17. @@ -93,7 +95,7 @@ class RippleCanvas @JvmOverloads constructor( END -> h else -> startY } - val maxRadius = Math.hypot(Math.max(x, w - x).toDouble(), Math.max(y, h - y).toDouble()).toFloat() + val maxRadius = hypot(max(x, w - x).toDouble(), max(y, h - y).toDouble()).toFloat() val ripple = Ripple(color, x, y, 0f, maxRadius) ripples.add(ripple) val animator = ValueAnimator.ofFloat(0f, maxRadius) 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 0a548ce..c8c8b34 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/AnimUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/AnimUtils.kt @@ -25,6 +25,8 @@ import android.view.animation.Animation import android.view.animation.AnimationUtils import android.widget.TextView import androidx.annotation.StringRes +import kotlin.math.hypot +import kotlin.math.max /** * Created by Allan Wang on 2017-06-01. @@ -52,10 +54,7 @@ fun View.circularReveal( 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 max(hypot(x.toDouble(), y.toDouble()), hypot((width - x.toDouble()), (height - y.toDouble()))).toFloat() val anim = ViewAnimationUtils.createCircularReveal(this, x, y, 0f, r).setDuration(duration) anim.startDelay = offset @@ -91,10 +90,7 @@ fun View.circularHide( 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 max(hypot(x.toDouble(), y.toDouble()), hypot((width - x.toDouble()), (height - y.toDouble()))).toFloat() val anim = ViewAnimationUtils.createCircularReveal(this, x, y, r, 0f).setDuration(duration) anim.startDelay = offset 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 ab7b341..0131302 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt @@ -36,6 +36,9 @@ import androidx.core.graphics.drawable.DrawableCompat import androidx.core.view.ViewCompat import com.afollestad.materialdialogs.R import java.util.Random +import kotlin.math.abs +import kotlin.math.max +import kotlin.math.roundToInt /** * Created by Allan Wang on 2017-06-08. @@ -86,9 +89,9 @@ fun Int.isColorVisibleOn( @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) + else !(abs(Color.red(this) - Color.red(color)) < delta && + abs(Color.green(this) - Color.green(color)) < delta && + abs(Color.blue(this) - Color.blue(color)) < delta) @ColorInt fun Context.getDisabledColor(): Int { @@ -99,7 +102,7 @@ fun Context.getDisabledColor(): Int { @ColorInt fun Int.adjustAlpha(factor: Float): Int { - val alpha = Math.round(Color.alpha(this) * factor) + val alpha = (Color.alpha(this) * factor).roundToInt() return Color.argb(alpha, Color.red(this), Color.green(this), Color.blue(this)) } @@ -122,7 +125,7 @@ fun Int.withAlpha(@IntRange(from = 0, to = 0xFF) alpha: Int): Int = @ColorInt fun Int.withMinAlpha(@IntRange(from = 0, to = 0xFF) alpha: Int): Int = - withAlpha(Math.max(alpha, this ushr 24)) + withAlpha(max(alpha, this ushr 24)) @ColorInt private inline fun Int.colorFactor(rgbFactor: (Int) -> Float): Int { 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 e4ec19c..d576be1 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt @@ -41,6 +41,7 @@ import com.google.android.material.snackbar.Snackbar import com.google.android.material.textfield.TextInputEditText import com.mikepenz.iconics.IconicsDrawable import com.mikepenz.iconics.typeface.IIcon +import kotlin.math.max /** * Created by Allan Wang on 2017-05-31. @@ -342,7 +343,7 @@ fun FloatingActionButton.hideOnDownwardsScroll(recycler: RecyclerView) { } inline var View.scaleXY - get() = Math.max(scaleX, scaleY) + get() = max(scaleX, scaleY) set(value) { scaleX = value scaleY = value diff --git a/core/src/test/kotlin/ca/allanwang/kau/ui/ProgressAnimatorTest.kt b/core/src/test/kotlin/ca/allanwang/kau/ui/ProgressAnimatorTest.kt index 6b99b14..8b99f3c 100644 --- a/core/src/test/kotlin/ca/allanwang/kau/ui/ProgressAnimatorTest.kt +++ b/core/src/test/kotlin/ca/allanwang/kau/ui/ProgressAnimatorTest.kt @@ -16,6 +16,7 @@ package ca.allanwang.kau.ui import org.junit.Test +import kotlin.math.min import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue @@ -67,7 +68,7 @@ class ProgressAnimatorTest { i > 0.5f } withAnimator { - assertEquals(Math.min(it, 0.5f), i) + assertEquals(min(it, 0.5f), i) } }.test() } diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt index 68d1121..5548812 100644 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt +++ b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt @@ -51,6 +51,7 @@ import com.mikepenz.iconics.typeface.IIcon import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial import kotlinx.coroutines.CancellationException import java.io.File +import kotlin.math.min /** * Created by Allan Wang on 2017-07-23. @@ -204,7 +205,7 @@ abstract class MediaPickerCore<T : GenericItem>( addItems(models.map { converter(it) }) if (!hasPreloaded && mediaType == MediaType.VIDEO) { hasPreloaded = true - val preloads = models.subList(0, Math.min(models.size, 50)).map { + val preloads = models.subList(0, min(models.size, 50)).map { glide.load(it.data) .applyMediaOptions(this@MediaPickerCore) .preload() |