Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into maximezmt/testcoverage
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximeZmt authored May 12, 2022
2 parents 12d0d3a + e02e591 commit 8d06095
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ class ProfileActivityTest {
every { mockUsersRepo.setFieldValue(any(), any(), any()) } answers {}
}

val mockImageURI = Uri.parse("https://raw.githubusercontent.com/Ashwinvalento/cartoon-avatar/master/lib/images/male/45.png")
// val mockImageURI = Uri.parse("https://raw.githubusercontent.com/Ashwinvalento/cartoon-avatar/master/lib/images/male/45.png")
private fun createMockImageGetter() {
every {mockImageGetter.fetchImage(any(), any())} answers {}
every {mockImageGetter.fetchImage(any(), any())} answers {
// secondArg<(Uri) -> Unit>().invoke(mockImageURI)
}
}


Expand Down Expand Up @@ -231,6 +233,28 @@ class ProfileActivityTest {
onView(withId(R.id.username)).check(matches(withText("Lalisa Bon")))
}

// @Test
// fun checkChangePhotoCancel() {
// val inputProfile = User( "Lalisa Bon","bit.ly/3IUnyAF", "[email protected]", 12, 8, "VvPB47tQCLdjz3YebilS6h5EXdJ3")
// val ctx = ApplicationProvider.getApplicationContext() as Context
// val intent = Intent(ctx, ProfileActivity::class.java)
//
// createMockDataGetter(inputProfile)
// createMockAuthenticator()
// createMockImageGetter()
//
// val scn: ActivityScenario<ProfileActivity> = ActivityScenario.launch(intent)
//
// //TODO: potentially refactor this test to not need Thread.sleep
// //This thread sleep is added for the mock image to load, might be a better way to test it but for now I'll leave it like that
// Thread.sleep(3000)
//
// onView(withId(R.id.avatar)).perform(click())
// onView(withText("No")).perform(click())
//
// onView(withId(R.id.avatar)).check(matches(isDisplayed()))
// }

@Test
fun checkQrCodeGenerator() {
val inputProfile = User( "Lalisa Bon", R.string.test_profile_image.toString(), "[email protected]", 12, 8,"VvPB47tQCLdjz3YebilS6h5EXdJ3")
Expand Down Expand Up @@ -263,4 +287,3 @@ class ProfileActivityTest {
// }

}

93 changes: 79 additions & 14 deletions app/src/main/java/ch/sdp/vibester/activity/ProfileActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ch.sdp.vibester.activity

import android.app.Activity
import android.content.Intent
import android.graphics.*
import android.net.Uri
import android.os.Bundle
Expand Down Expand Up @@ -49,7 +51,8 @@ class ProfileActivity : AppCompatActivity() {
@Inject
lateinit var imageGetter: ImageGetter

val imageSize = 1000
private val imageSize = 1000
private val imageRequestCode = 100

/**
* Generic onCreate method belonging to ProfileActivity.
Expand All @@ -65,6 +68,7 @@ class ProfileActivity : AppCompatActivity() {
setRetToMainBtnListener()
setShowQrCodeBtnListener()
setQrCodeToProfileBtnListener()
setChangeImageBtnListener()
setScoreBtnListener()

queryDatabase()
Expand All @@ -75,7 +79,46 @@ class ProfileActivity : AppCompatActivity() {
*/
private fun setEditUserNameBtnListener() {
findViewById<ImageView>(R.id.editUser).setOnClickListener {
showGeneralDialog(R.id.username, "username")
showGeneralDialog( "username", true)
}
}
/**
* Generic listener for the change profile picture.
*/
private fun setChangeImageBtnListener() {
findViewById<ImageView>(R.id.avatar).setOnClickListener {
showGeneralDialog( "Do you want to change your profile picture?", false)
}
}
/**
* A function that updates the image in the database.
* @param id ID of the image int the database
*/

private fun updateImage(id: String) {
deleteImage(id)

val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
startActivityForResult(intent, imageRequestCode)
}


/**
* A function that deletes an image from the database.
* @param id ID of the image int the database
*/
private fun deleteImage(id: String) {
imageGetter.deleteImage("profileImg/${id}")
}

//check the UID here not sure
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && requestCode == imageRequestCode) {
imageGetter.uploadFile("profileImg/${dataGetter.getCurrentUser()?.uid}", data?.data!!) {
imageGetter.fetchImage("profileImg/${dataGetter.getCurrentUser()?.uid}", this::setImage)
}
}
}

Expand Down Expand Up @@ -156,7 +199,7 @@ class ProfileActivity : AppCompatActivity() {
* @param textId id of the text in the dialog
* @param name of the dialog
*/
private fun showDialog(title: String, hint: String, id: Int, textId: Int, name: String) {
private fun showTextDialog(title: String, hint: String, id: Int, textId: Int, name: String) {
val builder: AlertDialog.Builder = AlertDialog.Builder(this)
builder.setTitle(title)

Expand All @@ -169,25 +212,47 @@ class ProfileActivity : AppCompatActivity() {
builder.setPositiveButton("OK") { _, _ ->
findViewById<TextView>(textId).text = input.text.toString()

if(name == "username"){
if (name == "username"){
dataGetter.setFieldValue(FireBaseAuthenticator().getCurrUID(), "username", input.text.toString())
}
}

builder.setNegativeButton("Cancel") { dialog, _ -> dialog.cancel() }
builder.show()
}

/**
* A function that displays the dialog.
* @param id: The id of the user to be shown.
* @param name: The name of the user to be shown.
* A function shows an image change dialog.
* @param title title of the dialog
*/
private fun showGeneralDialog(id: Int, name: String) {
val title = "Create $name"
val hint = "Enter new $name"

showDialog(title, hint, 0, id, name)
private fun showImageChangeDialog(title: String) {
val builder: AlertDialog.Builder = AlertDialog.Builder(this)
builder.setTitle(title)

builder.setPositiveButton("Yes") { _, _ ->
dataGetter.getCurrentUser()?.let { updateImage(it.uid) }
}

builder.setNegativeButton("No") { dialog, _ -> dialog.cancel() }
builder.show()
}

/**
* A function shows a dialog.
* @param name name of the dialog
* @param textDialog boolean to check the type of dialog
*/

private fun showGeneralDialog(name: String, textDialog: Boolean) {
if (textDialog) {
val title = "Create $name"
val hint = "Enter new $name"

showTextDialog(title, hint, 0, R.id.username, name)
}
else {
showImageChangeDialog(name)
}
}

/**
Expand Down Expand Up @@ -218,7 +283,7 @@ class ProfileActivity : AppCompatActivity() {
}
val bm = task.await()

if(bm != null){
if (bm != null) {
val avatar = findViewById<ImageView>(R.id.avatar)
avatar.setImageBitmap(Bitmap.createScaledBitmap(bm, imageSize,imageSize, false))
}
Expand Down Expand Up @@ -255,7 +320,7 @@ class ProfileActivity : AppCompatActivity() {
*/
private fun setupProfile(user: User){
// Currently assuming that empty username means no user !
if (user.username != ""){
if (user.username != "") {
findViewById<TextView>(R.id.username).text = user.username
setTextOfMultipleViews(user)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.recyclerview.widget.RecyclerView
import ch.sdp.vibester.R
import ch.sdp.vibester.auth.FireBaseAuthenticator
import ch.sdp.vibester.database.DataGetter
import ch.sdp.vibester.database.ImageGetter
import ch.sdp.vibester.user.User

import ch.sdp.vibester.user.UserProfileAdapter
Expand All @@ -31,6 +32,9 @@ class SearchUserActivity : AppCompatActivity() {
@Inject
lateinit var usersRepo: DataGetter

@Inject
lateinit var imageGetter: ImageGetter

@Inject
lateinit var authenticator: FireBaseAuthenticator

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/ch/sdp/vibester/database/ImageGetter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ class ImageGetter @Inject constructor() {
OnSuccessListener(callback))
}

fun deleteImage(imageID: String) {
storageRef.child(imageID).delete().addOnSuccessListener {}
}

}
20 changes: 17 additions & 3 deletions app/src/main/java/ch/sdp/vibester/user/UserProfileAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ch.sdp.vibester.user

import android.graphics.Bitmap
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -8,22 +10,33 @@ import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import ch.sdp.vibester.R
import ch.sdp.vibester.api.BitmapGetterApi
import ch.sdp.vibester.auth.FireBaseAuthenticator
import ch.sdp.vibester.database.DataGetter
import ch.sdp.vibester.database.ImageGetter
import ch.sdp.vibester.helper.loadImg
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import java.util.concurrent.TimeUnit


/**
* UserAdapter to set userProfile views with username and image in RecycleView. It is used to search for users.
*/
class UserProfileAdapter constructor(val users: MutableList<User>, val authenticator: FireBaseAuthenticator, val usersRepo: DataGetter):
class UserProfileAdapter constructor(
val users: MutableList<User>,
private val authenticator: FireBaseAuthenticator,
val dataGetter: DataGetter,
):
RecyclerView.Adapter<UserProfileAdapter.UserProfileViewHolder>() {

private val currentUser = authenticator.getCurrUser()
private var userFriends: Array<String> = arrayOf()

init{
if (currentUser != null) { usersRepo.getUserData(currentUser.uid, this::setFriends) }
if (currentUser != null) { dataGetter.getUserData(currentUser.uid, this::setFriends) }
}

// Callback for getUserData
Expand Down Expand Up @@ -80,7 +93,7 @@ class UserProfileAdapter constructor(val users: MutableList<User>, val authentic
else {
addFriendBtn.setOnClickListener {
if (currentUser != null) {
usersRepo.setSubFieldValue(currentUser.uid, "friends", user.uid,true)
dataGetter.setSubFieldValue(currentUser.uid, "friends", user.uid,true)
changeBtnToImage()
}
}
Expand All @@ -91,6 +104,7 @@ class UserProfileAdapter constructor(val users: MutableList<User>, val authentic
itemView.findViewById<Button>(R.id.addFriendBtn).visibility = View.INVISIBLE
itemView.findViewById<ImageView>(R.id.addedFriendIcon).visibility = View.VISIBLE
}

}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.sdp.vibester.user

import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/layout/activity_profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
android:id="@+id/myCardView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_marginStart="20dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="30dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="20dp"
app:cardCornerRadius="50dp"
Expand All @@ -51,7 +52,7 @@
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:foregroundGravity="left"
tools:srcCompat="@tools:sample/avatars" />
tools:srcCompat="@android:color/black" />
</androidx.cardview.widget.CardView>

<TextView
Expand Down

0 comments on commit 8d06095

Please sign in to comment.