Skip to content

Commit

Permalink
fix: show snackbar informing user of the status when adding artists t…
Browse files Browse the repository at this point in the history
…o collection

#1014
  • Loading branch information
lydavid committed Sep 13, 2024
1 parent 805a329 commit 11991b6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import com.slack.circuit.runtime.CircuitUiState
import com.slack.circuit.runtime.Navigator
import com.slack.circuit.runtime.presenter.Presenter
import kotlinx.coroutines.launch
import ly.david.musicsearch.shared.domain.ActionableResult
import ly.david.musicsearch.shared.domain.listitem.CollectionListItemModel
import ly.david.musicsearch.shared.domain.collection.CollectionRepository
import ly.david.musicsearch.shared.domain.collection.usecase.CreateCollection
import ly.david.musicsearch.shared.domain.collection.usecase.GetAllCollections
import ly.david.musicsearch.shared.feature.collections.create.NewCollection
import ly.david.musicsearch.ui.common.screen.AddToCollectionScreen
import ly.david.musicsearch.ui.common.screen.SnackbarPopResult

internal class AddToCollectionPresenter(
private val screen: AddToCollectionScreen,
Expand Down Expand Up @@ -46,12 +48,12 @@ internal class AddToCollectionPresenter(

is AddToCollectionUiEvent.AddToCollection -> {
scope.launch {
collectionRepository.addToCollection(
val result: ActionableResult = collectionRepository.addToCollection(
collectionId = event.id,
entity = screen.entity,
entityId = screen.id,
)
navigator.pop()
navigator.pop(SnackbarPopResult(message = result.message, actionLabel = result.actionLabel))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Snackbar
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SwipeToDismissBox
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberSwipeToDismissBoxState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
Expand All @@ -22,6 +26,7 @@ import androidx.compose.ui.input.nestedscroll.nestedScroll
import com.slack.circuit.foundation.CircuitContent
import com.slack.circuit.overlay.LocalOverlayHost
import kotlinx.coroutines.launch
import ly.david.musicsearch.shared.domain.common.ifNotNullOrEmpty
import ly.david.musicsearch.shared.domain.network.MusicBrainzEntity
import ly.david.musicsearch.ui.common.event.EventsListScreen
import ly.david.musicsearch.ui.common.fullscreen.DetailsWithErrorHandling
Expand Down Expand Up @@ -74,8 +79,16 @@ internal fun ArtistUi(

Scaffold(
modifier = modifier,
contentWindowInsets = WindowInsets(0, 0, 0, 0),
snackbarHost = { SnackbarHost(hostState = snackbarHostState) },
contentWindowInsets = WindowInsets(0),
snackbarHost = {
SnackbarHost(hostState = snackbarHostState) { snackbarData ->
SwipeToDismissBox(
state = rememberSwipeToDismissBoxState(),
backgroundContent = {},
content = { Snackbar(snackbarData) },
)
}
},
topBar = {
TopAppBarWithFilter(
onBack = {
Expand Down Expand Up @@ -129,12 +142,20 @@ internal fun ArtistUi(
)
AddToCollectionMenuItem {
scope.launch {
overlayHost.showInBottomSheet(
val result = overlayHost.showInBottomSheet(
AddToCollectionScreen(
entity = entity,
id = entityId,
),
)
result.message.ifNotNullOrEmpty {
snackbarHostState.showSnackbar(
message = result.message,
actionLabel = result.actionLabel,
duration = SnackbarDuration.Short,
withDismissAction = true,
)
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ly.david.musicsearch.ui.common.screen

import com.slack.circuit.runtime.screen.PopResult
import com.slack.circuit.runtime.screen.Screen
import ly.david.musicsearch.shared.domain.network.MusicBrainzEntity
import ly.david.musicsearch.ui.common.topappbar.Tab
Expand Down Expand Up @@ -33,6 +34,12 @@ data class AddToCollectionScreen(
val id: String,
) : Screen

@Parcelize
data class SnackbarPopResult(
val message: String = "",
val actionLabel: String? = null,
) : PopResult

@Parcelize
data class DetailsScreen(
val entity: MusicBrainzEntity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import com.slack.circuitx.overlays.BottomSheetOverlay

suspend fun OverlayHost.showInBottomSheet(
screen: Screen,
): Unit = show(
): SnackbarPopResult = show(
@OptIn(ExperimentalMaterial3Api::class)
BottomSheetOverlay(
model = Unit,
onDismiss = {}, // Crashes if we don't include this
onDismiss = { SnackbarPopResult() },
) { _, overlayNavigator ->
CircuitContent(
screen = screen,
onNavEvent = { event ->
when (event) {
is NavEvent.Pop -> overlayNavigator.finish(Unit)
is NavEvent.Pop -> overlayNavigator.finish(event.result as SnackbarPopResult)
else -> {}
}
},
Expand Down

0 comments on commit 11991b6

Please sign in to comment.