@@ -5,6 +5,7 @@ import androidx.lifecycle.viewModelScope
55import com.composegears.leviathan.compose.inject
66import com.composegears.tiamat.navigation.MutableSavedState
77import com.composegears.tiamat.navigation.recordOf
8+ import com.intellij.openapi.project.Project
89import io.github.composegears.valkyrie.generator.jvm.imagevector.ImageVectorGenerator
910import io.github.composegears.valkyrie.generator.jvm.imagevector.ImageVectorGeneratorConfig
1011import io.github.composegears.valkyrie.parser.unified.ParserType
@@ -13,11 +14,12 @@ import io.github.composegears.valkyrie.parser.unified.ext.isSvg
1314import io.github.composegears.valkyrie.parser.unified.ext.isXml
1415import io.github.composegears.valkyrie.parser.unified.ext.toIOPath
1516import io.github.composegears.valkyrie.sdk.core.extensions.safeAs
16- import io.github.composegears.valkyrie.sdk.core.extensions.writeToKt
1717import io.github.composegears.valkyrie.settings.ValkyriesSettings
1818import io.github.composegears.valkyrie.ui.di.DI
1919import io.github.composegears.valkyrie.ui.extension.updateState
2020import io.github.composegears.valkyrie.ui.foundation.picker.PickerEvent
21+ import io.github.composegears.valkyrie.ui.screen.mode.iconpack.common.util.ImageVectorFile
22+ import io.github.composegears.valkyrie.ui.screen.mode.iconpack.common.util.ImageVectorWriter
2123import io.github.composegears.valkyrie.ui.screen.mode.iconpack.conversion.ConversionEvent.OpenPreview
2224import io.github.composegears.valkyrie.ui.screen.mode.iconpack.conversion.IconPackConversionState.BatchProcessing
2325import io.github.composegears.valkyrie.ui.screen.mode.iconpack.conversion.IconPackConversionState.IconsPickering
@@ -36,7 +38,6 @@ import kotlinx.coroutines.flow.launchIn
3638import kotlinx.coroutines.flow.onEach
3739import kotlinx.coroutines.flow.receiveAsFlow
3840import kotlinx.coroutines.launch
39- import kotlinx.coroutines.withContext
4041
4142class IconPackConversionViewModel (
4243 savedState : MutableSavedState ,
@@ -171,7 +172,7 @@ class IconPackConversionViewModel(
171172 _events .send(OpenPreview (output.content))
172173 }
173174
174- fun import () = viewModelScope.launch(Dispatchers .Default ) {
175+ fun import (project : Project ) = viewModelScope.launch(Dispatchers .Default ) {
175176 val icons = when (val state = _state .value) {
176177 is BatchProcessing .IconPackCreationState -> state.icons
177178 else -> return @launch
@@ -181,9 +182,9 @@ class IconPackConversionViewModel(
181182
182183 val settings = inMemorySettings.current
183184
184- icons
185+ val filesToWrite = icons
185186 .filterIsInstance<BatchIcon .Valid >()
186- .forEach { icon ->
187+ .map { icon ->
187188 when (val iconPack = icon.iconPack) {
188189 is IconPack .Nested -> {
189190 val vectorSpecOutput = ImageVectorGenerator .convert(
@@ -194,16 +195,15 @@ class IconPackConversionViewModel(
194195 nestedPackName = iconPack.currentNestedPack,
195196 ),
196197 )
197-
198- withContext(Dispatchers .IO ) {
199- vectorSpecOutput.content.writeToKt(
200- outputDir = when {
201- settings.flatPackage -> settings.iconPackDestination
202- else -> " ${settings.iconPackDestination} /${iconPack.currentNestedPack.lowercase()} "
203- },
204- nameWithoutExtension = vectorSpecOutput.name,
205- )
198+ val outputDir = when {
199+ settings.flatPackage -> settings.iconPackDestination
200+ else -> " ${settings.iconPackDestination} /${iconPack.currentNestedPack.lowercase()} "
206201 }
202+ ImageVectorFile (
203+ directoryPath = outputDir,
204+ fileName = " ${vectorSpecOutput.name} .kt" ,
205+ content = vectorSpecOutput.content,
206+ )
207207 }
208208 is IconPack .Single -> {
209209 val vectorSpecOutput = ImageVectorGenerator .convert(
@@ -214,18 +214,20 @@ class IconPackConversionViewModel(
214214 nestedPackName = " " ,
215215 ),
216216 )
217-
218- withContext(Dispatchers .IO ) {
219- vectorSpecOutput.content.writeToKt(
220- outputDir = settings.iconPackDestination,
221- nameWithoutExtension = vectorSpecOutput.name,
222- )
223- }
217+ ImageVectorFile (
218+ directoryPath = settings.iconPackDestination,
219+ fileName = " ${vectorSpecOutput.name} .kt" ,
220+ content = vectorSpecOutput.content,
221+ )
224222 }
225223 }
226224 }
227225
228- _events .send(ConversionEvent .ImportCompleted )
226+ ImageVectorWriter .saveVectors(
227+ project = project,
228+ files = filesToWrite,
229+ )
230+
229231 reset()
230232 }
231233
@@ -383,6 +385,5 @@ class IconPackConversionViewModel(
383385
384386sealed interface ConversionEvent {
385387 data class OpenPreview (val iconContent : String ) : ConversionEvent
386- data object ImportCompleted : ConversionEvent
387388 data object NothingToImport : ConversionEvent
388389}
0 commit comments