Skip to content

Commit

Permalink
commented leaderboard code
Browse files Browse the repository at this point in the history
  • Loading branch information
leesunny790 committed Oct 15, 2023
1 parent 59c57b8 commit 9efc795
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class LeaderboardRepository {

fun fetchLeaderboard(): LiveData<List<Leaderboard>> {
Log.d("Leaderboard call", "fetchleaderboard")
// 'refreshAll()' coroutine is called only when leaderboard button on bottom app bar clicked
refreshAll()
val lb = leaderboardDao.getLeaderboard()
Log.d("LEADERBOARD CALL", lb.value.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class LeaderboardAdapter(private var itemList: List<Leaderboard>) :
private lateinit var context: Context

inner class ViewHolder(parent: View) : RecyclerView.ViewHolder(parent)

// onCreateViewHolder used to display scrollable list of items
// implemented as part of RecyclerView's adapter, responsible for creating new ViewHolder objects
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
// Log.d("Creating recyclerview", "")
val layoutResource = R.layout.leaderboard_tile
Expand All @@ -26,27 +27,32 @@ class LeaderboardAdapter(private var itemList: List<Leaderboard>) :
}

override fun getItemCount() = itemList.size

// onBindViewHolder called when ViewHolder needs to be filled with data
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = itemList[position]
// position is zero-indexed but we want the leaderboard to start at 1
// populating views within ViewHolder with data from 'item'
bind(item, holder.itemView, position + 1)
}

// bind used to update views within a ViewHolder in a RecyclerView that displays a leaderboard
private fun bind(item: Leaderboard, itemView: View, position: Int) {
itemView.apply {
// sets text of a TextView (rankTextView) to the 'position' value converted to a string
// used to display the rank of the item in the leaderboard
rankTextView.text = position.toString()
// used to display discord username
discordTextView.text = item.discord
val pointNum = item.points
pointsTextView.text = resources.getQuantityString(R.plurals.leaderboard_points_view, pointNum, pointNum)

if (position == 1) {
if (position == 1) { // first position
leaderboardCardView.setBackgroundResource(R.drawable.leaderboard_2023_top_bg)
} else if (position == 10) {
} else if (position == 10) { // last position
leaderboardCardView.setBackgroundResource(R.drawable.leaderboard_2023_bottom_bg)
} else if (position % 2 == 1) {
} else if (position % 2 == 1) { // odd position
leaderboardCardView.setBackgroundColor(ContextCompat.getColor(context, R.color.cloudMist))
} else {
} else { // even position
leaderboardCardView.setBackgroundColor(ContextCompat.getColor(context, R.color.cloudMist))
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.hackillinois.android.viewmodel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import org.hackillinois.android.database.entity.Leaderboard
Expand All @@ -12,5 +13,6 @@ class LeaderboardViewModel : ViewModel() {

fun init() {
leaderboardLiveData = leaderboardRepository.fetchLeaderboard()

}
}
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 1 addition & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Mon Jan 27 12:34:14 CST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
Loading

0 comments on commit 9efc795

Please sign in to comment.