Don't re-compress avatars before storing in DB

This commit is contained in:
Matéo Duparc 2021-08-17 19:17:41 +02:00
parent 8354490940
commit 928f97dc97
Signed by: hardcoresushi
GPG Key ID: 007F84120107191E
4 changed files with 24 additions and 48 deletions

View File

@ -46,15 +46,15 @@ android {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.5.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation "androidx.fragment:fragment-ktx:1.3.4"
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation "androidx.fragment:fragment-ktx:1.3.6"
implementation "androidx.preference:preference-ktx:1.1.1"
implementation 'com.google.android.material:material:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'net.i2p.crypto:eddsa:0.3.0'
implementation "org.whispersystems:curve25519-android:0.5.0"
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'

View File

@ -9,6 +9,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import sushi.hardcore.aira.databinding.FragmentCreateIdentityBinding
import sushi.hardcore.aira.utils.AvatarPicker
@ -25,11 +26,9 @@ class CreateIdentityFragment(private val activity: AppCompatActivity) : Fragment
}
}
private val avatarPicker = AvatarPicker(activity) { picker, avatar ->
picker.setOnAvatarCompressed { compressedAvatar ->
AIRADatabase.setIdentityAvatar(Constants.getDatabaseFolder(activity), compressedAvatar)
}
avatar.circleCrop().into(binding.avatar)
private val avatarPicker = AvatarPicker(activity) { avatar ->
AIRADatabase.setIdentityAvatar(Constants.getDatabaseFolder(activity), avatar)
Glide.with(this).load(avatar).circleCrop().into(binding.avatar)
}
private lateinit var binding: FragmentCreateIdentityBinding

View File

@ -29,11 +29,9 @@ class SettingsActivity: AppCompatActivity() {
class MySettingsFragment(private val activity: AppCompatActivity): PreferenceFragmentCompat() {
private lateinit var databaseFolder: String
private lateinit var airaService: AIRAService
private val avatarPicker = AvatarPicker(activity) { picker, avatar ->
private val avatarPicker = AvatarPicker(activity) { avatar ->
if (::airaService.isInitialized) {
picker.setOnAvatarCompressed { compressedAvatar ->
airaService.changeAvatar(compressedAvatar)
}
airaService.changeAvatar(avatar)
}
displayAvatar(avatar)
}
@ -174,19 +172,20 @@ class SettingsActivity: AppCompatActivity() {
if (avatar == null) {
identityAvatarPreference.setIcon(R.drawable.ic_face)
} else {
displayAvatar(Glide.with(this).load(avatar))
Glide
.with(this)
.load(avatar)
.apply(RequestOptions().override(90)) //reduce image to be the same size as other icons
.circleCrop()
.into(object : CustomTarget<Drawable>() {
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
identityAvatarPreference.icon = resource
}
override fun onLoadCleared(placeholder: Drawable?) {}
})
}
}
private fun displayAvatar(glideBuilder: RequestBuilder<Drawable>) {
glideBuilder.apply(RequestOptions().override(90)).circleCrop().into(object : CustomTarget<Drawable>() {
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
identityAvatarPreference.icon = resource
}
override fun onLoadCleared(placeholder: Drawable?) {}
})
}
private fun changePassword(isIdentityProtected: Boolean, oldPasswordEditText: EditText, newPassword: ByteArray?) {
val oldPassword = if (isIdentityProtected) {
oldPasswordEditText.text.toString().toByteArray()

View File

@ -1,26 +1,17 @@
package sushi.hardcore.aira.utils
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.drawable.toBitmap
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import sushi.hardcore.aira.Constants
import sushi.hardcore.aira.R
import java.io.ByteArrayOutputStream
class AvatarPicker(
private val activity: AppCompatActivity,
private val onAvatarPicked: (AvatarPicker, RequestBuilder<Drawable>) -> Unit,
private val onAvatarPicked: (ByteArray) -> Unit,
) {
private lateinit var picker: ActivityResultLauncher<String>
private lateinit var avatar: RequestBuilder<Drawable>
fun register() {
picker = activity.registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
if (uri != null) {
@ -30,26 +21,13 @@ class AvatarPicker(
if (image.size > Constants.MAX_AVATAR_SIZE) {
Toast.makeText(activity, R.string.avatar_too_large, Toast.LENGTH_SHORT).show()
} else {
avatar = Glide.with(activity).load(image).centerCrop()
onAvatarPicked(this, avatar)
onAvatarPicked(image)
}
}
}
}
}
fun setOnAvatarCompressed(onCompressed: (ByteArray) -> Unit) {
avatar.into(object: CustomTarget<Drawable>() {
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
val avatar = ByteArrayOutputStream()
if (resource.toBitmap().compress(Bitmap.CompressFormat.PNG, 100, avatar)) {
onCompressed(avatar.toByteArray())
}
}
override fun onLoadCleared(placeholder: Drawable?) {}
})
}
fun launch() {
picker.launch("image/*")
}