From a3502e509b0431af78723d08c3a126f1aa1bc6b8 Mon Sep 17 00:00:00 2001 From: Momin Tahir Date: Wed, 27 Mar 2024 02:38:01 +0500 Subject: [PATCH] fix loading state issue for first time search --- .../kmp/newsapp/presentation/Components.kt | 25 +++++++++++-------- .../screens/search_news/SearchNewsScreen.kt | 8 +++--- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/momin/tahir/kmp/newsapp/presentation/Components.kt b/composeApp/src/commonMain/kotlin/momin/tahir/kmp/newsapp/presentation/Components.kt index 7380565..4d730f0 100644 --- a/composeApp/src/commonMain/kotlin/momin/tahir/kmp/newsapp/presentation/Components.kt +++ b/composeApp/src/commonMain/kotlin/momin/tahir/kmp/newsapp/presentation/Components.kt @@ -35,7 +35,7 @@ import org.jetbrains.compose.resources.painterResource @Composable fun NewsList( - articles: List
, + articles: List
?, savedNews: List
? = null, onItemClick: (String) -> Unit, showSaveIcon: Boolean, @@ -47,16 +47,19 @@ fun NewsList( verticalArrangement = Arrangement.Top, contentPadding = PaddingValues(bottom = 70.dp) ) { - itemsIndexed(articles) { index, article -> - NewsItem( - article = article, - showSaveIcon = showSaveIcon, - isSelected = savedNews?.contains(article) == true, - onClick = { onItemClick(article.url) }, - onActionSave = { - onActionSave(article, index) }, - onActionRemove = {onActionRemove(article, index)} - ) + if (articles != null) { + itemsIndexed(articles) { index, article -> + NewsItem( + article = article, + showSaveIcon = showSaveIcon, + isSelected = savedNews?.contains(article) == true, + onClick = { onItemClick(article.url) }, + onActionSave = { + onActionSave(article, index) + }, + onActionRemove = { onActionRemove(article, index) } + ) + } } } } diff --git a/composeApp/src/commonMain/kotlin/momin/tahir/kmp/newsapp/presentation/screens/search_news/SearchNewsScreen.kt b/composeApp/src/commonMain/kotlin/momin/tahir/kmp/newsapp/presentation/screens/search_news/SearchNewsScreen.kt index 5ef8f2f..37b4aef 100644 --- a/composeApp/src/commonMain/kotlin/momin/tahir/kmp/newsapp/presentation/screens/search_news/SearchNewsScreen.kt +++ b/composeApp/src/commonMain/kotlin/momin/tahir/kmp/newsapp/presentation/screens/search_news/SearchNewsScreen.kt @@ -57,16 +57,14 @@ class SearchNewsScreen : Screen { search = it viewModel.onSearchTextChange(it) }) - val news = viewModel.search.collectAsState().value ?: return + val news = viewModel.search.collectAsState().value val isSearching = viewModel.isSearching.collectAsState().value - Spacer(Modifier.height(20.dp)) Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { - NewsList(news.articles, onActionSave = { article, _ -> + NewsList(news?.articles, onActionSave = { article, _ -> viewModel.saveArticle(article) }, onItemClick = { navigateToWebViewScreen(it, navigator) }, showSaveIcon = true) if (isSearching) { - println("isSearching $isSearching") - CircularProgressIndicator(modifier = Modifier.align(Alignment.Center)) + CircularProgressIndicator(modifier = Modifier.align(Alignment.TopCenter)) } } }