diff --git a/app/build.gradle b/app/build.gradle index c2d69aa..337dfaf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -43,6 +43,8 @@ configurations { } dependencies { + implementation 'com.firebaseui:firebase-ui-storage:7.2.0' + implementation 'com.google.firebase:firebase-storage-ktx' implementation 'com.github.bumptech.glide:glide:4.12.0' implementation platform('com.google.firebase:firebase-bom:32.1.0') implementation 'com.google.code.gson:gson:2.8.8' diff --git a/app/src/main/java/com/example/drp25/Database.kt b/app/src/main/java/com/example/drp25/Database.kt index e40bf35..f77700c 100644 --- a/app/src/main/java/com/example/drp25/Database.kt +++ b/app/src/main/java/com/example/drp25/Database.kt @@ -1,27 +1,58 @@ 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 import com.google.firebase.database.FirebaseDatabase import com.google.firebase.database.ValueEventListener +import com.google.firebase.ktx.Firebase +import com.google.firebase.storage.ktx.storage +import java.io.File private val unisRef = FirebaseDatabase.getInstance().reference.child("universities") private val matcher: Matcher = MyMatcher() val matches = mutableSetOf() 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) } -fun updatePfp(uniId: String, userId: String, imgUri: String) { - unisRef.child(uniId).child("users").child(userId).child("pfp").setValue(imgUri) +fun updatePfp(uniId: String, userId: String, imgUri: Uri) { + val pfpRef = imageRef.child("pfp_${uniId}_${userId}.png") + val uploadTask = pfpRef.putFile(imgUri) + + // Register observers to listen for when the download is done or if it fails + uploadTask.addOnFailureListener { + // Handle unsuccessful uploads + }.addOnSuccessListener { + unisRef.child(uniId).child("users").child(userId).child("pfp").setValue(true) + } } fun deletePfp(uniId: String, userId: String) { - unisRef.child(uniId).child("users").child(userId).child("pfp").removeValue() + unisRef.child(uniId).child("users").child(userId).child("pfp").setValue(false) } fun addMatchObserver(observer: Observer) { @@ -108,6 +139,7 @@ fun addUser(uniId: String, name: String, nationality: String, year: String, cour userRef.child("nationality").setValue(nationality) userRef.child("year").setValue(year) userRef.child("course").setValue(course) + userRef.child("pfp").setValue(false) } return userId } 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 13d8b57..f5fb5aa 100644 --- a/app/src/main/java/com/example/drp25/UserProfileActivity.kt +++ b/app/src/main/java/com/example/drp25/UserProfileActivity.kt @@ -20,6 +20,7 @@ import com.google.firebase.database.DataSnapshot import com.google.firebase.database.DatabaseError import com.google.firebase.database.FirebaseDatabase import com.google.firebase.database.ValueEventListener +import com.google.firebase.storage.FirebaseStorage import io.getstream.chat.android.client.models.UserId import java.io.File import java.io.FileOutputStream @@ -31,7 +32,6 @@ class UserProfileActivity : AppCompatActivity() { private lateinit var binding: ActivityUserProfileBinding private lateinit var stampLayout: LinearLayout private lateinit var noStampsTextView: TextView - private lateinit var imageFile: File companion object { private const val PICK_IMAGE_REQUEST_CODE = 123 @@ -62,11 +62,9 @@ class UserProfileActivity : AppCompatActivity() { val name: String = snapshot.child("name").value as String val course = snapshot.child("course").value val year = snapshot.child("year").value - val pfpUri = snapshot.child("pfp").value - if (pfpUri != null) { - Glide.with(this@UserProfileActivity) - .load(pfpUri as String) - .into(binding.profileImageView) + val hasPfp = snapshot.child("pfp").value as Boolean + if (hasPfp) { + displayPfp(UNI_ID, USER_ID, binding.profileImageView) } else { binding.profileImageView.setImageResource(R.drawable.default_profile) } @@ -150,7 +148,9 @@ class UserProfileActivity : AppCompatActivity() { super.onActivityResult(requestCode, resultCode, data) if (requestCode == PICK_IMAGE_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) { val selectedImageUri: Uri? = data.data - updatePfp(UNI_ID, USER_ID, selectedImageUri.toString()) + if (selectedImageUri != null) { + updatePfp(UNI_ID, USER_ID, selectedImageUri) + } } }