Skip to content

Commit

Permalink
Showing an error toast when the AltText update fails
Browse files Browse the repository at this point in the history
  • Loading branch information
hamorillo committed Dec 20, 2024
1 parent dbbe8bd commit e2ae454
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -37,11 +41,16 @@ import com.gravatar.quickeditor.ui.avatarpicker.AvatarPickerAction
import com.gravatar.quickeditor.ui.avatarpicker.AvatarPickerEvent
import com.gravatar.quickeditor.ui.avatarpicker.AvatarPickerViewModel
import com.gravatar.quickeditor.ui.avatarpicker.AvatarUpdateType
import com.gravatar.quickeditor.ui.avatarpicker.errorStringRes
import com.gravatar.quickeditor.ui.components.QEButton
import com.gravatar.quickeditor.ui.components.QESectionTitle
import com.gravatar.quickeditor.ui.extensions.QESnackbarHost
import com.gravatar.quickeditor.ui.extensions.SnackbarType
import com.gravatar.quickeditor.ui.extensions.showQESnackbar
import com.gravatar.restapi.models.Avatar
import com.gravatar.ui.GravatarTheme
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.net.URI
import java.net.URL
Expand All @@ -56,15 +65,29 @@ internal fun AltTextSection(
onBackPressed()
}

val context = LocalContext.current
val snackState = remember { SnackbarHostState() }
val state by viewModel.uiState.collectAsState()
val lifecycle = LocalLifecycleOwner.current.lifecycle
val scope = rememberCoroutineScope()

LaunchedEffect(Unit) {
withContext(Dispatchers.Main.immediate) {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.actions.collect { action ->
if (action is AvatarPickerAction.AvatarUpdated && action.type == AvatarUpdateType.ALT_TEXT) {
onBackPressed()
when {
action is AvatarPickerAction.AvatarUpdated && action.type == AvatarUpdateType.ALT_TEXT -> {
onBackPressed()
}

action is AvatarPickerAction.AvatarUpdateFailed && action.type == AvatarUpdateType.ALT_TEXT -> {
scope.launch {
snackState.showQESnackbar(
message = context.getString(action.type.errorStringRes),
snackbarType = SnackbarType.Error,
)
}
}
}
}
}
Expand All @@ -78,6 +101,11 @@ internal fun AltTextSection(
altTextState = altTextState,
onEvent = viewModel::onEvent,
)
QESnackbarHost(
modifier = Modifier
.align(Alignment.BottomStart),
hostState = snackState,
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,18 +460,6 @@ private val SectionError.buttonTextRes: Int
-> R.string.gravatar_qe_avatar_picker_error_retry_cta
}

private val AvatarUpdateType.successStringRes: Int
@StringRes get() = when (this) {
AvatarUpdateType.RATING -> R.string.gravatar_qe_avatar_picker_rating_update_success
AvatarUpdateType.ALT_TEXT -> R.string.gravatar_qe_avatar_picker_alt_text_update_success
}

private val AvatarUpdateType.errorStringRes: Int
@StringRes get() = when (this) {
AvatarUpdateType.RATING -> R.string.gravatar_qe_avatar_picker_rating_update_error
AvatarUpdateType.ALT_TEXT -> R.string.gravatar_qe_avatar_picker_alt_text_update_error
}

private val SectionError.event: AvatarPickerEvent
get() = when (this) {
is SectionError.InvalidToken -> AvatarPickerEvent.HandleAuthFailureTapped
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.gravatar.quickeditor.ui.avatarpicker

import android.net.Uri
import androidx.annotation.StringRes
import com.gravatar.quickeditor.R
import java.io.File

internal sealed class AvatarPickerAction {
Expand Down Expand Up @@ -29,3 +31,15 @@ internal enum class AvatarUpdateType {
RATING,
ALT_TEXT,
}

internal val AvatarUpdateType.successStringRes: Int
@StringRes get() = when (this) {
AvatarUpdateType.RATING -> R.string.gravatar_qe_avatar_picker_rating_update_success
AvatarUpdateType.ALT_TEXT -> R.string.gravatar_qe_avatar_picker_alt_text_update_success
}

internal val AvatarUpdateType.errorStringRes: Int
@StringRes get() = when (this) {
AvatarUpdateType.RATING -> R.string.gravatar_qe_avatar_picker_rating_update_error
AvatarUpdateType.ALT_TEXT -> R.string.gravatar_qe_avatar_picker_alt_text_update_error
}

0 comments on commit e2ae454

Please sign in to comment.