Search code examples
androidandroid-livedataandroid-databindingmutablelivedatasealed-class

How to use mutable live data of type sealed class with data binding


I am trying to handle visibility of a ProgressBar using mutable LiveData of type sealed class with data binding but getting below error.

cannot find symbol variable sealed class

Below is my code

ViewModel

    class RevampSplashViewModel: RevampBaseViewModel() {

    val splashEvents = MutableLiveData<SplashEvents>()

    sealed class SplashEvents {
        object Loading : SplashEvents()
        object Success : SplashEvents()
        data class Error(val message: String): SplashEvents()
    }
} 

Layout

    <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context=".revamp.ui.splash.RevampSplashActivity">

    <data>
        <import type="revamp.ui.splash.RevampSplashViewModel.SplashEvents"/>
        <variable
                name="vm"
                type="revamp.ui.splash.RevampSplashViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
            android:background="@color/colorAccent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <ProgressBar
                android:id="@+id/progress_bar"
                android:theme="@style/progressBarWhite"
                android:visibility="@{vm.splashEvents == SplashEvents.Loading}"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_width="@dimen/progressbar_dimen"
                android:layout_height="@dimen/progressbar_dimen" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout> 

Below are the exact errors I am facing.

error: cannot find symbol
                
vmSplashEventsRevampUiSplashRevampSplashViewModelSplashEventsLoading = (vmSplashEventsGetValue) == (revamp.ui.splash.RevampSplashViewModel.SplashEvents.Loading); 


error: illegal parenthesized expression

vmSplashEventsRevampUiSplashRevampSplashViewModelSplashEventsLoading = (vmSplashEventsGetValue) == (revamp.ui.splash.RevampSplashViewModel.SplashEvents.Loading);  

Solution

  • I realised that I was comparing incompatible data types. The fix is instead of == we have to use instanceof. Below is the code.

        <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            tools:context=".revamp.ui.splash.RevampSplashActivity">
    
        <data>
            <import type="revamp.ui.splash.RevampSplashViewModel.SplashEvents"/>
            <variable
                    name="vm"
                    type="revamp.ui.splash.RevampSplashViewModel" />
        </data>
    
        <androidx.constraintlayout.widget.ConstraintLayout
                android:background="@color/colorAccent"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
            <ProgressBar
                    android:id="@+id/progress_bar"
                    android:theme="@style/progressBarWhite"
                    android:visibility="@{vm.splashEvents instanceof SplashEvents.Loading}"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintBottom_toBottomOf="parent"
                    android:layout_width="@dimen/progressbar_dimen"
                    android:layout_height="@dimen/progressbar_dimen" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    </layout>