Skip to content

Commit 0003ecc

Browse files
anhanh11001iamareebjamal
authored andcommitted
feat: Add more field to update user detail (#2139)
Fixes: #1677
1 parent 7ee3322 commit 0003ecc

File tree

5 files changed

+141
-43
lines changed

5 files changed

+141
-43
lines changed

app/src/main/java/org/fossasia/openevent/general/auth/AuthService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class AuthService(
4242
}
4343
}
4444

45-
fun updateUser(user: User, id: Long): Single<User> {
46-
return authApi.updateUser(user, id).map {
45+
fun updateUser(user: User): Single<User> {
46+
return authApi.updateUser(user, user.id).map {
4747
userDao.insertUser(it)
4848
it
4949
}

app/src/main/java/org/fossasia/openevent/general/auth/EditProfileFragment.kt

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.fossasia.openevent.general.utils.ImageUtils.decodeBitmap
1010
import android.os.Bundle
1111
import android.provider.MediaStore
1212
import android.util.Base64
13+
import android.util.Patterns
1314
import android.view.LayoutInflater
1415
import android.view.View
1516
import android.view.ViewGroup
@@ -21,11 +22,16 @@ import androidx.fragment.app.Fragment
2122
import androidx.lifecycle.Observer
2223
import androidx.navigation.Navigation.findNavController
2324
import androidx.navigation.fragment.navArgs
25+
import com.google.android.material.textfield.TextInputEditText
2426
import com.squareup.picasso.Picasso
2527
import kotlinx.android.synthetic.main.fragment_edit_profile.view.updateButton
2628
import kotlinx.android.synthetic.main.fragment_edit_profile.view.toolbar
2729
import kotlinx.android.synthetic.main.fragment_edit_profile.view.firstName
2830
import kotlinx.android.synthetic.main.fragment_edit_profile.view.details
31+
import kotlinx.android.synthetic.main.fragment_edit_profile.view.facebook
32+
import kotlinx.android.synthetic.main.fragment_edit_profile.view.twitter
33+
import kotlinx.android.synthetic.main.fragment_edit_profile.view.instagram
34+
import kotlinx.android.synthetic.main.fragment_edit_profile.view.phone
2935
import com.squareup.picasso.MemoryPolicy
3036
import kotlinx.android.synthetic.main.dialog_edit_profile_image.view.editImage
3137
import kotlinx.android.synthetic.main.dialog_edit_profile_image.view.takeImage
@@ -54,6 +60,7 @@ import java.io.FileOutputStream
5460
import java.io.IOException
5561
import java.io.FileNotFoundException
5662
import org.fossasia.openevent.general.utils.Utils.setToolbar
63+
import org.fossasia.openevent.general.utils.emptyToNull
5764
import org.fossasia.openevent.general.utils.setRequired
5865
import org.jetbrains.anko.design.snackbar
5966

@@ -77,6 +84,10 @@ class EditProfileFragment : Fragment(), ComplexBackPressFragment {
7784
private lateinit var userLastName: String
7885
private lateinit var userDetails: String
7986
private lateinit var userAvatar: String
87+
private lateinit var userPhone: String
88+
private lateinit var userFacebook: String
89+
private lateinit var userTwitter: String
90+
private lateinit var userInstagram: String
8091

8192
override fun onCreateView(
8293
inflater: LayoutInflater,
@@ -125,9 +136,8 @@ class EditProfileFragment : Fragment(), ComplexBackPressFragment {
125136

126137
rootView.updateButton.setOnClickListener {
127138
hideSoftKeyboard(context, rootView)
128-
if (isValidUsername()) {
129-
editProfileViewModel.updateProfile(rootView.firstName.text.toString(),
130-
rootView.lastName.text.toString(), rootView.details.text.toString())
139+
if (isValidInput()) {
140+
updateUser()
131141
} else {
132142
rootView.snackbar(getString(R.string.fill_required_fields_message))
133143
}
@@ -176,7 +186,7 @@ class EditProfileFragment : Fragment(), ComplexBackPressFragment {
176186
}
177187
}
178188

179-
private fun isValidUsername(): Boolean {
189+
private fun isValidInput(): Boolean {
180190
var valid = true
181191
if (rootView.firstName.text.isNullOrBlank()) {
182192
rootView.firstName.error = getString(R.string.empty_field_error_message)
@@ -186,6 +196,18 @@ class EditProfileFragment : Fragment(), ComplexBackPressFragment {
186196
rootView.lastName.error = getString(R.string.empty_field_error_message)
187197
valid = false
188198
}
199+
if (!rootView.instagram.text.isNullOrEmpty() && !Patterns.WEB_URL.matcher(rootView.instagram.text).matches()) {
200+
rootView.instagram.error = getString(R.string.invalid_url_message)
201+
valid = false
202+
}
203+
if (!rootView.facebook.text.isNullOrEmpty() && !Patterns.WEB_URL.matcher(rootView.facebook.text).matches()) {
204+
rootView.facebook.error = getString(R.string.invalid_url_message)
205+
valid = false
206+
}
207+
if (!rootView.twitter.text.isNullOrEmpty() && !Patterns.WEB_URL.matcher(rootView.twitter.text).matches()) {
208+
rootView.twitter.error = getString(R.string.invalid_url_message)
209+
valid = false
210+
}
189211
return valid
190212
}
191213

@@ -194,6 +216,11 @@ class EditProfileFragment : Fragment(), ComplexBackPressFragment {
194216
userLastName = user.lastName.nullToEmpty()
195217
userDetails = user.details.nullToEmpty()
196218
userAvatar = user.avatarUrl.nullToEmpty()
219+
userPhone = user.contact.nullToEmpty()
220+
userFacebook = user.facebookUrl.nullToEmpty()
221+
userTwitter = user.twitterUrl.nullToEmpty()
222+
userInstagram = user.instagramUrl.nullToEmpty()
223+
197224
if (safeArgs.croppedImage.isEmpty()) {
198225
if (userAvatar.isNotEmpty() && !editProfileViewModel.avatarUpdated) {
199226
val drawable = requireDrawable(requireContext(), R.drawable.ic_account_circle_grey)
@@ -208,15 +235,17 @@ class EditProfileFragment : Fragment(), ComplexBackPressFragment {
208235
editProfileViewModel.encodedImage = encodeImage(croppedImage)
209236
editProfileViewModel.avatarUpdated = true
210237
}
211-
if (rootView.firstName.text.isNullOrBlank()) {
212-
rootView.firstName.setText(userFirstName)
213-
}
214-
if (rootView.lastName.text.isNullOrBlank()) {
215-
rootView.lastName.setText(userLastName)
216-
}
217-
if (rootView.details.text.isNullOrBlank()) {
218-
rootView.details.setText(userDetails)
219-
}
238+
setTextIfNull(rootView.firstName, userFirstName)
239+
setTextIfNull(rootView.lastName, userLastName)
240+
setTextIfNull(rootView.details, userDetails)
241+
setTextIfNull(rootView.phone, userPhone)
242+
setTextIfNull(rootView.facebook, userFacebook)
243+
setTextIfNull(rootView.twitter, userTwitter)
244+
setTextIfNull(rootView.instagram, userInstagram)
245+
}
246+
247+
private fun setTextIfNull(input: TextInputEditText, text: String) {
248+
if (input.text.isNullOrBlank()) input.setText(text)
220249
}
221250

222251
private fun showEditPhotoDialog() {
@@ -340,8 +369,7 @@ class EditProfileFragment : Fragment(), ComplexBackPressFragment {
340369
*/
341370
override fun handleBackPress() {
342371
val thisActivity = activity
343-
if (!editProfileViewModel.avatarUpdated && rootView.lastName.text.toString() == userLastName &&
344-
rootView.firstName.text.toString() == userFirstName && rootView.details.text.toString() == userDetails) {
372+
if (noDataChanged()) {
345373
findNavController(rootView).popBackStack()
346374
} else {
347375
hideSoftKeyboard(context, rootView)
@@ -351,13 +379,37 @@ class EditProfileFragment : Fragment(), ComplexBackPressFragment {
351379
if (thisActivity is MainActivity) thisActivity.onSuperBackPressed()
352380
}
353381
dialog.setPositiveButton(getString(R.string.save)) { _, _ ->
354-
editProfileViewModel.updateProfile(rootView.firstName.text.toString(),
355-
rootView.lastName.text.toString(), rootView.details.text.toString())
356-
}
382+
if (isValidInput()) {
383+
updateUser()
384+
} else {
385+
rootView.snackbar(getString(R.string.fill_required_fields_message))
386+
} }
357387
dialog.create().show()
358388
}
359389
}
360390

391+
private fun updateUser() {
392+
val newUser = User(
393+
id = editProfileViewModel.getId(),
394+
firstName = rootView.firstName.text.toString(),
395+
lastName = rootView.lastName.text.toString(),
396+
details = rootView.details.text.toString(),
397+
facebookUrl = rootView.facebook.text.toString().emptyToNull(),
398+
twitterUrl = rootView.twitter.text.toString().emptyToNull(),
399+
contact = rootView.phone.text.toString().emptyToNull()
400+
)
401+
editProfileViewModel.updateProfile(newUser)
402+
}
403+
404+
private fun noDataChanged() = !editProfileViewModel.avatarUpdated &&
405+
rootView.lastName.text.toString() == userLastName &&
406+
rootView.firstName.text.toString() == userFirstName &&
407+
rootView.details.text.toString() == userDetails &&
408+
rootView.facebook.text.toString() == userFacebook &&
409+
rootView.twitter.text.toString() == userTwitter &&
410+
rootView.instagram.text.toString() == userInstagram &&
411+
rootView.phone.text.toString() == userPhone
412+
361413
override fun onDestroyView() {
362414
val activity = activity as? AppCompatActivity
363415
activity?.supportActionBar?.setDisplayHomeAsUpEnabled(false)

app/src/main/java/org/fossasia/openevent/general/auth/EditProfileViewModel.kt

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,13 @@ class EditProfileViewModel(
3030
var avatarUpdated = false
3131
var encodedImage: String? = null
3232

33+
fun getId() = authHolder.getId()
34+
3335
fun isLoggedIn() = authService.isLoggedIn()
3436

35-
/**
36-
* @param firstName updated firstName
37-
* @param lastName updated lastName
38-
*/
39-
fun updateProfile(firstName: String, lastName: String, details: String) {
37+
fun updateProfile(user: User) {
4038
if (encodedImage.isNullOrEmpty()) {
41-
updateUser(null, firstName, lastName, details)
39+
updateUser(user)
4240
return
4341
}
4442
compositeDisposable += authService.uploadImage(UploadImage(encodedImage))
@@ -50,7 +48,7 @@ class EditProfileViewModel(
5048
mutableProgress.value = false
5149
}
5250
.subscribe({
53-
updateUser(it.url, firstName, lastName, details)
51+
updateUser(user.copy(avatarUrl = it.url))
5452
mutableMessage.value = resource.getString(R.string.image_upload_success_message)
5553
Timber.d("Image uploaded ${it.url}")
5654
}) {
@@ -59,21 +57,8 @@ class EditProfileViewModel(
5957
}
6058
}
6159

62-
private fun updateUser(url: String?, firstName: String, lastName: String, details: String) {
63-
val id = authHolder.getId()
64-
if (firstName.isEmpty() || lastName.isEmpty()) {
65-
mutableMessage.value = resource.getString(R.string.provide_name_message)
66-
return
67-
}
68-
compositeDisposable += authService.updateUser(
69-
User(
70-
id = id,
71-
firstName = firstName,
72-
lastName = lastName,
73-
avatarUrl = url,
74-
details = details
75-
), id
76-
)
60+
private fun updateUser(user: User) {
61+
compositeDisposable += authService.updateUser(user)
7762
.withDefaultSchedulers()
7863
.doOnSubscribe {
7964
mutableProgress.value = true

app/src/main/res/layout/fragment_edit_profile.xml

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
app:navigationIcon="@drawable/ic_arrow_back_white_cct"
1313
app:titleTextColor="@color/white"
1414
app:title="@string/edit_profile"/>
15+
<androidx.core.widget.NestedScrollView
16+
android:layout_width="match_parent"
17+
android:layout_height="wrap_content">
1518
<LinearLayout
1619
android:layout_width="match_parent"
1720
android:layout_height="wrap_content"
@@ -89,7 +92,7 @@
8992
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
9093
android:layout_width="match_parent"
9194
android:layout_height="wrap_content"
92-
android:paddingBottom="@dimen/padding_large"
95+
android:paddingBottom="@dimen/padding_medium"
9396
android:hint="@string/details_of_the_user">
9497

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

105+
<com.google.android.material.textfield.TextInputLayout
106+
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
107+
android:layout_width="match_parent"
108+
android:layout_height="wrap_content"
109+
android:paddingBottom="@dimen/padding_medium"
110+
android:hint="@string/phone">
111+
112+
<com.google.android.material.textfield.TextInputEditText
113+
android:id="@+id/phone"
114+
android:layout_width="match_parent"
115+
android:layout_height="wrap_content"
116+
android:inputType="number" />
117+
</com.google.android.material.textfield.TextInputLayout>
118+
119+
<com.google.android.material.textfield.TextInputLayout
120+
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
121+
android:layout_width="match_parent"
122+
android:layout_height="wrap_content"
123+
android:paddingBottom="@dimen/padding_medium"
124+
android:hint="@string/facebook">
125+
126+
<com.google.android.material.textfield.TextInputEditText
127+
android:id="@+id/facebook"
128+
android:layout_width="match_parent"
129+
android:layout_height="wrap_content"
130+
android:inputType="textUri"/>
131+
</com.google.android.material.textfield.TextInputLayout>
132+
133+
<com.google.android.material.textfield.TextInputLayout
134+
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
135+
android:layout_width="match_parent"
136+
android:layout_height="wrap_content"
137+
android:paddingBottom="@dimen/padding_medium"
138+
android:hint="@string/twitter">
139+
140+
<com.google.android.material.textfield.TextInputEditText
141+
android:id="@+id/twitter"
142+
android:layout_width="match_parent"
143+
android:layout_height="wrap_content"
144+
android:inputType="textUri" />
145+
</com.google.android.material.textfield.TextInputLayout>
146+
147+
<com.google.android.material.textfield.TextInputLayout
148+
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
149+
android:layout_width="match_parent"
150+
android:layout_height="wrap_content"
151+
android:paddingBottom="@dimen/padding_large"
152+
android:hint="@string/instagram">
153+
154+
<com.google.android.material.textfield.TextInputEditText
155+
android:id="@+id/instagram"
156+
android:layout_width="match_parent"
157+
android:layout_height="wrap_content"
158+
android:inputType="textUri" />
159+
</com.google.android.material.textfield.TextInputLayout>
160+
102161
<Button
103162
android:id="@+id/updateButton"
104163
android:layout_width="match_parent"
@@ -116,4 +175,5 @@
116175
android:layout_marginTop="@dimen/padding_large"
117176
android:visibility="gone" />
118177
</LinearLayout>
178+
</androidx.core.widget.NestedScrollView>
119179
</LinearLayout>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@
440440
<string name="others">Others</string>
441441
<string name="female">Female</string>
442442
<string name="male">Male</string>
443+
<string name="instagram">Instagram</string>
443444
<plurals name="ordersQuantity">
444445
<item quantity="one">%1$s ticket</item>
445446
<item quantity="other">%1$s tickets</item>

0 commit comments

Comments
 (0)