Skip to content

Commit

Permalink
✅ Update samples to match new API
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceglb committed May 1, 2024
1 parent 71c4e8d commit 5331394
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 76 deletions.
10 changes: 8 additions & 2 deletions samples/sample-compose/composeApp/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MainUiState> = MutableStateFlow(MainUiState())
val uiState: StateFlow<MainUiState> = _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<MainUiState> = MutableStateFlow(MainUiState())
val uiState: StateFlow<MainUiState> = _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<PlatformFile> = emptySet(), // Set instead of List to avoid duplicates
val directory: PlatformDirectory? = null,
val loading: Boolean = false
val files: Set<PlatformFile> = 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?

0 comments on commit 5331394

Please sign in to comment.