Created
November 20, 2018 15:44
-
-
Save tolmachevroman/ff0a1425a00b17b33cc0e4de8d646e42 to your computer and use it in GitHub Desktop.
Example of POST request using ViewModel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LoginViewModel @Inject constructor(private val repository: UsersRepository) : ViewModel() { | |
var loginInput: MutableLiveData<LoginRequest> = MutableLiveData() | |
val login: LiveData<Resource<User>> = Transformations.switchMap(loginInput) { emailPasswordRequest -> | |
repository.login(emailPasswordRequest) | |
} | |
} | |
class LoginActivity : BaseActivity() { | |
//... | |
override fun onCreate(savedInstanceState: Bundle?) { | |
//... | |
// Initialize loginViewModel | |
loginViewModel = ViewModelProviders.of(this, viewModelFactory).get(LoginViewModel::class.java) | |
// Login observer | |
loginViewModel.login.observe(this, ResourceObserver(this.localClassName, | |
hideLoading = { progress_bar.visibility = View.GONE }, | |
showLoading = { progress_bar.visibility = View.VISIBLE }, | |
onSuccess = { _ -> loadSomeDataAndFinish() }, | |
onError = { message -> Toast.makeText(this, message, Toast.LENGTH_LONG).show() })) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment