Skip to content

Commit

Permalink
correcting conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
ntissieres authored and alejandrocalles committed Jun 2, 2024
1 parent fe756db commit e1203d9
Showing 1 changed file with 22 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
package com.github.swent.echo.compose.components

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
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.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
Expand All @@ -24,7 +16,6 @@ import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.github.swent.echo.R
import com.github.swent.echo.data.model.Event
import com.github.swent.echo.viewmodels.myevents.MyEventStatus
import com.github.swent.echo.viewmodels.myevents.MyEventsViewModel

/**
Expand All @@ -43,46 +34,27 @@ fun JoinEventButton(event: Event, isOnline: Boolean, buttonWidth: Dp, refreshEve
val myEventsViewModel: MyEventsViewModel = hiltViewModel()
// Observe the list of events that the user has joined.
val joinedEvents by myEventsViewModel.joinedEvents.collectAsState()

// Snackbar for displaying errors
val snackbarHostState by remember { mutableStateOf(SnackbarHostState()) }
val status by myEventsViewModel.status.collectAsState()
val errorMessage =
LocalContext.current.resources.getString(R.string.event_join_error_network_failure)
if (status is MyEventStatus.Error) {
LaunchedEffect(status) {
snackbarHostState.showSnackbar(errorMessage, withDismissAction = true)
myEventsViewModel.resetErrorState()
}
}

val paddingValues = PaddingValues(2.dp)

Box {
// Create a button for joining or leaving the event.
Button(
// The button is enabled if the user is online and the event is not full.
enabled = isOnline && (event.participantCount < event.maxParticipants),
// When the button is clicked, the user joins or leaves the event.
onClick = { myEventsViewModel.joinOrLeaveEvent(event, refreshEvents) },
// Set the width of the button and a test tag for testing purposes.
modifier =
Modifier.width(buttonWidth)
.align(Alignment.Center)
.testTag("list_join_event_${event.eventId}"),
contentPadding = paddingValues
) {
// The text of the button depends on whether the user has joined the event.
Text(
if (joinedEvents.map { it.eventId }.contains(event.eventId))
// If the user has joined the event, the button says "Leave".
stringResource(id = R.string.list_drawer_leave_event)
else
// If the user has not joined the event, the button says "Join".
stringResource(id = R.string.list_drawer_join_event),
textAlign = TextAlign.Center
)
}
SnackbarHost(hostState = snackbarHostState, modifier = Modifier.align(Alignment.Center))
// Padding inside button
val paddingButton = PaddingValues(2.dp)
// Create a button for joining or leaving the event.
Button(
// The button is enabled if the user is online and the event is not full.
enabled = isOnline && (event.participantCount < event.maxParticipants),
// When the button is clicked, the user joins or leaves the event.
onClick = { myEventsViewModel.joinOrLeaveEvent(event, refreshEvents) },
// Set the width of the button and a test tag for testing purposes.
modifier = Modifier.width(buttonWidth).testTag("list_join_event_${event.eventId}"),
contentPadding = paddingButton
) {
// The text of the button depends on whether the user has joined the event.
Text(
if (joinedEvents.map { it.eventId }.contains(event.eventId))
// If the user has joined the event, the button says "Leave".
stringResource(id = R.string.list_drawer_leave_event)
else
// If the user has not joined the event, the button says "Join".
stringResource(id = R.string.list_drawer_join_event),
textAlign = TextAlign.Center
)
}
}

0 comments on commit e1203d9

Please sign in to comment.