-
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
Open
HenriqueDelgad0
wants to merge
4
commits into
main
Choose a base branch
from
feature/show-groups
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
111 changes: 107 additions & 4 deletions
111
app/src/main/java/br/com/mob1st/bet/features/groups/GroupTabScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 arrow.optics.Fold.Companion.string | ||
import br.com.mob1st.bet.R | ||
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(listGroups) { | ||
viewModel.getGroups() | ||
} | ||
|
||
Column( | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
verticalArrangement = Arrangement.Center , | ||
verticalArrangement = Arrangement.Top , | ||
modifier = Modifier.fillMaxSize() | ||
) { | ||
ProvideTextStyle(MaterialTheme.typography.headlineMedium){ | ||
Text("Seus grupos") | ||
Text(stringResource(id = R.string.group_title)) | ||
} | ||
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 = { stringResource(id = R.string.group_empty_list_title ) }, | ||
description = { stringResource(id = R.string.group_empty_list_description) } | ||
) | ||
} |
54 changes: 54 additions & 0 deletions
54
app/src/main/java/br/com/mob1st/bet/features/groups/GroupTabViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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.GetGroupsUseCase | ||
import br.com.mob1st.bet.features.groups.domain.GroupEntry | ||
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 getGroupsUseCase: GetGroupsUseCase | ||
) : 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() { | ||
setAsync { | ||
val listGroups = getGroupsUseCase() | ||
logger.d("fetch ${listGroups.size} groups") | ||
_groups.value = listGroups | ||
it.data(data = it.data.copy(groupList = listGroups)) | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
app/src/main/java/br/com/mob1st/bet/features/groups/domain/GetGroupsUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package br.com.mob1st.bet.features.groups.domain | ||
|
||
import br.com.mob1st.bet.features.profile.domain.UserRepository | ||
import org.koin.core.annotation.Factory | ||
|
||
@Factory | ||
class GetGroupsUseCase( | ||
private val repository: GroupRepository, | ||
private val userRepository: UserRepository | ||
) { | ||
|
||
suspend operator fun invoke(): List<GroupEntry> { | ||
return repository.getGroups(userRepository.get()) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,12 @@ class CreateGroupException( | |
override fun logProperties(): Map<String, Any> { | ||
return (groupEntry to competitionEntry).toLogMap() | ||
} | ||
} | ||
|
||
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. |
||
cause: Throwable | ||
) : Exception("Unable to get groups from user", cause), Debuggable { | ||
override fun logProperties(): Map<String, Any?> { | ||
return mapOf() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
tem algum motivo pra essa função ser pública?
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.
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.