From 580f7ca911fdc92ba42e20a694679ac81b93cf93 Mon Sep 17 00:00:00 2001 From: Fred <76-fgerber@users.noreply.shinice.net> Date: Mon, 22 Jan 2024 22:44:35 +0100 Subject: [PATCH] Fix profile pic edit See https://github.com/pixelfed/pixelfed/issues/4250 --- .../app/profile/EditProfileActivity.kt | 28 +++++------ .../app/profile/EditProfileViewModel.kt | 49 +++++++++++++------ .../pixeldroid/app/utils/api/PixelfedAPI.kt | 2 +- 3 files changed, 49 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/org/pixeldroid/app/profile/EditProfileActivity.kt b/app/src/main/java/org/pixeldroid/app/profile/EditProfileActivity.kt index 239f3140..8bea0137 100644 --- a/app/src/main/java/org/pixeldroid/app/profile/EditProfileActivity.kt +++ b/app/src/main/java/org/pixeldroid/app/profile/EditProfileActivity.kt @@ -112,18 +112,18 @@ class EditProfileActivity : BaseActivity() { } } -// binding.changeImageButton.setOnClickListener { -// Intent(Intent.ACTION_GET_CONTENT).apply { -// type = "*/*" -// putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/*")) -// action = Intent.ACTION_GET_CONTENT -// addCategory(Intent.CATEGORY_OPENABLE) -// putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false) -// uploadImageResultContract.launch( -// Intent.createChooser(this, null) -// ) -// } -// } + binding.profilePic.setOnClickListener { + Intent(Intent.ACTION_GET_CONTENT).apply { + type = "*/*" + putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/*")) + action = Intent.ACTION_GET_CONTENT + addCategory(Intent.CATEGORY_OPENABLE) + putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false) + uploadImageResultContract.launch( + Intent.createChooser(this, null) + ) + } + } } private val uploadImageResultContract = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> @@ -137,10 +137,10 @@ class EditProfileActivity : BaseActivity() { val imageUri: String = clipData.getItemAt(i).uri.toString() images.add(imageUri) } - model.uploadImage(images.first()) + model.updateImage(images.first()) } else if (data.data != null) { images.add(data.data!!.toString()) - model.uploadImage(images.first()) + model.updateImage(images.first()) } } } diff --git a/app/src/main/java/org/pixeldroid/app/profile/EditProfileViewModel.kt b/app/src/main/java/org/pixeldroid/app/profile/EditProfileViewModel.kt index 33fe8388..a74fb285 100644 --- a/app/src/main/java/org/pixeldroid/app/profile/EditProfileViewModel.kt +++ b/app/src/main/java/org/pixeldroid/app/profile/EditProfileViewModel.kt @@ -97,12 +97,19 @@ class EditProfileViewModel(application: Application) : AndroidViewModel(applicat note = bio, locked = privateAccount, ) + val newAvatarUri = if (profilePictureChanged) { + uploadImage() + profilePictureUri + } else { + account.anyAvatar()?.toUri() + } oldProfile = account _uiState.update { currentUiState -> 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, - profilePictureUri = account.anyAvatar()?.toUri(), + profilePictureUri = newAvatarUri, privateAccount = account.locked, sendingProfile = false, profileSent = true, @@ -153,12 +160,13 @@ class EditProfileViewModel(application: Application) : AndroidViewModel(applicat fun madeChanges(): Boolean = with(uiState.value) { - val bioUnchanged: Boolean = oldProfile?.source?.note?.let { it != bio } - // If source note is null, check note + val privateChanged = oldProfile?.locked != privateAccount + 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 } ?: true - oldProfile?.locked != privateAccount || oldProfile?.display_name != name - || bioUnchanged + profilePictureChanged || privateChanged || displayNameChanged || bioChanged } fun clickedCard() { @@ -178,16 +186,26 @@ class EditProfileViewModel(application: Application) : AndroidViewModel(applicat } } - fun uploadImage(image: String) { - //TODO fix + fun updateImage(image: String) { + _uiState.update { currentUiState -> + currentUiState.copy( + profilePictureUri = image.toUri(), + profilePictureChanged = true + ) + } + } + + private fun uploadImage() { + val image = uiState.value.profilePictureUri!! + val inputStream = - getApplication().contentResolver.openInputStream(image.toUri()) + getApplication().contentResolver.openInputStream(image) ?: return val size: Long = - if (image.toUri().scheme == "content") { + if (image.scheme == "content") { getApplication().contentResolver.query( - image.toUri(), + image, null, null, null, @@ -203,7 +221,7 @@ class EditProfileViewModel(application: Application) : AndroidViewModel(applicat cursor.getLong(sizeIndex) } ?: 0 } else { - image.toUri().toFile().length() + image.toFile().length() } val imagePart = ProgressRequestBody(inputStream, size, "image/*") @@ -232,8 +250,7 @@ class EditProfileViewModel(application: Application) : AndroidViewModel(applicat .observeOn(AndroidSchedulers.mainThread()) .subscribe( { it: Account -> - Log.e("qsdfqsdfs", it.toString()) - + Log.i("ACCOUNT", it.toString()) }, { e: Throwable -> _uiState.update { currentUiState -> @@ -250,6 +267,7 @@ class EditProfileViewModel(application: Application) : AndroidViewModel(applicat { _uiState.update { currentUiState -> currentUiState.copy( + profilePictureChanged = false, uploadProgress = 100, uploadingPicture = false ) @@ -265,7 +283,8 @@ class EditProfileViewModel(application: Application) : AndroidViewModel(applicat data class EditProfileActivityUiState( val name: String? = null, val bio: String? = null, - val profilePictureUri: Uri?= null, + val profilePictureUri: Uri? = null, + val profilePictureChanged: Boolean = false, val privateAccount: Boolean? = null, val loadingProfile: Boolean = true, val profileLoaded: Boolean = false, diff --git a/app/src/main/java/org/pixeldroid/app/utils/api/PixelfedAPI.kt b/app/src/main/java/org/pixeldroid/app/utils/api/PixelfedAPI.kt index e9377dab..6ed43155 100644 --- a/app/src/main/java/org/pixeldroid/app/utils/api/PixelfedAPI.kt +++ b/app/src/main/java/org/pixeldroid/app/utils/api/PixelfedAPI.kt @@ -348,7 +348,7 @@ interface PixelfedAPI { ): Account @Multipart - @PATCH("/api/v1/accounts/update_credentials") + @POST("/api/v1/accounts/update_credentials") fun updateProfilePicture( @Part avatar: MultipartBody.Part? ): Observable