Last active
February 24, 2018 15:21
-
-
Save douglasiacovelli/3af90ec7eedcba0fd9710f53820f6a18 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| fun onClickLikeButton() { | |
| if (viewModel.liked) { | |
| //First we update the local model so the user feels the action is instantaneous | |
| viewModel.liked = false | |
| viewModel.count-- | |
| //Then we call the API to update it on the server | |
| apiService.removeLike.enqueue({ response -> | |
| //If everything goes right, we update the model with the information from the server | |
| viewModel.count = response.count | |
| }, { failure -> | |
| //If any error occurs we should let the user know and go back to the previous state | |
| Toast.makeToast(activity, "Ops, an error occurred", LENGTH_LONG).show() | |
| viewModel.liked = true | |
| viewModel.count++ | |
| }) | |
| } else { | |
| //First we update the local model so the user feels the action is instantaneous | |
| viewModel.liked = true | |
| viewModel.count++ | |
| //Then we call the API to update it on the server | |
| apiService.removeLike.enqueue({ response -> | |
| //If everything goes right, we update the model with the information from the server | |
| viewModel.count = response.count | |
| }, { failure -> | |
| //If any error occurs we should let the user know and go back to the previous state | |
| Toast.makeToast(activity, "Ops, an error occurred", LENGTH_LONG).show() | |
| viewModel.liked = false | |
| viewModel.count-- | |
| }) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment