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

Added database querying #106

Merged
merged 4 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
implementation 'com.squareup.retrofit2:retrofit:2.0.0'
implementation 'com.squareup.retrofit2:converter-gson:2.0.0'
implementation 'com.google.firebase:firebase-database-ktx'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.json:json:20180813'
testImplementation 'org.robolectric:robolectric:4.6'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ class ProfileActivityTest {

@Test
fun checkProfileData() {
val inputProfile = UserProfile("user0", "username0","bit.ly/3IUnyAF", 5, 8, 34, 2)
val inputProfile = UserProfile("@lisa", "Lalisa Bon","bit.ly/3IUnyAF", 12, 8, 29, 0)
val intent = Intent(ApplicationProvider.getApplicationContext(), ProfileActivity::class.java)
intent.putExtra("userProfile", inputProfile)
val scn: ActivityScenario<ProfileActivity> = ActivityScenario.launch(intent)
Thread.sleep(3_000)
onView(withId(R.id.handle)).check(matches(withText(inputProfile.handle)))
onView(withId(R.id.username)).check(matches(withText(inputProfile.username)))
onView(withId(R.id.correctSongs)).check(matches(withText(inputProfile.correctSongs.toString())))
Expand All @@ -32,10 +33,11 @@ class ProfileActivityTest {

@Test
fun checkProfileLayout() {
val inputProfile = UserProfile("user0", "username0","bit.ly/3IUnyAF", 5, 8, 34, 2)
val inputProfile = UserProfile("@lisa", "Lalisa Bon","bit.ly/3IUnyAF", 12, 8, 29, 0)
val intent = Intent(ApplicationProvider.getApplicationContext(), ProfileActivity::class.java)
intent.putExtra("userProfile", inputProfile)
val scn: ActivityScenario<ProfileActivity> = ActivityScenario.launch(intent)
Thread.sleep(3_000)
onView(withId(R.id.profileStatistics)).check(matches(isDisplayed()))
onView(withId(R.id.handle)).check(matches(isDisplayed()))
onView(withId(R.id.username)).check(matches(isDisplayed()))
Expand All @@ -44,7 +46,7 @@ class ProfileActivityTest {

@Test
fun checkEditProfile() {
val inputProfile = UserProfile("user0", "username0","bit.ly/3IUnyAF", 5, 8, 34, 2)
val inputProfile = UserProfile("@lisa", "Lalisa Bon","bit.ly/3IUnyAF", 12, 8, 29, 0)
val intent = Intent(ApplicationProvider.getApplicationContext(), ProfileActivity::class.java)
intent.putExtra("userProfile", inputProfile)
val scn: ActivityScenario<ProfileActivity> = ActivityScenario.launch(intent)
Expand All @@ -59,23 +61,23 @@ class ProfileActivityTest {

@Test
fun checkEditProfileClickCancel() {
val inputProfile = UserProfile("user0", "username0","bit.ly/3IUnyAF", 5, 8, 34, 2)
val inputProfile = UserProfile("@lisa", "Lalisa Bon","bit.ly/3IUnyAF", 12, 8, 29, 0)
val intent = Intent(ApplicationProvider.getApplicationContext(), ProfileActivity::class.java)
intent.putExtra("userProfile", inputProfile)
val scn: ActivityScenario<ProfileActivity> = ActivityScenario.launch(intent)

Thread.sleep(3_000)
onView(withId(R.id.editUser)).perform(ViewActions.click())
onView(withText("Cancel")).perform(ViewActions.click())
onView(withId(R.id.username)).check(matches(withText("username0")))
onView(withId(R.id.username)).check(matches(withText("Lalisa Bon")))
}

@Test
fun checkEditHandle() {
val inputProfile = UserProfile("user0", "username0","bit.ly/3IUnyAF", 5, 8, 34, 2)
val inputProfile = UserProfile("@lisa", "Lalisa Bon","bit.ly/3IUnyAF", 12, 8, 29, 0)
val intent = Intent(ApplicationProvider.getApplicationContext(), ProfileActivity::class.java)
intent.putExtra("userProfile", inputProfile)
val scn: ActivityScenario<ProfileActivity> = ActivityScenario.launch(intent)

Thread.sleep(3_000)
val newUserHandle = "newHandle"
onView(withId(R.id.editHandle)).perform(ViewActions.click())
onView(withId(0)).perform(ViewActions.typeText(newUserHandle), ViewActions.closeSoftKeyboard())
Expand All @@ -86,14 +88,14 @@ class ProfileActivityTest {

@Test
fun checkEditHandleClickCancel() {
val inputProfile = UserProfile("user0", "username0","bit.ly/3IUnyAF", 5, 8, 34, 2)
val inputProfile = UserProfile("@lisa", "Lalisa Bon","bit.ly/3IUnyAF", 12, 8, 29, 0)
val intent = Intent(ApplicationProvider.getApplicationContext(), ProfileActivity::class.java)
intent.putExtra("userProfile", inputProfile)
val scn: ActivityScenario<ProfileActivity> = ActivityScenario.launch(intent)

Thread.sleep(3_000)
onView(withId(R.id.editHandle)).perform(ViewActions.click())
onView(withText("Cancel")).perform(ViewActions.click())
onView(withId(R.id.handle)).check(matches(withText("user0")))
onView(withId(R.id.handle)).check(matches(withText("@lisa")))
}

}
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
package ch.sdp.vibester.profile

import ch.sdp.vibester.profile.ProfileDataProvider
//import ch.sdp.vibester.profile.ProfileDataProvider
import ch.sdp.vibester.profile.UserProfile
import org.junit.Assert
import org.junit.Test


class ProfileDataProviderTest {

@Test
fun checkGetUserData(){
val userID = 0
val users: List<UserProfile> = listOf(
UserProfile("user0", "username0","https://bit.ly/3tO0k8W", 5, 8, 34),
UserProfile("user1", "username1", "https://bit.ly/3sZhbXm", 5, 8, 34)
)
val scoreboard: List<Int> = listOf(1,2,3,4,5,6)
val dataProvider = ProfileDataProvider(userID.toString(), users, scoreboard)
val finalUser = dataProvider.getUserProfileData()

// To change on recursive equality of objects
Assert.assertEquals(finalUser.handle, users[userID].handle)
Assert.assertEquals(finalUser.username, users[userID].username)
Assert.assertEquals(finalUser.image, users[userID].image)
Assert.assertEquals(finalUser.bestScore, users[userID].bestScore)
Assert.assertEquals(finalUser.totalGames, users[userID].totalGames)
Assert.assertEquals(finalUser.correctSongs, users[userID].correctSongs)
Assert.assertEquals(finalUser.ranking, scoreboard[userID])
}
// @Test
Tsathogguaa marked this conversation as resolved.
Show resolved Hide resolved
// fun checkGetUserData(){
// val userID = 0
// val users: List<UserProfile> = listOf(
// UserProfile("user0", "username0","https://bit.ly/3tO0k8W", 5, 8, 34),
// UserProfile("user1", "username1", "https://bit.ly/3sZhbXm", 5, 8, 34)
// )
// val scoreboard: List<Int> = listOf(1,2,3,4,5,6)
// val dataProvider = ProfileDataProvider(userID.toString(), users, scoreboard)
// val finalUser = dataProvider.getUserProfileData()
//
// // To change on recursive equality of objects
// Assert.assertEquals(finalUser.handle, users[userID].handle)
// Assert.assertEquals(finalUser.username, users[userID].username)
// Assert.assertEquals(finalUser.image, users[userID].image)
// Assert.assertEquals(finalUser.bestScore, users[userID].bestScore)
// Assert.assertEquals(finalUser.totalGames, users[userID].totalGames)
// Assert.assertEquals(finalUser.correctSongs, users[userID].correctSongs)
// Assert.assertEquals(finalUser.ranking, scoreboard[userID])
// }



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,24 @@ class AuthenticationActivity : AppCompatActivity() {
reload()
}
}

/**
* A function to updates the UI based on google sign in result
* @param requestCode a request code
* @param resultCode a result code
* @param data intent returned from google sign in
*/
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
updateUI(authenticator.googleActivityResult(requestCode, resultCode, data));
}

/**
* A function validates email and password
* @param email email
* @param password passwprd
* @return validity of email and password
*/
private fun stringValidation(username: String, password: String): Boolean{
if(username.isEmpty() || password.isEmpty()) {
email.text = "Empty email or password"
Expand Down Expand Up @@ -102,29 +115,18 @@ class AuthenticationActivity : AppCompatActivity() {
}
}

/**
* A function that launches google sing in activity
*/
private fun signInGoogle() {
val intent = googleSignInClient.signInIntent
startActivityForResult(intent, 1000)
}
/*
private fun createAccount(email: String, password: String) {
authenticator.createAccount(email, password)
.addOnCompleteListener(this) { task ->
onCompleteAuthentication(task)
}
}

private fun signIn(email: String, password: String) {
authenticator.signIn(email, password)
.addOnCompleteListener(this) { task ->
onCompleteAuthentication(task)
}
}

*/



/**
* A function changes the UI based on the authentication result
* @param Authentication result
*/
private fun onCompleteAuthentication(task: Task<AuthResult>) {
if (task.isSuccessful) {
Toast.makeText(
Expand Down
62 changes: 58 additions & 4 deletions app/src/main/java/ch/sdp/vibester/activity/ProfileActivity.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
package ch.sdp.vibester.activity

import android.content.ContentValues.TAG
import android.content.DialogInterface
import android.os.Bundle
import android.text.InputType
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import ch.sdp.vibester.R
import ch.sdp.vibester.profile.UserProfile
import com.google.firebase.ktx.Firebase

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.database.ktx.database
import java.util.*

class ProfileActivity : AppCompatActivity() {
private val EXTRA_ID = "userProfile"

private var database: FirebaseDatabase = Firebase.database("https://vibester-sdp-default-rtdb.europe-west1.firebasedatabase.app")

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_profile)
val bundle = intent.extras;
val user: UserProfile = bundle?.getSerializable(EXTRA_ID) as UserProfile
setupProfile(user)
queryDatabase()

val editUsername = findViewById<Button>(R.id.editUser)
val editHandle = findViewById<Button>(R.id.editHandle)
Expand All @@ -33,7 +43,13 @@ class ProfileActivity : AppCompatActivity() {
}
}


/**
* A function that displays the dialog
* @param title title of the dialog
* @param hint hint of the text in the dialog
* @param id id of the dialog
* @param textId id of the text in the dialog
*/

private fun showDialog(title: String, hint: String, id: Int, textId: Int) {
val builder: AlertDialog.Builder = AlertDialog.Builder(this)
Expand All @@ -52,13 +68,51 @@ class ProfileActivity : AppCompatActivity() {
builder.show()
}

/**
* A function that displays the dialog
* @param title title of the dialog
* @param hint hint of the text in the dialog
* @param id id of the dialog
* @param textId id of the text in the dialog
*/

Copy link
Collaborator

@zwierski zwierski Mar 24, 2022

Choose a reason for hiding this comment

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

Careful in the Javadoc, title and hint are not parameters of the function. Does textId correspond to the "name" parameter?

private fun showGeneralDialog(id: Int, name: String) {
val title = "Create $name"
val hint = "Enter new $name"

showDialog(title, hint, 0, id)
}

/**
* A function that queries the database and fetched the correct user
* Hard coded for now
*/

private fun queryDatabase() {
var user: UserProfile
val userRef = database.getReference("users")
userRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (dataSnapShot in dataSnapshot.children) {
val dbContents: Map<String, Objects> = dataSnapShot.value as Map<String, Objects>
user = UserProfile(
dbContents["handle"].toString(),
dbContents["username"].toString(),
dbContents["image"].toString(),
dbContents["totalGames"].toString().toInt(),
dbContents["bestScore"].toString().toInt(),
dbContents["correctSongs"].toString().toInt())
setupProfile(user)
break
}
}

override fun onCancelled(databaseError: DatabaseError) {
Log.w(TAG, "loadUsers:onCancelled", databaseError.toException())
}
})
}


private fun setupProfile(user: UserProfile){
findViewById<TextView>(R.id.handle).text = user.handle
Expand Down
16 changes: 8 additions & 8 deletions app/src/main/java/ch/sdp/vibester/activity/WelcomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import android.view.Window.FEATURE_NO_TITLE
import androidx.appcompat.app.AppCompatActivity
import ch.sdp.vibester.R
import ch.sdp.vibester.model.Song
import ch.sdp.vibester.profile.ProfileDataProvider
//import ch.sdp.vibester.profile.ProfileDataProvider
import ch.sdp.vibester.profile.UserProfile

class WelcomeActivity : AppCompatActivity() {
Expand All @@ -29,13 +29,13 @@ class WelcomeActivity : AppCompatActivity() {
}

fun switchToProfile(view: View) { //FILLER INTENT
// sendDirectIntent(GameSetupScreen::class.java)
Tsathogguaa marked this conversation as resolved.
Show resolved Hide resolved
val profileIntent = Intent(this, ProfileActivity::class.java)
val userID = (0..5).random().toString()
val dataProvider = userID.let { ProfileDataProvider(it) }
val user: UserProfile = dataProvider.getUserProfileData()
profileIntent.putExtra("userProfile", user)
startActivity(profileIntent)
//// sendDirectIntent(GameSetupScreen::class.java)
// val profileIntent = Intent(this, ProfileActivity::class.java)
// val userID = (0..5).random().toString()
// val dataProvider = userID.let { ProfileDataProvider(it) }
// val user: UserProfile = dataProvider.getUserProfileData()
// profileIntent.putExtra("userProfile", user)
sendDirectIntent(ProfileActivity::class.java)
}

fun switchToScoreboard(view: View) {
Expand Down
23 changes: 19 additions & 4 deletions app/src/main/java/ch/sdp/vibester/auth/FireBaseAuthenticator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,33 @@ class FireBaseAuthenticator() {

val auth: FirebaseAuth = Firebase.auth


/**
* A function to log in with email and password
* @param email email
* @param password passwprd
* @return Task of the result
*/
fun signIn(email: String, password: String): Task<AuthResult> {
return auth.signInWithEmailAndPassword(email, password)
}

/**
* A function to create an account with email and password
* @param email email
* @param password passwprd
* @return Task of the result
*/

fun createAccount(email: String, password: String): Task<AuthResult> {
return auth.createUserWithEmailAndPassword(email, password)
}

//TODO look for that ???
// @FixMe
// Commenting this for now until we find a proper way to test it, then will merge it to main
/**
* A function to return the result of google sign in
* @param requestCode a request code
* @param resultCode a result code
* @param data intent returned from google sign in
*/
fun googleActivityResult(requestCode: Int, resultCode: Int, data: Intent?): String? {
return if(requestCode == 1000) {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Good documentation on this file.

Expand Down
Loading