1
0
mirror of https://github.com/AllanWang/Frost-for-Facebook.git synced 2024-11-13 22:43:06 +01:00

Merge pull request #1502 from AllanWang/kotlin-math

Use kotlin math
This commit is contained in:
Allan Wang 2019-08-06 00:50:37 -07:00 committed by GitHub
commit c78e670c2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 62 additions and 30 deletions

View File

@ -138,10 +138,7 @@ class ImageActivity : KauBaseActivity() {
// a unique image identifier based on the id (if it exists), and its hash // a unique image identifier based on the id (if it exists), and its hash
private val imageHash: String by lazy { private val imageHash: String by lazy {
"${Math.abs( "${abs(FB_IMAGE_ID_MATCHER.find(imageUrl)[1]?.hashCode() ?: 0)}_${abs(imageUrl.hashCode())}"
FB_IMAGE_ID_MATCHER.find(imageUrl)[1]?.hashCode()
?: 0
)}_${Math.abs(imageUrl.hashCode())}"
} }
private lateinit var binding: ActivityImageBinding private lateinit var binding: ActivityImageBinding

View File

@ -31,6 +31,7 @@ import com.mikepenz.google_material_typeface_library.GoogleMaterial
import com.pitchedapps.frost.R import com.pitchedapps.frost.R
import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.Prefs
import com.pitchedapps.frost.utils.launchTabCustomizerActivity import com.pitchedapps.frost.utils.launchTabCustomizerActivity
import kotlin.math.abs
/** /**
* Created by Allan Wang on 2017-07-28. * Created by Allan Wang on 2017-07-28.
@ -69,7 +70,7 @@ abstract class BaseImageIntroFragment(
override fun onPageScrolledImpl(positionOffset: Float) { override fun onPageScrolledImpl(positionOffset: Float) {
super.onPageScrolledImpl(positionOffset) 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 //apply alpha to all layers except the phone base
(0 until imageDrawable.numberOfLayers).forEach { (0 until imageDrawable.numberOfLayers).forEach {
val d = imageDrawable.getDrawable(it) val d = imageDrawable.getDrawable(it)
@ -89,7 +90,9 @@ abstract class BaseImageIntroFragment(
} }
class IntroAccountFragment : 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() { override fun themeFragmentImpl() {
@ -132,16 +135,26 @@ class IntroTabTouchFragment : BaseImageIntroFragment(
} }
class IntroTabContextFragment : 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() { override fun themeFragmentImpl() {
super.themeFragmentImpl() super.themeFragmentImpl()
themeImageComponent(Prefs.headerColor, R.id.intro_phone_toolbar) themeImageComponent(Prefs.headerColor, R.id.intro_phone_toolbar)
themeImageComponent(Prefs.bgColor.colorToForeground(0.1f), R.id.intro_phone_image) 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.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( themeImageComponent(
Prefs.textColor, Prefs.textColor,
R.id.intro_phone_image_indicator, R.id.intro_phone_image_indicator,

View File

@ -37,6 +37,7 @@ import com.pitchedapps.frost.R
import com.pitchedapps.frost.activities.IntroActivity import com.pitchedapps.frost.activities.IntroActivity
import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.Prefs
import kotlinx.android.synthetic.main.intro_analytics.* import kotlinx.android.synthetic.main.intro_analytics.*
import kotlin.math.abs
/** /**
* Created by Allan Wang on 2017-07-28. * Created by Allan Wang on 2017-07-28.
@ -61,7 +62,7 @@ abstract class BaseIntroFragment(val layoutRes: Int) : Fragment() {
group.forEach { group.forEach {
it.translationX = it.translationX =
if (offset > 0) -maxTranslation + i * increment else -(i + 1) * increment if (offset > 0) -maxTranslation + i * increment else -(i + 1) * increment
it.alpha = 1 - Math.abs(offset) it.alpha = 1 - abs(offset)
} }
} }
} }

View File

@ -49,6 +49,7 @@ import com.pitchedapps.frost.utils.Prefs
import com.pitchedapps.frost.utils.frostEvent import com.pitchedapps.frost.utils.frostEvent
import com.pitchedapps.frost.utils.isIndependent import com.pitchedapps.frost.utils.isIndependent
import java.util.Locale import java.util.Locale
import kotlin.math.abs
/** /**
* Created by Allan Wang on 2017-07-08. * Created by Allan Wang on 2017-07-08.
@ -93,7 +94,10 @@ enum class NotificationType(
/** /**
* Optional binder to return the request bundle builder * 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) { private fun bindRequest(intent: Intent, content: NotificationContent) {
val cookie = content.data.cookie ?: return val cookie = content.data.cookie ?: return
@ -194,7 +198,8 @@ enum class NotificationType(
*/ */
fun putContentExtra(intent: Intent, content: NotificationContent): Intent { fun putContentExtra(intent: Intent, content: NotificationContent): Intent {
// We will show the notification page for dependent urls. We can trigger a click next time // 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) bindRequest(intent, content)
return intent return intent
} }
@ -213,12 +218,16 @@ enum class NotificationType(
/** /**
* Create and submit a new notification with the given [content] * 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) { with(content) {
val intent = createCommonIntent(context, content.data.id) val intent = createCommonIntent(context, content.data.id)
putContentExtra(intent, content) putContentExtra(intent, content)
val group = "${groupPrefix}_${data.id}" 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) val notifBuilder = context.frostNotification(channelId)
.setContentTitle(title ?: context.string(R.string.frost_name)) .setContentTitle(title ?: context.string(R.string.frost_name))
.setContentText(text) .setContentText(text)
@ -257,7 +266,8 @@ enum class NotificationType(
intent.data = Uri.parse(fbItem.url) intent.data = Uri.parse(fbItem.url)
intent.putExtra(ARG_USER_ID, userId) intent.putExtra(ARG_USER_ID, userId)
val group = "${groupPrefix}_$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) val notifBuilder = context.frostNotification(channelId)
.setContentTitle(context.string(R.string.frost_name)) .setContentTitle(context.string(R.string.frost_name))
.setContentText("$count ${context.string(fbItem.titleId)}") .setContentText("$count ${context.string(fbItem.titleId)}")
@ -289,7 +299,7 @@ data class NotificationContent(
val unread: Boolean val unread: Boolean
) { ) {
val notifId = Math.abs(id.toInt()) val notifId = abs(id.toInt())
} }
/** /**

View File

@ -35,6 +35,9 @@ import com.devbrackets.android.exomedia.ui.widget.VideoView
import com.pitchedapps.frost.R import com.pitchedapps.frost.R
import com.pitchedapps.frost.facebook.formattedFbUrl import com.pitchedapps.frost.facebook.formattedFbUrl
import com.pitchedapps.frost.utils.L 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. * Created by Allan Wang on 2017-10-13.
@ -119,11 +122,11 @@ class FrostVideoView @JvmOverloads constructor(
private fun mapBounds(): Triple<Float, Float, Float> { private fun mapBounds(): Triple<Float, Float, Float> {
if (videoDimensions.x <= 0f || videoDimensions.y <= 0f) { if (videoDimensions.x <= 0f || videoDimensions.y <= 0f) {
L.d { "Attempted to toggle video expansion when points have not been finalized" } 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) videoDimensions.set(dimen, dimen)
} }
val portrait = height > width val portrait = height > width
val scale = Math.min( val scale = min(
height / (if (portrait) 4f else 2.3f) / videoDimensions.y, height / (if (portrait) 4f else 2.3f) / videoDimensions.y,
width / (if (portrait) 2.3f else 4f) / videoDimensions.x width / (if (portrait) 2.3f else 4f) / videoDimensions.x
) )
@ -167,10 +170,8 @@ class FrostVideoView @JvmOverloads constructor(
v.setOnTouchListener(VideoTouchListener(context)) v.setOnTouchListener(VideoTouchListener(context))
setOnVideoSizedChangedListener { intrinsicWidth, intrinsicHeight, pixelWidthHeightRatio -> setOnVideoSizedChangedListener { intrinsicWidth, intrinsicHeight, pixelWidthHeightRatio ->
// todo use provided ratio? // todo use provided ratio?
val ratio = Math.min( val ratio =
width.toFloat() / intrinsicWidth, min(width.toFloat() / intrinsicWidth, height.toFloat() / intrinsicHeight.toFloat())
height.toFloat() / intrinsicHeight.toFloat()
)
/** /**
* Only remap if not expanded and if dimensions have changed * Only remap if not expanded and if dimensions have changed
*/ */
@ -240,7 +241,7 @@ class FrostVideoView @JvmOverloads constructor(
private fun onHorizontalSwipe(offset: Float) { private fun onHorizontalSwipe(offset: Float) {
val alpha = 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 this.alpha = alpha
} }
@ -305,9 +306,9 @@ class FrostVideoView @JvmOverloads constructor(
translationX = baseTranslateX - dx translationX = baseTranslateX - dx
onHorizontalSwipe(dx) onHorizontalSwipe(dx)
} else if (checkForDismiss) { } 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 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 onSwipe = true
baseSwipeX = event.rawX baseSwipeX = event.rawX
baseTranslateX = translationX baseTranslateX = translationX
@ -316,7 +317,7 @@ class FrostVideoView @JvmOverloads constructor(
} }
MotionEvent.ACTION_UP -> { MotionEvent.ACTION_UP -> {
if (onSwipe) { if (onSwipe) {
if (Math.abs(baseSwipeX - event.rawX) > SWIPE_TO_CLOSE_OFFSET_THRESHOLD) if (abs(baseSwipeX - event.rawX) > SWIPE_TO_CLOSE_OFFSET_THRESHOLD)
destroy() destroy()
else else
animate().translationX(baseTranslateX).setDuration( animate().translationX(baseTranslateX).setDuration(

View File

@ -43,6 +43,9 @@ import com.pitchedapps.frost.web.FrostJSI
import com.pitchedapps.frost.web.FrostWebViewClient import com.pitchedapps.frost.web.FrostWebViewClient
import com.pitchedapps.frost.web.NestedWebView import com.pitchedapps.frost.web.NestedWebView
import com.pitchedapps.frost.web.shouldUseDesktopAgent 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. * Created by Allan Wang on 2017-05-29.
@ -89,7 +92,14 @@ class FrostWebView @JvmOverloads constructor(
setDownloadListener { url, userAgent, contentDisposition, mimetype, contentLength -> setDownloadListener { url, userAgent, contentDisposition, mimetype, contentLength ->
context.ctxCoroutine.launchMain { context.ctxCoroutine.launchMain {
val cookie = db.cookieDao().currentCookie() ?: return@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 return this
@ -163,14 +173,14 @@ class FrostWebView @JvmOverloads constructor(
private fun smoothScrollTo(y: Int) { private fun smoothScrollTo(y: Int) {
ValueAnimator.ofInt(scrollY, y).apply { 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) interpolator = AnimHolder.fastOutSlowInInterpolator(context)
addUpdateListener { scrollY = it.animatedValue as Int } addUpdateListener { scrollY = it.animatedValue as Int }
start() 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 override var active: Boolean = true
set(value) { set(value) {

View File

@ -12,7 +12,7 @@
<item text="Update theme" /> <item text="Update theme" />
<item text="Disable bugsnag completely when opting out of analytics" /> <item text="Disable bugsnag completely when opting out of analytics" />
<item text="Filter urls before sending to other apps" /> <item text="Filter urls before sending to other apps" />
<item text="" /> <item text="Allow hiding main fab (see settings > newsfeed)" />
<item text="" /> <item text="" />
<version title="v2.3.1" /> <version title="v2.3.1" />