From 53313941d62ce66a5f44d0bbd56d9a72fa752b24 Mon Sep 17 00:00:00 2001 From: Vincent Guillebaud Date: Wed, 1 May 2024 17:54:54 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Update=20samples=20to=20match=20new?= =?UTF-8?q?=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../composeApp/src/commonMain/kotlin/App.kt | 10 +- .../vinceglb/sample/core/MainViewModel.kt | 151 +++++++++--------- 2 files changed, 85 insertions(+), 76 deletions(-) diff --git a/samples/sample-compose/composeApp/src/commonMain/kotlin/App.kt b/samples/sample-compose/composeApp/src/commonMain/kotlin/App.kt index 12073dc..7044050 100644 --- a/samples/sample-compose/composeApp/src/commonMain/kotlin/App.kt +++ b/samples/sample-compose/composeApp/src/commonMain/kotlin/App.kt @@ -24,6 +24,8 @@ import io.github.vinceglb.picker.compose.rememberSaverLauncher import io.github.vinceglb.picker.core.PickerSelectionMode import io.github.vinceglb.picker.core.PlatformDirectory import io.github.vinceglb.picker.core.PlatformFile +import io.github.vinceglb.picker.core.baseName +import io.github.vinceglb.picker.core.extension import kotlinx.coroutines.launch import org.jetbrains.compose.ui.tooling.preview.Preview @@ -62,14 +64,18 @@ private fun SampleApp() { ) val saver = rememberSaverLauncher( - fileExtension = "jpg", onResult = { file -> file?.let { files += it } } ) val scope = rememberCoroutineScope() fun saveFile(file: PlatformFile) { scope.launch { - saver.launch(file.readBytes(), file.name, directory?.path) + saver.launch( + bytes = file.readBytes(), + baseName = file.baseName, + extension = file.extension, + initialDirectory = directory?.path + ) } } diff --git a/samples/sample-core/shared/src/commonMain/kotlin/io/github/vinceglb/sample/core/MainViewModel.kt b/samples/sample-core/shared/src/commonMain/kotlin/io/github/vinceglb/sample/core/MainViewModel.kt index c8bd78c..e129764 100644 --- a/samples/sample-core/shared/src/commonMain/kotlin/io/github/vinceglb/sample/core/MainViewModel.kt +++ b/samples/sample-core/shared/src/commonMain/kotlin/io/github/vinceglb/sample/core/MainViewModel.kt @@ -6,90 +6,93 @@ import io.github.vinceglb.picker.core.Picker import io.github.vinceglb.picker.core.PickerSelectionMode import io.github.vinceglb.picker.core.PlatformDirectory import io.github.vinceglb.picker.core.PlatformFile +import io.github.vinceglb.picker.core.extension +import io.github.vinceglb.picker.core.baseName import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch class MainViewModel : KMMViewModel() { - private val _uiState: MutableStateFlow = MutableStateFlow(MainUiState()) - val uiState: StateFlow = _uiState - - fun pickImage() = executeWithLoading { - // Single file mode - val mode = PickerSelectionMode.SingleFile(extensions = listOf("jpg", "jpeg", "png")) - - // Pick a file - val file = Picker.pick( - mode = mode, - title = "Custom title here", - initialDirectory = downloadDirectoryPath() - ) - - // Add file to the state - if (file != null) { - val newFiles = _uiState.value.files + file - _uiState.update { it.copy(files = newFiles) } - } - } - - fun pickImages() = executeWithLoading { - // Multiple files mode - val mode = PickerSelectionMode.MultipleFiles(extensions = listOf("jpg", "jpeg", "png")) - - // Pick files - val files = Picker.pick(mode = mode) - - // Add files to the state - if (files != null) { - // Add files to the state - val newFiles = _uiState.value.files + files - _uiState.update { it.copy(files = newFiles) } - } - } - - fun pickDirectory() = executeWithLoading { - // Directory mode - val mode = PickerSelectionMode.Directory - - // Pick a directory - val directory = Picker.pick(mode) - - // Update the state - if (directory != null) { - _uiState.update { it.copy(directory = directory) } - } - } - - fun saveFile(file: PlatformFile) = executeWithLoading { - // Save a file - val newFile = Picker.save( - bytes = file.readBytes(), - fileName = file.name, - ) - - // Add file to the state - if (newFile != null) { - val newFiles = _uiState.value.files + newFile - _uiState.update { it.copy(files = newFiles) } - } - } - - private fun executeWithLoading(block: suspend () -> Unit) { - viewModelScope.coroutineScope.launch { - _uiState.update { it.copy(loading = true) } - block() - _uiState.update { it.copy(loading = false) } - } - } + private val _uiState: MutableStateFlow = MutableStateFlow(MainUiState()) + val uiState: StateFlow = _uiState + + fun pickImage() = executeWithLoading { + // Single file mode + val mode = PickerSelectionMode.SingleFile(extensions = listOf("jpg", "jpeg", "png")) + + // Pick a file + val file = Picker.pick( + mode = mode, + title = "Custom title here", + initialDirectory = downloadDirectoryPath() + ) + + // Add file to the state + if (file != null) { + val newFiles = _uiState.value.files + file + _uiState.update { it.copy(files = newFiles) } + } + } + + fun pickImages() = executeWithLoading { + // Multiple files mode + val mode = PickerSelectionMode.MultipleFiles(extensions = listOf("jpg", "jpeg", "png")) + + // Pick files + val files = Picker.pick(mode = mode) + + // Add files to the state + if (files != null) { + // Add files to the state + val newFiles = _uiState.value.files + files + _uiState.update { it.copy(files = newFiles) } + } + } + + fun pickDirectory() = executeWithLoading { + // Directory mode + val mode = PickerSelectionMode.Directory + + // Pick a directory + val directory = Picker.pick(mode) + + // Update the state + if (directory != null) { + _uiState.update { it.copy(directory = directory) } + } + } + + fun saveFile(file: PlatformFile) = executeWithLoading { + // Save a file + val newFile = Picker.save( + bytes = file.readBytes(), + baseName = file.baseName, + extension = file.extension + ) + + // Add file to the state + if (newFile != null) { + val newFiles = _uiState.value.files + newFile + _uiState.update { it.copy(files = newFiles) } + } + } + + private fun executeWithLoading(block: suspend () -> Unit) { + viewModelScope.coroutineScope.launch { + _uiState.update { it.copy(loading = true) } + block() + _uiState.update { it.copy(loading = false) } + } + } } data class MainUiState( - val files: Set = emptySet(), // Set instead of List to avoid duplicates - val directory: PlatformDirectory? = null, - val loading: Boolean = false + val files: Set = emptySet(), // Set instead of List to avoid duplicates + val directory: PlatformDirectory? = null, + val loading: Boolean = false ) { - constructor() : this(emptySet(), null, false) + constructor() : this(emptySet(), null, false) } expect fun downloadDirectoryPath(): String?