Skip to content

Commit

Permalink
refactor: remove uistate cast in component VM.
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardPaligot committed Feb 16, 2024
1 parent b898a96 commit f50730e
Show file tree
Hide file tree
Showing 26 changed files with 434 additions and 452 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@ fun EventListVM(
modifier: Modifier = Modifier,
viewModel: EventListViewModel = koinViewModel()
) {
val uiState = viewModel.uiState.collectAsState()
when (uiState.value) {
when (val uiState = viewModel.uiState.collectAsState().value) {
is EventListUiState.Loading -> EventList(
events = (uiState.value as EventListUiState.Loading).events,
events = uiState.events,
onEventClicked = {},
modifier = modifier,
isLoading = true
)

is EventListUiState.Failure -> Text(text = stringResource(id = R.string.text_error))
is EventListUiState.Success -> EventList(
events = (uiState.value as EventListUiState.Success).events,
events = uiState.events,
onEventClicked = {
viewModel.savedEventId(it)
onEventClicked()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ fun CoCVM(
modifier: Modifier = Modifier,
viewModel: CoCViewModel = koinViewModel()
) {
val uiState = viewModel.uiState.collectAsState()
when (uiState.value) {
when (val uiState = viewModel.uiState.collectAsState().value) {
is CoCUiState.Loading -> Text(text = stringResource(R.string.text_loading))
is CoCUiState.Failure -> Text(text = stringResource(id = R.string.text_error))
is CoCUiState.Success -> CoC(
coc = (uiState.value as CoCUiState.Success).coc,
coc = uiState.coc,
modifier = modifier,
onReportByPhoneClicked = onReportByPhoneClicked,
onReportByEmailClicked = onReportByEmailClicked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ fun EventVM(
modifier: Modifier = Modifier,
viewModel: EventViewModel = koinViewModel()
) {
val uiState = viewModel.uiState.collectAsState()
when (uiState.value) {
when (val uiState = viewModel.uiState.collectAsState().value) {
is EventUiState.Loading -> Event(
event = (uiState.value as EventUiState.Loading).event,
event = uiState.event,
modifier = modifier,
isLoading = true,
onLinkClicked = onLinkClicked,
Expand All @@ -27,7 +26,7 @@ fun EventVM(

is EventUiState.Failure -> Text(text = stringResource(id = R.string.text_error))
is EventUiState.Success -> Event(
event = (uiState.value as EventUiState.Success).event,
event = uiState.event,
modifier = modifier,
onLinkClicked = onLinkClicked,
onItineraryClicked = onItineraryClicked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ fun InfoCompactVM(
modifier: Modifier = Modifier,
viewModel: InfoViewModel = koinViewModel()
) {
val uiState = viewModel.uiState.collectAsState()
val title = stringResource(id = R.string.screen_info)
when (uiState.value) {
when (val uiState = viewModel.uiState.collectAsState().value) {
is InfoUiState.Loading -> Scaffold(title = title, modifier = modifier) {
EventVM(
onLinkClicked = onLinkClicked,
Expand All @@ -41,18 +40,18 @@ fun InfoCompactVM(
}

is InfoUiState.Success -> {
val uiModel = uiState.value as InfoUiState.Success
val pagerState =
rememberPagerState(pageCount = { uiModel.tabActionsUi.actions.count() })
val pagerState = rememberPagerState(
pageCount = { uiState.tabActionsUi.actions.count() }
)
LaunchedEffect(pagerState.currentPage) {
viewModel.innerScreenConfig(uiModel.tabActionsUi.actions[pagerState.currentPage].route)
viewModel.innerScreenConfig(uiState.tabActionsUi.actions[pagerState.currentPage].route)
}
Scaffold(
title = title,
modifier = modifier,
topActions = uiModel.topActionsUi,
tabActions = uiModel.tabActionsUi,
fabAction = uiModel.fabAction,
topActions = uiState.topActionsUi,
tabActions = uiState.tabActionsUi,
fabAction = uiState.fabAction,
onActionClicked = {
when (it.id) {
ActionIds.DISCONNECT -> {
Expand All @@ -76,7 +75,7 @@ fun InfoCompactVM(
state = pagerState,
modifier = Modifier.padding(it)
) { page ->
when (uiModel.tabActionsUi.actions[page].route) {
when (uiState.tabActionsUi.actions[page].route) {
TabActions.event.route -> EventVM(
onLinkClicked = onLinkClicked,
onItineraryClicked = onItineraryClicked,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ fun MenusVM(
modifier: Modifier = Modifier,
viewModel: MenusViewModel = koinViewModel()
) {
val uiState = viewModel.uiState.collectAsState()
when (uiState.value) {
when (val uiState = viewModel.uiState.collectAsState().value) {
is MenusUiState.Loading -> Menus(
menuItems = (uiState.value as MenusUiState.Loading).menus,
menuItems = uiState.menus,
modifier = modifier,
isLoading = true
)

is MenusUiState.Failure -> Text(text = stringResource(id = R.string.text_error))
is MenusUiState.Success -> Menus(
menuItems = (uiState.value as MenusUiState.Success).menus,
menuItems = uiState.menus,
modifier = modifier,
isLoading = false
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ fun QAndAListVM(
modifier: Modifier = Modifier,
viewModel: QAndAListViewModel = koinViewModel()
) {
val uiState = viewModel.uiState.collectAsState()
when (uiState.value) {
when (val uiState = viewModel.uiState.collectAsState().value) {
is QAndAUiState.Loading -> QAndAList(
qAndA = (uiState.value as QAndAUiState.Loading).qanda,
qAndA = uiState.qanda,
modifier = modifier,
isLoading = true,
onExpandedClicked = {},
Expand All @@ -26,12 +25,10 @@ fun QAndAListVM(

is QAndAUiState.Failure -> Text(text = stringResource(id = R.string.text_error))
is QAndAUiState.Success -> QAndAList(
qAndA = (uiState.value as QAndAUiState.Success).qanda,
qAndA = uiState.qanda,
modifier = modifier,
isLoading = false,
onExpandedClicked = {
viewModel.expanded(it)
},
onExpandedClicked = { viewModel.expanded(it) },
onLinkClicked = onLinkClicked
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import org.gdglille.devfest.android.theme.m3.navigation.Screen
import org.gdglille.devfest.android.theme.m3.networking.feature.NetworkingCompactVM
import org.gdglille.devfest.android.theme.m3.networking.feature.ProfileInputVM
import org.gdglille.devfest.android.theme.m3.networking.feature.VCardQrCodeScanner
import org.gdglille.devfest.android.theme.m3.partners.feature.PartnerDetailOrientableVM
import org.gdglille.devfest.android.theme.m3.partners.feature.PartnersListCompactVM
import org.gdglille.devfest.android.theme.m3.partners.feature.PartnerDetailVM
import org.gdglille.devfest.android.theme.m3.partners.feature.PartnersGridVM
import org.gdglille.devfest.android.theme.m3.schedules.feature.AgendaFiltersCompactVM
import org.gdglille.devfest.android.theme.m3.schedules.feature.ScheduleDetailOrientableVM
import org.gdglille.devfest.android.theme.m3.schedules.feature.ScheduleGridAdaptive
Expand Down Expand Up @@ -208,7 +208,7 @@ fun MainNavigation(
)
}
composable(Screen.PartnerList.route) {
PartnersListCompactVM(
PartnersGridVM(
onPartnerClick = { navController.navigate(Screen.Partner.route(it)) }
)
}
Expand All @@ -219,7 +219,7 @@ fun MainNavigation(
uriPattern = "$rootUri/${Screen.Partner.route}"
})
) {
PartnerDetailOrientableVM(
PartnerDetailVM(
partnerId = it.arguments?.getString("partnerId")!!,
onLinkClicked = { launchUrl(it) },
onItineraryClicked = onItineraryClicked,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ fun ContactsCompactVM(
modifier: Modifier = Modifier,
viewModel: ContactsViewModel = koinViewModel()
) {
val uiState = viewModel.uiState.collectAsState()
when (uiState.value) {
when (val uiState = viewModel.uiState.collectAsState().value) {
is ContactsUiState.Loading -> Text(text = stringResource(id = R.string.text_loading))
is ContactsUiState.Failure -> Text(text = stringResource(id = R.string.text_error))
is ContactsUiState.Success -> {
val usersUi = (uiState.value as ContactsUiState.Success).users
ContactsScreen(
users = usersUi,
users = uiState.users,
modifier = modifier,
onNetworkDeleted = viewModel::deleteNetworking
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ fun MyProfileCompactVM(
modifier: Modifier = Modifier,
viewModel: MyProfileViewModel = koinViewModel()
) {
val uiState = viewModel.uiState.collectAsState()
when (uiState.value) {
when (val uiState = viewModel.uiState.collectAsState().value) {
is MyProfileUiState.Loading -> Text(text = stringResource(id = R.string.text_loading))
is MyProfileUiState.Failure -> Text(text = stringResource(id = R.string.text_error))
is MyProfileUiState.Success -> {
val profileUi = (uiState.value as MyProfileUiState.Success).profile
val profileUi = uiState.profile
if (profileUi.qrCode == null) {
EmptyNetworkingScreen(modifier = modifier)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,27 @@ fun NetworkingCompactVM(
modifier: Modifier = Modifier,
viewModel: NetworkingViewModel = koinViewModel()
) {
val uiState = viewModel.uiState.collectAsState()
val exportPath = viewModel.exportPath.collectAsState(null)
LaunchedEffect(exportPath.value) {
exportPath.value?.let(onContactExportClicked)
}
val title = stringResource(id = R.string.screen_networking)
when (uiState.value) {
when (val uiState = viewModel.uiState.collectAsState().value) {
is NetworkingUiState.Loading -> Scaffold(title = title, modifier = modifier) {
EmptyNetworkingScreen()
}

is NetworkingUiState.Success -> {
val uiModel = uiState.value as NetworkingUiState.Success
val pagerState: PagerState =
rememberPagerState(pageCount = { uiModel.tabActionsUi.actions.count() })
rememberPagerState(pageCount = { uiState.tabActionsUi.actions.count() })
LaunchedEffect(pagerState.currentPage) {
viewModel.innerScreenConfig(uiModel.tabActionsUi.actions[pagerState.currentPage].route)
viewModel.innerScreenConfig(uiState.tabActionsUi.actions[pagerState.currentPage].route)
}
Scaffold(
title = title,
topActions = uiModel.topActionsUi,
tabActions = uiModel.tabActionsUi,
fabAction = uiModel.fabAction,
topActions = uiState.topActionsUi,
tabActions = uiState.tabActionsUi,
fabAction = uiState.fabAction,
onActionClicked = {
when (it.id) {
ActionIds.EXPORT -> {
Expand All @@ -77,7 +75,7 @@ fun NetworkingCompactVM(
state = pagerState,
modifier = Modifier.padding(it)
) { page ->
when (uiModel.tabActionsUi.actions[page].route) {
when (uiState.tabActionsUi.actions[page].route) {
TabActions.myProfile.route -> MyProfileCompactVM(
onEditInformation = onCreateProfileClicked
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ fun ProfileInputVM(
modifier: Modifier = Modifier,
viewModel: ProfileInputViewModel = koinViewModel()
) {
val uiState = viewModel.uiState.collectAsState()
when (uiState.value) {
when (val uiState = viewModel.uiState.collectAsState().value) {
is ProfileInputUiState.Loading -> Text(text = stringResource(id = R.string.text_loading))
is ProfileInputUiState.Failure -> Text(text = stringResource(id = R.string.text_error))
is ProfileInputUiState.Success -> ProfileInputScreen(
profile = (uiState.value as ProfileInputUiState.Success).profile,
profile = uiState.profile,
modifier = modifier,
onBackClicked = onBackClicked,
onValueChanged = viewModel::fieldChanged,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.gdglille.devfest.android.theme.m3.partners.feature

import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import org.gdglille.devfest.android.theme.m3.partners.screens.PartnerDetailOrientable
import org.gdglille.devfest.android.theme.m3.style.R
import org.koin.androidx.compose.koinViewModel
import org.koin.core.parameter.parametersOf

@Composable
fun PartnerDetailVM(
partnerId: String,
onLinkClicked: (url: String) -> Unit,
onItineraryClicked: (lat: Double, lng: Double) -> Unit,
onBackClicked: () -> Unit,
modifier: Modifier = Modifier,
viewModel: PartnerDetailViewModel = koinViewModel(parameters = { parametersOf(partnerId) })
) {
when (val uiState = viewModel.uiState.collectAsState().value) {
is PartnerUiState.Loading -> PartnerDetailOrientable(
partnerItemUi = uiState.partner,
onLinkClicked = {},
onItineraryClicked = { _, _ -> },
onBackClicked = onBackClicked,
modifier = modifier,
isLoading = true
)

is PartnerUiState.Failure -> Text(text = stringResource(id = R.string.text_error))
is PartnerUiState.Success -> PartnerDetailOrientable(
partnerItemUi = uiState.partner,
onLinkClicked = onLinkClicked,
onItineraryClicked = onItineraryClicked,
onBackClicked = onBackClicked,
modifier = modifier
)
}
}
Loading

0 comments on commit f50730e

Please sign in to comment.