One thing I am trying to figure out where I make m...
# compose
j
One thing I am trying to figure out where I make mistake. Have screen A and Screens B, C, D ( bottom tabs) and screen E outside of bottom tab bar. In screen A i get data from my viewmodel all good. I go to tab B than from there I open screen A. Once I am done I want to return to screen A. And I achieve it. But problem is that my viewmodel call init and make network request that I do not want to make. Any idea how to possible solve it?
m
How do you initialize your ViewModel?
j
Viewmodel
Copy code
@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
Copy code
@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
                    )
                }
            }
        }
}