From 6b74eb9cccf03844a0052af21c4a8eb82d6f62d4 Mon Sep 17 00:00:00 2001 From: XinyueZ Date: Sun, 7 Oct 2018 00:51:56 +0200 Subject: [PATCH 1/2] Rebased --- .../CloudLandmarkGraphic.kt | 26 +++++++------------ 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/kotlin/cloudlandmarkrecognition/CloudLandmarkGraphic.kt b/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/kotlin/cloudlandmarkrecognition/CloudLandmarkGraphic.kt index 6716fce53d..8dd0412044 100644 --- a/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/kotlin/cloudlandmarkrecognition/CloudLandmarkGraphic.kt +++ b/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/kotlin/cloudlandmarkrecognition/CloudLandmarkGraphic.kt @@ -10,21 +10,16 @@ import com.google.firebase.samples.apps.mlkit.common.GraphicOverlay /** Graphic instance for rendering detected landmark. */ class CloudLandmarkGraphic(overlay: GraphicOverlay) : GraphicOverlay.Graphic(overlay) { - private val rectPaint: Paint - private val landmarkPaint: Paint - private lateinit var landmark: FirebaseVisionCloudLandmark - - init { - - rectPaint = Paint() - rectPaint.color = TEXT_COLOR - rectPaint.style = Paint.Style.STROKE - rectPaint.strokeWidth = STROKE_WIDTH - - landmarkPaint = Paint() - landmarkPaint.color = TEXT_COLOR - landmarkPaint.textSize = TEXT_SIZE + private val rectPaint = Paint().apply { + color = TEXT_COLOR + style = Paint.Style.STROKE + strokeWidth = STROKE_WIDTH } + private val landmarkPaint = Paint().apply { + color = TEXT_COLOR + textSize = TEXT_SIZE + } + private lateinit var landmark: FirebaseVisionCloudLandmark /** * Updates the landmark instance from the detection of the most recent frame. Invalidates the @@ -39,9 +34,6 @@ class CloudLandmarkGraphic(overlay: GraphicOverlay) : GraphicOverlay.Graphic(ove * Draws the landmark block annotations for position, size, and raw value on the supplied canvas. */ override fun draw(canvas: Canvas) { - if (landmark == null) { - throw IllegalStateException("Attempting to draw a null landmark.") - } if (landmark.landmark == null || landmark.boundingBox == null) { return } From c8259f9d162402cba8359790cad8e02b00d389c4 Mon Sep 17 00:00:00 2001 From: XinyueZ Date: Sun, 7 Oct 2018 00:54:34 +0200 Subject: [PATCH 2/2] Define landmark as nullable which is more meanful than lateinit --- .../CloudLandmarkGraphic.kt | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/kotlin/cloudlandmarkrecognition/CloudLandmarkGraphic.kt b/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/kotlin/cloudlandmarkrecognition/CloudLandmarkGraphic.kt index 8dd0412044..bf7da27eeb 100644 --- a/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/kotlin/cloudlandmarkrecognition/CloudLandmarkGraphic.kt +++ b/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/kotlin/cloudlandmarkrecognition/CloudLandmarkGraphic.kt @@ -19,7 +19,7 @@ class CloudLandmarkGraphic(overlay: GraphicOverlay) : GraphicOverlay.Graphic(ove color = TEXT_COLOR textSize = TEXT_SIZE } - private lateinit var landmark: FirebaseVisionCloudLandmark + private var landmark: FirebaseVisionCloudLandmark? = null /** * Updates the landmark instance from the detection of the most recent frame. Invalidates the @@ -34,23 +34,24 @@ class CloudLandmarkGraphic(overlay: GraphicOverlay) : GraphicOverlay.Graphic(ove * Draws the landmark block annotations for position, size, and raw value on the supplied canvas. */ override fun draw(canvas: Canvas) { - if (landmark.landmark == null || landmark.boundingBox == null) { - return - } - - // Draws the bounding box around the LandmarkBlock. - val rect = RectF(landmark.boundingBox) - with(rect) { - left = translateX(left) - top = translateY(top) - right = translateX(right) - bottom = translateY(bottom) - canvas.drawRect(this, rectPaint) - - // Renders the landmark at the bottom of the box. - canvas.drawText(landmark.landmark, left, bottom, landmarkPaint) - } - + landmark?.let { lm -> + if (lm.landmark == null || lm.boundingBox == null) { + return + } + + // Draws the bounding box around the LandmarkBlock. + val rect = RectF(lm.boundingBox) + with(rect) { + left = translateX(left) + top = translateY(top) + right = translateX(right) + bottom = translateY(bottom) + canvas.drawRect(this, rectPaint) + + // Renders the landmark at the bottom of the box. + canvas.drawText(lm.landmark, left, bottom, landmarkPaint) + } + } ?: kotlin.run { throw IllegalStateException("Attempting to draw a null landmark.") } } companion object {