From aecf5e915905909cade8a2b69c932f71a6f9e9fe Mon Sep 17 00:00:00 2001 From: jiabaow Date: Sat, 7 May 2022 16:16:54 +0200 Subject: [PATCH 1/9] load data per genre --- .../vibester/activity/GameSetupActivity.kt | 12 +++++----- .../vibester/activity/ScoreBoardActivity.kt | 19 ++++++++------- .../ch/sdp/vibester/database/DataGetter.kt | 23 ++----------------- app/src/main/res/values/strings.xml | 4 ++++ 4 files changed, 21 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/ch/sdp/vibester/activity/GameSetupActivity.kt b/app/src/main/java/ch/sdp/vibester/activity/GameSetupActivity.kt index 28358519a..74c8ee7f1 100644 --- a/app/src/main/java/ch/sdp/vibester/activity/GameSetupActivity.kt +++ b/app/src/main/java/ch/sdp/vibester/activity/GameSetupActivity.kt @@ -163,12 +163,12 @@ class GameSetupActivity : AppCompatActivity(), AdapterView.OnItemSelectedListene var mode = "" val uri = LastfmUri() when (view.id) { - R.id.btsButton -> {method = LastfmMethod.BY_ARTIST.method; artist = "BTS"; mode = "BTS" } - R.id.kpopButton -> {method = LastfmMethod.BY_TAG.method; tag = "kpop"; mode = "kpop" } - R.id.imagDragonsButton -> {method = LastfmMethod.BY_ARTIST.method; artist = "Imagine Dragons"; mode = "Imagine Dragons"} - R.id.rockButton-> {method = LastfmMethod.BY_TAG.method; tag = "rock"; mode = "rock" } - R.id.topTracksButton -> {method = LastfmMethod.BY_CHART.method; mode = "top tracks"} - R.id.billieEilishButton -> {method = LastfmMethod.BY_ARTIST.method; artist = "Billie Eilish"; mode = "billie eilish"} + R.id.btsButton -> {method = LastfmMethod.BY_ARTIST.method; artist = "BTS"; mode = R.string.bts.toString() } + R.id.kpopButton -> {method = LastfmMethod.BY_TAG.method; tag = "kpop"; mode = R.string.kpop.toString() } + R.id.imagDragonsButton -> {method = LastfmMethod.BY_ARTIST.method; artist = "Imagine Dragons"; mode = R.string.imagine_dragons.toString()} + R.id.rockButton-> {method = LastfmMethod.BY_TAG.method; tag = "rock"; mode = R.string.rock.toString() } + R.id.topTracksButton -> {method = LastfmMethod.BY_CHART.method; mode = R.string.top_tracks.toString()} + R.id.billieEilishButton -> {method = LastfmMethod.BY_ARTIST.method; artist = "Billie Eilish"; mode = R.string.billie_eilish.toString()} } uri.method = method uri.artist = artist diff --git a/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt b/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt index 2798568c3..e558f1917 100644 --- a/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt +++ b/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt @@ -37,15 +37,14 @@ class ScoreBoardActivity : AppCompatActivity() { * TODO: replace "ranking" by appropriate label */ fun selectScoreboard(view: View) { - var sortedBy = "" - val ranking = "ranking" + var sortedBy = "scores/" when (view.id) { - R.id.btsButton -> sortedBy = ranking - R.id.kpopButton -> sortedBy = ranking - R.id.imagDragonsButton -> sortedBy = ranking - R.id.billieEilishButton -> sortedBy = ranking - R.id.rockButton -> sortedBy = ranking - R.id.topTracksButton -> sortedBy = ranking + R.id.btsButton -> sortedBy += R.string.bts + R.id.kpopButton -> sortedBy += R.string.kpop + R.id.imagDragonsButton -> sortedBy += R.string.imagine_dragons + R.id.billieEilishButton -> sortedBy += R.string.billie_eilish + R.id.rockButton -> sortedBy += R.string.rock + R.id.topTracksButton -> sortedBy += R.string.top_tracks } findViewById(R.id.genrePerScoreboard).visibility = GONE @@ -62,8 +61,8 @@ class ScoreBoardActivity : AppCompatActivity() { } } - private fun loadPlayersSortedBy(order: String) { - dbRef.orderByChild(order) + private fun loadPlayersSortedBy(genre: String) { + dbRef.orderByChild(genre) .addValueEventListener(object : ValueEventListener { override fun onDataChange(snapshots: DataSnapshot) { for (snapshot in snapshots.children) { diff --git a/app/src/main/java/ch/sdp/vibester/database/DataGetter.kt b/app/src/main/java/ch/sdp/vibester/database/DataGetter.kt index e01043ee6..593a06a07 100644 --- a/app/src/main/java/ch/sdp/vibester/database/DataGetter.kt +++ b/app/src/main/java/ch/sdp/vibester/database/DataGetter.kt @@ -28,9 +28,7 @@ class DataGetter @Inject constructor() { * @param fieldName */ fun setFieldValue(uid: String, fieldName: String, newVal: Any) { - dbUserRef.child(uid) - .child(fieldName) - .setValue(newVal) + dbUserRef.child(uid).child(fieldName).setValue(newVal) } /** @@ -41,10 +39,7 @@ class DataGetter @Inject constructor() { * @param subFieldName */ fun setSubFieldValue(uid: String, fieldName: String, subFieldName: String, newVal: Any) { - dbUserRef.child(uid) - .child(fieldName) - .child(subFieldName) - .setValue(newVal) + dbUserRef.child(uid).child(fieldName).child(subFieldName).setValue(newVal) } @@ -58,13 +53,6 @@ class DataGetter @Inject constructor() { dbUserRef.child(uid).child(fieldName) .get().addOnSuccessListener { t -> var finalVal = checkValue(t, method, newVal)//newVal TO DELETE IF TESTS PASS, OTHERWISE ROLL BACK! - /*if(t.value != null) { - val previousVal = (t.value as Long?)!!.toInt() - when (method) { - "sum" -> finalVal += previousVal - "best" -> finalVal = maxOf(previousVal, newVal) - } - }*/ setFieldValue(uid, fieldName, finalVal) } } @@ -81,13 +69,6 @@ class DataGetter @Inject constructor() { dbUserRef.child(userID).child(fieldName).child(subFieldName) .get().addOnSuccessListener { t -> var finalVal = checkValue(t, method, newVal) //newVal: TO DELETE IF TESTS PASS, OTHERWISE ROLL BACK! - /*if(t.value != null) { - val previousVal = (t.value as Long?)!!.toInt() - when (method) { - "sum" -> finalVal += previousVal - "best" -> finalVal = maxOf(previousVal, newVal) - } - }*/ setSubFieldValue(userID, fieldName, subFieldName, finalVal) } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 00299b2ba..a28f64d5d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -187,10 +187,14 @@ Sadly you\'re wrong Sadly you\'re wrong, it was %1$s by %2$s Add + Billie Eilish Imagine Dragons BTS R O C K K P O P T O P T R A C K S + rock + kpop + Top Tracks \ No newline at end of file From ed32d1d95485ec3bceaf491ddc257cf877498b02 Mon Sep 17 00:00:00 2001 From: jiabaow Date: Sat, 7 May 2022 16:32:54 +0200 Subject: [PATCH 2/9] small clean up --- .../main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt | 3 --- app/src/main/java/ch/sdp/vibester/user/User.kt | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt b/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt index e558f1917..a0ccf1816 100644 --- a/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt +++ b/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt @@ -33,9 +33,6 @@ class ScoreBoardActivity : AppCompatActivity() { players = ArrayList() } - /** - * TODO: replace "ranking" by appropriate label - */ fun selectScoreboard(view: View) { var sortedBy = "scores/" when (view.id) { diff --git a/app/src/main/java/ch/sdp/vibester/user/User.kt b/app/src/main/java/ch/sdp/vibester/user/User.kt index e7cc70bf8..fc60ad86e 100644 --- a/app/src/main/java/ch/sdp/vibester/user/User.kt +++ b/app/src/main/java/ch/sdp/vibester/user/User.kt @@ -2,6 +2,7 @@ package ch.sdp.vibester.user import java.io.Serializable data class User( + var uid: String = "", var username: String = "", var image: String = "", var email: String = "", @@ -9,7 +10,6 @@ data class User( var bestScore: Int = 0, var correctSongs: Int = 0, var ranking: Int = 0, - var uid: String = "", var friends: Map = mapOf() /* Comment on friend's structure * There is no list structure in the firebase. The most conventional way to push and pull data with From 58d1bc63231524af352a9ddddb849e768a351145 Mon Sep 17 00:00:00 2001 From: jiabaow Date: Sat, 7 May 2022 17:38:29 +0200 Subject: [PATCH 3/9] add genre to scoreboard --- .../sdp/vibester/activity/ScoreBoardActivity.kt | 17 +++++++++-------- app/src/main/java/ch/sdp/vibester/user/User.kt | 1 + .../sdp/vibester/user/UserScoreboardAdapter.kt | 7 ++++++- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt b/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt index a0ccf1816..6e53f13b2 100644 --- a/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt +++ b/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt @@ -24,6 +24,7 @@ class ScoreBoardActivity : AppCompatActivity() { private val dbRef: DatabaseReference = Database.get().getReference("users") private var players: List? = null private var userScoreboardAdapter: UserScoreboardAdapter? = null + private var genre: String = "" override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -36,12 +37,12 @@ class ScoreBoardActivity : AppCompatActivity() { fun selectScoreboard(view: View) { var sortedBy = "scores/" when (view.id) { - R.id.btsButton -> sortedBy += R.string.bts - R.id.kpopButton -> sortedBy += R.string.kpop - R.id.imagDragonsButton -> sortedBy += R.string.imagine_dragons - R.id.billieEilishButton -> sortedBy += R.string.billie_eilish - R.id.rockButton -> sortedBy += R.string.rock - R.id.topTracksButton -> sortedBy += R.string.top_tracks + R.id.btsButton -> {sortedBy += R.string.bts; genre = R.string.bts.toString()} + R.id.kpopButton -> {sortedBy += R.string.kpop; genre = R.string.kpop.toString()} + R.id.imagDragonsButton -> {sortedBy += R.string.imagine_dragons; genre = R.string.imagine_dragons.toString()} + R.id.billieEilishButton -> {sortedBy += R.string.billie_eilish; genre = R.string.billie_eilish.toString()} + R.id.rockButton -> {sortedBy += R.string.rock; genre = R.string.rock.toString()} + R.id.topTracksButton -> {sortedBy += R.string.top_tracks; genre = R.string.top_tracks.toString()} } findViewById(R.id.genrePerScoreboard).visibility = GONE @@ -53,7 +54,7 @@ class ScoreBoardActivity : AppCompatActivity() { private fun setupRecycleView() { findViewById(R.id.recycler_view).apply { layoutManager = LinearLayoutManager(context) - adapter = players?.let { UserScoreboardAdapter(it) } + adapter = players?.let { UserScoreboardAdapter(it, genre) } setHasFixedSize(true) } } @@ -77,7 +78,7 @@ class ScoreBoardActivity : AppCompatActivity() { } private fun showPlayersPosition(players: List?) { - userScoreboardAdapter = UserScoreboardAdapter(players!!) + userScoreboardAdapter = UserScoreboardAdapter(players!!, genre) findViewById(R.id.recycler_view)!!.adapter = userScoreboardAdapter } } \ No newline at end of file diff --git a/app/src/main/java/ch/sdp/vibester/user/User.kt b/app/src/main/java/ch/sdp/vibester/user/User.kt index fc60ad86e..048a07b5c 100644 --- a/app/src/main/java/ch/sdp/vibester/user/User.kt +++ b/app/src/main/java/ch/sdp/vibester/user/User.kt @@ -10,6 +10,7 @@ data class User( var bestScore: Int = 0, var correctSongs: Int = 0, var ranking: Int = 0, + //var scores: Map = mapOf(), var friends: Map = mapOf() /* Comment on friend's structure * There is no list structure in the firebase. The most conventional way to push and pull data with diff --git a/app/src/main/java/ch/sdp/vibester/user/UserScoreboardAdapter.kt b/app/src/main/java/ch/sdp/vibester/user/UserScoreboardAdapter.kt index 8d63af014..8155a9176 100644 --- a/app/src/main/java/ch/sdp/vibester/user/UserScoreboardAdapter.kt +++ b/app/src/main/java/ch/sdp/vibester/user/UserScoreboardAdapter.kt @@ -9,10 +9,11 @@ import androidx.recyclerview.widget.RecyclerView import ch.sdp.vibester.R import ch.sdp.vibester.helper.loadImg -class UserScoreboardAdapter(playersInit: List) : +class UserScoreboardAdapter(playersInit: List, genre: String) : RecyclerView.Adapter() { var players: MutableList = playersInit.toMutableList() + //private var playerScores: Array = arrayOf() override fun getItemCount(): Int = players.size @@ -36,6 +37,10 @@ class UserScoreboardAdapter(playersInit: List) : notifyDataSetChanged() } + /*private fun setScorePerGenre(player: User) { + playerScores = player.scores.keys.toTypedArray() + }*/ + /** * Customer ViewHolder class for PlayerAdapter */ From d1749fd426eec14525c6d728d0426b632aff144e Mon Sep 17 00:00:00 2001 From: jiabaow Date: Sat, 7 May 2022 18:01:28 +0200 Subject: [PATCH 4/9] retrieve score per genre --- .../ch/sdp/vibester/activity/ScoreBoardActivity.kt | 13 +++++++------ app/src/main/java/ch/sdp/vibester/user/User.kt | 2 +- .../ch/sdp/vibester/user/UserScoreboardAdapter.kt | 11 +++++------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt b/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt index 6e53f13b2..f632cd49a 100644 --- a/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt +++ b/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt @@ -36,13 +36,14 @@ class ScoreBoardActivity : AppCompatActivity() { fun selectScoreboard(view: View) { var sortedBy = "scores/" + // R.string doesn't work for genre (getOrDefault in setScore in UserScoreboardAdapter) when (view.id) { - R.id.btsButton -> {sortedBy += R.string.bts; genre = R.string.bts.toString()} - R.id.kpopButton -> {sortedBy += R.string.kpop; genre = R.string.kpop.toString()} - R.id.imagDragonsButton -> {sortedBy += R.string.imagine_dragons; genre = R.string.imagine_dragons.toString()} - R.id.billieEilishButton -> {sortedBy += R.string.billie_eilish; genre = R.string.billie_eilish.toString()} - R.id.rockButton -> {sortedBy += R.string.rock; genre = R.string.rock.toString()} - R.id.topTracksButton -> {sortedBy += R.string.top_tracks; genre = R.string.top_tracks.toString()} + R.id.btsButton -> {sortedBy += R.string.bts; genre = "BTS"} + R.id.kpopButton -> {sortedBy += R.string.kpop; genre = "kpop"} + R.id.imagDragonsButton -> {sortedBy += R.string.imagine_dragons; genre = "Imagine Dragons"} + R.id.billieEilishButton -> {sortedBy += R.string.billie_eilish; genre = "Bellie Eilish"} + R.id.rockButton -> {sortedBy += R.string.rock; genre = "rock"} + R.id.topTracksButton -> {sortedBy += R.string.top_tracks; genre = "top tracks"} } findViewById(R.id.genrePerScoreboard).visibility = GONE diff --git a/app/src/main/java/ch/sdp/vibester/user/User.kt b/app/src/main/java/ch/sdp/vibester/user/User.kt index 048a07b5c..06b512ec8 100644 --- a/app/src/main/java/ch/sdp/vibester/user/User.kt +++ b/app/src/main/java/ch/sdp/vibester/user/User.kt @@ -10,7 +10,7 @@ data class User( var bestScore: Int = 0, var correctSongs: Int = 0, var ranking: Int = 0, - //var scores: Map = mapOf(), + var scores: Map = mapOf(), var friends: Map = mapOf() /* Comment on friend's structure * There is no list structure in the firebase. The most conventional way to push and pull data with diff --git a/app/src/main/java/ch/sdp/vibester/user/UserScoreboardAdapter.kt b/app/src/main/java/ch/sdp/vibester/user/UserScoreboardAdapter.kt index 8155a9176..61bb1006d 100644 --- a/app/src/main/java/ch/sdp/vibester/user/UserScoreboardAdapter.kt +++ b/app/src/main/java/ch/sdp/vibester/user/UserScoreboardAdapter.kt @@ -9,11 +9,10 @@ import androidx.recyclerview.widget.RecyclerView import ch.sdp.vibester.R import ch.sdp.vibester.helper.loadImg -class UserScoreboardAdapter(playersInit: List, genre: String) : +class UserScoreboardAdapter(playersInit: List, private val genre: String) : RecyclerView.Adapter() { var players: MutableList = playersInit.toMutableList() - //private var playerScores: Array = arrayOf() override fun getItemCount(): Int = players.size @@ -37,9 +36,9 @@ class UserScoreboardAdapter(playersInit: List, genre: String) : notifyDataSetChanged() } - /*private fun setScorePerGenre(player: User) { - playerScores = player.scores.keys.toTypedArray() - }*/ + private fun setScore(player: User): Int { + return player.scores.getOrDefault(genre, 0) + } /** * Customer ViewHolder class for PlayerAdapter @@ -54,7 +53,7 @@ class UserScoreboardAdapter(playersInit: List, genre: String) : val newPosition = position + 1 itemView.findViewById(R.id.tv_position).text = (newPosition).toString() itemView.findViewById(R.id.tv_name).text = player.username - itemView.findViewById(R.id.tv_score).text = player.ranking.toString() + itemView.findViewById(R.id.tv_score).text = setScore(player).toString() itemView.findViewById(R.id.iv_photo).loadImg(player.image) } } From 6ebac728bb81cee9ebfe2064d3180a872421cb45 Mon Sep 17 00:00:00 2001 From: jiabaow Date: Sun, 8 May 2022 11:11:52 +0200 Subject: [PATCH 5/9] fix typo --- .../java/ch/sdp/vibester/activity/ScoreBoardActivity.kt | 7 +++++-- .../java/ch/sdp/vibester/auth/FireBaseAuthenticator.kt | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt b/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt index f632cd49a..824d38e50 100644 --- a/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt +++ b/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt @@ -36,12 +36,12 @@ class ScoreBoardActivity : AppCompatActivity() { fun selectScoreboard(view: View) { var sortedBy = "scores/" - // R.string doesn't work for genre (getOrDefault in setScore in UserScoreboardAdapter) + // can't use R.string for genre (getOrDefault in setScore in UserScoreboardAdapter) when (view.id) { R.id.btsButton -> {sortedBy += R.string.bts; genre = "BTS"} R.id.kpopButton -> {sortedBy += R.string.kpop; genre = "kpop"} R.id.imagDragonsButton -> {sortedBy += R.string.imagine_dragons; genre = "Imagine Dragons"} - R.id.billieEilishButton -> {sortedBy += R.string.billie_eilish; genre = "Bellie Eilish"} + R.id.billieEilishButton -> {sortedBy += R.string.billie_eilish; genre = "Billie Eilish"} R.id.rockButton -> {sortedBy += R.string.rock; genre = "rock"} R.id.topTracksButton -> {sortedBy += R.string.top_tracks; genre = "top tracks"} } @@ -70,6 +70,9 @@ class ScoreBoardActivity : AppCompatActivity() { (players as? ArrayList)?.add(player) } } + //players = players?.sortedByDescending { it.scores.getOrDefault(genre, 0) } + /*players = players?.sortedWith(Comparator{ p1, p2 -> + if (p1.scores.getOrDefault(genre, -1) > p2.scores.getOrDefault(genre, -1)) -1 else 1})*/ showPlayersPosition(players) } diff --git a/app/src/main/java/ch/sdp/vibester/auth/FireBaseAuthenticator.kt b/app/src/main/java/ch/sdp/vibester/auth/FireBaseAuthenticator.kt index cc5d4a24a..61aaaa11a 100644 --- a/app/src/main/java/ch/sdp/vibester/auth/FireBaseAuthenticator.kt +++ b/app/src/main/java/ch/sdp/vibester/auth/FireBaseAuthenticator.kt @@ -98,7 +98,7 @@ class FireBaseAuthenticator @Inject constructor() { /** * A function to create an account with email and password * @param email email - * @param password passwprd + * @param password password * @return Task of the result */ fun createAccount(email: String, password: String): Task { From 5a17535f3c82cae4f32bc81cb1d09b973056d05b Mon Sep 17 00:00:00 2001 From: jiabaow Date: Sun, 8 May 2022 11:17:23 +0200 Subject: [PATCH 6/9] update tests --- .../sdp/vibester/scoreboard/UserScoreboardAdapterTest.kt | 8 ++++---- app/src/main/java/ch/sdp/vibester/user/User.kt | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/src/androidTest/java/ch/sdp/vibester/scoreboard/UserScoreboardAdapterTest.kt b/app/src/androidTest/java/ch/sdp/vibester/scoreboard/UserScoreboardAdapterTest.kt index 8a1075ee5..16c30ccc0 100644 --- a/app/src/androidTest/java/ch/sdp/vibester/scoreboard/UserScoreboardAdapterTest.kt +++ b/app/src/androidTest/java/ch/sdp/vibester/scoreboard/UserScoreboardAdapterTest.kt @@ -18,7 +18,7 @@ class UserScoreboardAdapterTest { val players: MutableList = arrayListOf() players.addAll(listOf(user1, user2)) val userScoreboardViewHolder: RecyclerView.Adapter = - UserScoreboardAdapter(players) + UserScoreboardAdapter(players, "rock") assertThat(userScoreboardViewHolder.itemCount, equalTo(2)) } @@ -29,7 +29,7 @@ class UserScoreboardAdapterTest { val players: MutableList = arrayListOf() players.addAll(listOf(user1, user2)) val userScoreboardViewHolder: RecyclerView.Adapter = - UserScoreboardAdapter(players) + UserScoreboardAdapter(players, "BTS") val defaultType = 0 assertThat(userScoreboardViewHolder.getItemViewType(0), equalTo(defaultType)) } @@ -43,7 +43,7 @@ class UserScoreboardAdapterTest { val recyclerView = RecyclerView(ApplicationProvider.getApplicationContext()) recyclerView.layoutManager = LinearLayoutManager(ApplicationProvider.getApplicationContext()) - val playerAdapter = UserScoreboardAdapter(players) + val playerAdapter = UserScoreboardAdapter(players, "Imagine Dragons") recyclerView.adapter = playerAdapter val newPlayers: MutableList = arrayListOf() newPlayers.add(User("test3","https://images.app.goo.gl/YkBi16zwyjB7ejj96", "test3@gmail.com")) @@ -60,7 +60,7 @@ class UserScoreboardAdapterTest { val recyclerView = RecyclerView(ApplicationProvider.getApplicationContext()) recyclerView.layoutManager = LinearLayoutManager(ApplicationProvider.getApplicationContext()) - val playerAdapter = UserScoreboardAdapter(players) + val playerAdapter = UserScoreboardAdapter(players, "Billie Eilish") val player3 = User("test3","https://images.app.goo.gl/YkBi16zwyjB7ejj96", "test3@gmail.com") val updatedList = arrayListOf(player3) playerAdapter.addPlayers(updatedList) diff --git a/app/src/main/java/ch/sdp/vibester/user/User.kt b/app/src/main/java/ch/sdp/vibester/user/User.kt index 06b512ec8..1785b6415 100644 --- a/app/src/main/java/ch/sdp/vibester/user/User.kt +++ b/app/src/main/java/ch/sdp/vibester/user/User.kt @@ -2,7 +2,6 @@ package ch.sdp.vibester.user import java.io.Serializable data class User( - var uid: String = "", var username: String = "", var image: String = "", var email: String = "", @@ -10,10 +9,11 @@ data class User( var bestScore: Int = 0, var correctSongs: Int = 0, var ranking: Int = 0, - var scores: Map = mapOf(), - var friends: Map = mapOf() + var uid: String = "", + var friends: Map = mapOf(), /* Comment on friend's structure * There is no list structure in the firebase. The most conventional way to push and pull data with * several values in one field was to use it as a map. String is user ID, and Boolean plays a filler role here, can be anything. * */ + var scores: Map = mapOf(), ) : Serializable {} \ No newline at end of file From c6fc8433c3c80edcdad3f488c2a79d3bf7a3dc1e Mon Sep 17 00:00:00 2001 From: jiabaow Date: Sun, 8 May 2022 12:13:30 +0200 Subject: [PATCH 7/9] change to mutableList --- .../sdp/vibester/activity/ScoreBoardActivity.kt | 17 ++++++++++++++--- .../sdp/vibester/user/UserScoreboardAdapter.kt | 4 ++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt b/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt index 824d38e50..bf184b2d8 100644 --- a/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt +++ b/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt @@ -22,7 +22,7 @@ import com.google.firebase.database.ValueEventListener class ScoreBoardActivity : AppCompatActivity() { private val dbRef: DatabaseReference = Database.get().getReference("users") - private var players: List? = null + private var players: MutableList? = null private var userScoreboardAdapter: UserScoreboardAdapter? = null private var genre: String = "" @@ -70,9 +70,12 @@ class ScoreBoardActivity : AppCompatActivity() { (players as? ArrayList)?.add(player) } } - //players = players?.sortedByDescending { it.scores.getOrDefault(genre, 0) } + //players = players?.sortedByDescending { it.scores.toSortedMap()[genre] } /*players = players?.sortedWith(Comparator{ p1, p2 -> if (p1.scores.getOrDefault(genre, -1) > p2.scores.getOrDefault(genre, -1)) -1 else 1})*/ + //players = players?.sortedByDescending { (_, v) -> v } + players?.forEach{player -> player.ranking = player.scores.getOrDefault(genre, 0)} + players = players?.sortedByDescending { it.ranking } as MutableList? showPlayersPosition(players) } @@ -81,7 +84,15 @@ class ScoreBoardActivity : AppCompatActivity() { }) } - private fun showPlayersPosition(players: List?) { + /*private fun replaceRankingByScore(list: MutableList): MutableList { + val iterator = list.listIterator() + while(iterator.hasNext()) { + val value = iterator.next() + iterator.set() + } + }*/ + + private fun showPlayersPosition(players: MutableList?) { userScoreboardAdapter = UserScoreboardAdapter(players!!, genre) findViewById(R.id.recycler_view)!!.adapter = userScoreboardAdapter } diff --git a/app/src/main/java/ch/sdp/vibester/user/UserScoreboardAdapter.kt b/app/src/main/java/ch/sdp/vibester/user/UserScoreboardAdapter.kt index 61bb1006d..db67ce736 100644 --- a/app/src/main/java/ch/sdp/vibester/user/UserScoreboardAdapter.kt +++ b/app/src/main/java/ch/sdp/vibester/user/UserScoreboardAdapter.kt @@ -9,10 +9,10 @@ import androidx.recyclerview.widget.RecyclerView import ch.sdp.vibester.R import ch.sdp.vibester.helper.loadImg -class UserScoreboardAdapter(playersInit: List, private val genre: String) : +class UserScoreboardAdapter(playersInit: MutableList, private val genre: String) : RecyclerView.Adapter() { - var players: MutableList = playersInit.toMutableList() + var players: MutableList = playersInit override fun getItemCount(): Int = players.size From 034d8b1fbec7416eb65ce961df4fe97b0da85fbd Mon Sep 17 00:00:00 2001 From: jiabaow Date: Sun, 8 May 2022 14:37:20 +0200 Subject: [PATCH 8/9] descending order --- .../sdp/vibester/activity/ScoreBoardActivity.kt | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt b/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt index bf184b2d8..d65afad01 100644 --- a/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt +++ b/app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt @@ -70,11 +70,7 @@ class ScoreBoardActivity : AppCompatActivity() { (players as? ArrayList)?.add(player) } } - //players = players?.sortedByDescending { it.scores.toSortedMap()[genre] } - /*players = players?.sortedWith(Comparator{ p1, p2 -> - if (p1.scores.getOrDefault(genre, -1) > p2.scores.getOrDefault(genre, -1)) -1 else 1})*/ - //players = players?.sortedByDescending { (_, v) -> v } - players?.forEach{player -> player.ranking = player.scores.getOrDefault(genre, 0)} + players = players?.let { replaceRankingByScore(it) } players = players?.sortedByDescending { it.ranking } as MutableList? showPlayersPosition(players) } @@ -84,13 +80,15 @@ class ScoreBoardActivity : AppCompatActivity() { }) } - /*private fun replaceRankingByScore(list: MutableList): MutableList { + private fun replaceRankingByScore(list: MutableList): MutableList { val iterator = list.listIterator() while(iterator.hasNext()) { - val value = iterator.next() - iterator.set() + val player = iterator.next() + player.ranking = player.scores.getOrDefault(genre, 0) + iterator.set(player) } - }*/ + return list + } private fun showPlayersPosition(players: MutableList?) { userScoreboardAdapter = UserScoreboardAdapter(players!!, genre) From 1c2f01aa1ecb5aef3293c6928a596546d34cc17d Mon Sep 17 00:00:00 2001 From: jiabaow <60721007+jiabaow@users.noreply.github.com> Date: Sun, 8 May 2022 16:17:26 +0200 Subject: [PATCH 9/9] Update app/src/main/java/ch/sdp/vibester/user/User.kt Co-authored-by: kamilababayeva <51053878+kamilababayeva@users.noreply.github.com> --- app/src/main/java/ch/sdp/vibester/user/User.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/ch/sdp/vibester/user/User.kt b/app/src/main/java/ch/sdp/vibester/user/User.kt index a271d3973..10b80d476 100644 --- a/app/src/main/java/ch/sdp/vibester/user/User.kt +++ b/app/src/main/java/ch/sdp/vibester/user/User.kt @@ -15,5 +15,5 @@ data class User( * There is no list structure in the firebase. The most conventional way to push and pull data with * several values in one field was to use it as a map. String is user ID, and Boolean plays a filler role here, can be anything. * */ - var scores: Map = mapOf(), + var scores: Map = mapOf() ) : Serializable {} \ No newline at end of file