From 55f59ff4ebb669cc5db3c49307772a87e029662f Mon Sep 17 00:00:00 2001 From: Hieu Vu Date: Sat, 7 Dec 2024 19:37:05 +0700 Subject: [PATCH] Reduce duplicate code --- .../canvas/presentation/utils/SVG.kt | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/canvas/src/main/java/dev/arkbuilders/canvas/presentation/utils/SVG.kt b/canvas/src/main/java/dev/arkbuilders/canvas/presentation/utils/SVG.kt index 31645a7..2d87d24 100644 --- a/canvas/src/main/java/dev/arkbuilders/canvas/presentation/utils/SVG.kt +++ b/canvas/src/main/java/dev/arkbuilders/canvas/presentation/utils/SVG.kt @@ -155,38 +155,30 @@ class SVG { event = next() } - + fun extractStrokeFromCommand(originalCommand: String, commandElements: List) { + if (commandElements.size > 3) { + strokeColor = commandElements[3].toULong() + } + if (commandElements.size > 4) { + strokeSize = commandElements[4].toFloat() + } + commands.addLast(SVGCommand.MoveTo.fromString(originalCommand).apply { + paintColor = strokeColor + brushSize = strokeSize + }) + } pathData.split(COMMA).forEach { val command = it.trim() if (command.isEmpty()) return@forEach val commandElements = command.split(" ") + when (command.first()) { SVGCommand.MoveTo.CODE -> { - if (commandElements.size > 3) { - strokeColor = commandElements[3].toULong() - } - if (commandElements.size > 4) { - strokeSize = commandElements[4].toFloat() - } - commands.addLast(SVGCommand.MoveTo.fromString(command).apply { - paintColor = strokeColor - brushSize = strokeSize - }) + extractStrokeFromCommand(originalCommand = command, commandElements = commandElements) } - SVGCommand.AbsLineTo.CODE -> { - if (commandElements.size > 3) { - strokeColor = commandElements[3].toULong() - } - if (commandElements.size > 4) { - strokeSize = commandElements[4].toFloat() - } - commands.addLast(SVGCommand.MoveTo.fromString(command).apply { - paintColor = strokeColor - brushSize = strokeSize - }) + extractStrokeFromCommand(originalCommand = command, commandElements = commandElements) } - SVGCommand.AbsQuadTo.CODE -> { if (commandElements.size > 5) { strokeColor = commandElements[5].toULong()