Skip to content

Commit

Permalink
HomeScreen: Show unread btn according to visible items
Browse files Browse the repository at this point in the history
Signed-off-by: Shashank Verma <[email protected]>
  • Loading branch information
shank03 committed Nov 21, 2023
1 parent f1910f5 commit 2f4344b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.surfaceColorAtElevation
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -30,6 +31,7 @@ import com.mnnit.moticlubs.domain.model.Club
import com.mnnit.moticlubs.domain.model.User
import com.mnnit.moticlubs.domain.util.PublishedList
import com.mnnit.moticlubs.domain.util.PublishedMap
import com.mnnit.moticlubs.domain.util.PublishedState
import com.mnnit.moticlubs.domain.util.publishedStateListOf
import com.mnnit.moticlubs.domain.util.setExpandedChannel
import com.mnnit.moticlubs.ui.components.ProfilePicture
Expand All @@ -40,7 +42,9 @@ import com.mnnit.moticlubs.ui.viewmodel.HomeScreenViewModel
fun ClubList(
viewModel: HomeScreenViewModel,
listState: LazyListState,
showUnreadBtn: PublishedState<Boolean>,
clubsList: PublishedList<Club>,
clubsInfo: PublishedList<Pair<Boolean, Boolean>>,
channelMap: PublishedMap<Long, PublishedList<Channel>>,
onNavigateChannelClick: (channelId: Long, clubId: Long) -> Unit,
onNavigateToClubDetails: (clubId: Long) -> Unit,
Expand All @@ -60,13 +64,13 @@ fun ClubList(
.fillMaxWidth()
.padding(bottom = 16.dp),
onClick = {
viewModel.clubsInfo.value[idx] = viewModel.clubsInfo.value[idx].copy(
!viewModel.clubsInfo.value[idx].first,
clubsInfo.value[idx] = clubsInfo.value[idx].copy(
!clubsInfo.value[idx].first,
)
context.setExpandedChannel(clubsList.value[idx].clubId, viewModel.clubsInfo.value[idx].first)
context.setExpandedChannel(clubsList.value[idx].clubId, clubsInfo.value[idx].first)
},
shape = RoundedCornerShape(24.dp),
elevation = CardDefaults.cardElevation(if (viewModel.clubsInfo.value[idx].first) 8.dp else 0.dp),
elevation = CardDefaults.cardElevation(if (clubsInfo.value[idx].first) 8.dp else 0.dp),
colors = CardDefaults.cardColors(colorScheme.surfaceColorAtElevation(2.dp)),
) {
Row(modifier = Modifier.padding(16.dp)) {
Expand Down Expand Up @@ -98,7 +102,7 @@ fun ClubList(
Spacer(modifier = Modifier.padding(8.dp))

AnimatedVisibility(
visible = viewModel.clubsInfo.value[idx].second,
visible = clubsInfo.value[idx].second,
modifier = Modifier.align(Alignment.CenterVertically),
) {
BadgedBox(
Expand All @@ -111,7 +115,7 @@ fun ClubList(
Spacer(modifier = Modifier.padding(8.dp))
}

AnimatedVisibility(visible = viewModel.clubsInfo.value[idx].first) {
AnimatedVisibility(visible = clubsInfo.value[idx].first) {
ChannelList(
list = channelMap.value.getOrDefault(clubsList.value[idx].clubId, emptyList),
viewModel = viewModel,
Expand All @@ -120,6 +124,17 @@ fun ClubList(
)
}
}

LaunchedEffect(idx) {
val layoutInfo = listState.layoutInfo
val visibleItemsInfo = layoutInfo.visibleItemsInfo
val visibleItemIds = if (visibleItemsInfo.isEmpty()) {
emptyList()
} else {
visibleItemsInfo.map { it.index }
}
showUnreadBtn.value = !visibleItemIds.any { vis -> clubsInfo.value[vis].second }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.material3.surfaceColorAtElevation
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -37,6 +38,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import com.mnnit.moticlubs.R
import com.mnnit.moticlubs.domain.util.publishedStateOf
import com.mnnit.moticlubs.ui.components.ProfilePicture
import com.mnnit.moticlubs.ui.components.ProgressDialog
import com.mnnit.moticlubs.ui.components.homescreen.ClubList
Expand Down Expand Up @@ -66,6 +68,7 @@ fun HomeScreen(
val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior(rememberTopAppBarState())
val listState = rememberLazyListState()
val scope = rememberCoroutineScope()
val showUnreadBtn = remember { publishedStateOf(false) }

MotiClubsTheme {
SetTransparentSystemBars(setStatusBar = scrollBehavior.state.collapsedFraction)
Expand Down Expand Up @@ -138,7 +141,9 @@ fun HomeScreen(
ClubList(
viewModel,
listState,
showUnreadBtn,
clubsList = viewModel.clubsList,
clubsInfo = viewModel.clubsInfo,
channelMap = viewModel.channelMap,
onNavigateChannelClick = onNavigateChannelClick,
onNavigateToClubDetails = onNavigateToClubDetails,
Expand All @@ -147,7 +152,7 @@ fun HomeScreen(
},
floatingActionButton = {
Row(modifier = Modifier.padding()) {
AnimatedVisibility(visible = viewModel.clubsInfo.value.any { it.second }) {
AnimatedVisibility(visible = showUnreadBtn.value) {
Card(
modifier = Modifier.align(Alignment.Bottom),
onClick = {
Expand Down

0 comments on commit 2f4344b

Please sign in to comment.