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

Sequential Content with IndefiniteSurface

Create engaging user experiences with sequential content.

IndefiniteSurface Composable: A Dynamic Component for Sequential Content

Purpose:

The IndefiniteSurface composable is a versatile component that can be used to display a series of items sequentially, one at a time. It's particularly useful for scenarios where the number of items is unknown or dynamic, such as an exam with an undefined number of questions.

Parameters:

  • modifier: A Modifier to apply to the surface.

  • items: A Map<Int, T> containing the items to be displayed.

  • selectedIndex: The current index of the selected item.

  • itemView: A composable function that renders the item content.

  • resultView: A composable function that renders the result view after the user has interacted with the item.

  • onFinished: A callback function that is invoked when all items have been displayed.

Functionality:

  1. Initialization: The component initializes with the provided items and selectedIndex.

  2. Index Tracking: The selectedIndex is used to determine which item is currently visible.

  3. Item Rendering: The itemView function is called for the selected item.

  4. Result Handling: If the user indicates that they have finished with the current item, the resultView is displayed, and the selectedIndex is incremented.

  5. Completion: When the selectedIndex reaches the end of the items list, the onFinished callback is invoked.

Scenario: Exam Questionnaire

In the provided scenario, the IndefiniteSurface can be used to display the exam questions sequentially:

  1. Load Initial Question: The component loads the first question and displays it using the itemView function.

  2. Handle Next/Previous: When the user clicks the "next" or "previous" buttons, the selectedIndex is updated, and the corresponding question is displayed.

  3. Handle Completion: When all questions have been answered, the onFinished callback can be used to display a final result or navigate to the next screen.

Example Usage

@Composable
fun QuizApp() {
    val questions = listOf(
        "What is the capital of Bangladesh?",
        "Who is the author of 'Harry Potter'?",
        // ... more questions
    )

    IndefiniteSurface(
        items = questions.withIndex().associate { it },
        selectedIndex = 0,
        itemView = { index, question, onNext ->
            QuestionCard(
                question = question,
                onAnswerSubmitted = { isCorrect ->
                    // Handle answer submission here
                    onNext(isCorrect)
                }
            )
        },
        resultView = { question, onNext ->
            ResultCard(
                question = question,
                isCorrect = true, // Replace with actual correctness
                onNext = onNext
            )
        },
        onFinished = {
            // Show final score or other completion message
        }
    )
}

Key Points:

  • Dynamic Content: The component can handle an arbitrary number of items.

  • Sequential Display: Items are displayed one at a time.

  • Result Handling: The resultView can be used to display additional information or actions after the user has interacted with an item.

  • Customizable: The itemView and resultView functions can be customized to suit specific requirements.

By using IndefiniteSurface, you can create dynamic and engaging user interfaces that can handle various scenarios involving sequential content.

PreviousHandle a screen with 3 statesNextHandle multiple predefined states

Last updated 8 months ago