Thread
#android-architecture
    Ofir Bar

    Ofir Bar

    2 years ago
    Source:
    class MyPublishedAdvertisementsViewModel(private val advertisementRepository : AdvertisementRepository) : ViewModel(){
    
        var _myPublishedAdvertisements : MutableLiveData<NetworkCall<MyPublishedAdvertisementsResponse>> = MutableLiveData()
        val myPublishedAdvertisements : LiveData< NetworkCall<MyPublishedAdvertisementsResponse> > = _myPublishedAdvertisements
    
        // Called every time our UI is #onStarted
        fun getFreshData(){
            _myPublishedAdvertisements.value = liveData {
                emit(NetworkCall<MyPublishedAdvertisementsResponse>(NetworkStatus.LOADING, null, ""))
                val publishedAdvertisements = advertisementRepository.getMyPublishedAdvertisements()
                emit(publishedAdvertisements)
            }
        }
    
    }
    Kirill Prybylsky

    Kirill Prybylsky

    2 years ago
    Does your NetworkCall extends LiveData?
    Ofir Bar

    Ofir Bar

    2 years ago
    No
    It is a wrapper data class to be used for all my network responses, this is the code:
    data class NetworkCall<out T>(val status: NetworkStatus, val data: T?, val message: String?) {
    
        companion object {
            fun <T> success(data: T?): NetworkCall<T> {
                return NetworkCall(NetworkStatus.SUCCESS, data, null)
            }
    
            fun <T> error(msg: String, data: T?): NetworkCall<T> {
                return NetworkCall(NetworkStatus.ERROR, data, msg)
            }
    
            fun <T> loading(data: T?): NetworkCall<T> {
                return NetworkCall(NetworkStatus.LOADING, data, null)
            }
        }
    
    }
    Kirill Prybylsky

    Kirill Prybylsky

    2 years ago
    But you’re putting liveData{} as a value of another live data
    Ofir Bar

    Ofir Bar

    2 years ago
    I am not sure what I am doing wrong 🤔
    Kirill Prybylsky

    Kirill Prybylsky

    2 years ago
    Seems like you are doing this, isn’t?
    Ofir Bar

    Ofir Bar

    2 years ago
    Ohhhh, ok I got what you’re saying But I still don’t understand how I use liveData{} to change the values of a MutableLiveData. Why for LiveData I can use liveData{}, and for MutableLiveData I can’t?
    Kirill Prybylsky

    Kirill Prybylsky

    2 years ago
    For my opinion you are trying to achieve something completely wrong. You should rethink this code. Also, there shouldn’t be any network requests from your view model class this is bad practice.
    Ofir Bar

    Ofir Bar

    2 years ago
    I will check thanks for the help anyway @Kirill Prybylsky