Cognito KMM Template
  • Welcome
  • Getting Started
    • Quickstart: Getting Started
    • Prerequisites
    • Installation
    • Creating New App
  • Architecture
    • Overview
    • Modules
    • Data Flow
  • Feautures
    • Http Client
    • DataStore
    • Analytics
    • AdMob Integration
    • Crash Reporting
    • Built in Settings Page
  • Handle View States
    • StatelessSurface<T> Composable
    • Handle a screen with 3 states
  • Sequential Content with IndefiniteSurface
  • Handle multiple predefined states
  • Showing Paginated Data
  • Handling list data
Powered by GitBook
On this page

Handling list data

LazyColumnWithLoadingForList<T>

Example Usage of LazyColumnWithLoadingForList

Scenario:

Let's assume we're building a news app that fetches a list of articles from an API. We want to display the articles in a LazyColumn with appropriate loading, error, and empty state handling.

Code Implementation:

Kotlin

@Composable
fun NewsFeed() {
    val newsList = viewModel.newsList.collectAsState()

    LazyColumnWithLoadingForList(
        remoteData = newsList.value,
        emptyView = {
            Text(text = "No news found.")
        },
        itemView = { newsItem ->
            NewsItem(newsItem)
        }
    )
}

Explanation:

  1. Data Fetching: The newsList state flow is used to collect the list of news articles from the view model.

  2. Component Usage: The LazyColumnWithLoadingForList composable is used with the following parameters:

    • remoteData: The newsList state flow is passed to provide the data.

    • emptyView: A simple Text component is used to display a message when no news is found.

    • itemView: A custom NewsItem composable is used to render each news article.

  3. State Handling: The component automatically handles the loading, error, and empty states based on the remoteData value.

Additional Considerations:

  • Error Handling: You can customize the errorView parameter to provide more specific error messages or actions.

  • Pagination: If your news API supports pagination, you can modify the RemoteListData type to include pagination information and update the LazyColumnWithLoadingForList component accordingly.

  • Loading Indicator: The default loading view can be customized by providing a custom composable to the loadingView parameter.

Benefits:

  • Simplified Data Handling: The component automatically handles loading, error, and empty states.

  • Efficient Rendering: The LazyColumn ensures efficient rendering of the list items.

  • Customizability: You can customize the appearance and behavior of the component to fit your specific needs.

By using LazyColumnWithLoadingForList, you can create a clean and efficient news feed component that provides a great user experience.

PreviousShowing Paginated Data

Last updated 8 months ago