-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/Show groups list #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,133 @@ | ||
package br.com.mob1st.bet.features.groups | ||
|
||
import androidx.compose.foundation.layout.* | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.itemsIndexed | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.Person | ||
import androidx.compose.material3.CircularProgressIndicator | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.ProvideTextStyle | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.lifecycle.compose.ExperimentalLifecycleComposeApi | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import br.com.mob1st.bet.core.ui.compose.LocalLazyListState | ||
import br.com.mob1st.bet.core.ui.compose.LocalSnackbarState | ||
import br.com.mob1st.bet.core.ui.ds.molecule.AddButton | ||
import br.com.mob1st.bet.core.ui.ds.molecule.RetrySnackbar | ||
import br.com.mob1st.bet.core.ui.ds.organisms.FetchedCrossfade | ||
import br.com.mob1st.bet.core.ui.ds.organisms.GroupRow | ||
import br.com.mob1st.bet.core.ui.ds.page.DefaultErrorPage | ||
import br.com.mob1st.bet.core.ui.ds.templates.InfoTemplate | ||
import br.com.mob1st.bet.core.ui.state.AsyncState | ||
import br.com.mob1st.bet.core.ui.state.SimpleMessage | ||
import br.com.mob1st.bet.core.utils.extensions.ifNotEmpty | ||
|
||
@OptIn(ExperimentalLifecycleComposeApi::class) | ||
@Composable | ||
fun GroupsTabScreen( | ||
viewModel: GroupTabViewModel, | ||
onNavigateToCreateGroups: () -> Unit, | ||
onNavigateToGroupDetails: () -> Unit | ||
) { | ||
val state by viewModel.uiState.collectAsStateWithLifecycle() | ||
val listGroups = viewModel.groups | ||
|
||
LaunchedEffect(key1 = true) { | ||
listGroups.collect() { | ||
viewModel.getGroups() | ||
} | ||
} | ||
|
||
Column( | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
verticalArrangement = Arrangement.Center , | ||
verticalArrangement = Arrangement.Top , | ||
modifier = Modifier.fillMaxSize() | ||
) { | ||
ProvideTextStyle(MaterialTheme.typography.headlineMedium){ | ||
Text("Seus grupos") | ||
} | ||
AddButton(onNavigateToCreateGroups) | ||
GroupRow(groupName = "Primeiro grupo", currentMembersNumber = 1, maxMembers = 25, onNavigateToGroupDetails) | ||
GroupRow(groupName = "Segundo grupo", currentMembersNumber = 3, maxMembers = 25, onNavigateToGroupDetails) | ||
|
||
GroupsPage( | ||
state = state, | ||
onTryAgain = { viewModel.fromUi(GroupsUIEvent.TryAgain(it)) } , | ||
onDismiss = { viewModel.messageShown(it) } | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
fun GroupsPage( | ||
state: AsyncState<GroupsListData>, | ||
onTryAgain: (SimpleMessage) -> Unit, | ||
onDismiss: (SimpleMessage) -> Unit | ||
) { | ||
Box( | ||
contentAlignment = Alignment.Center | ||
) { | ||
FetchedCrossfade( | ||
state = state, | ||
emptyError = {_, message -> DefaultErrorPage(message, onTryAgain)}, | ||
emptyLoading = { EmptyLoading() }, | ||
empty = { GroupsEmptyData() }, | ||
data = { | ||
GroupsData( | ||
state = it, | ||
onTryAgain = onTryAgain, | ||
onDismiss = onDismiss | ||
) | ||
} | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
fun GroupsData( | ||
state: AsyncState<GroupsListData>, | ||
onTryAgain: (SimpleMessage) -> Unit, | ||
onDismiss: (SimpleMessage) -> Unit | ||
) { | ||
state.messages.ifNotEmpty { | ||
RetrySnackbar( | ||
snackbarHostState = LocalSnackbarState.current, | ||
message = stringResource(id = it.descriptionResId), | ||
onDismiss = { onDismiss(it) }, | ||
onRetry = { onTryAgain(it) } | ||
) | ||
} | ||
LazyColumn( | ||
modifier = Modifier.fillMaxSize(), | ||
state = LocalLazyListState.current | ||
) { | ||
itemsIndexed( | ||
state.data.groupList, | ||
key = { _, item -> item.id }, | ||
) { _, item -> | ||
GroupRow(groupName = item.name, currentMembersNumber = 4, maxMembers = 4) {} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun EmptyLoading() { | ||
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { | ||
CircularProgressIndicator() | ||
} | ||
} | ||
|
||
@Composable | ||
fun GroupsEmptyData() { | ||
InfoTemplate( | ||
icon = { Icon(imageVector = Icons.Default.Person, contentDescription = "groups tab") }, | ||
title = { Text("Sem grupos") }, | ||
description = { Text("Tente criar ou entrar em algum grupo.") } | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package br.com.mob1st.bet.features.groups | ||
|
||
import br.com.mob1st.bet.core.ui.state.FetchedData | ||
import br.com.mob1st.bet.core.ui.state.SimpleMessage | ||
import br.com.mob1st.bet.core.ui.state.StateViewModel | ||
import br.com.mob1st.bet.features.groups.domain.GroupEntry | ||
import br.com.mob1st.bet.features.groups.domain.GroupRepository | ||
import br.com.mob1st.bet.features.profile.domain.UserRepository | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import org.koin.android.annotation.KoinViewModel | ||
|
||
data class GroupsListData( | ||
val groupList: List<GroupEntry> = emptyList() | ||
) : FetchedData { | ||
override fun hasData(): Boolean = groupList.isNotEmpty() | ||
} | ||
|
||
sealed class GroupsUIEvent { | ||
data class TryAgain(val message: SimpleMessage) : GroupsUIEvent() | ||
} | ||
|
||
@KoinViewModel | ||
class GroupTabViewModel( | ||
private val repository: GroupRepository, | ||
private val userRepository: UserRepository | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lá vem textão: Portanto, talvez seja melhor criar uma class UseCase, tipo GetGroupsUseCase, e fazer com que esse usecase use os repositórios pra implementar as regras de negócio. é um detalhe besta em um primeiro momento, mas essas pequenas decisões tem um peso grande conforme o projeto vai crescendo. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pode crer, faz sentido. Como essa tela não ia ter useCase por enquanto acabei fazendo tudo no viewModel mesmo, mas vou me atentar mais nisso nas próximas. |
||
) : StateViewModel<GroupsListData, GroupsUIEvent>(GroupsListData(), loading = false) { | ||
override fun fromUi(uiEvent: GroupsUIEvent) { | ||
when (uiEvent) { | ||
is GroupsUIEvent.TryAgain -> tryAgain(uiEvent.message) | ||
} | ||
} | ||
|
||
private val _groups = MutableStateFlow<List<GroupEntry>>(emptyList()) | ||
val groups: StateFlow<List<GroupEntry>> = _groups | ||
|
||
init { | ||
getGroups() | ||
} | ||
|
||
private fun tryAgain(message: SimpleMessage) { | ||
messageShown(message, loading = true) | ||
getGroups() | ||
} | ||
|
||
fun getGroups() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tem algum motivo pra essa função ser pública? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sim, ele é usado no GroupTabScreen, pra quando eu adicionar um novo grupo a tela ja mostrar a lista atualizada, ai chama essa função de novo. |
||
setAsync { | ||
val listGroups = repository.getGroups(userRepository.get()) | ||
logger.d("fetch ${listGroups.size} groups") | ||
_groups.value = listGroups | ||
it.data(data = it.data.copy(groupList = listGroups)) | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
package br.com.mob1st.bet.features.groups.data | ||
|
||
import br.com.mob1st.bet.core.firebase.awaitWithTimeout | ||
import br.com.mob1st.bet.core.firebase.getStringNotNull | ||
import br.com.mob1st.bet.features.competitions.data.competitions | ||
import br.com.mob1st.bet.features.groups.domain.Group | ||
import br.com.mob1st.bet.features.groups.domain.GroupEntry | ||
import br.com.mob1st.bet.features.profile.data.memberships | ||
import br.com.mob1st.bet.features.profile.data.users | ||
import br.com.mob1st.bet.features.profile.domain.User | ||
|
@@ -43,7 +45,6 @@ class GroupCollection( | |
"ref" to firestore.users.document(founder.id), | ||
"name" to founder.name, | ||
"image" to founder.imageUrl.orEmpty(), | ||
|
||
"points" to 0L | ||
) | ||
) | ||
|
@@ -61,9 +62,20 @@ class GroupCollection( | |
batch.commit().awaitWithTimeout() | ||
} | ||
|
||
suspend fun getGroupsByUserId(founder: User) : List<GroupEntry> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alerta de dica irrelevante: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Foi |
||
val groups = firestore.memberships(founder.id).get().awaitWithTimeout() | ||
return groups.map { doc -> | ||
GroupEntry( | ||
id = doc.id, | ||
name = doc.getStringNotNull(GroupEntry::name.name), | ||
imageUrl = doc.getString(GroupEntry::imageUrl.name) | ||
) | ||
} | ||
} | ||
} | ||
|
||
val FirebaseFirestore.groups get() = | ||
collection("groups") | ||
|
||
fun FirebaseFirestore.members(groupId: String) = | ||
groups.document(groupId).collection("members") | ||
groups.document(groupId).collection("members") |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,13 +27,14 @@ class CreateGroupUseCase( | |
): GroupEntry { | ||
val user = userRepository.get() | ||
|
||
if (user.authType == Anonymous) { | ||
throw NotAuthorizedForItException() | ||
} | ||
// if (user.authType == Anonymous) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pode deletar o código There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Foi There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Foi |
||
// throw NotAuthorizedForItException() | ||
// } | ||
|
||
if (user.membershipCount >= MEMBERSHIP_LIMIT) { | ||
throw MembershipLimitException(user.membershipCount) | ||
} | ||
|
||
val competitionEntry = competitionRepository.getDefaultCompetition().toEntry() | ||
val groupEntry = groupRepository.create( | ||
founder = user, | ||
|
@@ -47,6 +48,7 @@ class CreateGroupUseCase( | |
competitionEntry = competitionEntry | ||
) | ||
) | ||
|
||
return groupEntry | ||
} | ||
|
||
|
@@ -58,7 +60,8 @@ class CreateGroupUseCase( | |
|
||
class MembershipLimitException( | ||
private val currentCount: Int | ||
) : Exception("a user can't have more then $MEMBERSHIP_LIMIT memberships. Your current memberships is $currentCount"), Debuggable { | ||
) : Exception("a user can't have more then $MEMBERSHIP_LIMIT memberships. Your current memberships is $currentCount"), | ||
Debuggable { | ||
override fun logProperties(): Map<String, Any> { | ||
return mapOf( | ||
"currentMemberships" to currentCount, | ||
|
@@ -67,4 +70,5 @@ class MembershipLimitException( | |
} | ||
} | ||
|
||
class NotAuthorizedForItException : Exception("The user have to be logged in with some provider to be able to create and join groups") | ||
class NotAuthorizedForItException : | ||
Exception("The user have to be logged in with some provider to be able to create and join groups") |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ interface GroupRepository { | |
competitionEntry: CompetitionEntry | ||
) : GroupEntry | ||
|
||
suspend fun getGroups(founder: User) : List<GroupEntry> | ||
} | ||
|
||
class CreateGroupException( | ||
|
@@ -32,5 +33,14 @@ class CreateGroupException( | |
override fun logProperties(): Map<String, Any> { | ||
return (groupEntry to competitionEntry).toLogMap() | ||
} | ||
} | ||
|
||
//Faz sentido essa Exception que eu criei? | ||
class GetGroupsListException( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. faz sim. se essa request pro firebase falhar em produção vai ser bem fácil ver ela no crashlytics. |
||
private val id: String, | ||
cause: Throwable | ||
) : Exception("Unable to get groups from user id $id", cause), Debuggable { | ||
override fun logProperties(): Map<String, Any?> { | ||
return mapOf("UserId" to id) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eu acho uma boa já colocar esses textos nos strings resources, porque assim evitamos que por algum vacilo a gente esqueça de remover um deles
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Foi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Foi