-
Notifications
You must be signed in to change notification settings - Fork 906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve XRay implementation #614
base: main
Are you sure you want to change the base?
Changes from 1 commit
94b59b1
3054784
4516ba4
a332eff
160f0b7
7574b86
39d3ae0
3a4b8d1
c738a5f
b9acc28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,70 +23,76 @@ import android.graphics.drawable.BitmapDrawable | |
import android.graphics.drawable.Drawable | ||
import android.view.Gravity | ||
import android.view.View | ||
import androidx.annotation.VisibleForTesting | ||
|
||
/** Utility class that shows riblets name in its background. */ | ||
class XRay private constructor() { | ||
private var isEnabled = false | ||
private var textPaint: Paint? = null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's all static info, why not immediately initialize it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW, XRay is called in every There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lucasnlm but this is a singleton object, right? So it will really only initialize once. There's only one instance of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "I rather initialize it only if needed. WDYT?" |
||
private fun writeOnBitmap(bitmap: Bitmap, text: String) { | ||
val canvas = Canvas(bitmap) | ||
val textPaint = getTextPaint() | ||
val xStartPoint = (bitmap.width - textPaint.measureText(text)) / 2f | ||
val yStartPoint = bitmap.height / 2f | ||
canvas.drawText(text, xStartPoint, yStartPoint, textPaint) | ||
private var config = XRayConfig() | ||
private val textPaint: Paint by lazy { | ||
Paint().apply { | ||
textSize = TEXT_SIZE.toFloat() | ||
color = TEXT_COLOR | ||
} | ||
} | ||
|
||
private fun getTextPaint(): Paint { | ||
if (textPaint == null) { | ||
textPaint = | ||
Paint().apply { | ||
textSize = TEXT_SIZE.toFloat() | ||
color = TEXT_COLOR | ||
} | ||
private fun writeOnBitmap(bitmap: Bitmap, text: String) { | ||
Canvas(bitmap).run { | ||
val xStartPoint = (bitmap.width - textPaint.measureText(text)) / 2f | ||
val yStartPoint = bitmap.height / 2f | ||
drawText(text, xStartPoint, yStartPoint, textPaint) | ||
} | ||
return textPaint!! | ||
} | ||
|
||
companion object { | ||
private val INSTANCE = XRay() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider changing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense! I will do that. |
||
private const val FRAME_WIDTH = 500 | ||
private const val FRAME_HEIGHT = 150 | ||
private const val XRAY_ALFA = 0.9f | ||
private const val XRAY_ALPHA = 0.9f | ||
private const val TEXT_SIZE = 30 | ||
private const val TEXT_COLOR = Color.RED | ||
|
||
/** Toggles state of XRay. */ | ||
/** Setup XRay using a [XRayConfig] */ | ||
@JvmStatic | ||
fun toggle() { | ||
INSTANCE.isEnabled = !INSTANCE.isEnabled | ||
public fun setup(config: XRayConfig) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need thread-safety? If so, we must instead provide a way to atomically "read and write" -- an
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we don´t need thread-safety. At least I never had thread issues with XRay. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it's ok, was just wondering. |
||
INSTANCE.config = config | ||
} | ||
|
||
/** Toggles state of XRay. */ | ||
public fun toggle() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If so, I suggest we deprecate it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
val config = INSTANCE.config | ||
setup(config.copy(enabled = !config.enabled)) | ||
} | ||
|
||
/** @return `true` if XRay is enabled, `false` otherwise. */ | ||
@JvmStatic | ||
fun isEnabled(): Boolean { | ||
return INSTANCE.isEnabled | ||
public fun isEnabled(): Boolean { | ||
return INSTANCE.config.enabled | ||
} | ||
|
||
/** | ||
* Puts [ViewBuilder]s riblet name in the background of the [View] | ||
* | ||
* @param viewRouter a [ViewRouter] which riblets name should be written. | ||
* @param routerName the riblets name to be written. | ||
* @param view a [View] to put the name behind. | ||
*/ | ||
@JvmStatic | ||
fun apply(viewRouter: ViewRouter<*, *>, view: View) { | ||
internal fun apply(routerName: String, view: View) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good change |
||
val oldBackground = view.background | ||
val bitmap: Bitmap = | ||
if (oldBackground != null) { | ||
drawableToBitmap(oldBackground) | ||
} else { | ||
Bitmap.createBitmap(FRAME_WIDTH, FRAME_HEIGHT, Bitmap.Config.ARGB_8888) | ||
} | ||
INSTANCE.writeOnBitmap(bitmap, getRibletName(viewRouter)) | ||
val newBackground = BitmapDrawable(view.context.resources, bitmap) | ||
newBackground.gravity = Gravity.CENTER | ||
view.background = newBackground | ||
view.alpha = XRAY_ALFA | ||
INSTANCE.writeOnBitmap(bitmap, getShortRibletName(routerName)) | ||
view.background = | ||
BitmapDrawable(view.context.resources, bitmap).apply { | ||
gravity = Gravity.CENTER | ||
} | ||
|
||
if (INSTANCE.config.alphaEnabled) { | ||
view.alpha = XRAY_ALPHA | ||
} | ||
} | ||
|
||
private fun drawableToBitmap(drawable: Drawable): Bitmap { | ||
|
@@ -110,8 +116,13 @@ class XRay private constructor() { | |
return bitmap | ||
} | ||
|
||
private fun getRibletName(viewRouter: ViewRouter<*, *>): String { | ||
return viewRouter.javaClass.simpleName.replace("Router", "") | ||
@VisibleForTesting | ||
internal fun getShortRibletName(originalName: String): String { | ||
return if (originalName != "Router") { | ||
originalName.replace("Router", "") | ||
} else { | ||
originalName | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.uber.rib.core | ||
|
||
/** | ||
* Configuration for XRay. | ||
* @property enabled `true` to enable XRay. By default it is disabled. | ||
* @property alphaEnabled `true` to enable alpha changes when XRay is enabled. | ||
*/ | ||
public data class XRayConfig( | ||
val enabled: Boolean = false, | ||
val alphaEnabled: Boolean = true, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change this because it was leaking a
this
reference to a static function.