Skip to content

Commit

Permalink
Fix UI handling of ErrorAddingFile state
Browse files Browse the repository at this point in the history
We weren't properly showing the empty state when the last file got removed.
  • Loading branch information
grote committed Jun 9, 2023
1 parent e178152 commit b95adf9
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions app/src/main/java/org/onionshare/android/ui/share/ShareUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.SnackbarDuration
import androidx.compose.material.SnackbarHostState
import androidx.compose.material.SnackbarResult
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.rememberBottomSheetScaffoldState
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
Expand Down Expand Up @@ -65,6 +67,11 @@ import org.onionshare.android.ui.theme.topBar

private val bottomSheetPeekHeight = 60.dp

private fun isEmptyState(shareState: ShareUiState, filesState: FilesState): Boolean {
return (shareState == ShareUiState.AddingFiles || shareState is ShareUiState.ErrorAddingFile)
&& filesState.files.isEmpty()
}

@Composable
@OptIn(ExperimentalMaterialApi::class)
fun ShareUi(
Expand All @@ -78,9 +85,13 @@ fun ShareUi(
) {
val scaffoldState = rememberBottomSheetScaffoldState()
val offset = getOffsetInDp(scaffoldState.bottomSheetState)
if (shareState == ShareUiState.AddingFiles && filesState.files.isEmpty()) {
val snackbarHostState: SnackbarHostState
if (isEmptyState(shareState, filesState)) {
val normalScaffoldState = rememberScaffoldState()
snackbarHostState = normalScaffoldState.snackbarHostState
Scaffold(
topBar = { ActionBar(navController, R.string.app_name, shareState.allowsModifyingFiles) },
scaffoldState = normalScaffoldState,
floatingActionButton = {
Fab(scaffoldState.bottomSheetState, onFabClicked)
},
Expand All @@ -92,28 +103,11 @@ fun ShareUi(
scaffoldState.bottomSheetState.collapse()
}
} else {
snackbarHostState = scaffoldState.snackbarHostState
LaunchedEffect("showSheet") {
delay(750)
scaffoldState.bottomSheetState.expand()
}
if (shareState is ShareUiState.ErrorAddingFile) {
val errorFile = shareState.errorFile
val text = if (errorFile != null) {
stringResource(R.string.share_error_file_snackbar_text, errorFile.basename)
} else {
stringResource(R.string.share_error_snackbar_text)
}
val action =
if (filesState.files.isEmpty()) null else stringResource(R.string.share_error_snackbar_action)
LaunchedEffect("showSnackbar") {
val snackbarResult = scaffoldState.snackbarHostState.showSnackbar(
message = text,
actionLabel = action,
duration = SnackbarDuration.Long,
)
if (snackbarResult == SnackbarResult.ActionPerformed) onSheetButtonClicked()
}
}
if (!shareState.collapsableSheet && scaffoldState.bottomSheetState.isCollapsed) {
// ensure the bottom sheet is visible
LaunchedEffect(shareState) {
Expand All @@ -135,6 +129,23 @@ fun ShareUi(
MainContent(shareState, filesState, offset, onFileRemove, onRemoveAll, Modifier.padding(innerPadding))
}
}
if (shareState is ShareUiState.ErrorAddingFile) {
val errorFile = shareState.errorFile
val text = if (errorFile != null) {
stringResource(R.string.share_error_file_snackbar_text, errorFile.basename)
} else {
stringResource(R.string.share_error_snackbar_text)
}
val action = if (filesState.files.isEmpty()) null else stringResource(R.string.share_error_snackbar_action)
LaunchedEffect("showSnackbar") {
val snackbarResult = snackbarHostState.showSnackbar(
message = text,
actionLabel = action,
duration = SnackbarDuration.Long,
)
if (snackbarResult == SnackbarResult.ActionPerformed) onSheetButtonClicked()
}
}
}

@Composable
Expand Down Expand Up @@ -218,7 +229,7 @@ fun MainContent(
onRemoveAll: () -> Unit,
modifier: Modifier,
) {
if (shareState is ShareUiState.AddingFiles && filesState.files.isEmpty()) {
if (isEmptyState(shareState, filesState)) {
Box(
modifier = modifier
.fillMaxWidth()
Expand Down

0 comments on commit b95adf9

Please sign in to comment.