fix(android): dismiss existing snackbars when showing a new one

This commit is contained in:
Harsh Shandilya 2023-06-07 02:37:43 +05:30
parent 7d491de86c
commit 2bb71238bc
No known key found for this signature in database

View file

@ -55,13 +55,13 @@ private fun ImportOption(
val importAction = val importAction =
rememberLauncherForActivityResult(GetContent()) { uri -> rememberLauncherForActivityResult(GetContent()) { uri ->
if (uri == null) { if (uri == null) {
coroutineScope.launch { snackbarHostState.showSnackbar("No file selected") } coroutineScope.launch { snackbarHostState.showSnackbarDismissing("No file selected") }
return@rememberLauncherForActivityResult return@rememberLauncherForActivityResult
} }
coroutineScope.launch { coroutineScope.launch {
context.contentResolver.openInputStream(uri)?.use { stream -> context.contentResolver.openInputStream(uri)?.use { stream ->
dataTransferRepository.importPosts(stream) dataTransferRepository.importPosts(stream)
snackbarHostState.showSnackbar("Successfully imported posts") snackbarHostState.showSnackbarDismissing("Successfully imported posts")
} }
} }
} }
@ -84,13 +84,13 @@ private fun ExportOption(
val exportAction = val exportAction =
rememberLauncherForActivityResult(CreateDocument(MIME_TYPE)) { uri -> rememberLauncherForActivityResult(CreateDocument(MIME_TYPE)) { uri ->
if (uri == null) { if (uri == null) {
coroutineScope.launch { snackbarHostState.showSnackbar("No file selected") } coroutineScope.launch { snackbarHostState.showSnackbarDismissing("No file selected") }
return@rememberLauncherForActivityResult return@rememberLauncherForActivityResult
} }
coroutineScope.launch { coroutineScope.launch {
context.contentResolver.openOutputStream(uri)?.use { stream -> context.contentResolver.openOutputStream(uri)?.use { stream ->
dataTransferRepository.exportPosts(stream) dataTransferRepository.exportPosts(stream)
snackbarHostState.showSnackbar("Successfully exported posts") snackbarHostState.showSnackbarDismissing("Successfully exported posts")
} }
} }
} }
@ -126,3 +126,11 @@ private fun SettingsActionItem(
modifier = modifier.clickable { onClick?.invoke() }, modifier = modifier.clickable { onClick?.invoke() },
) )
} }
/** Shows a Snackbar but dismisses any existing ones first. */
private suspend fun SnackbarHostState.showSnackbarDismissing(
text: String,
) {
currentSnackbarData?.dismiss()
showSnackbar(text)
}