diff options
author | Allan Wang <me@allanwang.ca> | 2019-10-19 22:30:16 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2019-10-19 22:31:14 -0700 |
commit | 7007e61879bf33c09115b5d8bc16f27af2731045 (patch) | |
tree | 19bc5e07f16ce1b6b9d13c711fed5160e25bc3ad /core | |
parent | 63f920c3257e955da1ef63c91f8b5e2de5478e55 (diff) | |
download | kau-7007e61879bf33c09115b5d8bc16f27af2731045.tar.gz kau-7007e61879bf33c09115b5d8bc16f27af2731045.tar.bz2 kau-7007e61879bf33c09115b5d8bc16f27af2731045.zip |
Convert java math to kotlin math
Diffstat (limited to 'core')
8 files changed, 30 insertions, 22 deletions
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() } |