Jasmin Fajkic
10/29/2022, 7:52 AMMorgane Soula
10/29/2022, 9:46 AMJasmin Fajkic
10/29/2022, 12:21 PM@HiltViewModel
internal class CommunityViewModel @Inject constructor(getTabs: GetTabs) : ViewModel() {
private val _state = MutableStateFlow(CommunityViewState.Empty)
val state = _state.asStateFlow()
init {
viewModelScope.launch {
if (_state.value.tabs.isEmpty()) {
val data = getTabs(Unit).first()
val selectedTab = data?.selectedTab ?: ""
val tabs = data?.tabs ?: listOf()
_state.value = CommunityViewState(selectedTab, tabs)
}
}
}
}
Composable
@Composable
fun Community(
actionManager: (String) -> Unit,
navigateToPostGalleryView: (imagesList: ImagesList) -> Unit,
openComments: (String) -> Unit
) {
CollapsingToolbarScaffold(
state = rememberCollapsingToolbarScaffoldState(),
scrollStrategy = ScrollStrategy.EnterAlways,
modifier = Modifier,
toolbar = {
TopAppBarTitle()
})
{
Box {
TabbedView(
viewModel = hiltViewModel(),
actionManager = actionManager,
navigateToPostGalleryView = navigateToPostGalleryView,
openComments = openComments
)
}
}
}
@Composable
private fun TopAppBarTitle() {
Column(
Modifier
.statusBarsPadding()
.padding(start = 5.dp)
) {
Image(
painter = painterResource(id = R.drawable.ic_woc_logo),
contentDescription = "",
modifier = Modifier
.size(30.dp)
)
Text(
text = stringResource(id = R.string.community_feed_title),
color = Style.colors.textStandard,
style = Style.typography.titleLarge,
fontWeight = FontWeight.W800,
modifier = Modifier.padding(top = 10.dp)
)
}
}
@OptIn(ExperimentalPagerApi::class)
@Composable
private fun TabbedView(
viewModel: CommunityViewModel,
actionManager: (String) -> Unit,
navigateToPostGalleryView: (imagesList: ImagesList) -> Unit,
openComments: (String) -> Unit
) {
val state = viewModel.state.collectAsState().value
if (state.tabs.isNotEmpty()) {
val indexOfTile = state.selectedIndex()
val pagerState = rememberPagerState(indexOfTile)
Column(modifier = Modifier
.background(Style.colors.appBackground)
.fillMaxWidth()) {
ScrollableTabRow(
selectedTabIndex = pagerState.currentPage,
containerColor = Style.colors.content,
edgePadding = 5.dp,
modifier = Modifier
.height(50.dp)
.widthIn(min = 1000.dp),
divider = {},
indicator = {}
) {
val scope = rememberCoroutineScope()
state.tabs.forEachIndexed { index, tab ->
val isSelected = pagerState.currentPage == index
Tab(
selected = isSelected,
onClick = {
scope.launch {
pagerState.animateScrollToPage(index)
}
},
modifier = Modifier
.background(Style.colors.content)
.height(30.dp)
.clip(RoundedCornerShape(50))
.background(
if (isSelected) Style.colors.inputActive else Style.colors.content
),
text = {
Text(
text = tab.title,
style = Style.typography.bodyBold,
color = if (isSelected) Style.colors.textLink else Style.colors.textStandard,
)
})
}
}
/*AnimatedVisibility(visible = newPostStatus == NewPostStatus.UPLOADING) {
LinearProgressIndicator(
modifier = Modifier.fillMaxWidth(),
trackColor = Style.colors.appBackground,
color = colorResource(
id = R.color.light_blue
),
progress = postUploadProgress
)
}*/
HorizontalPager(
count = state.tabs.size,
state = pagerState
) { page ->
CommunityPage(
tab = state.tabs.elementAt(page),
actionManager = actionManager,
navigateToPostGalleryView = navigateToPostGalleryView,
openComments = openComments
)
}
}
}
}