From fbe524e184522d40dd7962df6c7d841eac6c8c96 Mon Sep 17 00:00:00 2001 From: jiabaow Date: Tue, 7 Jun 2022 16:37:02 +0200 Subject: [PATCH 01/22] extract gpToPlayerProfileWithSection --- .../ch/sdp/vibester/fragment/ScoreBoardFragment.kt | 5 ++++- .../ch/sdp/vibester/fragment/SearchUserFragment.kt | 4 +++- app/src/main/java/ch/sdp/vibester/helper/Helper.kt | 11 +++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/ch/sdp/vibester/fragment/ScoreBoardFragment.kt b/app/src/main/java/ch/sdp/vibester/fragment/ScoreBoardFragment.kt index 2a888813f..f9c50159f 100644 --- a/app/src/main/java/ch/sdp/vibester/fragment/ScoreBoardFragment.kt +++ b/app/src/main/java/ch/sdp/vibester/fragment/ScoreBoardFragment.kt @@ -16,6 +16,7 @@ import ch.sdp.vibester.R import ch.sdp.vibester.activity.PublicProfileActivity import ch.sdp.vibester.database.Database import ch.sdp.vibester.database.ImageGetter +import ch.sdp.vibester.helper.Helper import ch.sdp.vibester.helper.IntentSwitcher import ch.sdp.vibester.helper.ViewModel import ch.sdp.vibester.user.OnItemClickListener @@ -110,9 +111,11 @@ class ScoreBoardFragment : Fragment(R.layout.fragment_layout_scoreboard), OnItem * go to the profile of the player at index position */ override fun onItemClick(position: Int) { + val playerId = players[position].uid + //mapOf(Pair("UserId", players[position].uid), Pair("ScoresOrFollowing", R.string.profile_scores.toString())) IntentSwitcher.switch(vmScoreBoard.ctx, PublicProfileActivity::class.java, - mapOf(Pair("UserId", players[position].uid), Pair("ScoresOrFollowing", R.string.profile_scores.toString()))) + Helper().goToPlayerProfileWithSection(playerId, true)) } private fun setGenreListeners(genre: String){ diff --git a/app/src/main/java/ch/sdp/vibester/fragment/SearchUserFragment.kt b/app/src/main/java/ch/sdp/vibester/fragment/SearchUserFragment.kt index 446413055..e2394114d 100644 --- a/app/src/main/java/ch/sdp/vibester/fragment/SearchUserFragment.kt +++ b/app/src/main/java/ch/sdp/vibester/fragment/SearchUserFragment.kt @@ -14,6 +14,7 @@ import ch.sdp.vibester.activity.PublicProfileActivity import ch.sdp.vibester.auth.FireBaseAuthenticator import ch.sdp.vibester.database.DataGetter import ch.sdp.vibester.database.ImageGetter +import ch.sdp.vibester.helper.Helper import ch.sdp.vibester.helper.IntentSwitcher import ch.sdp.vibester.helper.ViewModel import ch.sdp.vibester.user.OnItemClickListener @@ -113,7 +114,8 @@ class SearchUserFragment : Fragment(R.layout.fragment_layout_search_user), OnIte } override fun onItemClick(position: Int) { - val extras = mapOf(Pair("UserId", users[position].uid), Pair("ScoresOrFollowing", R.string.profile_following.toString())) + //val extras = mapOf(Pair("UserId", users[position].uid), Pair("ScoresOrFollowing", R.string.profile_following.toString())) + val extras = Helper().goToPlayerProfileWithSection(users[position].uid, false) IntentSwitcher.switch(vmSearchUser.ctx, PublicProfileActivity::class.java, extras) } } diff --git a/app/src/main/java/ch/sdp/vibester/helper/Helper.kt b/app/src/main/java/ch/sdp/vibester/helper/Helper.kt index 6e620bd2f..4237ae4d3 100644 --- a/app/src/main/java/ch/sdp/vibester/helper/Helper.kt +++ b/app/src/main/java/ch/sdp/vibester/helper/Helper.kt @@ -5,12 +5,14 @@ import android.graphics.Bitmap import android.net.Uri import android.view.View import android.widget.ImageView +import ch.sdp.vibester.R import ch.sdp.vibester.activity.MainActivity import ch.sdp.vibester.api.BitmapGetterApi import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async import kotlinx.coroutines.launch +import java.io.Serializable import java.util.concurrent.TimeUnit class Helper { @@ -49,4 +51,13 @@ class Helper { IntentSwitcher.switch(ctx, MainActivity::class.java) } } + + /** + * @param uid uid of the player + * @param scoresOrFollowing True goes scores False goes to following + */ + fun goToPlayerProfileWithSection(uid: String, scoresOrFollowing: Boolean): Map { + val section = if (scoresOrFollowing) R.string.profile_scores else R.string.profile_following + return mapOf(Pair("UserId", uid), Pair("ScoresOrFollowing", section.toString())) + } } \ No newline at end of file From 95050e71dcb5e75ce3b3c4cc966edc952766b9a1 Mon Sep 17 00:00:00 2001 From: jiabaow Date: Tue, 7 Jun 2022 16:38:14 +0200 Subject: [PATCH 02/22] clean up --- .../main/java/ch/sdp/vibester/fragment/ScoreBoardFragment.kt | 4 +--- .../main/java/ch/sdp/vibester/fragment/SearchUserFragment.kt | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/app/src/main/java/ch/sdp/vibester/fragment/ScoreBoardFragment.kt b/app/src/main/java/ch/sdp/vibester/fragment/ScoreBoardFragment.kt index f9c50159f..e52f47e89 100644 --- a/app/src/main/java/ch/sdp/vibester/fragment/ScoreBoardFragment.kt +++ b/app/src/main/java/ch/sdp/vibester/fragment/ScoreBoardFragment.kt @@ -112,9 +112,7 @@ class ScoreBoardFragment : Fragment(R.layout.fragment_layout_scoreboard), OnItem */ override fun onItemClick(position: Int) { val playerId = players[position].uid - //mapOf(Pair("UserId", players[position].uid), Pair("ScoresOrFollowing", R.string.profile_scores.toString())) - IntentSwitcher.switch(vmScoreBoard.ctx, - PublicProfileActivity::class.java, + IntentSwitcher.switch(vmScoreBoard.ctx, PublicProfileActivity::class.java, Helper().goToPlayerProfileWithSection(playerId, true)) } diff --git a/app/src/main/java/ch/sdp/vibester/fragment/SearchUserFragment.kt b/app/src/main/java/ch/sdp/vibester/fragment/SearchUserFragment.kt index e2394114d..2d9558e1c 100644 --- a/app/src/main/java/ch/sdp/vibester/fragment/SearchUserFragment.kt +++ b/app/src/main/java/ch/sdp/vibester/fragment/SearchUserFragment.kt @@ -114,7 +114,6 @@ class SearchUserFragment : Fragment(R.layout.fragment_layout_search_user), OnIte } override fun onItemClick(position: Int) { - //val extras = mapOf(Pair("UserId", users[position].uid), Pair("ScoresOrFollowing", R.string.profile_following.toString())) val extras = Helper().goToPlayerProfileWithSection(users[position].uid, false) IntentSwitcher.switch(vmSearchUser.ctx, PublicProfileActivity::class.java, extras) } From ad15012c353b5b92f111b50b551b17abc59e2363 Mon Sep 17 00:00:00 2001 From: jiabaow Date: Tue, 7 Jun 2022 16:39:45 +0200 Subject: [PATCH 03/22] change var to val in OfflineSongList.kt --- app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt b/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt index f5dd36b26..a7177a64f 100644 --- a/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt +++ b/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt @@ -31,18 +31,17 @@ class OfflineSongList(externalDir: File) { * Saves the list of songs in songList */ private fun fillList() { - var records = File(externals, "records.txt") - + val records = File(externals, "records.txt") if (!records.exists() || records.length() == 0L) { //There are no downloaded songs, keep the song list empty emptySongs = true } else { - var reader = BufferedReader(FileReader(records)) + val reader = BufferedReader(FileReader(records)) var currentLine = reader.readLine() while (currentLine != null) { - var trimmed = currentLine.trim() + val trimmed = currentLine.trim() if (trimmed.isNotEmpty()) { val split = trimmed.split("-") if (split.size == 2) { From 1e42393812d15ddc3bc7e47446306097b12c6b8c Mon Sep 17 00:00:00 2001 From: jiabaow Date: Tue, 7 Jun 2022 16:44:55 +0200 Subject: [PATCH 04/22] refactor getShuffledList of songList --- app/src/main/java/ch/sdp/vibester/helper/Helper.kt | 4 ++++ app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt | 6 ++---- app/src/main/java/ch/sdp/vibester/model/SongList.kt | 3 ++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/ch/sdp/vibester/helper/Helper.kt b/app/src/main/java/ch/sdp/vibester/helper/Helper.kt index 4237ae4d3..0202ead44 100644 --- a/app/src/main/java/ch/sdp/vibester/helper/Helper.kt +++ b/app/src/main/java/ch/sdp/vibester/helper/Helper.kt @@ -60,4 +60,8 @@ class Helper { val section = if (scoresOrFollowing) R.string.profile_scores else R.string.profile_following return mapOf(Pair("UserId", uid), Pair("ScoresOrFollowing", section.toString())) } + + fun getShuffledList(songList: MutableList>): MutableList> { + return songList.asSequence().shuffled().toMutableList() + } } \ No newline at end of file diff --git a/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt b/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt index a7177a64f..63742e81a 100644 --- a/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt +++ b/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt @@ -1,8 +1,6 @@ package ch.sdp.vibester.model -import android.content.Context -import android.os.Environment -import ch.sdp.vibester.api.LastfmMethod +import ch.sdp.vibester.helper.Helper import java.io.BufferedReader import java.io.File import java.io.FileReader @@ -68,7 +66,7 @@ class OfflineSongList(externalDir: File) { * @return MutableList> of type Pair("$songName", "$artistName") */ fun getShuffledDownloadedSongList(): MutableList> { - return songList.asSequence().shuffled().toMutableList() + return Helper().getShuffledList(songList) } /** diff --git a/app/src/main/java/ch/sdp/vibester/model/SongList.kt b/app/src/main/java/ch/sdp/vibester/model/SongList.kt index 93c9a8b9d..940535074 100644 --- a/app/src/main/java/ch/sdp/vibester/model/SongList.kt +++ b/app/src/main/java/ch/sdp/vibester/model/SongList.kt @@ -1,6 +1,7 @@ package ch.sdp.vibester.model import ch.sdp.vibester.api.LastfmMethod +import ch.sdp.vibester.helper.Helper import org.json.JSONArray import org.json.JSONObject @@ -70,7 +71,7 @@ class SongList(jsonMeta: String, method: String) { * @return MutableList> of type Pair("$songName", "$artistName") */ fun getShuffledSongList(): MutableList> { - return songList.asSequence().shuffled().toMutableList() + return Helper().getShuffledList(songList) } /** From a017886847725590237661353d04ca237962c3d9 Mon Sep 17 00:00:00 2001 From: jiabaow Date: Tue, 7 Jun 2022 16:47:08 +0200 Subject: [PATCH 05/22] clean doc --- app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt b/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt index 63742e81a..245e788a0 100644 --- a/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt +++ b/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt @@ -8,8 +8,6 @@ import java.io.FileReader /** * Process the fetched data from the external storage of the app. * Mainly, it creates a list of songs in the form Pair("$songName", "$artistName") - * @param ctx: Context of the caller, to fetch the downloaded folder - * @param method: Chosen playlist type */ class OfflineSongList(externalDir: File) { private var songList = mutableListOf>() From 91ba5a9965941e12ef8977b39f743a26796fa88d Mon Sep 17 00:00:00 2001 From: jiabaow Date: Tue, 7 Jun 2022 16:47:31 +0200 Subject: [PATCH 06/22] rename OfflineSongList.kt --- .../main/java/ch/sdp/vibester/model/OfflineSongList.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt b/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt index 245e788a0..9b9fa8e0c 100644 --- a/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt +++ b/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt @@ -10,7 +10,7 @@ import java.io.FileReader * Mainly, it creates a list of songs in the form Pair("$songName", "$artistName") */ class OfflineSongList(externalDir: File) { - private var songList = mutableListOf>() + private var offlineSongList = mutableListOf>() private val page = "1" private val songsPerPage = "100" private val totalPages = "20" @@ -41,7 +41,7 @@ class OfflineSongList(externalDir: File) { if (trimmed.isNotEmpty()) { val split = trimmed.split("-") if (split.size == 2) { - songList.add(Pair(split[0].trim(), split[1].trim())) + offlineSongList.add(Pair(split[0].trim(), split[1].trim())) } } currentLine = reader.readLine() @@ -56,7 +56,7 @@ class OfflineSongList(externalDir: File) { * @return MutableList> of type Pair("$songName", "$artistName") */ fun getSongList(): MutableList> { - return songList + return offlineSongList } /** @@ -64,7 +64,7 @@ class OfflineSongList(externalDir: File) { * @return MutableList> of type Pair("$songName", "$artistName") */ fun getShuffledDownloadedSongList(): MutableList> { - return Helper().getShuffledList(songList) + return Helper().getShuffledList(offlineSongList) } /** From b5b018a16760406e612685dd73500d53acdc450d Mon Sep 17 00:00:00 2001 From: jiabaow Date: Tue, 7 Jun 2022 16:57:52 +0200 Subject: [PATCH 07/22] use super class for SongList.kt --- .../main/java/ch/sdp/vibester/helper/Helper.kt | 4 ---- .../ch/sdp/vibester/model/OfflineSongList.kt | 16 +++------------- .../java/ch/sdp/vibester/model/SongList.kt | 18 +----------------- .../ch/sdp/vibester/model/SuperSongList.kt | 13 +++++++++++++ 4 files changed, 17 insertions(+), 34 deletions(-) create mode 100644 app/src/main/java/ch/sdp/vibester/model/SuperSongList.kt diff --git a/app/src/main/java/ch/sdp/vibester/helper/Helper.kt b/app/src/main/java/ch/sdp/vibester/helper/Helper.kt index 0202ead44..4237ae4d3 100644 --- a/app/src/main/java/ch/sdp/vibester/helper/Helper.kt +++ b/app/src/main/java/ch/sdp/vibester/helper/Helper.kt @@ -60,8 +60,4 @@ class Helper { val section = if (scoresOrFollowing) R.string.profile_scores else R.string.profile_following return mapOf(Pair("UserId", uid), Pair("ScoresOrFollowing", section.toString())) } - - fun getShuffledList(songList: MutableList>): MutableList> { - return songList.asSequence().shuffled().toMutableList() - } } \ No newline at end of file diff --git a/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt b/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt index 9b9fa8e0c..ac21e4492 100644 --- a/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt +++ b/app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt @@ -1,6 +1,5 @@ package ch.sdp.vibester.model -import ch.sdp.vibester.helper.Helper import java.io.BufferedReader import java.io.File import java.io.FileReader @@ -9,8 +8,7 @@ import java.io.FileReader * Process the fetched data from the external storage of the app. * Mainly, it creates a list of songs in the form Pair("$songName", "$artistName") */ -class OfflineSongList(externalDir: File) { - private var offlineSongList = mutableListOf>() +class OfflineSongList(externalDir: File): SuperSongList() { private val page = "1" private val songsPerPage = "100" private val totalPages = "20" @@ -41,7 +39,7 @@ class OfflineSongList(externalDir: File) { if (trimmed.isNotEmpty()) { val split = trimmed.split("-") if (split.size == 2) { - offlineSongList.add(Pair(split[0].trim(), split[1].trim())) + songList.add(Pair(split[0].trim(), split[1].trim())) } } currentLine = reader.readLine() @@ -51,20 +49,12 @@ class OfflineSongList(externalDir: File) { } } - /** - * Getter that return songs for the given tag - * @return MutableList> of type Pair("$songName", "$artistName") - */ - fun getSongList(): MutableList> { - return offlineSongList - } - /** * Getter that return shuffled song list * @return MutableList> of type Pair("$songName", "$artistName") */ fun getShuffledDownloadedSongList(): MutableList> { - return Helper().getShuffledList(offlineSongList) + return super.getShuffledSongList() } /** diff --git a/app/src/main/java/ch/sdp/vibester/model/SongList.kt b/app/src/main/java/ch/sdp/vibester/model/SongList.kt index 940535074..440a7a6bb 100644 --- a/app/src/main/java/ch/sdp/vibester/model/SongList.kt +++ b/app/src/main/java/ch/sdp/vibester/model/SongList.kt @@ -10,8 +10,7 @@ import org.json.JSONObject * Mainly, it creates a list of songs in the form Pair("$songName", "$artistName") * @param jsonMeta: Lastfm fetched data */ -class SongList(jsonMeta: String, method: String) { - private var songList = mutableListOf>() +class SongList(jsonMeta: String, method: String): SuperSongList() { private var page = "" private var songsPerPage = "" private var totalPages = "" @@ -58,21 +57,6 @@ class SongList(jsonMeta: String, method: String) { } } - /** - * Getter that return songs for the given tag - * @return MutableList> of type Pair("$songName", "$artistName") - */ - fun getSongList(): MutableList> { - return songList - } - - /** - * Getter that return shuffled song list - * @return MutableList> of type Pair("$songName", "$artistName") - */ - fun getShuffledSongList(): MutableList> { - return Helper().getShuffledList(songList) - } /** * Getter that return page number from the query diff --git a/app/src/main/java/ch/sdp/vibester/model/SuperSongList.kt b/app/src/main/java/ch/sdp/vibester/model/SuperSongList.kt new file mode 100644 index 000000000..bf5e80032 --- /dev/null +++ b/app/src/main/java/ch/sdp/vibester/model/SuperSongList.kt @@ -0,0 +1,13 @@ +package ch.sdp.vibester.model + +open class SuperSongList { + var songList = mutableListOf>() + + /** + * Getter that return shuffled song list + * @return MutableList> of type Pair("$songName", "$artistName") + */ + fun getShuffledSongList(): MutableList> { + return songList.asSequence().shuffled().toMutableList() + } +} \ No newline at end of file From 6fa51eb97cf68eaf8d9dbc3575c1a826062c7bff Mon Sep 17 00:00:00 2001 From: jiabaow Date: Tue, 7 Jun 2022 19:45:49 +0200 Subject: [PATCH 08/22] update song list tests --- .../java/ch/sdp/vibester/model/OfflineSongListTest.kt | 7 +++---- .../androidTest/java/ch/sdp/vibester/model/SongListTest.kt | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/app/src/androidTest/java/ch/sdp/vibester/model/OfflineSongListTest.kt b/app/src/androidTest/java/ch/sdp/vibester/model/OfflineSongListTest.kt index e7be83155..fea884d8f 100644 --- a/app/src/androidTest/java/ch/sdp/vibester/model/OfflineSongListTest.kt +++ b/app/src/androidTest/java/ch/sdp/vibester/model/OfflineSongListTest.kt @@ -2,7 +2,6 @@ package ch.sdp.vibester.model import android.os.Environment import androidx.test.platform.app.InstrumentationRegistry -import ch.sdp.vibester.api.LastfmMethod import org.junit.Assert import org.junit.Test import java.io.File @@ -24,7 +23,7 @@ class OfflineSongListTest { val totalPages = "20" val totalSongs = "2000" - Assert.assertEquals(inputSongsList, mySongsList.getSongList()) + Assert.assertEquals(inputSongsList, mySongsList.songList) Assert.assertEquals(page, mySongsList.getPage()) Assert.assertEquals(songsPerPage, mySongsList.getSongsPerPage()) Assert.assertEquals(totalPages, mySongsList.getTotalPages()) @@ -35,7 +34,7 @@ class OfflineSongListTest { @Test fun addSpecificSongToList() { val context = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext - var records = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt") + val records = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt") records.createNewFile() records.appendText("bones - imagine dragons\n") @@ -53,7 +52,7 @@ class OfflineSongListTest { val totalPages = "20" val totalSongs = "2000" - Assert.assertEquals(inputSongsList, mySongsList.getSongList()) + Assert.assertEquals(inputSongsList, mySongsList.songList) Assert.assertEquals(page, mySongsList.getPage()) Assert.assertEquals(songsPerPage, mySongsList.getSongsPerPage()) Assert.assertEquals(totalPages, mySongsList.getTotalPages()) diff --git a/app/src/androidTest/java/ch/sdp/vibester/model/SongListTest.kt b/app/src/androidTest/java/ch/sdp/vibester/model/SongListTest.kt index 4f2e8e8a1..3663f0063 100644 --- a/app/src/androidTest/java/ch/sdp/vibester/model/SongListTest.kt +++ b/app/src/androidTest/java/ch/sdp/vibester/model/SongListTest.kt @@ -37,7 +37,7 @@ class SongListTest { assertEquals(songName, mySongsList.getShuffledSongList().get(0).first) - assertEquals(inputSongsList, mySongsList.getSongList()) + assertEquals(inputSongsList, mySongsList.songList) assertEquals(page, mySongsList.getPage()) assertEquals(songsPerPage, mySongsList.getSongsPerPage()) assertEquals(totalPages, mySongsList.getTotalPages()) From e921d8dad7035a1a48e6e33074a39f45ee22acab Mon Sep 17 00:00:00 2001 From: jiabaow Date: Tue, 7 Jun 2022 19:56:48 +0200 Subject: [PATCH 09/22] refactor skip btn in BuzzerScreenActivity.kt --- .../java/ch/sdp/vibester/activity/BuzzerScreenActivity.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/ch/sdp/vibester/activity/BuzzerScreenActivity.kt b/app/src/main/java/ch/sdp/vibester/activity/BuzzerScreenActivity.kt index 826f2723d..7f0e82282 100644 --- a/app/src/main/java/ch/sdp/vibester/activity/BuzzerScreenActivity.kt +++ b/app/src/main/java/ch/sdp/vibester/activity/BuzzerScreenActivity.kt @@ -75,7 +75,8 @@ class BuzzerScreenActivity : GameActivity() { buildScores(getPlayers, allPoints) buildBuzzers(getPlayers, findViewById(R.id.answer)) - findViewById