Skip to content

Commit

Permalink
Merge pull request #25 from mee-kell/globalPfp
Browse files Browse the repository at this point in the history
Global pfp
  • Loading branch information
hettysymes committed Jun 15, 2023
2 parents d15e065 + 24a15ba commit f151fa4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
38 changes: 35 additions & 3 deletions app/src/main/java/com/example/drp25/Database.kt
Original file line number Diff line number Diff line change
@@ -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<String>()
val matchObservers = mutableListOf<Observer>()

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) {
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 3 additions & 5 deletions app/src/main/java/com/example/drp25/MatchActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/java/com/example/drp25/UserProfileActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
}
}

Expand Down

0 comments on commit f151fa4

Please sign in to comment.