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

Commit

Permalink
add doc and reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
jiabaow committed Mar 24, 2022
1 parent aa7fe7b commit 5c00063
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class GenreTemporaryTest {
}

@Test
fun GenreLayoutIsDisplayed() {
fun genreLayoutIsDisplayed() {
onView(withId(R.id.rockButton)).check(matches(isDisplayed()))
onView(withId(R.id.kpopButton)).check(matches(isDisplayed()))
onView(withId(R.id.topTracksButton)).check(matches(isDisplayed()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,17 @@ class LyricsBelongGameActivity : AppCompatActivity() {
}
}

/**
* display the given String in lyricResult
*/
private fun updateSpeechResult(speechInput: String) {
findViewById<TextView>(R.id.lyricResult).text = speechInput
findViewById<Button>(R.id.lyricMatchButton).visibility = View.VISIBLE
}

/**
* check if the given string belongs to the lyrics of the song
*/
private fun checkLyrics(lyricToBeCheck: String) {
val service = LyricsOVHApiInterface.create()
val call = service.getLyrics("Imagine Dragons", "Thunder")
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/ch/sdp/vibester/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class MainActivity : AppCompatActivity() {
val genreIntent = Intent(this, GenreTemporary::class.java)
btnGenre.setOnClickListener {
startActivity(genreIntent)

}

}
Expand Down
16 changes: 8 additions & 8 deletions app/src/main/java/ch/sdp/vibester/activity/TypingGameActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,27 @@ class TypingGameActivity : AppCompatActivity() {
/**
* Print Toast message to announce the user if he wons or not
*/
private fun hasWon(ctx: Context, hasWon: Boolean, itwas: Song){
private fun hasWon(ctx: Context, hasWon: Boolean, itWas: Song){
if(hasWon){
Toast.makeText(ctx,"Well Done!",Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(ctx,"Sadly you're wrong, it was: "+itwas.getTrackName()+" by "+itwas.getArtistName(),Toast.LENGTH_SHORT).show()
Toast.makeText(ctx,"Sadly you're wrong, it was: "+itWas.getTrackName()+" by "+itWas.getArtistName(),Toast.LENGTH_SHORT).show()
}
}

/**
* Generate a change of intent at the end of the game
*/
fun intentGen(ctx: Context, choosenSong: Song?, playedSong: Song):Intent{
fun intentGen(ctx: Context, chosenSong: Song?, playedSong: Song):Intent{
val newIntent = Intent(ctx, TypingGameActivity::class.java)
newIntent.putExtra("song", playedSong)
newIntent.putExtra("isPlaying", false)
if(choosenSong != null && choosenSong.getTrackName() == playedSong.getTrackName() && choosenSong.getArtistName() == playedSong.getArtistName()){
if(chosenSong != null && chosenSong.getTrackName() == playedSong.getTrackName() && chosenSong.getArtistName() == playedSong.getArtistName()){
newIntent.putExtra("hasWon", true)
}else{
newIntent.putExtra("hasWon", false)
}
newIntent.setFlags(FLAG_ACTIVITY_NEW_TASK)
newIntent.flags = FLAG_ACTIVITY_NEW_TASK
return newIntent
}

Expand All @@ -82,7 +82,7 @@ class TypingGameActivity : AppCompatActivity() {
frameLay.addView(linLay)
guessLayout.addView(frameLay)

//Create the Listener that is executed if we click on the framelayer
//Create the Listener that is executed if we click on the frameLayer
frameLay.setOnClickListener {
frameLay.setBackgroundColor(getColor(ctx, R.color.tiffany_blue))
guessLayout.removeAllViews()
Expand Down Expand Up @@ -124,7 +124,7 @@ class TypingGameActivity : AppCompatActivity() {
*/
fun generateText(txt: String, ctx: Context): TextView {
val txtView = TextView(ctx)
txtView.setText(txt)
txtView.text = txt
txtView.gravity = Gravity.CENTER
txtView.minHeight= 200
txtView.textSize = 20F
Expand Down Expand Up @@ -178,7 +178,7 @@ class TypingGameActivity : AppCompatActivity() {
if(!isPlaying){
//Is the activity showing the result
hasWon(this, hasWon, playableSong)
inputTxt.setKeyListener(null)
inputTxt.keyListener = null
}else{
//Is the activity playing music
mediaPlayer = AudioPlayer.playAudio(playableSong.getPreviewUrl())
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/ch/sdp/vibester/api/ItunesMusicApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.util.concurrent.CompletableFuture
class ItunesMusicApi private constructor(){

companion object{
private val LOOKUP_URL_BASE = "https://itunes.apple.com/search?media=music&"
private const val LOOKUP_URL_BASE = "https://itunes.apple.com/search?media=music&"

/**
* Given a String query it will provide the music url preview
Expand All @@ -19,10 +19,10 @@ class ItunesMusicApi private constructor(){
* @return CompletableFuture<String> that contains the result of the query
*/
fun querySong(query: String, okHttp: OkHttpClient, limit: Int, baseUrl: String = LOOKUP_URL_BASE): CompletableFuture<String> {
val buildedUrl = baseUrl+"limit="+limit+"&term="+query.replace(' ', '+')
val req = okhttp3.Request.Builder().url(buildedUrl).build()
val builtUrl = baseUrl+"limit="+limit+"&term="+query.replace(' ', '+')
val req = okhttp3.Request.Builder().url(builtUrl).build()

var retFuture = CompletableFuture<String>()
val retFuture = CompletableFuture<String>()

okHttp.newCall(req).enqueue(ApiCallback(retFuture))

Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/ch/sdp/vibester/api/LastfmApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import java.util.concurrent.CompletableFuture
class LastfmApi private constructor(){

companion object{
private val API_KEY = "52bfdc690dd8373bba5351571a01ac14"
private val LOOKUP_URL_BASE ="https://ws.audioscrobbler.com/2.0/"
private const val API_KEY = "52bfdc690dd8373bba5351571a01ac14"
private const val LOOKUP_URL_BASE ="https://ws.audioscrobbler.com/2.0/"

/**
* Provide a list of songs by given tag or chart
Expand All @@ -22,7 +22,7 @@ class LastfmApi private constructor(){
* @param baseUrl: baseUrl if changed
*/
fun querySongList(okHttp: OkHttpClient, params:LastfmUri, baseUrl: String = LOOKUP_URL_BASE): CompletableFuture<String> {
var builtUri: Uri= Uri.parse(baseUrl)
val builtUri: Uri= Uri.parse(baseUrl)
.buildUpon()
.appendQueryParameter("method", params.method)
.appendQueryParameter("api_key", API_KEY)
Expand All @@ -34,7 +34,7 @@ class LastfmApi private constructor(){
val uri = builtUri.toString()
val req = Request.Builder().url(uri).build()

var retFuture = CompletableFuture<String>()
val retFuture = CompletableFuture<String>()

okHttp.newCall(req).enqueue(ApiCallback(retFuture))

Expand Down
27 changes: 17 additions & 10 deletions app/src/main/java/ch/sdp/vibester/api/LastfmHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,39 @@ class LastfmHelper {

companion object{
private var pageLimitQueryTwo = 200;
private val GAME_SIZE = 10;
private const val GAME_SIZE = 10;

/**
* Function that performs queries to two pages within given tag
* @param method: method to retrieve songs BY_TAG or BY_CHART
* @param tag: tag to retrieve songs. If method BY_CHART is used, tag is empty.
* @return list of songs in a format ("$songName $artistName")
*/
fun getRandomSongList(method: String, tag:String = ""): List<String> {
val firstQuery = SongList(LastfmApi.querySongList(OkHttpClient(),
LastfmUri(method = method, tag = tag)).get())
fun getRandomSongList(method: String, tag: String = ""): List<String> {
val firstQuery = SongList(
LastfmApi.querySongList(
OkHttpClient(),
LastfmUri(method = method, tag = tag)
).get()
)
val firstSongList = firstQuery.getSongList()
val pagesQueryOne = firstQuery.getTotalPages().toInt()

if(pagesQueryOne < pageLimitQueryTwo){
if (pagesQueryOne < pageLimitQueryTwo) {
pageLimitQueryTwo = pagesQueryOne
}

val pageQueryTwo =(2..pageLimitQueryTwo).random()
val secondQuery = SongList(LastfmApi.querySongList(OkHttpClient(),
LastfmUri(method = method, tag = tag, page = pageQueryTwo.toString())).get())
val pageQueryTwo = (2..pageLimitQueryTwo).random()
val secondQuery = SongList(
LastfmApi.querySongList(
OkHttpClient(),
LastfmUri(method = method, tag = tag, page = pageQueryTwo.toString())
).get()
)
val secondSongList = secondQuery.getSongList()

val mergedLists = merge(firstSongList, secondSongList)
var finalList = mergedLists.asSequence().shuffled().take(GAME_SIZE).toList()
return finalList
return mergedLists.asSequence().shuffled().take(GAME_SIZE).toList()
}

fun <T> merge(first: List<T>, second: List<T>): List<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.google.firebase.ktx.Firebase

class FireBaseAuthenticator() {


val auth: FirebaseAuth = Firebase.auth


Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/ch/sdp/vibester/model/Song.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Song(jsonO: JSONObject): Serializable{
val jsonObj = JSONObject(str)
val countResult = jsonObj.getInt("resultCount")
val jsonArray = jsonObj.getJSONArray("results")
for (i in 0..(countResult-1)){
for (i in 0 until countResult){
myList.add(Song(jsonArray.getJSONObject(i)))
}
return myList
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/ch/sdp/vibester/model/SongList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class SongList(jsonMeta: String) {
init {
try {
val jsonObj = JSONObject(jsonMeta)
var jsonRes = jsonObj.getJSONObject("tracks")
var nonFilteredSongs = jsonRes.getJSONArray("track")
val jsonRes = jsonObj.getJSONObject("tracks")
val nonFilteredSongs = jsonRes.getJSONArray("track")
filterSongs(nonFilteredSongs)

var attributes = jsonRes.getJSONObject("@attr")
val attributes = jsonRes.getJSONObject("@attr")
page = attributes.getString("page")
songsPerPage = attributes.getString("perPage")
totalPages = attributes.getString("totalPages")
Expand All @@ -43,7 +43,7 @@ class SongList(jsonMeta: String) {
val songsLength = nonFilteredSongs.length()
var i = 0
while(i < songsLength) {
var songObj = nonFilteredSongs.getJSONObject(i)
val songObj = nonFilteredSongs.getJSONObject(i)
val songName = songObj.getString("name")
val artistDetails = songObj.getJSONObject("artist")
val artistName = artistDetails.getString("name")
Expand Down

0 comments on commit 5c00063

Please sign in to comment.