How to set data in Model?

  android, android-room, kotlin, mvvm, repository

I have one entity "drinks" which have [id;name;thumb] and I`m using these entities for 2 response calls. One response returns me a NonAlcohol list of drinks, another AlcoholList, I’m using Room for caching the data. But when I run the app, I saw that my lists merged, after some thought, I found a solution to this problem, I added one Boolean field to my entity "alcoholStatus".But I can’t understand how to set the data into this variable correctly using this AccessDataStrategy. I’m new to Android, and this is my learning project. Please give me the right way how to solve this problem.

https://github.com/YaroslavSulyma/LetsDrink/tree/master/app/src/main/java/com/example/letsdrink

Thanks a lot!

Entity

@Entity(tableName = "drinks")
data class DrinksModel(
    @SerializedName("strDrink")
    val strDrink: String,
    @SerializedName("strDrinkThumb")
    val strDrinkThumb: String?,
    @SerializedName("idDrink")
    @PrimaryKey
    val idDrink: Int,
    var alcohol: Boolean
)

DataAccessStrategyCode

fun <T, A> performGetOperation(
    databaseQuery: () -> LiveData<T>,
    networkCall: suspend () -> Resource<A>,
    saveCallResult: suspend (A) -> Unit
): LiveData<Resource<T>> =
    liveData(Dispatchers.IO) {
        emit(Resource.loading())
        val source = databaseQuery.invoke().map { Resource.success(it) }
        emitSource(source)

        val responseStatus = networkCall.invoke()
        if (responseStatus.status == SUCCESS) {
            saveCallResult(responseStatus.data!!)

        } else if (responseStatus.status == ERROR) {
            emit(Resource.error(responseStatus.message!!))
            emitSource(source)
        }
    }

Resource

data class Resource<out T>(val status: Status, val data: T?, val message: String?) {

    enum class Status {
        SUCCESS,
        ERROR,
        LOADING
    }

    companion object {
        fun <T> success(data: T): Resource<T> {
            return Resource(Status.SUCCESS, data, null)
        }

        fun <T> error(message: String, data: T? = null): Resource<T> {
            return Resource(Status.ERROR, data, message)
        }

        fun <T> loading(data: T? = null): Resource<T> {
            return Resource(Status.LOADING, data, null)
        }
    }
}

Repository

class CocktailsRepository @Inject constructor(
        private val remoteDataSource: CocktailsRemoteDataSource,
        private val localDataSource: CocktailsDao
) {
        fun getAlcoholicCocktails() = performGetOperation(
            databaseQuery = { localDataSource.getAlcoholicCocktails() },
            networkCall = { remoteDataSource.getAllAlcoholicCocktails()},
            saveCallResult = { localDataSource.insertAllDrinks(it.drinks) }
        )
    
        fun getNonAlcoholicCocktails() = performGetOperation(
            databaseQuery = { localDataSource.getNonAlcoholicCocktails() },
            networkCall = { remoteDataSource.getAllNonAlcoholicCocktails() },
            saveCallResult = { localDataSource.insertAllDrinks(it.drinks) }
        )
    }

DAO

@Dao
interface CocktailsDao {
    @Query("SELECT * FROM drinks WHERE alcohol = 'true'")
    fun getAlcoholicCocktails(): LiveData<List<DrinksModel>>

    @Query("SELECT * FROM drinks WHERE alcohol = 'false'")
    fun getNonAlcoholicCocktails(): LiveData<List<DrinksModel>>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertAllDrinks(drinks: List<DrinksModel>)
}

RemoteDataSource

class CocktailsRemoteDataSource @Inject constructor(private val iCocktailApisService: ICocktailApisService) :
    BaseDataSource() {

    suspend fun getAllAlcoholicCocktails() =
        getResult { iCocktailApisService.allAlcoholicAndNonAlcoholicCocktails("Alcoholic") }

    suspend fun getAllNonAlcoholicCocktails() =
        getResult { iCocktailApisService.allAlcoholicAndNonAlcoholicCocktails("Non_Alcoholic") }
}

Source: Android Questions

LEAVE A COMMENT