Skip to content

Commit

Permalink
Update CodeEditor and implement SwiftPoet library
Browse files Browse the repository at this point in the history
This update integrates the SwiftPoet library in the CodeEditor module and modifies several classes to support this integration. Specifically, the XML file extension is enabled for the CodeLang class and language parsing is updated for the file reader. New functionalities to generate Swift code are added to the IconWriter and Svg2SwiftUI classes.
  • Loading branch information
jacobrein committed Apr 23, 2024
1 parent c042f85 commit 6ee6657
Show file tree
Hide file tree
Showing 9 changed files with 617 additions and 48 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies {
val ktor_version = "2.3.10"
implementation("io.ktor:ktor-client-core:$ktor_version")
implementation("io.ktor:ktor-client-cio:$ktor_version")
implementation("io.outfoxx:swiftpoet:1.6.5")
}

compose.desktop {
Expand Down
96 changes: 81 additions & 15 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,21 @@ val outputDirectory = File(System.getProperty("user.home") + "/Desktop/svgconver
@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
@Composable
fun FrameWindowScope.App() {
var svgText by remember { mutableStateOf("") }
var showPreview by remember { mutableStateOf(true) }
val listOfConversions = remember { mutableStateListOf<FileInfo>() }
val filesToConvert = remember { mutableStateListOf<File>() }
var chosenVectorFile by remember { mutableStateOf<FileInfo?>(null) }
var language by remember { mutableStateOf(CodeLang.Kotlin) }
val svgText by remember {
derivedStateOf {
when (language) {
CodeLang.Kotlin -> chosenVectorFile?.fileSpec?.toString()
CodeLang.Swift -> chosenVectorFile?.swiftFileSpec?.toString()
CodeLang.XML -> chosenVectorFile?.file?.readText()
else -> null
} ?: ""
}
}

var dragState by remember { mutableStateOf(false) }

Expand Down Expand Up @@ -236,7 +246,7 @@ fun FrameWindowScope.App() {
OutlinedCard(
onClick = {
chosenVectorFile = it
svgText = it.fileSpec.toString()
//svgText = it.fileSpec.toString()
}
) {
ListItem(
Expand Down Expand Up @@ -277,27 +287,83 @@ fun FrameWindowScope.App() {
modifier = Modifier.matchParentSize()
) {
val clipboard = LocalClipboardManager.current
IconButton(
onClick = { clipboard.setText(AnnotatedString(svgText)) },
enabled = svgText.isNotEmpty(),
modifier = Modifier.align(Alignment.End)
) { Icon(Icons.Default.CopyAll, null) }
Row(
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.fillMaxWidth()
) {
var showDropDown by remember { mutableStateOf(false) }
ExposedDropdownMenuBox(
showDropDown,
onExpandedChange = { showDropDown = it },
modifier = Modifier.padding(horizontal = 4.dp)
) {
ExposedDropdownMenu(
showDropDown,
onDismissRequest = { showDropDown = false }
) {
DropdownMenuItem(
text = { Text("Kotlin") },
onClick = {
language = CodeLang.Kotlin
showDropDown = false
}
)
DropdownMenuItem(
text = { Text("Swift") },
onClick = {
language = CodeLang.Swift
showDropDown = false
}
)
DropdownMenuItem(
text = { Text("Original") },
onClick = {
language = CodeLang.XML
showDropDown = false
}
)
}
OutlinedTextField(
when (language) {
CodeLang.Kotlin -> "Kotlin"
CodeLang.Swift -> "Swift"
else -> "Original"
},
onValueChange = {},
readOnly = true,
singleLine = true,
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = showDropDown) },
modifier = Modifier.menuAnchor()
)
}

IconButton(
onClick = { clipboard.setText(AnnotatedString(svgText)) },
enabled = svgText.isNotEmpty(),
) { Icon(Icons.Default.CopyAll, null) }
}

HorizontalDivider()

SelectionContainer(
modifier = Modifier
.fillMaxHeight()
.verticalScroll(verticalScroll),
) {
val language = CodeLang.Kotlin
val parser = remember { PrettifyParser() }
val themeState by remember { mutableStateOf(CodeThemeType.Monokai) }
val theme = remember(themeState) { themeState.theme }
val parsedCode = remember(svgText) {
parseCodeAsAnnotatedString(
parser = parser,
theme = theme,
lang = language,
code = svgText
)
val parsedCode = remember(svgText, language) {
if (language == CodeLang.XML) {
AnnotatedString(svgText)
} else {
parseCodeAsAnnotatedString(
parser = parser,
theme = theme,
lang = language,
code = svgText
)
}
}
Text(
parsedCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package androidx.compose.material.icons.generator

import androidx.compose.ui.util.fastZip
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.MemberName
Expand Down Expand Up @@ -103,23 +104,30 @@ class IconWriter(
icons: Map<File, Icon>,
iconNamePredicate: (String) -> Boolean = { true },
): List<FileInfo> {

return icons.filter { icon ->
iconNamePredicate(icon.value.kotlinName)
}.map { icon ->
val iconName = icon.value.kotlinName

val vector = IconParser(icon.value).parse()

val (fileSpec, _) = VectorAssetGenerator(
iconName,
groupPackage,
vector,
generatePreview
).createFileSpec(groupClass)
FileInfo(fileSpec, icon.value.kotlinName, icon.key)
}
return icons
.filter { icon -> iconNamePredicate(icon.value.kotlinName) }
.map { icon ->
val iconName = icon.value.kotlinName

val vector = IconParser(icon.value).parse()

val (fileSpec, _) = VectorAssetGenerator(
iconName,
groupPackage,
vector,
generatePreview
).createFileSpec(groupClass)
FileInfo(fileSpec, icon.value.kotlinName, icon.key)
}
.fastZip(Svg2SwiftUi.parseToString("asdf", icons.keys.toList())) { fi, fs ->
fi.copy(swiftFileSpec = fs)
}
}
}

data class FileInfo(val fileSpec: FileSpec, val name: String, val file: File? = null)
data class FileInfo(
val fileSpec: FileSpec,
val name: String,
val file: File? = null,
val swiftFileSpec: io.outfoxx.swiftpoet.FileSpec? = null,
)
Loading

0 comments on commit 6ee6657

Please sign in to comment.