Skip to content

Commit

Permalink
Remove redundant code from SmileyFaceDrawer
Browse files Browse the repository at this point in the history
Signed-off-by: YuganshT79 <yugansh39@gmail.com>
  • Loading branch information
YuganshT79 committed Jul 14, 2020
1 parent dec91d2 commit 4386efe
Showing 1 changed file with 22 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,15 @@ import dev.yuganshtyagi.smileyrating.SmileyState.*
internal class SmileyFaceDrawer(context: Context, attributeSet: AttributeSet?) {

private var config: SmileyViewConfig = SmileyViewConfig(context, attributeSet)
private var animator: SmileyFaceAnimator = SmileyFaceAnimator(config)

fun drawFace(canvas: Canvas) {
config.paint.color = config.faceColor
config.paint.isAntiAlias = true
config.paint.style = Style.FILL
canvas.drawArc(config.faceBgRect, 0f, 180f, true, config.paint)

when (SmileyState.of(config.defaultRating)) {
Sad -> drawSadFace(canvas)
Neutral -> drawNeutralFace(canvas)
Okay -> drawOkayFace(canvas)
Happy -> drawHappyFace(canvas)
Amazing -> drawAmazingFace(canvas)
}
drawEyes(canvas)
drawFaceForState(canvas, SmileyState.of(config.defaultRating))
}

fun onMeasure(width: Int, height: Int) {
Expand All @@ -38,31 +32,19 @@ internal class SmileyFaceDrawer(context: Context, attributeSet: AttributeSet?) {
config.updateCurrentEyePos()
}

private fun drawSadFace(canvas: Canvas) {
//Draw Eyes
config.paint.color = config.eyesColor
config.paint.style = Style.FILL
canvas.drawCircle(
config.currEyeLX.toFloat(),
config.currEyeY.toFloat(),
config.eyeRadius,
config.paint
)
canvas.drawCircle(
config.currEyeRX.toFloat(),
config.currEyeY.toFloat(),
config.eyeRadius,
config.paint
)

//Draw mouth
private fun drawFaceForState(canvas: Canvas, state: SmileyState) {
//update paint
config.paint.color = config.mouthColor
config.paint.style = Style.STROKE
canvas.drawArc(config.sadFaceRect, 0f, -180f, false, config.paint)
when (state) {
Sad -> drawSadFace(canvas)
Neutral -> drawNeutralFace(canvas)
Okay, Happy -> drawHappyFace(canvas, state)
Amazing -> drawAmazingFace(canvas)
}
}

private fun drawNeutralFace(canvas: Canvas) {
//Draw Eyes
private fun drawEyes(canvas: Canvas) {
config.paint.color = config.eyesColor
config.paint.style = Style.FILL
canvas.drawCircle(
Expand All @@ -77,10 +59,13 @@ internal class SmileyFaceDrawer(context: Context, attributeSet: AttributeSet?) {
config.eyeRadius,
config.paint
)
}

//Draw mouth
config.paint.color = config.mouthColor
config.paint.style = Style.STROKE
private fun drawSadFace(canvas: Canvas) {
canvas.drawArc(config.sadFaceRect, 0f, -180f, false, config.paint)
}

private fun drawNeutralFace(canvas: Canvas) {
canvas.drawLine(
config.neutralFaceRect.left,
config.neutralFaceRect.top,
Expand All @@ -90,77 +75,19 @@ internal class SmileyFaceDrawer(context: Context, attributeSet: AttributeSet?) {
)
}

private fun drawOkayFace(canvas: Canvas) {
//Draw Eyes
config.paint.color = config.eyesColor
config.paint.style = Style.FILL
canvas.drawCircle(
config.currEyeLX.toFloat(),
config.currEyeY.toFloat(),
config.eyeRadius,
config.paint
)
canvas.drawCircle(
config.currEyeRX.toFloat(),
config.currEyeY.toFloat(),
config.eyeRadius,
config.paint
)

//Draw mouth
config.paint.color = config.mouthColor
config.paint.style = Style.STROKE
canvas.drawArc(config.okayFaceRect, 0f, 180f, false, config.paint)
}

private fun drawHappyFace(canvas: Canvas) {
//Draw Eyes
config.paint.color = config.eyesColor
config.paint.style = Style.FILL
canvas.drawCircle(
config.currEyeLX.toFloat(),
config.currEyeY.toFloat(),
config.eyeRadius,
config.paint
)
canvas.drawCircle(
config.currEyeRX.toFloat(),
config.currEyeY.toFloat(),
config.eyeRadius,
config.paint
)

//Draw mouth
config.paint.color = config.mouthColor
config.paint.style = Style.STROKE
canvas.drawArc(config.happyFaceRect, 0f, 180f, false, config.paint)
private fun drawHappyFace(canvas: Canvas, state: SmileyState) {
val rect = if (state is Okay) config.okayFaceRect else config.happyFaceRect
canvas.drawArc(rect, 0f, 180f, false, config.paint)
}

private fun drawAmazingFace(canvas: Canvas) {
//Draw Eyes
config.paint.color = config.eyesColor
config.paint.style = Style.FILL
canvas.drawCircle(
config.currEyeLX.toFloat(),
config.currEyeY.toFloat(),
config.eyeRadius,
config.paint
)
canvas.drawCircle(
config.currEyeRX.toFloat(),
config.currEyeY.toFloat(),
config.eyeRadius,
config.paint
)

//Draw mouth
config.paint.color = config.mouthColor
config.paint.style = Style.FILL
config.paint.color = config.mouthColor
canvas.drawArc(config.amazingFaceRect, 0f, 180f, true, config.paint)

//Draw tongue
config.paint.color = config.tongueColor
config.paint.style = Style.FILL
canvas.drawArc(config.tongueRect, 0f, 180f, false, config.paint)
}
}
}

0 comments on commit 4386efe

Please sign in to comment.