Merge pull request #1 from Lakoja/3486-separate-diff-from-encode

3486 separate diff from encode
This commit is contained in:
Martin Marconcini 2023-08-23 15:50:26 +02:00 committed by GitHub
commit a58f29fa57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 64 deletions

View File

@ -100,6 +100,14 @@ class EditProfileActivity : BaseActivity(), Injectable {
} }
} }
private val currentProfileData
get() = ProfileData(
displayName = binding.displayNameEditText.text.toString(),
note = binding.noteEditText.text.toString(),
locked = binding.lockedCheckBox.isChecked,
fields = accountFieldEditAdapter.getFieldData()
)
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -206,14 +214,14 @@ class EditProfileActivity : BaseActivity(), Injectable {
} }
val onBackCallback = object : OnBackPressedCallback(enabled = true) { val onBackCallback = object : OnBackPressedCallback(enabled = true) {
override fun handleOnBackPressed() = checkForPotentialUnsavedChanges() override fun handleOnBackPressed() = checkForUnsavedChanges()
} }
onBackPressedDispatcher.addCallback(this, onBackCallback) onBackPressedDispatcher.addCallback(this, onBackCallback)
} }
fun checkForPotentialUnsavedChanges() { fun checkForUnsavedChanges() {
if (hasUnsavedChanges()) { if (viewModel.hasUnsavedChanges(currentProfileData)) {
showUnsavedChangesDialog() showUnsavedChangesDialog()
} else { } else {
finish() finish()
@ -223,18 +231,10 @@ class EditProfileActivity : BaseActivity(), Injectable {
override fun onStop() { override fun onStop() {
super.onStop() super.onStop()
if (!isFinishing) { if (!isFinishing) {
viewModel.updateProfile(profileData) viewModel.updateProfile(currentProfileData)
} }
} }
private val profileData
get() = ProfileData(
displayName = binding.displayNameEditText.text.toString(),
note = binding.noteEditText.text.toString(),
locked = binding.lockedCheckBox.isChecked,
fields = accountFieldEditAdapter.getFieldData()
)
private fun observeImage( private fun observeImage(
liveData: LiveData<Uri>, liveData: LiveData<Uri>,
imageView: ImageView, imageView: ImageView,
@ -308,7 +308,7 @@ class EditProfileActivity : BaseActivity(), Injectable {
return super.onOptionsItemSelected(item) return super.onOptionsItemSelected(item)
} }
private fun save() = viewModel.save(profileData) private fun save() = viewModel.save(currentProfileData)
private fun onSaveFailure(msg: String?) { private fun onSaveFailure(msg: String?) {
val errorMsg = msg ?: getString(R.string.error_media_upload_sending) val errorMsg = msg ?: getString(R.string.error_media_upload_sending)
@ -328,8 +328,6 @@ class EditProfileActivity : BaseActivity(), Injectable {
} }
} }
private fun hasUnsavedChanges() = viewModel.hasUnsavedChanges(profileData)
private suspend fun launchAlertDialog() = AlertDialog.Builder(this) private suspend fun launchAlertDialog() = AlertDialog.Builder(this)
.setTitle(getString(R.string.title_edit_profile_save_changes_prompt)) .setTitle(getString(R.string.title_edit_profile_save_changes_prompt))
.setMessage(getString(R.string.message_edit_profile_save_changes_prompt)) .setMessage(getString(R.string.message_edit_profile_save_changes_prompt))

View File

@ -42,7 +42,6 @@ import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.RequestBody.Companion.toRequestBody
import java.io.File import java.io.File
@ -76,7 +75,7 @@ class EditProfileViewModel @Inject constructor(
val instanceData: Flow<InstanceInfo> = instanceInfoRepo::getInstanceInfo.asFlow() val instanceData: Flow<InstanceInfo> = instanceInfoRepo::getInstanceInfo.asFlow()
.shareIn(viewModelScope, SharingStarted.Eagerly, replay = 1) .shareIn(viewModelScope, SharingStarted.Eagerly, replay = 1)
private var oldProfileData: Account? = null private var apiProfileAccount: Account? = null
fun obtainProfile() = viewModelScope.launch { fun obtainProfile() = viewModelScope.launch {
if (profileData.value == null || profileData.value is Error) { if (profileData.value == null || profileData.value is Error) {
@ -84,7 +83,7 @@ class EditProfileViewModel @Inject constructor(
mastodonApi.accountVerifyCredentials().fold( mastodonApi.accountVerifyCredentials().fold(
{ profile -> { profile ->
oldProfileData = profile apiProfileAccount = profile
profileData.postValue(Success(profile)) profileData.postValue(Success(profile))
}, },
{ {
@ -113,21 +112,42 @@ class EditProfileViewModel @Inject constructor(
saveData.value = Loading() saveData.value = Loading()
val encoded = encodeChangedProfileFields(newProfileData) val diff = getProfileDiff(apiProfileAccount, newProfileData)
if (encoded.allFieldsAreNull()) { if (!diff.hasChanges()) {
// if nothing has changed, there is no need to make a network request // if nothing has changed, there is no need to make an api call
saveData.postValue(Success()) saveData.postValue(Success())
return return
} }
viewModelScope.launch { viewModelScope.launch {
var avatarFileBody: MultipartBody.Part? = null
diff.avatarFile?.let {
avatarFileBody = MultipartBody.Part.createFormData("avatar", randomAlphanumericString(12), it.asRequestBody("image/png".toMediaTypeOrNull()))
}
var headerFileBody: MultipartBody.Part? = null
diff.headerFile?.let {
headerFileBody = MultipartBody.Part.createFormData("header", randomAlphanumericString(12), it.asRequestBody("image/png".toMediaTypeOrNull()))
}
mastodonApi.accountUpdateCredentials( mastodonApi.accountUpdateCredentials(
encoded.displayName, encoded.note, encoded.locked, encoded.avatar, encoded.header, diff.displayName?.toRequestBody(MultipartBody.FORM),
encoded.field1?.first, encoded.field1?.second, encoded.field2?.first, encoded.field2?.second, encoded.field3?.first, encoded.field3?.second, encoded.field4?.first, encoded.field4?.second diff.note?.toRequestBody(MultipartBody.FORM),
diff.locked?.toString()?.toRequestBody(MultipartBody.FORM),
avatarFileBody,
headerFileBody,
diff.field1?.first?.toRequestBody(MultipartBody.FORM),
diff.field1?.second?.toRequestBody(MultipartBody.FORM),
diff.field2?.first?.toRequestBody(MultipartBody.FORM),
diff.field2?.second?.toRequestBody(MultipartBody.FORM),
diff.field3?.first?.toRequestBody(MultipartBody.FORM),
diff.field3?.second?.toRequestBody(MultipartBody.FORM),
diff.field4?.first?.toRequestBody(MultipartBody.FORM),
diff.field4?.second?.toRequestBody(MultipartBody.FORM)
).fold( ).fold(
{ newProfileData -> { newAccountData ->
saveData.postValue(Success()) saveData.postValue(Success())
eventHub.dispatch(ProfileEditedEvent(newProfileData)) eventHub.dispatch(ProfileEditedEvent(newAccountData))
}, },
{ throwable -> { throwable ->
saveData.postValue(Error(errorMessage = throwable.getServerErrorMessage())) saveData.postValue(Error(errorMessage = throwable.getServerErrorMessage()))
@ -151,63 +171,61 @@ class EditProfileViewModel @Inject constructor(
} }
internal fun hasUnsavedChanges(newProfileData: ProfileData): Boolean { internal fun hasUnsavedChanges(newProfileData: ProfileData): Boolean {
val encoded = encodeChangedProfileFields(newProfileData) val diff = getProfileDiff(apiProfileAccount, newProfileData)
// If all fields are null, there are no changes.
return !encoded.allFieldsAreNull() return diff.hasChanges()
} }
private fun encodeChangedProfileFields(newProfileData: ProfileData): EncodedProfileData { private fun getProfileDiff(oldProfileAccount: Account?, newProfileData: ProfileData): DiffProfileData {
val displayName = if (oldProfileData?.displayName == newProfileData.displayName) { val displayName = if (oldProfileAccount?.displayName == newProfileData.displayName) {
null null
} else { } else {
newProfileData.displayName.toRequestBody(MultipartBody.FORM) newProfileData.displayName
} }
val note = if (oldProfileData?.source?.note == newProfileData.note) { val note = if (oldProfileAccount?.source?.note == newProfileData.note) {
null null
} else { } else {
newProfileData.note.toRequestBody(MultipartBody.FORM) newProfileData.note
} }
val locked = if (oldProfileData?.locked == newProfileData.locked) { val locked = if (oldProfileAccount?.locked == newProfileData.locked) {
null null
} else { } else {
newProfileData.locked.toString().toRequestBody(MultipartBody.FORM) newProfileData.locked
} }
val avatar = if (avatarData.value != null) { val avatarFile = if (avatarData.value != null) {
val avatarBody = getCacheFileForName(AVATAR_FILE_NAME).asRequestBody("image/png".toMediaTypeOrNull()) getCacheFileForName(AVATAR_FILE_NAME)
MultipartBody.Part.createFormData("avatar", randomAlphanumericString(12), avatarBody)
} else { } else {
null null
} }
val header = if (headerData.value != null) { val headerFile = if (headerData.value != null) {
val headerBody = getCacheFileForName(HEADER_FILE_NAME).asRequestBody("image/png".toMediaTypeOrNull()) getCacheFileForName(HEADER_FILE_NAME)
MultipartBody.Part.createFormData("header", randomAlphanumericString(12), headerBody)
} else { } else {
null null
} }
// when one field changed, all have to be sent or they unchanged ones would get overridden // when one field changed, all have to be sent or they unchanged ones would get overridden
val fieldsUnchanged = oldProfileData?.source?.fields == newProfileData.fields val allFieldsUnchanged = oldProfileAccount?.source?.fields == newProfileData.fields
val field1 = calculateFieldToUpdate(newProfileData.fields.getOrNull(0), fieldsUnchanged) val field1 = calculateFieldToUpdate(newProfileData.fields.getOrNull(0), allFieldsUnchanged)
val field2 = calculateFieldToUpdate(newProfileData.fields.getOrNull(1), fieldsUnchanged) val field2 = calculateFieldToUpdate(newProfileData.fields.getOrNull(1), allFieldsUnchanged)
val field3 = calculateFieldToUpdate(newProfileData.fields.getOrNull(2), fieldsUnchanged) val field3 = calculateFieldToUpdate(newProfileData.fields.getOrNull(2), allFieldsUnchanged)
val field4 = calculateFieldToUpdate(newProfileData.fields.getOrNull(3), fieldsUnchanged) val field4 = calculateFieldToUpdate(newProfileData.fields.getOrNull(3), allFieldsUnchanged)
return EncodedProfileData( return DiffProfileData(
displayName, note, locked, field1, field2, field3, field4, header, avatar displayName, note, locked, field1, field2, field3, field4, headerFile, avatarFile
) )
} }
private fun calculateFieldToUpdate(newField: StringField?, fieldsUnchanged: Boolean): Pair<RequestBody, RequestBody>? { private fun calculateFieldToUpdate(newField: StringField?, fieldsUnchanged: Boolean): Pair<String, String>? {
if (fieldsUnchanged || newField == null) { if (fieldsUnchanged || newField == null) {
return null return null
} }
return Pair( return Pair(
newField.name.toRequestBody(MultipartBody.FORM), newField.name,
newField.value.toRequestBody(MultipartBody.FORM) newField.value
) )
} }
@ -215,19 +233,19 @@ class EditProfileViewModel @Inject constructor(
return File(application.cacheDir, filename) return File(application.cacheDir, filename)
} }
private data class EncodedProfileData( private data class DiffProfileData(
val displayName: RequestBody?, val displayName: String?,
val note: RequestBody?, val note: String?,
val locked: RequestBody?, val locked: Boolean?,
val field1: Pair<RequestBody, RequestBody>?, val field1: Pair<String, String>?,
val field2: Pair<RequestBody, RequestBody>?, val field2: Pair<String, String>?,
val field3: Pair<RequestBody, RequestBody>?, val field3: Pair<String, String>?,
val field4: Pair<RequestBody, RequestBody>?, val field4: Pair<String, String>?,
val header: MultipartBody.Part?, val headerFile: File?,
val avatar: MultipartBody.Part? val avatarFile: File?
) { ) {
fun allFieldsAreNull() = displayName == null && note == null && locked == null && fun hasChanges() = displayName != null || note != null || locked != null ||
avatar == null && header == null && field1 == null && field2 == null && avatarFile != null || headerFile != null || field1 != null || field2 != null ||
field3 == null && field4 == null field3 != null || field4 != null
} }
} }