Skip to content

Commit

Permalink
Refactor extension checkers
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler authored and egorikftp committed Jul 23, 2024
1 parent dafc372 commit 4d49873
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object IconParser {

@Throws(IllegalStateException::class)
fun toVector(path: Path): IconParserOutput {
val iconType = IconTypeParser.getIconType(path.extension) ?: error("File not SVG or XML")
val iconType = IconType.from(path.extension) ?: error("File not SVG or XML")

val fileName = getIconName(fileName = path.name)
val icon = when (iconType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.github.composegears.valkyrie.parser

import java.nio.file.Path
import kotlin.io.path.extension

@PublishedApi
internal enum class IconType(val extension: String) {
SVG("svg"),
XML("xml"),
;

companion object {
fun from(extension: String): IconType? = when {
extension.isSvg -> SVG
extension.isXml -> XML
else -> null
}
}
}

inline val String?.isSvg: Boolean get() = equals(other = IconType.SVG.extension, ignoreCase = true)
inline val String?.isXml: Boolean get() = equals(other = IconType.XML.extension, ignoreCase = true)

inline val Path.isSvg: Boolean get() = extension.isSvg
inline val Path.isXml: Boolean get() = extension.isXml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@
package io.github.composegears.valkyrie.ui.extension

inline fun String?.or(default: String): String = this ?: default

fun String.isSvg() = equals(other = "svg", ignoreCase = true)
fun String.isXml() = equals(other = "xml", ignoreCase = true)
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import com.intellij.openapi.fileChooser.FileChooserDescriptor
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Condition
import com.intellij.openapi.vfs.VirtualFile
import io.github.composegears.valkyrie.ui.extension.isSvg
import io.github.composegears.valkyrie.ui.extension.isXml
import io.github.composegears.valkyrie.parser.isSvg
import io.github.composegears.valkyrie.parser.isXml
import io.github.composegears.valkyrie.ui.extension.toPath
import io.github.composegears.valkyrie.ui.foundation.theme.LocalProject
import java.nio.file.Path
Expand All @@ -26,10 +26,9 @@ fun rememberFilePicker(): Picker<Path?> {
return remember {
FilePicker(
project = project,
filterCondition = { path ->
val extension = path.extension

extension != null && (extension.isSvg() || extension.isXml())
filterCondition = { vf ->
val extension = vf.extension
extension.isSvg || extension.isXml
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import com.intellij.openapi.fileChooser.FileChooserDescriptor
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Condition
import com.intellij.openapi.vfs.VirtualFile
import io.github.composegears.valkyrie.ui.extension.isSvg
import io.github.composegears.valkyrie.ui.extension.isXml
import io.github.composegears.valkyrie.parser.isSvg
import io.github.composegears.valkyrie.parser.isXml
import io.github.composegears.valkyrie.ui.foundation.theme.LocalProject
import java.nio.file.Path
import kotlinx.coroutines.Dispatchers
Expand All @@ -25,10 +25,9 @@ fun rememberMultipleFilesPicker(): Picker<List<Path>> {
return remember {
MultipleFilesPicker(
project = project,
filterCondition = { path ->
val extension = path.extension

extension != null && (extension.isSvg() || extension.isXml())
filterCondition = { vf ->
val extension = vf.extension
extension.isSvg || extension.isXml
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import io.github.composegears.valkyrie.generator.imagevector.ImageVectorGenerato
import io.github.composegears.valkyrie.generator.imagevector.ImageVectorGeneratorConfig
import io.github.composegears.valkyrie.generator.imagevector.ImageVectorSpecOutput
import io.github.composegears.valkyrie.parser.IconParser
import io.github.composegears.valkyrie.parser.isSvg
import io.github.composegears.valkyrie.parser.isXml
import io.github.composegears.valkyrie.processing.writter.FileWriter
import io.github.composegears.valkyrie.settings.InMemorySettings
import io.github.composegears.valkyrie.settings.ValkyriesSettings
Expand Down Expand Up @@ -192,7 +194,7 @@ class IconPackConversionViewModel(
}

private fun List<Path>.processFiles() {
val paths = filter { it.isRegularFile() && (it.extension == "xml" || it.extension == "svg") }
val paths = filter { it.isRegularFile() && (it.isXml || it.isSvg) }

if (paths.isNotEmpty()) {
_state.updateState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.toPainter
import com.android.ide.common.vectordrawable.VdPreview
import io.github.composegears.valkyrie.parser.SvgToXmlParser
import io.github.composegears.valkyrie.parser.isSvg
import io.github.composegears.valkyrie.parser.isXml
import java.nio.file.Path
import kotlin.io.path.createTempFile
import kotlin.io.path.extension
import kotlin.io.path.name
import kotlin.io.path.readText

fun Path.toPainterOrNull(): Painter? = when (extension) {
"svg" -> svgToPainter()
"xml" -> xmlToPainter()
fun Path.toPainterOrNull(): Painter? = when {
isSvg -> svgToPainter()
isXml -> xmlToPainter()
else -> error("Unsupported file type: $extension")
}

Expand Down

0 comments on commit 4d49873

Please sign in to comment.