From 8ef5d04f28f63a34ca099a9b7e3b887f0ed122a6 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 6 Aug 2019 00:45:36 -0700 Subject: Use kotlin math --- .../pitchedapps/frost/activities/ImageActivity.kt | 5 +---- .../pitchedapps/frost/intro/IntroImageFragments.kt | 23 +++++++++++++++++----- .../pitchedapps/frost/intro/IntroMainFragments.kt | 3 ++- .../frost/services/FrostNotifications.kt | 22 +++++++++++++++------ .../com/pitchedapps/frost/views/FrostVideoView.kt | 21 ++++++++++---------- .../com/pitchedapps/frost/views/FrostWebView.kt | 16 ++++++++++++--- 6 files changed, 61 insertions(+), 29 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt index f05cd6a9..2e92c814 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt @@ -138,10 +138,7 @@ class ImageActivity : KauBaseActivity() { // a unique image identifier based on the id (if it exists), and its hash private val imageHash: String by lazy { - "${Math.abs( - FB_IMAGE_ID_MATCHER.find(imageUrl)[1]?.hashCode() - ?: 0 - )}_${Math.abs(imageUrl.hashCode())}" + "${abs(FB_IMAGE_ID_MATCHER.find(imageUrl)[1]?.hashCode() ?: 0)}_${abs(imageUrl.hashCode())}" } private lateinit var binding: ActivityImageBinding diff --git a/app/src/main/kotlin/com/pitchedapps/frost/intro/IntroImageFragments.kt b/app/src/main/kotlin/com/pitchedapps/frost/intro/IntroImageFragments.kt index 1fd8c76e..53de58af 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/intro/IntroImageFragments.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/intro/IntroImageFragments.kt @@ -31,6 +31,7 @@ import com.mikepenz.google_material_typeface_library.GoogleMaterial import com.pitchedapps.frost.R import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.launchTabCustomizerActivity +import java.lang.Math.abs /** * Created by Allan Wang on 2017-07-28. @@ -69,7 +70,7 @@ abstract class BaseImageIntroFragment( override fun onPageScrolledImpl(positionOffset: Float) { super.onPageScrolledImpl(positionOffset) - val alpha = ((1 - Math.abs(positionOffset)) * 255).toInt() + val alpha = ((1 - abs(positionOffset)) * 255).toInt() //apply alpha to all layers except the phone base (0 until imageDrawable.numberOfLayers).forEach { val d = imageDrawable.getDrawable(it) @@ -89,7 +90,9 @@ abstract class BaseImageIntroFragment( } class IntroAccountFragment : BaseImageIntroFragment( - R.string.intro_multiple_accounts, R.drawable.intro_phone_nav, R.string.intro_multiple_accounts_desc + R.string.intro_multiple_accounts, + R.drawable.intro_phone_nav, + R.string.intro_multiple_accounts_desc ) { override fun themeFragmentImpl() { @@ -132,16 +135,26 @@ class IntroTabTouchFragment : BaseImageIntroFragment( } class IntroTabContextFragment : BaseImageIntroFragment( - R.string.intro_context_aware, R.drawable.intro_phone_long_press, R.string.intro_context_aware_desc + R.string.intro_context_aware, + R.drawable.intro_phone_long_press, + R.string.intro_context_aware_desc ) { override fun themeFragmentImpl() { super.themeFragmentImpl() themeImageComponent(Prefs.headerColor, R.id.intro_phone_toolbar) themeImageComponent(Prefs.bgColor.colorToForeground(0.1f), R.id.intro_phone_image) - themeImageComponent(Prefs.bgColor.colorToForeground(0.2f), R.id.intro_phone_like, R.id.intro_phone_share) + themeImageComponent( + Prefs.bgColor.colorToForeground(0.2f), + R.id.intro_phone_like, + R.id.intro_phone_share + ) themeImageComponent(Prefs.bgColor.colorToForeground(0.3f), R.id.intro_phone_comment) - themeImageComponent(Prefs.bgColor.colorToForeground(0.1f), R.id.intro_phone_card_1, R.id.intro_phone_card_2) + themeImageComponent( + Prefs.bgColor.colorToForeground(0.1f), + R.id.intro_phone_card_1, + R.id.intro_phone_card_2 + ) themeImageComponent( Prefs.textColor, R.id.intro_phone_image_indicator, diff --git a/app/src/main/kotlin/com/pitchedapps/frost/intro/IntroMainFragments.kt b/app/src/main/kotlin/com/pitchedapps/frost/intro/IntroMainFragments.kt index 22dd086b..f3d64b32 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/intro/IntroMainFragments.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/intro/IntroMainFragments.kt @@ -37,6 +37,7 @@ import com.pitchedapps.frost.R import com.pitchedapps.frost.activities.IntroActivity import com.pitchedapps.frost.utils.Prefs import kotlinx.android.synthetic.main.intro_analytics.* +import kotlin.math.abs /** * Created by Allan Wang on 2017-07-28. @@ -61,7 +62,7 @@ abstract class BaseIntroFragment(val layoutRes: Int) : Fragment() { group.forEach { it.translationX = if (offset > 0) -maxTranslation + i * increment else -(i + 1) * increment - it.alpha = 1 - Math.abs(offset) + it.alpha = 1 - abs(offset) } } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt index 0b1deb4e..bb5594fe 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt @@ -49,6 +49,7 @@ import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.frostEvent import com.pitchedapps.frost.utils.isIndependent import java.util.Locale +import kotlin.math.abs /** * Created by Allan Wang on 2017-07-08. @@ -93,7 +94,10 @@ enum class NotificationType( /** * Optional binder to return the request bundle builder */ - internal open fun bindRequest(content: NotificationContent, cookie: String): (BaseBundle.() -> Unit)? = null + internal open fun bindRequest( + content: NotificationContent, + cookie: String + ): (BaseBundle.() -> Unit)? = null private fun bindRequest(intent: Intent, content: NotificationContent) { val cookie = content.data.cookie ?: return @@ -194,7 +198,8 @@ enum class NotificationType( */ fun putContentExtra(intent: Intent, content: NotificationContent): Intent { // We will show the notification page for dependent urls. We can trigger a click next time - intent.data = Uri.parse(if (content.href.isIndependent) content.href else FbItem.NOTIFICATIONS.url) + intent.data = + Uri.parse(if (content.href.isIndependent) content.href else FbItem.NOTIFICATIONS.url) bindRequest(intent, content) return intent } @@ -213,12 +218,16 @@ enum class NotificationType( /** * Create and submit a new notification with the given [content] */ - private fun createNotification(context: Context, content: NotificationContent): FrostNotification = + private fun createNotification( + context: Context, + content: NotificationContent + ): FrostNotification = with(content) { val intent = createCommonIntent(context, content.data.id) putContentExtra(intent, content) val group = "${groupPrefix}_${data.id}" - val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) + val pendingIntent = + PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) val notifBuilder = context.frostNotification(channelId) .setContentTitle(title ?: context.string(R.string.frost_name)) .setContentText(text) @@ -257,7 +266,8 @@ enum class NotificationType( intent.data = Uri.parse(fbItem.url) intent.putExtra(ARG_USER_ID, userId) val group = "${groupPrefix}_$userId" - val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) + val pendingIntent = + PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) val notifBuilder = context.frostNotification(channelId) .setContentTitle(context.string(R.string.frost_name)) .setContentText("$count ${context.string(fbItem.titleId)}") @@ -289,7 +299,7 @@ data class NotificationContent( val unread: Boolean ) { - val notifId = Math.abs(id.toInt()) + val notifId = abs(id.toInt()) } /** diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoView.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoView.kt index f21476fb..351f2236 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoView.kt @@ -35,6 +35,9 @@ import com.devbrackets.android.exomedia.ui.widget.VideoView import com.pitchedapps.frost.R import com.pitchedapps.frost.facebook.formattedFbUrl import com.pitchedapps.frost.utils.L +import kotlin.math.abs +import kotlin.math.max +import kotlin.math.min /** * Created by Allan Wang on 2017-10-13. @@ -119,11 +122,11 @@ class FrostVideoView @JvmOverloads constructor( private fun mapBounds(): Triple { if (videoDimensions.x <= 0f || videoDimensions.y <= 0f) { L.d { "Attempted to toggle video expansion when points have not been finalized" } - val dimen = Math.min(height, width).toFloat() + val dimen = min(height, width).toFloat() videoDimensions.set(dimen, dimen) } val portrait = height > width - val scale = Math.min( + val scale = min( height / (if (portrait) 4f else 2.3f) / videoDimensions.y, width / (if (portrait) 2.3f else 4f) / videoDimensions.x ) @@ -167,10 +170,8 @@ class FrostVideoView @JvmOverloads constructor( v.setOnTouchListener(VideoTouchListener(context)) setOnVideoSizedChangedListener { intrinsicWidth, intrinsicHeight, pixelWidthHeightRatio -> // todo use provided ratio? - val ratio = Math.min( - width.toFloat() / intrinsicWidth, - height.toFloat() / intrinsicHeight.toFloat() - ) + val ratio = + min(width.toFloat() / intrinsicWidth, height.toFloat() / intrinsicHeight.toFloat()) /** * Only remap if not expanded and if dimensions have changed */ @@ -240,7 +241,7 @@ class FrostVideoView @JvmOverloads constructor( private fun onHorizontalSwipe(offset: Float) { val alpha = - Math.max((1f - Math.abs(offset / SWIPE_TO_CLOSE_OFFSET_THRESHOLD)) * 0.5f + 0.5f, 0f) + max((1f - abs(offset / SWIPE_TO_CLOSE_OFFSET_THRESHOLD)) * 0.5f + 0.5f, 0f) this.alpha = alpha } @@ -305,9 +306,9 @@ class FrostVideoView @JvmOverloads constructor( translationX = baseTranslateX - dx onHorizontalSwipe(dx) } else if (checkForDismiss) { - if (Math.abs(event.rawY - downLoc.y) > SWIPE_TO_CLOSE_VERTICAL_THRESHOLD) + if (abs(event.rawY - downLoc.y) > SWIPE_TO_CLOSE_VERTICAL_THRESHOLD) checkForDismiss = false - else if (Math.abs(event.rawX - downLoc.x) > SWIPE_TO_CLOSE_HORIZONTAL_THRESHOLD) { + else if (abs(event.rawX - downLoc.x) > SWIPE_TO_CLOSE_HORIZONTAL_THRESHOLD) { onSwipe = true baseSwipeX = event.rawX baseTranslateX = translationX @@ -316,7 +317,7 @@ class FrostVideoView @JvmOverloads constructor( } MotionEvent.ACTION_UP -> { if (onSwipe) { - if (Math.abs(baseSwipeX - event.rawX) > SWIPE_TO_CLOSE_OFFSET_THRESHOLD) + if (abs(baseSwipeX - event.rawX) > SWIPE_TO_CLOSE_OFFSET_THRESHOLD) destroy() else animate().translationX(baseTranslateX).setDuration( diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt index 1dd027fd..cbcd1054 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt @@ -43,6 +43,9 @@ import com.pitchedapps.frost.web.FrostJSI import com.pitchedapps.frost.web.FrostWebViewClient import com.pitchedapps.frost.web.NestedWebView import com.pitchedapps.frost.web.shouldUseDesktopAgent +import kotlin.math.abs +import kotlin.math.max +import kotlin.math.min /** * Created by Allan Wang on 2017-05-29. @@ -89,7 +92,14 @@ class FrostWebView @JvmOverloads constructor( setDownloadListener { url, userAgent, contentDisposition, mimetype, contentLength -> context.ctxCoroutine.launchMain { val cookie = db.cookieDao().currentCookie() ?: return@launchMain - context.frostDownload(cookie, url, userAgent, contentDisposition, mimetype, contentLength) + context.frostDownload( + cookie, + url, + userAgent, + contentDisposition, + mimetype, + contentLength + ) } } return this @@ -163,14 +173,14 @@ class FrostWebView @JvmOverloads constructor( private fun smoothScrollTo(y: Int) { ValueAnimator.ofInt(scrollY, y).apply { - duration = Math.min(Math.abs(scrollY - y), 500).toLong() + duration = min(abs(scrollY - y), 500).toLong() interpolator = AnimHolder.fastOutSlowInInterpolator(context) addUpdateListener { scrollY = it.animatedValue as Int } start() } } - private fun smoothScrollBy(y: Int) = smoothScrollTo(Math.max(0, scrollY + y)) + private fun smoothScrollBy(y: Int) = smoothScrollTo(max(0, scrollY + y)) override var active: Boolean = true set(value) { -- cgit v1.2.3