Fix profile pic edit

See https://github.com/pixelfed/pixelfed/issues/4250
This commit is contained in:
Fred 2024-01-22 22:44:35 +01:00 committed by Matthieu
parent 23fbebfe44
commit 580f7ca911
3 changed files with 49 additions and 30 deletions

View File

@ -112,18 +112,18 @@ class EditProfileActivity : BaseActivity() {
} }
} }
// binding.changeImageButton.setOnClickListener { binding.profilePic.setOnClickListener {
// Intent(Intent.ACTION_GET_CONTENT).apply { Intent(Intent.ACTION_GET_CONTENT).apply {
// type = "*/*" type = "*/*"
// putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/*")) putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/*"))
// action = Intent.ACTION_GET_CONTENT action = Intent.ACTION_GET_CONTENT
// addCategory(Intent.CATEGORY_OPENABLE) addCategory(Intent.CATEGORY_OPENABLE)
// putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false) putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false)
// uploadImageResultContract.launch( uploadImageResultContract.launch(
// Intent.createChooser(this, null) Intent.createChooser(this, null)
// ) )
// } }
// } }
} }
private val uploadImageResultContract = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> private val uploadImageResultContract = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
@ -137,10 +137,10 @@ class EditProfileActivity : BaseActivity() {
val imageUri: String = clipData.getItemAt(i).uri.toString() val imageUri: String = clipData.getItemAt(i).uri.toString()
images.add(imageUri) images.add(imageUri)
} }
model.uploadImage(images.first()) model.updateImage(images.first())
} else if (data.data != null) { } else if (data.data != null) {
images.add(data.data!!.toString()) images.add(data.data!!.toString())
model.uploadImage(images.first()) model.updateImage(images.first())
} }
} }
} }

View File

@ -97,12 +97,19 @@ class EditProfileViewModel(application: Application) : AndroidViewModel(applicat
note = bio, note = bio,
locked = privateAccount, locked = privateAccount,
) )
val newAvatarUri = if (profilePictureChanged) {
uploadImage()
profilePictureUri
} else {
account.anyAvatar()?.toUri()
}
oldProfile = account oldProfile = account
_uiState.update { currentUiState -> _uiState.update { currentUiState ->
currentUiState.copy( currentUiState.copy(
bio = account.source?.note ?: account.note?.let {fromHtml(it).toString()}, bio = account.source?.note
?: account.note?.let { fromHtml(it).toString() },
name = account.display_name, name = account.display_name,
profilePictureUri = account.anyAvatar()?.toUri(), profilePictureUri = newAvatarUri,
privateAccount = account.locked, privateAccount = account.locked,
sendingProfile = false, sendingProfile = false,
profileSent = true, profileSent = true,
@ -153,12 +160,13 @@ class EditProfileViewModel(application: Application) : AndroidViewModel(applicat
fun madeChanges(): Boolean = fun madeChanges(): Boolean =
with(uiState.value) { with(uiState.value) {
val bioUnchanged: Boolean = oldProfile?.source?.note?.let { it != bio } val privateChanged = oldProfile?.locked != privateAccount
// If source note is null, check note val displayNameChanged = oldProfile?.display_name != name
val bioChanged: Boolean = oldProfile?.source?.note?.let { it != bio }
// If source note is null, check note
?: oldProfile?.note?.let { fromHtml(it).toString() != bio } ?: oldProfile?.note?.let { fromHtml(it).toString() != bio }
?: true ?: true
oldProfile?.locked != privateAccount || oldProfile?.display_name != name profilePictureChanged || privateChanged || displayNameChanged || bioChanged
|| bioUnchanged
} }
fun clickedCard() { fun clickedCard() {
@ -178,16 +186,26 @@ class EditProfileViewModel(application: Application) : AndroidViewModel(applicat
} }
} }
fun uploadImage(image: String) { fun updateImage(image: String) {
//TODO fix _uiState.update { currentUiState ->
currentUiState.copy(
profilePictureUri = image.toUri(),
profilePictureChanged = true
)
}
}
private fun uploadImage() {
val image = uiState.value.profilePictureUri!!
val inputStream = val inputStream =
getApplication<PixelDroidApplication>().contentResolver.openInputStream(image.toUri()) getApplication<PixelDroidApplication>().contentResolver.openInputStream(image)
?: return ?: return
val size: Long = val size: Long =
if (image.toUri().scheme == "content") { if (image.scheme == "content") {
getApplication<PixelDroidApplication>().contentResolver.query( getApplication<PixelDroidApplication>().contentResolver.query(
image.toUri(), image,
null, null,
null, null,
null, null,
@ -203,7 +221,7 @@ class EditProfileViewModel(application: Application) : AndroidViewModel(applicat
cursor.getLong(sizeIndex) cursor.getLong(sizeIndex)
} ?: 0 } ?: 0
} else { } else {
image.toUri().toFile().length() image.toFile().length()
} }
val imagePart = ProgressRequestBody(inputStream, size, "image/*") val imagePart = ProgressRequestBody(inputStream, size, "image/*")
@ -232,8 +250,7 @@ class EditProfileViewModel(application: Application) : AndroidViewModel(applicat
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
{ it: Account -> { it: Account ->
Log.e("qsdfqsdfs", it.toString()) Log.i("ACCOUNT", it.toString())
}, },
{ e: Throwable -> { e: Throwable ->
_uiState.update { currentUiState -> _uiState.update { currentUiState ->
@ -250,6 +267,7 @@ class EditProfileViewModel(application: Application) : AndroidViewModel(applicat
{ {
_uiState.update { currentUiState -> _uiState.update { currentUiState ->
currentUiState.copy( currentUiState.copy(
profilePictureChanged = false,
uploadProgress = 100, uploadProgress = 100,
uploadingPicture = false uploadingPicture = false
) )
@ -265,7 +283,8 @@ class EditProfileViewModel(application: Application) : AndroidViewModel(applicat
data class EditProfileActivityUiState( data class EditProfileActivityUiState(
val name: String? = null, val name: String? = null,
val bio: String? = null, val bio: String? = null,
val profilePictureUri: Uri?= null, val profilePictureUri: Uri? = null,
val profilePictureChanged: Boolean = false,
val privateAccount: Boolean? = null, val privateAccount: Boolean? = null,
val loadingProfile: Boolean = true, val loadingProfile: Boolean = true,
val profileLoaded: Boolean = false, val profileLoaded: Boolean = false,

View File

@ -348,7 +348,7 @@ interface PixelfedAPI {
): Account ): Account
@Multipart @Multipart
@PATCH("/api/v1/accounts/update_credentials") @POST("/api/v1/accounts/update_credentials")
fun updateProfilePicture( fun updateProfilePicture(
@Part avatar: MultipartBody.Part? @Part avatar: MultipartBody.Part?
): Observable<Account> ): Observable<Account>