@@ -11,15 +11,15 @@ import io.github.composegears.valkyrie.generator.jvm.imagevector.ImageVectorGene
1111import io.github.composegears.valkyrie.parser.unified.ParserType
1212import io.github.composegears.valkyrie.parser.unified.SvgXmlParser
1313import io.github.composegears.valkyrie.parser.unified.ext.toIOPath
14+ import io.github.composegears.valkyrie.sdk.core.extensions.safeAs
1415import io.github.composegears.valkyrie.ui.di.DI
1516import io.github.composegears.valkyrie.ui.screen.mode.simple.conversion.model.IconContent
1617import io.github.composegears.valkyrie.ui.screen.mode.simple.conversion.model.IconSource.FileBasedIcon
1718import io.github.composegears.valkyrie.ui.screen.mode.simple.conversion.model.IconSource.StringBasedIcon
1819import io.github.composegears.valkyrie.ui.screen.mode.simple.conversion.model.SimpleConversionState
20+ import io.github.composegears.valkyrie.ui.screen.mode.simple.conversion.model.SimpleConversionState.ConversionState
1921import java.nio.file.Path
2022import kotlinx.coroutines.Dispatchers
21- import kotlinx.coroutines.flow.MutableSharedFlow
22- import kotlinx.coroutines.flow.asSharedFlow
2323import kotlinx.coroutines.flow.flowOn
2424import kotlinx.coroutines.flow.launchIn
2525import kotlinx.coroutines.flow.onEach
@@ -32,47 +32,41 @@ class SimpleConversionViewModel(
3232
3333 val inMemorySettings = inject(DI .core.inMemorySettings)
3434
35- private val stateRecord = savedState.recordOf<SimpleConversionState ? >(
35+ private val stateRecord = savedState.recordOf<SimpleConversionState >(
3636 key = " conversionState" ,
37- initialValue = null ,
37+ initialValue = SimpleConversionState . Loading ,
3838 )
3939 val state = stateRecord.asStateFlow()
4040
41- private val _events = MutableSharedFlow <String >()
42- val events = _events .asSharedFlow()
43-
4441 init {
4542 when (params) {
4643 is SimpleConversionParamsSource .PathSource -> selectPath(params.path)
4744 is SimpleConversionParamsSource .TextSource -> fromText(text = params.text, name = params.name)
4845 }
4946 inMemorySettings.settings
5047 .onEach {
51- val currentState = stateRecord.value ? : return @onEach
48+ val currentState = stateRecord.value.safeAs< ConversionState >() ? : return @onEach
5249
5350 when (val source = currentState.iconSource) {
5451 is FileBasedIcon -> {
55- val output = parseIcon(
52+ parseIcon(
5653 path = source.path,
5754 iconName = currentState.iconContent.name,
58- )
59- if (output != null ) {
60- stateRecord.value = SimpleConversionState (
55+ ).onSuccess {
56+ stateRecord.value = ConversionState (
6157 iconSource = FileBasedIcon (source.path),
62- iconContent = output ,
58+ iconContent = it ,
6359 )
6460 }
6561 }
6662 is StringBasedIcon -> {
67- val output = parseIcon(
63+ parseIcon(
6864 text = source.text,
6965 iconName = currentState.iconContent.name,
70- )
71-
72- if (output != null ) {
73- stateRecord.value = SimpleConversionState (
66+ ).onSuccess {
67+ stateRecord.value = ConversionState (
7468 iconSource = StringBasedIcon (source.text),
75- iconContent = output ,
69+ iconContent = it ,
7670 )
7771 }
7872 }
@@ -83,16 +77,19 @@ class SimpleConversionViewModel(
8377 }
8478
8579 fun selectPath (path : Path ) = viewModelScope.launch(Dispatchers .Default ) {
86- val output = parseIcon(path)
87-
88- if (output == null ) {
89- _events .emit(" Failed to parse icon" )
90- } else {
91- stateRecord.value = SimpleConversionState (
92- iconSource = FileBasedIcon (path),
93- iconContent = output,
94- )
95- }
80+ parseIcon(path)
81+ .onFailure {
82+ stateRecord.value = SimpleConversionState .Error (
83+ message = " Failed to parse icon" ,
84+ stacktrace = " Error: ${it.message} " ,
85+ )
86+ }
87+ .onSuccess {
88+ stateRecord.value = ConversionState (
89+ iconSource = FileBasedIcon (path),
90+ iconContent = it,
91+ )
92+ }
9693 }
9794
9895 fun fromText (text : String , name : String ) = pasteFromClipboard(text = text, iconName = name)
@@ -101,55 +98,50 @@ class SimpleConversionViewModel(
10198 text : String ,
10299 iconName : String = "IconName ",
103100 ) = viewModelScope.launch(Dispatchers .Default ) {
104- val output = parseIcon(text = text, iconName = iconName)
105-
106- if (output == null ) {
107- _events .emit(" Failed to parse icon from clipboard" )
108- } else {
109- stateRecord.value = SimpleConversionState (
110- iconSource = StringBasedIcon (text),
111- iconContent = output,
112- )
113- }
101+ parseIcon(text = text, iconName = iconName)
102+ .onFailure {
103+ stateRecord.value = SimpleConversionState .Error (
104+ message = " Failed to parse icon from clipboard" ,
105+ stacktrace = " Error: ${it.message} " ,
106+ )
107+ }
108+ .onSuccess {
109+ stateRecord.value = ConversionState (
110+ iconSource = StringBasedIcon (text),
111+ iconContent = it,
112+ )
113+ }
114114 }
115115
116116 fun changeIconName (name : String ) = viewModelScope.launch(Dispatchers .Default ) {
117- val conversionState = stateRecord.value ? : return @launch
117+ val conversionState = stateRecord.value.safeAs< ConversionState >() ? : return @launch
118118
119- when (conversionState.iconSource) {
119+ when (val source = conversionState.iconSource) {
120120 is FileBasedIcon -> {
121- val output = parseIcon(
122- path = conversionState.iconSource.path,
123- iconName = name,
124- )
125-
126- if (output != null ) {
127- stateRecord.value = SimpleConversionState (
128- iconSource = FileBasedIcon (conversionState.iconSource.path),
129- iconContent = output,
130- )
131- }
121+ parseIcon(path = source.path, iconName = name)
122+ .onSuccess {
123+ stateRecord.value = ConversionState (
124+ iconSource = FileBasedIcon (conversionState.iconSource.path),
125+ iconContent = it,
126+ )
127+ }
132128 }
133129 is StringBasedIcon -> {
134- val output = parseIcon(
135- text = conversionState.iconSource.text,
136- iconName = name,
137- )
138-
139- if (output != null ) {
140- stateRecord.value = SimpleConversionState (
141- iconSource = StringBasedIcon (conversionState.iconSource.text),
142- iconContent = output,
143- )
144- }
130+ parseIcon(text = source.text, iconName = name)
131+ .onSuccess {
132+ stateRecord.value = ConversionState (
133+ iconSource = StringBasedIcon (conversionState.iconSource.text),
134+ iconContent = it,
135+ )
136+ }
145137 }
146138 }
147139 }
148140
149141 private fun parseIcon (
150142 path : Path ,
151143 iconName : String? = null,
152- ): IconContent ? {
144+ ): Result < IconContent > {
153145 return runCatching {
154146 val parserOutput = SvgXmlParser .toIrImageVector(parser = ParserType .Jvm , path.toIOPath())
155147 val name = iconName ? : parserOutput.iconName
@@ -164,13 +156,13 @@ class SimpleConversionViewModel(
164156 code = output.content,
165157 irImageVector = parserOutput.irImageVector,
166158 )
167- }.getOrNull()
159+ }
168160 }
169161
170162 private fun parseIcon (
171163 text : String ,
172164 iconName : String ,
173- ): IconContent ? {
165+ ): Result < IconContent > {
174166 return runCatching {
175167 val parserOutput = SvgXmlParser .toIrImageVector(parser = ParserType .Jvm , text, iconName)
176168
@@ -184,7 +176,7 @@ class SimpleConversionViewModel(
184176 code = output.content,
185177 irImageVector = parserOutput.irImageVector,
186178 )
187- }.getOrNull()
179+ }
188180 }
189181
190182 private fun createGeneratorConfig (): ImageVectorGeneratorConfig {
0 commit comments