danilshik
danilshik

Reputation: 289

Changing data in Live Data Kotlin

I have allRecords - Live Data values obtained from the Room, through the Repository.

I want the handleSelectedItem method to change the values of one item in the LiveData<List<...>> if it is matched by id. I tried to do this with Transformation.map (), but this code does not work

class RecordListViewModel @Inject constructor(val repository: RecordRepository): ViewModel() {
    private var allRecords : LiveData<List<RecordItem>> = Transformations.map(repository.getAllRecording()){
        records -> return@map records.map{ it.toItem()}

    }



    fun getAllRecords() : LiveData<List<RecordItem>>{
        return allRecords
    }

    fun handleSelectedItem(id : Int) {
        Log.d("HandleSelectedItem", "Test1")
        allRecords = Transformations.map(allRecords) { records ->
            return@map records.map {
                if (it.id == id){
                    Log.d("HandleSelectedItem", "Test2")
                    it.copy(isSelected = true)
                }
                else{
                    Log.d("HandleSelectedItem", "Test3")
                    it.copy(isSelected = false)
                }
            }
        }
    }
}

Help to solve this problem

Update Here offered MutableLiveData instead of LiveData. Then both Repository and Dao should return MutableLiveData

Repository

fun getAllRecording(): MutableLiveData<List<RecordEntity>> =
        appDatabase.getRecordDao().getAllRecording()

Dao

@Query("SELECT * FROM record")
fun getAllRecording() : MutableLiveData<List<RecordEntity>>

But Room database cannot return MutableLiveData

Error

D:\Project\VoiceRecording\app\build\tmp\kapt3\stubs\debug\ru\ddstudio\voicerecording\data\database\daos\RecordDao.java:17: error: Not sure how to convert a Cursor to this method's return type (androidx.lifecycle.MutableLiveData<java.util.List<ru.ddstudio.voicerecording.data.database.entities.RecordEntity>>).
    public abstract androidx.lifecycle.MutableLiveData<java.util.List<ru.ddstudio.voicerecording.data.database.entities.RecordEntity>> getAllRecording();

Update2

private val allRecords = MediatorLiveData<List<RecordItem>>().apply {
            val recordsRepository = repository.getAllRecording().map { records -> records.map { it.toItem() } }
            addSource(recordsRepository)
        }

Error addSource()

None of the following functions can be called with the arguments supplied:
@MainThread public final fun <S : Any!> addSource(@NonNull p0: LiveData<List<RecordItem>!>, @NonNull p1: (List<RecordItem>!) -> Unit): Unit defined in androidx.lifecycle.MediatorLiveData
@MainThread public open fun <S : Any!> addSource(@NonNull p0: LiveData<List<RecordItem>!>, @NonNull p1: Observer<in List<RecordItem>!>): Unit defined in androidx.lifecycle.MediatorLiveData

Upvotes: 1

Views: 2302

Answers (1)

IR42
IR42

Reputation: 9682

Your LiveData objects should be val. Use MutableLiveData/ MediatorLiveData and setValue or postValue to change the value in LiveData

class RecordListViewModel @Inject constructor(val repository: RecordRepository): ViewModel() {

    private val allRecords = MediatorLiveData<List<RecordItem>>().apply {
        val recordsLiveData = repository.getAllRecording().map { records -> records.map { it.toItem() } }
        addSource(recordsLiveData) { records -> 
            value = records
        }
    }

    fun getAllRecords() : LiveData<List<RecordItem>> {
        return allRecords
    }

    fun handleSelectedItem(id : Int) {
        Log.d("HandleSelectedItem", "Test1")
        allRecords.value?.let { records ->
            allRecords.value = records.map { it.copy(isSelected = it.id == id) }
        }
    }
}

Then both Repository and Dao should return MutableLiveData

No, it shouldn't. Use LiveData in your DAO and Repository

Upvotes: 2

Related Questions