From d19a1fa1b295464c7f2d68d2534eee3056f113d8 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 27 Jan 2023 22:04:59 -0300 Subject: [PATCH] Refactoring --- .../ui/screen/ChatroomListFeedView.kt | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/ChatroomListFeedView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/ChatroomListFeedView.kt index 7215ab3ab..fb39f2442 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/ChatroomListFeedView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/ChatroomListFeedView.kt @@ -4,6 +4,7 @@ import androidx.compose.animation.Crossfade import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.LazyListState import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.runtime.Composable @@ -31,7 +32,6 @@ fun ChatroomListFeedView(viewModel: FeedViewModel, accountViewModel: AccountView var isRefreshing by remember { mutableStateOf(false) } val swipeRefreshState = rememberSwipeRefreshState(isRefreshing) - val listState = rememberLazyListState() val coroutineScope = rememberCoroutineScope() LaunchedEffect(isRefreshing) { @@ -61,17 +61,7 @@ fun ChatroomListFeedView(viewModel: FeedViewModel, accountViewModel: AccountView } } is FeedState.Loaded -> { - LazyColumn( - contentPadding = PaddingValues( - top = 10.dp, - bottom = 10.dp - ), - state = listState - ) { - itemsIndexed(state.feed.value, key = { index, item -> item.idHex }) { index, item -> - ChatroomCompose(item, accountViewModel = accountViewModel, navController = navController) - } - } + FeedLoaded(state, accountViewModel, navController) } FeedState.Loading -> { LoadingFeed() @@ -81,3 +71,28 @@ fun ChatroomListFeedView(viewModel: FeedViewModel, accountViewModel: AccountView } } } + +@Composable +private fun FeedLoaded( + state: FeedState.Loaded, + accountViewModel: AccountViewModel, + navController: NavController +) { + val listState = rememberLazyListState() + + LazyColumn( + contentPadding = PaddingValues( + top = 10.dp, + bottom = 10.dp + ), + state = listState + ) { + itemsIndexed(state.feed.value, key = { index, item -> item.idHex }) { index, item -> + ChatroomCompose( + item, + accountViewModel = accountViewModel, + navController = navController + ) + } + } +}