Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

feat: async loading of files #101

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/jvmMain/kotlin/ui/controls/Controls.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ui.controls

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
Expand All @@ -16,7 +17,7 @@ import viewmodel.FilesViewModel
fun Controls(viewModel: FilesViewModel) {
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
modifier = Modifier.wrapContentSize(align = Alignment.Companion.CenterStart, unbounded = true),
modifier = Modifier.wrapContentSize(align = Alignment.Companion.CenterStart, unbounded = true).height(40.dp),
) {
OpenFileButton(viewModel)
SaveButton(viewModel)
Expand Down
29 changes: 28 additions & 1 deletion src/jvmMain/kotlin/ui/controls/OpenFileButton.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package ui.controls

import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.scale
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.AwtWindow
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import viewmodel.FileTypes
import viewmodel.FilesViewModel
import java.awt.FileDialog
Expand All @@ -18,19 +30,34 @@ import java.nio.file.Path
@Composable
fun OpenFileButton(viewModel: FilesViewModel) {
var isOpen by remember { mutableStateOf(false) }
var loadCount by remember { mutableStateOf(0) }

if (isOpen) {
FileDialog(
onCloseRequest = { directory, file ->
isOpen = false
if (directory != null && file != null) {
viewModel.loadFile(Path.of(directory, file))
CoroutineScope(Dispatchers.Default).launch {
loadCount++
try {
viewModel.loadFile(Path.of(directory, file))
} finally {
loadCount--
}
}
}
},
)
}

Button(onClick = { isOpen = true }) {
if (loadCount > 0) {
CircularProgressIndicator(
color = MaterialTheme.colorScheme.inverseOnSurface,
modifier = Modifier.fillMaxHeight().aspectRatio(1f).scale(0.9f),
)
Spacer(Modifier.width(16.dp))
}
Text("Open file")
}
}
Expand Down