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

Jwen/scoreboard real data per genre #264

Merged
merged 10 commits into from
May 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class UserScoreboardAdapterTest {
val players: MutableList<User> = arrayListOf()
players.addAll(listOf(user1, user2))
val userScoreboardViewHolder: RecyclerView.Adapter<UserScoreboardAdapter.PlayerViewHolder> =
UserScoreboardAdapter(players)
UserScoreboardAdapter(players, "rock")
assertThat(userScoreboardViewHolder.itemCount, equalTo(2))
}

Expand All @@ -29,7 +29,7 @@ class UserScoreboardAdapterTest {
val players: MutableList<User> = arrayListOf()
players.addAll(listOf(user1, user2))
val userScoreboardViewHolder: RecyclerView.Adapter<UserScoreboardAdapter.PlayerViewHolder> =
UserScoreboardAdapter(players)
UserScoreboardAdapter(players, "BTS")
val defaultType = 0
assertThat(userScoreboardViewHolder.getItemViewType(0), equalTo(defaultType))
}
Expand All @@ -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<User> = arrayListOf()
newPlayers.add(User("test3","https://images.app.goo.gl/YkBi16zwyjB7ejj96", "[email protected]"))
Expand All @@ -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", "[email protected]")
val updatedList = arrayListOf(player3)
playerAdapter.addPlayers(updatedList)
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/ch/sdp/vibester/activity/GameSetupActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
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 5 locations. Consider refactoring.

R.id.kpopButton -> {method = LastfmMethod.BY_TAG.method; tag = "kpop"; mode = R.string.kpop.toString() }
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 5 locations. Consider refactoring.

R.id.imagDragonsButton -> {method = LastfmMethod.BY_ARTIST.method; artist = "Imagine Dragons"; mode = R.string.imagine_dragons.toString()}
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 5 locations. Consider refactoring.

R.id.rockButton-> {method = LastfmMethod.BY_TAG.method; tag = "rock"; mode = R.string.rock.toString() }
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 5 locations. Consider refactoring.

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()}
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 5 locations. Consider refactoring.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it be possible to refactor these lines in, for example, a function that takes the "method" and the "mode" as parameters and the artist/tag as an optional String parameter? It's not urgent but it could solve the detected duplication.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I will keep this in mind when we do the general refactoring.

}
uri.method = method
uri.artist = artist
Expand Down
44 changes: 27 additions & 17 deletions app/src/main/java/ch/sdp/vibester/activity/ScoreBoardActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import com.google.firebase.database.ValueEventListener

class ScoreBoardActivity : AppCompatActivity() {
private val dbRef: DatabaseReference = Database.get().getReference("users")
private var players: List<User>? = null
private var players: MutableList<User>? = null
private var userScoreboardAdapter: UserScoreboardAdapter? = null
private var genre: String = ""

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -33,19 +34,16 @@ class ScoreBoardActivity : AppCompatActivity() {
players = ArrayList()
}

/**
* TODO: replace "ranking" by appropriate label
*/
fun selectScoreboard(view: View) {
var sortedBy = ""
val ranking = "ranking"
var sortedBy = "scores/"
// can't use R.string for genre (getOrDefault in setScore in UserScoreboardAdapter)
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; 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 = "Billie Eilish"}
R.id.rockButton -> {sortedBy += R.string.rock; genre = "rock"}
R.id.topTracksButton -> {sortedBy += R.string.top_tracks; genre = "top tracks"}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we put genre into the strings as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried but that doesn't work, I explained in the comment in line 39.

}

