A user can update its information such as first name, last name from the Edit Profile Fragment in Open Event Android. Edit Profile Fragment can be accessed from the menu inside the Profile page. On opening Edit Profile Fragment user can interact with the simple UI to update his/her information. This blog post will guide you on how its implemented in Open Event Android.
To update a User we send a patch request to Open Event Server. The patch request contains the Updated User as body and auth token as header and it returns the updated user in response. Following it what the interface method looks like
@PATCH(“users/{id}”) fun updateUser(@Body user: User, @Path(“id”) id: Long): Single<User> |
This method is exposed to the View Model using a service layer function which calls the above function and also inserts the returned user in the database.
fun updateUser(user: User, id: Long): Single<User> { return authApi.updateUser(user, id).map { userDao.insertUser(it) it } } |
On using map on Single<User> returned by updateUser we can access the user inside the scope which is then inserted into the database using the DAO method insert user and the same user object is returned by the function.
This service layer method is then used in updateUser method of EditProfileViewModel class which specifies how it is subscribed and on which thread observer should be set etc. The Edit Profile Fragment fragment calls this method whenever the user clicks on the Update button.
fun updateUser(firstName: String, lastName: String) { val id = authHolder.getId() if (firstName.isEmpty() || lastName.isEmpty()) { message.value = “Please provide first name and last name!” return } compositeDisposable.add(authService.updateUser(User(id = id, firstName = firstName, lastName = lastName), id) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .doOnSubscribe { progress.value = true } .doFinally { progress.value = false } .subscribe({ message.value = “User updated successfully!” Timber.d(“User updated”) }) { message.value = “Error updating user!” Timber.e(it, “Error updating user!”) }) } |
UpdateUser takes two parameters first name and last name if any of these parameters is empty the function returns with an error message which is displayed on the UI else service layer update user function is called with argument a User object with first name and last name as provided to view model function and an id which is accessed using authHolder’s getId method. Whenever this is subscribed we set progress.value true which displays spinner on the UI this is set false after the operation is complete. If the patch request results in success then toast message is shown on screen and a success message is logged similar to this in case of error, an error toast is displayed and an error is logged.
This goes for the logic to update user we also need UI and menu item which launches this fragment.
Inside Menu.xml add the following snippet of code
<item android:id=“@+id/edit_profile” android:title=“@string/edit_profile” /> |
This will create a menu item inside the ProfileFragment. The next step is to wire this logic which tells what happens when the user selects this menu item. The following code wires it to EditProfileFragment.
R.id.edit_profile -> { val fragment = EditProfileFragment() activity?.supportFragmentManager?.beginTransaction()?.replace(R.id.frameContainer, fragment)?.addToBackStack(null)?.commit() return true |
Resources
- Open Event Server User Endpoint: http://open-event-api-dev.herokuapp.com/#users
- Stack OverFlow Creating Retrofit Post Request: https://stackoverflow.com/questions/30180957/send-post-request-with-params-using-retrofit
- Google Android Developers, App Permissions: https://developer.android.com/training/permissions/requesting