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 {
// 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())
}
}
}

View File

@ -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<PixelDroidApplication>().contentResolver.openInputStream(image.toUri())
getApplication<PixelDroidApplication>().contentResolver.openInputStream(image)
?: return
val size: Long =
if (image.toUri().scheme == "content") {
if (image.scheme == "content") {
getApplication<PixelDroidApplication>().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,

View File

@ -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<Account>