From 4e8ba912f34a98251a95fada3ad7842b22a2661b Mon Sep 17 00:00:00 2001 From: hettysymes Date: Thu, 15 Jun 2023 22:28:19 +0100 Subject: [PATCH] pfp shown for matches from storage --- app/src/main/java/com/example/drp25/Database.kt | 16 ++++++++++++++++ .../main/java/com/example/drp25/MatchActivity.kt | 8 +++----- .../com/example/drp25/UserProfileActivity.kt | 16 +--------------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/example/drp25/Database.kt b/app/src/main/java/com/example/drp25/Database.kt index b8d7c49..f77700c 100644 --- a/app/src/main/java/com/example/drp25/Database.kt +++ b/app/src/main/java/com/example/drp25/Database.kt @@ -1,6 +1,8 @@ package com.example.drp25 +import android.graphics.BitmapFactory import android.net.Uri +import android.widget.ImageView import com.example.drp25.matchers.MyMatcher import com.google.firebase.database.DataSnapshot import com.google.firebase.database.DatabaseError @@ -18,6 +20,20 @@ val matchObservers = mutableListOf() val storage = Firebase.storage val imageRef = storage.reference.child("images") +fun displayPfp(uniId: String, userId: String, imageView: ImageView) { + val pfpRef = imageRef.child("pfp_${uniId}_${userId}.png") + + pfpRef.getBytes(Long.MAX_VALUE).addOnSuccessListener {imageData -> + // Use the bytes to display the image + // Convert the image data to a Bitmap + val bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.size) + // Set the Bitmap to your ImageView + imageView.setImageBitmap(bitmap) + }.addOnFailureListener { + // Handle any errors + } +} + fun sendStamp(uniId: String, userId: String, stampName: String) { val stampsRef = unisRef.child(uniId).child("users").child(userId).child("stamps") stampsRef.push().setValue(stampName) diff --git a/app/src/main/java/com/example/drp25/MatchActivity.kt b/app/src/main/java/com/example/drp25/MatchActivity.kt index 9f746e0..c3f8b8e 100644 --- a/app/src/main/java/com/example/drp25/MatchActivity.kt +++ b/app/src/main/java/com/example/drp25/MatchActivity.kt @@ -54,11 +54,9 @@ class MatchActivity : AppCompatActivity() { val course = snapshot.child("course").value val year = snapshot.child("year").value - val pfpUri = snapshot.child("pfp").value - if (pfpUri != null) { - Glide.with(this@MatchActivity) - .load(pfpUri as String) - .into(pfpImage) + val hasPfp = snapshot.child("pfp").value as Boolean + if (hasPfp) { + displayPfp(UNI_ID, matchId, pfpImage) } else { pfpImage.setImageResource(R.drawable.default_profile) } diff --git a/app/src/main/java/com/example/drp25/UserProfileActivity.kt b/app/src/main/java/com/example/drp25/UserProfileActivity.kt index 8cbaa36..8ebb6ba 100644 --- a/app/src/main/java/com/example/drp25/UserProfileActivity.kt +++ b/app/src/main/java/com/example/drp25/UserProfileActivity.kt @@ -65,7 +65,7 @@ class UserProfileActivity : AppCompatActivity() { val year = snapshot.child("year").value val hasPfp = snapshot.child("pfp").value as Boolean if (hasPfp) { - displayPfp() + displayPfp(UNI_ID, USER_ID, binding.profileImageView) } else { binding.profileImageView.setImageResource(R.drawable.default_profile) } @@ -155,18 +155,4 @@ class UserProfileActivity : AppCompatActivity() { } } - private fun displayPfp() { - val pfpRef = imageRef.child("pfp_${UNI_ID}_${USER_ID}.png") - - pfpRef.getBytes(Long.MAX_VALUE).addOnSuccessListener {imageData -> - // Use the bytes to display the image - // Convert the image data to a Bitmap - val bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.size) - // Set the Bitmap to your ImageView - binding.profileImageView.setImageBitmap(bitmap) - }.addOnFailureListener { - // Handle any errors - } - } - } \ No newline at end of file