🥄
One Library
  • 🤖onelib
  • 🏡Home
    • Overview
    • Setup Project
    • What's New
  • ☀️Fundamental
    • Basic Android
    • Data Flow
    • Dependency Injection
      • Hilt
      • Koin
  • 📿Layers
    • Data Layer
    • Domain Layer
    • Presentation Layer
  • 🪶Features
    • Views
      • Activity
      • Fragment
      • Bottom Sheet
      • Dialog
    • Adapters
      • Recycler Adapter
      • Filterable Adapter
      • Paging Adapter
      • Selectable Adapter
      • ViewPager Adapter
    • Firebase
      • One Firebase Auth
      • One Firebase Firestore
      • One Firebase Realtime
      • One Firebase Storage
    • Extension Functions
      • Any
      • Common
      • Context
      • Edittext
      • Image View
      • LiveData
      • OneMap
      • State Flow (Jetpack Compose)
      • View
    • Notification
    • One State View
    • Local Database
    • Permission
    • Validation
      • Passive Validator
      • Reactive Validator
      • Extension Validation
Powered by GitBook
On this page
  • ObserverLiveData
  • Example:
  1. Features
  2. Extension Functions

LiveData

ObserverLiveData

Parameters:

  • owner: The LifecycleOwner (typically an Activity or Fragment) to which the LiveData is scoped.

  • onLoading: A callback function to be invoked when the state is Loading.

  • onSuccess: A callback function to be invoked when the state is Success, with the successful data passed as a parameter.

  • onEmpty: An optional callback function to be invoked when the state is Empty.

  • onFailure: A callback function to be invoked when the state is Failure, with the failure message passed as a parameter.

Process:

Observes the LiveData and invokes the appropriate callback based on the current state (Default, Loading, Failure, Empty, or Success).

Example:

To use this function, you'll need a LiveData<Resource<T>> and an Activity or Fragment to observe it. Here’s an example:

viewModel.myLiveData.observerLiveData(
    owner = this,
    onLoading = { showLoading() },
    onSuccess = { data -> showData(data) },
    onEmpty = { showEmptyState() },
    onFailure = { message -> showError(message) }
)

This extension function allows you to cleanly handle different states of a LiveData<Resource<T>> in your Activity or Fragment. It improves code readability and maintainability by centralizing the state handling logic.

Last updated 11 months ago

🪶