Apply PR suggestions:

* Remove dialog title and string
* Rename Data Class
* Use liveData.value directly when possible
This commit is contained in:
Martin Marconcini 2023-08-24 10:06:25 +02:00
parent 2d52ab9072
commit 387c3989d7
No known key found for this signature in database
3 changed files with 13 additions and 18 deletions

View File

@ -52,7 +52,7 @@ import com.keylesspalace.tusky.util.await
import com.keylesspalace.tusky.util.show
import com.keylesspalace.tusky.util.viewBinding
import com.keylesspalace.tusky.viewmodel.EditProfileViewModel
import com.keylesspalace.tusky.viewmodel.ProfileData
import com.keylesspalace.tusky.viewmodel.ProfileDataInUi
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import com.mikepenz.iconics.utils.colorInt
@ -101,7 +101,7 @@ class EditProfileActivity : BaseActivity(), Injectable {
}
private val currentProfileData
get() = ProfileData(
get() = ProfileDataInUi(
displayName = binding.displayNameEditText.text.toString(),
note = binding.noteEditText.text.toString(),
locked = binding.lockedCheckBox.isChecked,
@ -322,15 +322,14 @@ class EditProfileActivity : BaseActivity(), Injectable {
}
private fun showUnsavedChangesDialog() = lifecycleScope.launch {
when (launchAlertDialog()) {
when (launchSaveDialog()) {
AlertDialog.BUTTON_POSITIVE -> save()
else -> finish()
}
}
private suspend fun launchAlertDialog() = AlertDialog.Builder(this)
.setTitle(getString(R.string.title_edit_profile_save_changes_prompt))
.setMessage(getString(R.string.message_edit_profile_save_changes_prompt))
private suspend fun launchSaveDialog() = AlertDialog.Builder(this)
.setMessage(getString(R.string.dialog_save_profile_changes_message))
.create()
.await(R.string.action_save, R.string.action_discard)
}

View File

@ -50,10 +50,7 @@ import javax.inject.Inject
private const val HEADER_FILE_NAME = "header.png"
private const val AVATAR_FILE_NAME = "avatar.png"
/**
* Conveniently groups Profile Data users can modify in the UI.
*/
internal data class ProfileData(
internal data class ProfileDataInUi(
val displayName: String,
val note: String,
val locked: Boolean,
@ -105,7 +102,7 @@ class EditProfileViewModel @Inject constructor(
headerData.value = getHeaderUri()
}
internal fun save(newProfileData: ProfileData) {
internal fun save(newProfileData: ProfileDataInUi) {
if (saveData.value is Loading || profileData.value !is Success) {
return
}
@ -115,7 +112,7 @@ class EditProfileViewModel @Inject constructor(
val diff = getProfileDiff(apiProfileAccount, newProfileData)
if (!diff.hasChanges()) {
// if nothing has changed, there is no need to make an api call
saveData.postValue(Success())
saveData.value = Success()
return
}
@ -157,7 +154,7 @@ class EditProfileViewModel @Inject constructor(
}
// cache activity state for rotation change
internal fun updateProfile(newProfileData: ProfileData) {
internal fun updateProfile(newProfileData: ProfileDataInUi) {
if (profileData.value is Success) {
val newProfileSource = profileData.value?.data?.source?.copy(note = newProfileData.note, fields = newProfileData.fields)
val newProfile = profileData.value?.data?.copy(
@ -166,17 +163,17 @@ class EditProfileViewModel @Inject constructor(
source = newProfileSource
)
profileData.postValue(Success(newProfile))
profileData.value = Success(newProfile)
}
}
internal fun hasUnsavedChanges(newProfileData: ProfileData): Boolean {
internal fun hasUnsavedChanges(newProfileData: ProfileDataInUi): Boolean {
val diff = getProfileDiff(apiProfileAccount, newProfileData)
return diff.hasChanges()
}
private fun getProfileDiff(oldProfileAccount: Account?, newProfileData: ProfileData): DiffProfileData {
private fun getProfileDiff(oldProfileAccount: Account?, newProfileData: ProfileDataInUi): DiffProfileData {
val displayName = if (oldProfileAccount?.displayName == newProfileData.displayName) {
null
} else {

View File

@ -819,6 +819,5 @@
<string name="error_media_playback">Playback failed: %s</string>
<string name="dialog_delete_filter_text">Delete filter \'%1$s\'?"</string>
<string name="dialog_delete_filter_positive_action">Delete</string>
<string name="title_edit_profile_save_changes_prompt">Unsaved Changes</string>
<string name="message_edit_profile_save_changes_prompt">Do you want to save your profile changes?</string>
<string name="dialog_save_profile_changes_message">Do you want to save your profile changes?</string>
</resources>