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
  1. Architecture

Data Flow

The diagram illustrates a typical code flow for a feature-based architecture in a Kotlin Multiplatform Mobile (KMM) application.He

PreviousModulesNextHttp Client

Last updated 8 months ago

Let's get an overview of the basic data structures used in the system and how they are consumed by predefined automated views to streamline data flow and handle common view states.

Data Structures

  • RemoteData<T>which is an alias of Option<Either<ErrMessage, T>>: Represents a single piece of data, including its loading state and potential errors.

  • RemoteDataPaginated<T> an alias of Option<Either<ErrMessage, Page>>: Represents a paginated list of data, including information about the current page, total pages, and the data itself.

  • RemoteListData<T> alias of Option<Either<ErrMessage, List>>: Represents a simple list of data.

Key Components:

  • Option: A type from ArrowKT that represents an optional value, which can be Some (present) or None (absent).

  • Either: Another type from ArrowKT that represents a value that can be either a Right (success) or a Left (error).

Purpose of Option and Either:

  • Option: Used to represent the initial loading state (none) and potential errors (left).

  • Either: Used to represent the success or error outcomes of data fetching.

Data Flow

  1. Data Fetching: The repository fetches data from the server using HTTP requests.

  2. Data Processing: The repository processes the fetched data, transforming it into one of the three data structures.

  3. Data Propagation: The data structure is propagated through the view model to the view.

  4. View Rendering: The view automatically handles the loading state, error display, and data rendering based on the data structure.

Usage Examples

RemoteData<T>:

Kotlin

// ViewModel
private val _todoItem = MutableStateFlow<RemoteData<TodoResponse>>(none())
val todoItem = _todoItem.asStateFlow()

// View
StatelessSurface(
    remoteData = todoVM.todoItem.collectAsState().value
) { todoItem ->
    // Your view for that item
}

Use code with caution.

RemoteDataPaginated<T>:

Kotlin

// ViewModel
private val _todos = MutableStateFlow<RemoteDataPaginated<TodoResponse>>(none())
val todos = _todos.asStateFlow()

// View
LazyColumnWithLoading(
    remoteData = todoVM.todos.collectAsState().value
) { todoItem ->
    TodoItemView(todoItem) // Your custom view for single item
}

Use code with caution.

RemoteListData<T>:

Kotlin

// ViewModel
private val _todos = MutableStateFlow<RemoteListData<TodoResponse>>(none())
val todos = _todos.asStateFlow()

// View
LazyColumnWithLoadingForList(
    remoteData = todoVM.todos.collectAsState().value
) { todoItem ->
    TodoItemView(todoItem) // Your custom view for single item
}

Use code with caution.

Benefits of Using These Data Structures

  • Simplified View Logic: Predefined views automatically handle loading states and errors, reducing boilerplate code.

  • Consistent Error Handling: The Either type provides a consistent way to handle errors.

  • Flexibility: These data structures can be used to represent various types of data.

  • Improved Readability: The Option type makes the code more expressive and easier to understand.

By understanding these data structures and their usage, you can effectively manage data flow and improve the user experience in your KMM applications.

If you have multiple files, GitBook makes it easy to import full repositories too — allowing you to keep your GitBook content in sync.