Skip to content

feat: Add more field to update user detail #2139

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

Merged
merged 1 commit into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class AuthService(
}
}

fun updateUser(user: User, id: Long): Single<User> {
return authApi.updateUser(user, id).map {
fun updateUser(user: User): Single<User> {
return authApi.updateUser(user, user.id).map {
userDao.insertUser(it)
it
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.fossasia.openevent.general.utils.ImageUtils.decodeBitmap
import android.os.Bundle
import android.provider.MediaStore
import android.util.Base64
import android.util.Patterns
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -21,11 +22,16 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.navigation.Navigation.findNavController
import androidx.navigation.fragment.navArgs
import com.google.android.material.textfield.TextInputEditText
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.fragment_edit_profile.view.updateButton
import kotlinx.android.synthetic.main.fragment_edit_profile.view.toolbar
import kotlinx.android.synthetic.main.fragment_edit_profile.view.firstName
import kotlinx.android.synthetic.main.fragment_edit_profile.view.details
import kotlinx.android.synthetic.main.fragment_edit_profile.view.facebook
import kotlinx.android.synthetic.main.fragment_edit_profile.view.twitter
import kotlinx.android.synthetic.main.fragment_edit_profile.view.instagram
import kotlinx.android.synthetic.main.fragment_edit_profile.view.phone
import com.squareup.picasso.MemoryPolicy
import kotlinx.android.synthetic.main.dialog_edit_profile_image.view.editImage
import kotlinx.android.synthetic.main.dialog_edit_profile_image.view.takeImage
Expand Down Expand Up @@ -54,6 +60,7 @@ import java.io.FileOutputStream
import java.io.IOException
import java.io.FileNotFoundException
import org.fossasia.openevent.general.utils.Utils.setToolbar
import org.fossasia.openevent.general.utils.emptyToNull
import org.fossasia.openevent.general.utils.setRequired
import org.jetbrains.anko.design.snackbar

Expand All @@ -77,6 +84,10 @@ class EditProfileFragment : Fragment(), ComplexBackPressFragment {
private lateinit var userLastName: String
private lateinit var userDetails: String
private lateinit var userAvatar: String
private lateinit var userPhone: String
private lateinit var userFacebook: String
private lateinit var userTwitter: String
private lateinit var userInstagram: String

override fun onCreateView(
inflater: LayoutInflater,
Expand Down Expand Up @@ -125,9 +136,8 @@ class EditProfileFragment : Fragment(), ComplexBackPressFragment {

rootView.updateButton.setOnClickListener {
hideSoftKeyboard(context, rootView)
if (isValidUsername()) {
editProfileViewModel.updateProfile(rootView.firstName.text.toString(),
rootView.lastName.text.toString(), rootView.details.text.toString())
if (isValidInput()) {
updateUser()
} else {
rootView.snackbar(getString(R.string.fill_required_fields_message))
}
Expand Down Expand Up @@ -176,7 +186,7 @@ class EditProfileFragment : Fragment(), ComplexBackPressFragment {
}
}

private fun isValidUsername(): Boolean {
private fun isValidInput(): Boolean {
var valid = true
if (rootView.firstName.text.isNullOrBlank()) {
rootView.firstName.error = getString(R.string.empty_field_error_message)
Expand All @@ -186,6 +196,18 @@ class EditProfileFragment : Fragment(), ComplexBackPressFragment {
rootView.lastName.error = getString(R.string.empty_field_error_message)
valid = false
}
if (!rootView.instagram.text.isNullOrEmpty() && !Patterns.WEB_URL.matcher(rootView.instagram.text).matches()) {
rootView.instagram.error = getString(R.string.invalid_url_message)
valid = false
}
if (!rootView.facebook.text.isNullOrEmpty() && !Patterns.WEB_URL.matcher(rootView.facebook.text).matches()) {
rootView.facebook.error = getString(R.string.invalid_url_message)
valid = false
}
if (!rootView.twitter.text.isNullOrEmpty() && !Patterns.WEB_URL.matcher(rootView.twitter.text).matches()) {
rootView.twitter.error = getString(R.string.invalid_url_message)
valid = false
}
return valid
}

Expand All @@ -194,6 +216,11 @@ class EditProfileFragment : Fragment(), ComplexBackPressFragment {
userLastName = user.lastName.nullToEmpty()
userDetails = user.details.nullToEmpty()
userAvatar = user.avatarUrl.nullToEmpty()
userPhone = user.contact.nullToEmpty()
userFacebook = user.facebookUrl.nullToEmpty()
userTwitter = user.twitterUrl.nullToEmpty()
userInstagram = user.instagramUrl.nullToEmpty()

if (safeArgs.croppedImage.isEmpty()) {
if (userAvatar.isNotEmpty() && !editProfileViewModel.avatarUpdated) {
val drawable = requireDrawable(requireContext(), R.drawable.ic_account_circle_grey)
Expand All @@ -208,15 +235,17 @@ class EditProfileFragment : Fragment(), ComplexBackPressFragment {
editProfileViewModel.encodedImage = encodeImage(croppedImage)
editProfileViewModel.avatarUpdated = true
}
if (rootView.firstName.text.isNullOrBlank()) {
rootView.firstName.setText(userFirstName)
}
if (rootView.lastName.text.isNullOrBlank()) {
rootView.lastName.setText(userLastName)
}
if (rootView.details.text.isNullOrBlank()) {
rootView.details.setText(userDetails)
}
setTextIfNull(rootView.firstName, userFirstName)
setTextIfNull(rootView.lastName, userLastName)
setTextIfNull(rootView.details, userDetails)
setTextIfNull(rootView.phone, userPhone)
setTextIfNull(rootView.facebook, userFacebook)
setTextIfNull(rootView.twitter, userTwitter)
setTextIfNull(rootView.instagram, userInstagram)
}

private fun setTextIfNull(input: TextInputEditText, text: String) {
if (input.text.isNullOrBlank()) input.setText(text)
}

private fun showEditPhotoDialog() {
Expand Down Expand Up @@ -340,8 +369,7 @@ class EditProfileFragment : Fragment(), ComplexBackPressFragment {
*/
override fun handleBackPress() {
val thisActivity = activity
if (!editProfileViewModel.avatarUpdated && rootView.lastName.text.toString() == userLastName &&
rootView.firstName.text.toString() == userFirstName && rootView.details.text.toString() == userDetails) {
if (noDataChanged()) {
findNavController(rootView).popBackStack()
} else {
hideSoftKeyboard(context, rootView)
Expand All @@ -351,13 +379,37 @@ class EditProfileFragment : Fragment(), ComplexBackPressFragment {
if (thisActivity is MainActivity) thisActivity.onSuperBackPressed()
}
dialog.setPositiveButton(getString(R.string.save)) { _, _ ->
editProfileViewModel.updateProfile(rootView.firstName.text.toString(),
rootView.lastName.text.toString(), rootView.details.text.toString())
}
if (isValidInput()) {
updateUser()
} else {
rootView.snackbar(getString(R.string.fill_required_fields_message))
} }
dialog.create().show()
}
}

private fun updateUser() {
val newUser = User(
id = editProfileViewModel.getId(),
firstName = rootView.firstName.text.toString(),
lastName = rootView.lastName.text.toString(),
details = rootView.details.text.toString(),
facebookUrl = rootView.facebook.text.toString().emptyToNull(),
twitterUrl = rootView.twitter.text.toString().emptyToNull(),
contact = rootView.phone.text.toString().emptyToNull()
)
editProfileViewModel.updateProfile(newUser)
}

private fun noDataChanged() = !editProfileViewModel.avatarUpdated &&
rootView.lastName.text.toString() == userLastName &&
rootView.firstName.text.toString() == userFirstName &&
rootView.details.text.toString() == userDetails &&
rootView.facebook.text.toString() == userFacebook &&
rootView.twitter.text.toString() == userTwitter &&
rootView.instagram.text.toString() == userInstagram &&
rootView.phone.text.toString() == userPhone

override fun onDestroyView() {
val activity = activity as? AppCompatActivity
activity?.supportActionBar?.setDisplayHomeAsUpEnabled(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ class EditProfileViewModel(
var avatarUpdated = false
var encodedImage: String? = null

fun getId() = authHolder.getId()

fun isLoggedIn() = authService.isLoggedIn()

/**
* @param firstName updated firstName
* @param lastName updated lastName
*/
fun updateProfile(firstName: String, lastName: String, details: String) {
fun updateProfile(user: User) {
if (encodedImage.isNullOrEmpty()) {
updateUser(null, firstName, lastName, details)
updateUser(user)
return
}
compositeDisposable += authService.uploadImage(UploadImage(encodedImage))
Expand All @@ -50,7 +48,7 @@ class EditProfileViewModel(
mutableProgress.value = false
}
.subscribe({
updateUser(it.url, firstName, lastName, details)
updateUser(user.copy(avatarUrl = it.url))
mutableMessage.value = resource.getString(R.string.image_upload_success_message)
Timber.d("Image uploaded ${it.url}")
}) {
Expand All @@ -59,21 +57,8 @@ class EditProfileViewModel(
}
}

private fun updateUser(url: String?, firstName: String, lastName: String, details: String) {
val id = authHolder.getId()
if (firstName.isEmpty() || lastName.isEmpty()) {
mutableMessage.value = resource.getString(R.string.provide_name_message)
return
}
compositeDisposable += authService.updateUser(
User(
id = id,
firstName = firstName,
lastName = lastName,
avatarUrl = url,
details = details
), id
)
private fun updateUser(user: User) {
compositeDisposable += authService.updateUser(user)
.withDefaultSchedulers()
.doOnSubscribe {
mutableProgress.value = true
Expand Down
62 changes: 61 additions & 1 deletion app/src/main/res/layout/fragment_edit_profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
app:navigationIcon="@drawable/ic_arrow_back_white_cct"
app:titleTextColor="@color/white"
app:title="@string/edit_profile"/>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -89,7 +92,7 @@
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/padding_large"
android:paddingBottom="@dimen/padding_medium"
android:hint="@string/details_of_the_user">

<com.google.android.material.textfield.TextInputEditText
Expand All @@ -99,6 +102,62 @@
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/padding_medium"
android:hint="@string/phone">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/padding_medium"
android:hint="@string/facebook">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/facebook"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textUri"/>
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/padding_medium"
android:hint="@string/twitter">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/twitter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textUri" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/padding_large"
android:hint="@string/instagram">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/instagram"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textUri" />
</com.google.android.material.textfield.TextInputLayout>

<Button
android:id="@+id/updateButton"
android:layout_width="match_parent"
Expand All @@ -116,4 +175,5 @@
android:layout_marginTop="@dimen/padding_large"
android:visibility="gone" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@
<string name="others">Others</string>
<string name="female">Female</string>
<string name="male">Male</string>
<string name="instagram">Instagram</string>
<plurals name="ordersQuantity">
<item quantity="one">%1$s ticket</item>
<item quantity="other">%1$s tickets</item>
Expand Down