Skip to content

Commit

Permalink
Resolve OOM when process huge amount of icons
Browse files Browse the repository at this point in the history
  • Loading branch information
egorikftp committed Jul 23, 2024
1 parent 9d841d5 commit cd65da1
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package io.github.composegears.valkyrie.ui.screen.mode.iconpack.conversion
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -42,10 +45,11 @@ import io.github.composegears.valkyrie.ui.foundation.ClearAction
import io.github.composegears.valkyrie.ui.foundation.SettingsAction
import io.github.composegears.valkyrie.ui.foundation.TopAppBar
import io.github.composegears.valkyrie.ui.foundation.WeightSpacer
import io.github.composegears.valkyrie.ui.screen.mode.iconpack.conversion.IconPackConversionState.BatchFilesProcessing
import io.github.composegears.valkyrie.ui.foundation.theme.PreviewTheme
import io.github.composegears.valkyrie.ui.screen.mode.iconpack.conversion.IconPackConversionState.BatchProcessing
import io.github.composegears.valkyrie.ui.screen.mode.iconpack.conversion.IconPackConversionState.IconsPickering
import io.github.composegears.valkyrie.ui.screen.mode.iconpack.conversion.ui.BatchProcessingState
import io.github.composegears.valkyrie.ui.screen.mode.iconpack.conversion.ui.IconPackPickerState
import io.github.composegears.valkyrie.ui.screen.mode.iconpack.conversion.ui.BatchProcessingStateUi
import io.github.composegears.valkyrie.ui.screen.mode.iconpack.conversion.ui.IconPackPickerStateUi
import io.github.composegears.valkyrie.ui.screen.preview.CodePreviewScreen
import io.github.composegears.valkyrie.ui.screen.settings.SettingsScreen
import kotlinx.coroutines.flow.launchIn
Expand All @@ -68,6 +72,7 @@ val IconPackConversionScreen by navDestination<Unit> {
)
}
is ConversionEvent.ExportCompleted -> {
@Suppress("UnstableApiUsage")
writeAction {
VirtualFileManager.getInstance().syncRefresh()
}
Expand Down Expand Up @@ -133,7 +138,7 @@ private fun IconPackConversionUi(
) {
Column(modifier = Modifier.fillMaxSize()) {
TopAppBar {
if (state is BatchFilesProcessing) {
if (state is BatchProcessing.IconPackCreationState) {
ClearAction(onClear = onReset)
}
AppBarTitle(title = "IconPack generation")
Expand All @@ -142,12 +147,18 @@ private fun IconPackConversionUi(
}
when (state) {
is IconsPickering -> {
IconPackPickerState(onPickerEvent = onPickEvent)
IconPackPickerStateUi(onPickerEvent = onPickEvent)
}
is BatchFilesProcessing -> {
BatchProcessingState(
BatchProcessing.ExportingState -> {
LoadingStateUi(message = "Exporting icons...")
}
BatchProcessing.ImportValidationState -> {
LoadingStateUi(message = "Processing icons...")
}
is BatchProcessing.IconPackCreationState -> {
BatchProcessingStateUi(
modifier = Modifier.nestedScroll(nestedScrollConnection),
icons = state.iconsToProcess,
state = state,
onDeleteIcon = onDeleteIcon,
onUpdatePack = updatePack,
onPreviewClick = onPreviewClick,
Expand All @@ -157,7 +168,7 @@ private fun IconPackConversionUi(
}
}

if (state is BatchFilesProcessing) {
if (state is BatchProcessing.IconPackCreationState) {
AnimatedVisibility(
modifier = Modifier
.align(Alignment.BottomCenter)
Expand Down Expand Up @@ -190,3 +201,26 @@ private fun IconPackConversionUi(
}
}
}

@Composable
private fun LoadingStateUi(message: String) {
Box(modifier = Modifier.fillMaxSize()) {
Column(
modifier = Modifier.align(Alignment.Center),
verticalArrangement = Arrangement.spacedBy(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
) {
CircularProgressIndicator()
Text(
text = message,
style = MaterialTheme.typography.bodySmall,
)
}
}
}

@Preview
@Composable
private fun LoadingStateUiPreview() = PreviewTheme {
LoadingStateUi(message = "Exporting icons...")
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package io.github.composegears.valkyrie.ui.screen.mode.iconpack.conversion

import androidx.compose.ui.graphics.painter.Painter
import java.nio.file.Path

sealed interface IconPackConversionState {

data object IconsPickering : IconPackConversionState

data class BatchFilesProcessing(
val iconsToProcess: List<BatchIcon> = emptyList(),
) : IconPackConversionState {

val exportEnabled: Boolean
get() = iconsToProcess.isNotEmpty() &&
iconsToProcess.all { it is BatchIcon.Valid } &&
iconsToProcess.all { icon ->
icon.iconName.value.isNotEmpty() &&
!icon.iconName.value.contains(" ")
}
sealed interface BatchProcessing : IconPackConversionState {

data class IconPackCreationState(
val icons: List<BatchIcon>,
val exportEnabled: Boolean,
) : BatchProcessing

data object ImportValidationState : BatchProcessing
data object ExportingState : BatchProcessing
}
}

Expand All @@ -34,7 +31,6 @@ sealed interface BatchIcon {
val iconPack: IconPack,
override val iconName: IconName,
override val extension: String,
val painter: Painter,
val path: Path,
) : BatchIcon
}
Expand Down
Loading

0 comments on commit cd65da1

Please sign in to comment.