findViewById<ConstraintLayout>(R.id.genrePerScoreboard).visibility = GONE
Expand All @@ -57,13 +55,13 @@ class ScoreBoardActivity : AppCompatActivity() {
private fun setupRecycleView() {
findViewById<RecyclerView>(R.id.recycler_view).apply {
layoutManager = LinearLayoutManager(context)
adapter = players?.let { UserScoreboardAdapter(it) }
adapter = players?.let { UserScoreboardAdapter(it, genre) }
setHasFixedSize(true)
}
}

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) {
Expand All @@ -72,6 +70,8 @@ class ScoreBoardActivity : AppCompatActivity() {
(players as? ArrayList<User>)?.add(player)
}
}
players = players?.let { replaceRankingByScore(it) }
players = players?.sortedByDescending { it.ranking } as MutableList<User>?
showPlayersPosition(players)
}

Expand All @@ -80,8 +80,18 @@ class ScoreBoardActivity : AppCompatActivity() {
})
}

private fun showPlayersPosition(players: List<User>?) {
userScoreboardAdapter = UserScoreboardAdapter(players!!)
private fun replaceRankingByScore(list: MutableList<User>): MutableList<User> {
val iterator = list.listIterator()
while(iterator.hasNext()) {
val player = iterator.next()
player.ranking = player.scores.getOrDefault(genre, 0)
iterator.set(player)
}
return list
}

private fun showPlayersPosition(players: MutableList<User>?) {
userScoreboardAdapter = UserScoreboardAdapter(players!!, genre)
findViewById<RecyclerView>(R.id.recycler_view)!!.adapter = userScoreboardAdapter
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuthResult> {
Expand Down
23 changes: 2 additions & 21 deletions app/src/main/java/ch/sdp/vibester/database/DataGetter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand All @@ -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)
}


Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/ch/sdp/vibester/user/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ data class User(
var correctSongs: Int = 0,
var ranking: Int = 0,
var uid: String = "",
var friends: Map<String, Boolean> = mapOf()
var friends: Map<String, Boolean> = 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<String, Int> = mapOf()
) : Serializable {}
10 changes: 7 additions & 3 deletions app/src/main/java/ch/sdp/vibester/user/UserScoreboardAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import androidx.recyclerview.widget.RecyclerView
import ch.sdp.vibester.R
import ch.sdp.vibester.helper.loadImg

class UserScoreboardAdapter(playersInit: List<User>) :
class UserScoreboardAdapter(playersInit: MutableList<User>, private val genre: String) :
RecyclerView.Adapter<UserScoreboardAdapter.PlayerViewHolder>() {

var players: MutableList<User> = playersInit.toMutableList()
var players: MutableList<User> = playersInit

override fun getItemCount(): Int = players.size

Expand All @@ -36,6 +36,10 @@ class UserScoreboardAdapter(playersInit: List<User>) :
notifyDataSetChanged()
}

private fun setScore(player: User): Int {
return player.scores.getOrDefault(genre, 0)
}

/**
* Customer ViewHolder class for PlayerAdapter
*/
Expand All @@ -49,7 +53,7 @@ class UserScoreboardAdapter(playersInit: List<User>) :
val newPosition = position + 1
itemView.findViewById<TextView>(R.id.tv_position).text = (newPosition).toString()
itemView.findViewById<TextView>(R.id.tv_name).text = player.username
itemView.findViewById<TextView>(R.id.tv_score).text = player.ranking.toString()
itemView.findViewById<TextView>(R.id.tv_score).text = setScore(player).toString()
itemView.findViewById<ImageView>(R.id.iv_photo).loadImg(player.image)
}
}
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,14 @@
<string name="wrong_message">Sadly you\'re wrong</string>
<string name="wrong_message_with_answer">Sadly you\'re wrong, it was %1$s by %2$s</string>
<string name="user_add_friend">Add</string>

<string name="billie_eilish">Billie Eilish</string>
<string name="imagine_dragons">Imagine Dragons</string>
<string name="bts">BTS</string>
<string name="r_o_c_k">R O C K</string>
<string name="k_p_o_p">K P O P</string>
<string name="t_o_p_t_r_a_c_k_s">T O P T R A C K S</string>
<string name="rock">rock</string>
<string name="kpop">kpop</string>
<string name="top_tracks">Top Tracks</string>
</resources>