Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question #4

Open
gilangsatria09 opened this issue Aug 20, 2020 · 5 comments
Open

Question #4

gilangsatria09 opened this issue Aug 20, 2020 · 5 comments

Comments

@gilangsatria09
Copy link

sorry it's not an issue, but i really need help for POST method, how can do POST in viewmodel? if i have username and password

@TheSomeshKumar
Copy link

I think we can do it with LiveData Transformations. You listen to the changes in the post body and when the body changes Transformation.switchMap() will trigger and it will call the Repository method inside it. There are examples in the Google's advance github browser repo on GitHub.

@AnwarSamir
Copy link

you can do some thing like this
@gilangsatria09
`
data class logedinUserData(var userName:String,var pass:String,var token:String)
private val _userData= MutableLiveData()

private val _login =_userData.switchMap { userData ->
    repository.userLogin(userData.userName,userData.pass,userData.token)
}
val userLoginResponse: LiveData<Resource<UserLoginResponse>> = _login


fun start(userData: logedinUserData) {
    _userData.value = userData
}`

@mofada
Copy link

mofada commented Aug 8, 2021

I think I have the same problem, I asked a question on stackoverflow

https://stackoverflow.com/questions/68702117/i-have-a-problem-with-the-code-in-viewmodel-kotlin-coroutines-livedata

@mofada
Copy link

mofada commented Aug 8, 2021

I think we can do it with LiveData Transformations. You listen to the changes in the post body and when the body changes Transformation.switchMap() will trigger and it will call the Repository method inside it. There are examples in the Google's advance github browser repo on GitHub.

There are examples in the Google's advance github browser repo on GitHub. Can you post the address of this project? I'll take a look

@einschneidend
Copy link

einschneidend commented Sep 10, 2021

Hello, I did a post like this and it's working fine so far:

//call

repository.commitChanges( getChangesObject() ).observeForever(commitChangesObserver)

//observer

commitChangesObserver = Observer<ResultClass<ModelClass>> {
            if (it is ResultClass.Error)
                handleError(it.error)//tested and working
        }

//remember to remove observer

override fun onCleared() {
        super.onCleared()
        repository.commitChanges( getChangesObject() ).removeObserver(commitChangesObserver) //at this point the changes object is empty
    }

//Repository

fun commitChanges(changes: LocalClass) = performPostOperation(
        networkCall = { remoteDataSource.commitChanges(LocalClass.toModelClass(changes)) }, //the class on DB is not the same needed on the endpoint so we transform it
        saveCallResult = { localDataSource.update(LocalClass.fromModel(it)) }//again switch back from model to local class
    )

//Strategy utils

    fun <A> performPostOperation(
    networkCall: suspend () -> ResultClass<A>,
    saveCallResult: suspend (A) -> Unit,
) : LiveData<ResultClass<A>> =
    liveData(Dispatchers.IO) {
        val responseStatus = networkCall.invoke()
        if (responseStatus is ResultClass.Success) {
            saveCallResult(responseStatus.data)
        } 
        emit(responseStatus)
    }

*Notice we don\t listen to success on commitChangesObserver since live data is already being observed (also with observefovever on the viewmodel, whish is also removed oncleared() )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants