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

Upgrade of Profile_Activity Layout + image in search user activity #301

Merged
merged 15 commits into from
May 16, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package ch.sdp.vibester.activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.core.widget.ImageViewCompat
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.scrollTo
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.matcher.IntentMatchers
Expand All @@ -24,6 +26,7 @@ import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import io.mockk.every
import io.mockk.mockk
import net.glxn.qrgen.core.scheme.Url
import org.hamcrest.CoreMatchers.not
import org.junit.After
import org.junit.Before
Expand Down Expand Up @@ -154,7 +157,7 @@ class ProfileActivityTest {
createMockImageGetter()

val scn: ActivityScenario<ProfileActivity> = ActivityScenario.launch(intent)
onView(withId(R.id.profile_returnToMain)).perform(click())
onView(withId(R.id.profile_returnToMain)).perform(scrollTo(), click())
Intents.intended(IntentMatchers.hasComponent(MainActivity::class.java.name))
}
// FIXME failing test
Expand Down Expand Up @@ -237,8 +240,8 @@ class ProfileActivityTest {
createMockImageGetter()

val scn: ActivityScenario<ProfileActivity> = ActivityScenario.launch(intent)
onView(withId(R.id.editUser)).perform(click())
onView(withText("Cancel")).perform(click())
onView(withId(R.id.editUser)).perform(scrollTo(), click())
onView(withText("Cancel")).perform(scrollTo(), click())

onView(withId(R.id.username)).check(matches(withText("Lalisa Bon")))
}
Expand Down Expand Up @@ -276,9 +279,32 @@ class ProfileActivityTest {
createMockImageGetter()

val scn: ActivityScenario<ProfileActivity> = ActivityScenario.launch(intent)
onView(withId(R.id.showQRCode)).perform(click())

onView(withId(R.id.showQRCode)).perform(click())
onView(withId(R.id.qrCode)).check(matches(isDisplayed()))

onView(withId(R.id.qrCode_returnToProfile)).perform(click())
onView(withId(R.id.myCardView)).check(matches(isDisplayed()))

onView(withId(R.id.logout)).perform(scrollTo(), click())
onView(withId(R.id.fragment)).check(matches(isDisplayed()))
}

@Test
fun checkChangeImage() {
val inputProfile = User( "Lalisa Bon", R.string.test_profile_image.toString(), "[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)

onView(withId(R.id.myCardView)).perform(click())
onView(withText("NO")).perform(click())
onView(withId(R.id.myCardView)).check(matches(isDisplayed()))
}
// FIXME failing test

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ch.sdp.vibester.profile
import androidx.recyclerview.widget.RecyclerView
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
import org.hamcrest.CoreMatchers
Expand All @@ -17,7 +18,7 @@ class UserProfileAdapterTest {
val user2 = User("test2", "https://images.app.goo.gl/yiPpy7JDRFaZRiAg9", "[email protected]")
val users: MutableList<User> = arrayListOf()
users.addAll(listOf(user1, user2))
val userProfileViewHolder: RecyclerView.Adapter<UserProfileAdapter.UserProfileViewHolder> = UserProfileAdapter(users, FireBaseAuthenticator(), DataGetter())
val userProfileViewHolder: RecyclerView.Adapter<UserProfileAdapter.UserProfileViewHolder> = UserProfileAdapter(users, FireBaseAuthenticator(), DataGetter(), ImageGetter())
MatcherAssert.assertThat(userProfileViewHolder.itemCount, CoreMatchers.equalTo(2))
}

Expand All @@ -27,7 +28,7 @@ class UserProfileAdapterTest {
val user2 = User("test2", "https://images.app.goo.gl/yiPpy7JDRFaZRiAg9", "[email protected]")
val users: MutableList<User> = arrayListOf()
users.addAll(listOf(user1, user2))
val userProfileViewHolder: RecyclerView.Adapter<UserProfileAdapter.UserProfileViewHolder> = UserProfileAdapter(users, FireBaseAuthenticator(), DataGetter())
val userProfileViewHolder: RecyclerView.Adapter<UserProfileAdapter.UserProfileViewHolder> = UserProfileAdapter(users, FireBaseAuthenticator(), DataGetter(), ImageGetter())
val defaultType = 0
MatcherAssert.assertThat(
userProfileViewHolder.getItemViewType(0),
Expand Down
12 changes: 9 additions & 3 deletions app/src/main/java/ch/sdp/vibester/activity/ProfileActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.view.Window
import android.widget.*
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView
import androidx.constraintlayout.widget.ConstraintLayout
import ch.sdp.vibester.R
import ch.sdp.vibester.api.BitmapGetterApi
Expand Down Expand Up @@ -89,6 +90,9 @@ class ProfileActivity : AppCompatActivity() {
findViewById<ImageView>(R.id.avatar).setOnClickListener {
showGeneralDialog( "Do you want to change your profile picture?", false)
}
findViewById<CardView>(R.id.myCardView).setOnClickListener {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

showGeneralDialog( "Do you want to change your profile picture?", false)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we test this?

}
}
/**
* A function that updates the image in the database.
Expand Down Expand Up @@ -149,7 +153,8 @@ class ProfileActivity : AppCompatActivity() {
private fun setShowQrCodeBtnListener() {
findViewById<ImageView>(R.id.showQRCode).setOnClickListener {
setLayoutVisibility(findViewById<ConstraintLayout>(R.id.QrCodePage), true)
setLayoutVisibility(findViewById<RelativeLayout>(R.id.profileContent), false)
setLayoutVisibility(findViewById<ScrollView>(R.id.profileContent), false)
setLayoutVisibility(findViewById<FloatingActionButton>(R.id.profile_returnToMain), false)
}
}

Expand All @@ -159,7 +164,8 @@ class ProfileActivity : AppCompatActivity() {
private fun setQrCodeToProfileBtnListener() {
findViewById<FloatingActionButton>(R.id.qrCode_returnToProfile).setOnClickListener {
setLayoutVisibility(findViewById<ConstraintLayout>(R.id.QrCodePage), false)
setLayoutVisibility(findViewById<RelativeLayout>(R.id.profileContent), true)
setLayoutVisibility(findViewById<ScrollView>(R.id.profileContent), true)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we test this?

setLayoutVisibility(findViewById<FloatingActionButton>(R.id.profile_returnToMain), true)
}
}

Expand Down Expand Up @@ -271,7 +277,7 @@ class ProfileActivity : AppCompatActivity() {
* A function that downloads an image and sets it.
* @param imageURI URI of the image
*/
private fun setImage(imageURI: Uri) {
fun setImage(imageURI: Uri) {
CoroutineScope(Dispatchers.Main).launch {
val task = async(Dispatchers.IO) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Identical blocks of code found in 2 locations. Consider refactoring.

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class SearchUserActivity : AppCompatActivity() {
recyclerView!!.setHasFixedSize(true)
recyclerView!!.layoutManager = LinearLayoutManager(this)

userProfileAdapter = UserProfileAdapter(this.users, authenticator, usersRepo)
userProfileAdapter = UserProfileAdapter(this.users, authenticator, usersRepo, imageGetter)

recyclerView!!.adapter = userProfileAdapter

Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/ch/sdp/vibester/database/ImageGetter.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.sdp.vibester.database

import android.net.Uri
import android.view.View
import com.google.android.gms.tasks.OnSuccessListener
import javax.inject.Inject

Expand Down
28 changes: 27 additions & 1 deletion app/src/main/java/ch/sdp/vibester/user/UserProfileAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import java.util.concurrent.TimeUnit
import javax.inject.Inject


/**
Expand All @@ -29,11 +30,13 @@ class UserProfileAdapter constructor(
val users: MutableList<User>,
private val authenticator: FireBaseAuthenticator,
val dataGetter: DataGetter,
val imageGetter: ImageGetter
):
RecyclerView.Adapter<UserProfileAdapter.UserProfileViewHolder>() {

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

init{
if (currentUser != null) { dataGetter.getUserData(currentUser.uid, this::setFriends) }
Expand Down Expand Up @@ -84,7 +87,9 @@ class UserProfileAdapter constructor(
*/
fun bind(user: User) {
itemView.findViewById<TextView>(R.id.search_user_username).text = user.username
itemView.findViewById<ImageView>(R.id.profile_image).loadImg(user.image)

imageGetter.fetchImage("profileImg/${user.uid}", this::setImage)
//itemView.findViewById<ImageView>(R.id.profile_image).loadImg(user.image)
val addFriendBtn = itemView.findViewById<Button>(R.id.addFriendBtn)

if(userFriends.isNotEmpty() && user.uid in userFriends){
Expand All @@ -100,6 +105,27 @@ class UserProfileAdapter constructor(
}
}


private fun setImage(imageURI: Uri) {
CoroutineScope(Dispatchers.Main).launch {
val task = async(Dispatchers.IO) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Identical blocks of code found in 2 locations. Consider refactoring.

try {
val bit = BitmapGetterApi.download(imageURI.toString())
bit.get(10, TimeUnit.SECONDS)
} catch (e: Exception){
null
}
}
val bm = task.await()

if (bm != null) {
val avatar = itemView.findViewById<ImageView>(R.id.profile_image)
avatar.setImageBitmap(Bitmap.createScaledBitmap(bm, imageSize, imageSize, false))
}
}
}


private fun changeBtnToImage(){
itemView.findViewById<Button>(R.id.addFriendBtn).visibility = View.INVISIBLE
itemView.findViewById<ImageView>(R.id.addedFriendIcon).visibility = View.VISIBLE
Expand Down
Loading