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

Commit

Permalink
Merge pull request #311 from MaximeZmt/kamila/scoreboard-fragment
Browse files Browse the repository at this point in the history
Kamila/scoreboard fragment
  • Loading branch information
kamilababayeva authored May 20, 2022
2 parents 2f8c7f6 + 147126b commit cd66ca4
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch.sdp.vibester.activity
package ch.sdp.vibester.fragment

import androidx.recyclerview.widget.RecyclerView
import androidx.test.core.app.ApplicationProvider
Expand All @@ -9,9 +9,9 @@ import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.sdp.vibester.R
import ch.sdp.vibester.launchFragmentInHiltContainer
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.hamcrest.CoreMatchers.not
Expand All @@ -23,18 +23,19 @@ import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class ScoreBoardActivityTest {
class ScoreBoardFragmentTest {

@get:Rule(order=0)
var hiltRule = HiltAndroidRule(this)

@get:Rule(order=1)
val activityRule = ActivityScenarioRule(ScoreBoardActivity::class.java)

@Before
fun setUp() {
hiltRule.inject()
Intents.init()
launchFragmentInHiltContainer<ScoreBoardFragment>(
themeResId = R.style.AppTheme
)
}

@After
Expand Down Expand Up @@ -94,17 +95,4 @@ class ScoreBoardActivityTest {
fun btsButtonClick() {
onView(withId(R.id.btsButton)).perform(click())
}

@Test
fun recycleViewScrollDownTest() {
val recyclerView = RecyclerView(ApplicationProvider.getApplicationContext())
val itemCount = recyclerView.adapter?.itemCount
if (itemCount != null) {
onView(withId(R.id.recycler_view)).perform(
RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(
itemCount.minus(1)
)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,6 @@ class WelcomeScreenFragmentTest {
onView(withId(R.id.welcome_profile)).perform(click())
intended(hasComponent(MyProfileActivity::class.java.name))
}

@Test
fun checkIntentOnScoreboard() {
val intent = Intent(ApplicationProvider.getApplicationContext(), MainActivity::class.java)
createMockAuthenticatorLoggedOut()
val scn: ActivityScenario<MainActivity> = ActivityScenario.launch(intent)

onView(withId(R.id.welcome_scoreboard)).perform(click())
intended(hasComponent(ScoreBoardActivity::class.java.name))
}

@Test
fun checkIntentOnDownload() {
val intent = Intent(ApplicationProvider.getApplicationContext(), MainActivity::class.java)
Expand Down
4 changes: 0 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@
android:name=".activity.profile.PublicProfileActivity"
android:windowSoftInputMode="adjustPan"
android:exported="false" />
<activity
android:name=".activity.ScoreBoardActivity"
android:exported="false"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".activity.AuthenticationActivity"
android:exported="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class GameSetupFragment : Fragment(), View.OnClickListener, AdapterView.OnItemSe
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_game_setup, container, false)
val ctx = inflater.getContext();
val ctx = inflater.context

view.findViewById<Button>(R.id.local_buzzer_game_button).setOnClickListener(this)
view.findViewById<Button>(R.id.local_typing_game_button).setOnClickListener(this)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package ch.sdp.vibester.activity
package ch.sdp.vibester.fragment

import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import ch.sdp.vibester.R
Expand All @@ -26,42 +30,43 @@ import com.google.firebase.database.ValueEventListener
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class ScoreBoardActivity : AppCompatActivity(), OnItemClickListener {
class ScoreBoardFragment : Fragment(), OnItemClickListener, View.OnClickListener{
private val dbRef: DatabaseReference = Database.get().getReference("users")
private var players: MutableList<User>? = null
private var players: MutableList<User> = mutableListOf()
private var userScoreboardAdapter: UserScoreboardAdapter? = null
private var genre: String = ""

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.scoreboard_scoreboard)
setSupportActionBar(findViewById(R.id.toolbar))
setupRecycleView()
players = ArrayList()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_scoreboard, container, false)
val ctx = inflater.context

view.findViewById<Button>(R.id.kpopButton).setOnClickListener(this)
view.findViewById<Button>(R.id.rockButton).setOnClickListener(this)
view.findViewById<Button>(R.id.btsButton).setOnClickListener(this)
view.findViewById<Button>(R.id.topTracksButton).setOnClickListener(this)
view.findViewById<Button>(R.id.imagDragonsButton).setOnClickListener(this)
view.findViewById<Button>(R.id.billieEilishButton).setOnClickListener(this)
setupRecycleView(view, ctx)

return view
}

fun selectScoreboard(view: View) {
var sortedBy = "scores/"
// 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 = "Billie Eilish"}
R.id.rockButton -> {sortedBy += R.string.rock; genre = "rock"}
R.id.topTracksButton -> {sortedBy += R.string.top_tracks; genre = "top tracks"}
}
private fun selectScoreboard() {
val sortedBy = "scores/" + genre

findViewById<LinearLayout>(R.id.genrePerScoreboard).visibility = GONE
findViewById<NestedScrollView>(R.id.scoreboard_content_scrolling).visibility = VISIBLE
requireView().findViewById<ConstraintLayout>(R.id.genrePerScoreboard).visibility = GONE
requireView().findViewById<NestedScrollView>(R.id.scoreboard_content_scrolling).visibility = VISIBLE

loadPlayersSortedBy(sortedBy)
}

private fun setupRecycleView() {
findViewById<RecyclerView>(R.id.recycler_view).apply {
private fun setupRecycleView(view:View, context: Context) {
view.findViewById<RecyclerView>(R.id.recycler_view).apply {
layoutManager = LinearLayoutManager(context)
adapter = players?.let { UserScoreboardAdapter(it, genre, this@ScoreBoardActivity) }
adapter = UserScoreboardAdapter(players, genre, this@ScoreBoardFragment)
setHasFixedSize(true)
}
}
Expand All @@ -76,8 +81,8 @@ class ScoreBoardActivity : AppCompatActivity(), OnItemClickListener {
(players as? ArrayList<User>)?.add(player)
}
}
players = players?.let { replaceRankingByScore(it) }
players = players?.sortedByDescending { it.ranking } as MutableList<User>?
players = replaceRankingByScore(players)
players = players.sortedByDescending { it.ranking } as MutableList<User>
showPlayersPosition(players)
}

Expand All @@ -98,17 +103,42 @@ class ScoreBoardActivity : AppCompatActivity(), OnItemClickListener {

private fun showPlayersPosition(players: MutableList<User>?) {
userScoreboardAdapter = UserScoreboardAdapter(players!!, genre, this)
findViewById<RecyclerView>(R.id.recycler_view)!!.adapter = userScoreboardAdapter
requireView().findViewById<RecyclerView>(R.id.recycler_view)!!.adapter = userScoreboardAdapter
}

/**
* @param position index of the item on click
* go to the profile of the player at index position
*/
override fun onItemClick(position: Int) {
val intent = Intent(this, PublicProfileActivity::class.java)
intent.putExtra("UserId", players?.get(position)?.uid)
val intent = Intent(requireActivity(), PublicProfileActivity::class.java)
intent.putExtra("UserId", players.get(position).uid)
intent.putExtra("ScoresOrFollowing", R.string.profile_scores.toString() )
startActivity(intent)
}

override fun onClick(v: View?) {
when(v!!.getId()) {
R.id.btsButton -> {
genre = "BTS"; selectScoreboard()
}
R.id.kpopButton -> {
genre = "kpop"; selectScoreboard()
}
R.id.imagDragonsButton -> {
genre = "Imagine Dragons"; selectScoreboard()
}
R.id.billieEilishButton -> {
genre = "Billie Eilish"; selectScoreboard()
}
R.id.rockButton -> {
genre = "rock";selectScoreboard()
}
R.id.topTracksButton -> {
genre = "top tracks";selectScoreboard()
}

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class WelcomeScreenFragment : Fragment(),View.OnClickListener {

view.findViewById<Button>(R.id.welcome_profile).setOnClickListener(this)
view.findViewById<Button>(R.id.welcome_download).setOnClickListener(this)
view.findViewById<Button>(R.id.welcome_scoreboard).setOnClickListener(this)
view.findViewById<Button>(R.id.welcome_search).setOnClickListener(this)

return view
Expand Down Expand Up @@ -71,9 +70,6 @@ class WelcomeScreenFragment : Fragment(),View.OnClickListener {
}
}

private fun switchToScoreboard() {
sendDirectIntent(ScoreBoardActivity::class.java)
}

private fun switchToDownload() {
sendDirectIntent(DownloadActivity::class.java)
Expand All @@ -88,7 +84,6 @@ class WelcomeScreenFragment : Fragment(),View.OnClickListener {
R.id.welcome_search -> switchToSearch()
R.id.welcome_download -> switchToDownload()
R.id.welcome_profile -> switchToProfile()
R.id.welcome_scoreboard -> switchToScoreboard()
}
}
}
Binary file added app/src/main/res/drawable/scoreboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 0 additions & 13 deletions app/src/main/res/layout/fragment_welcome_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,6 @@
android:gravity="center"
android:orientation="vertical">


<Button
android:id="@+id/welcome_scoreboard"
android:layout_width="200dp"
android:layout_height="48dp"
android:layout_marginTop="10dp"
android:backgroundTint="@color/cg_blue"
android:elevation="10dp"
android:stateListAnimator="@null"
android:text="@string/welcomeScreen_scoreboard"
android:translationZ="10dp"
app:icon="@android:drawable/ic_menu_share" />

<Button
android:id="@+id/welcome_profile"
android:layout_width="200dp"
Expand Down
10 changes: 2 additions & 8 deletions app/src/main/res/layout/genre_for_scoreboard.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/genrePerScoreboard"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/genrePerScoreboard"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -31,7 +31,6 @@
android:layout_height="130dp"
android:layout_weight="1"
android:background="@drawable/rounded_button"
android:onClick="selectScoreboard"
android:text="@string/imagine_dragons"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand All @@ -51,7 +50,6 @@
android:layout_height="130dp"
android:layout_weight="1"
android:background="@drawable/rounded_button"
android:onClick="selectScoreboard"
android:text="@string/billie_eilish"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand All @@ -76,7 +74,6 @@
android:layout_height="130sp"
android:layout_weight="1"
android:background="@drawable/rounded_button"
android:onClick="selectScoreboard"
android:text="@string/r_o_c_k"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand All @@ -96,7 +93,6 @@
android:layout_height="130dp"
android:layout_weight="1"
android:background="@drawable/rounded_button"
android:onClick="selectScoreboard"
android:text="@string/bts"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand All @@ -121,7 +117,6 @@
android:layout_height="130dp"
android:layout_weight="1"
android:background="@drawable/rounded_button"
android:onClick="selectScoreboard"
android:text="@string/t_o_p_t_r_a_c_k_s"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand All @@ -141,7 +136,6 @@
android:layout_height="130dp"
android:layout_weight="1"
android:background="@drawable/rounded_button"
android:onClick="selectScoreboard"
android:text="@string/k_p_o_p"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/menu/bottom_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@
/>


<item
android:id="@+id/scoreboardBtn"
android:title="Scoreboard"
android:icon="@drawable/scoreboard"
/>


</menu>
2 changes: 2 additions & 0 deletions app/src/main/res/navigation/bottom_navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
android:label="fragment_home" tools:layout="@layout/fragment_welcome_screen"/>
<fragment android:id="@+id/gameSetupBtn" android:name="ch.sdp.vibester.fragment.GameSetupFragment"
android:label="fragment_settings" tools:layout="@layout/fragment_game_setup"/>
<fragment android:id="@+id/scoreboardBtn" android:name="ch.sdp.vibester.fragment.ScoreBoardFragment"
android:label="fragment_scoreboard" tools:layout="@layout/fragment_scoreboard"/>
</navigation>

0 comments on commit cd66ca4

Please sign in to comment.