-
Notifications
You must be signed in to change notification settings - Fork 0
Add logic for offline game - PART 2 #349
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import android.content.Intent | |
import android.os.Bundle | ||
import android.os.Environment | ||
import android.text.Editable | ||
import android.util.Log | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.View.GONE | ||
|
@@ -58,6 +59,7 @@ class GameSetupFragment : Fragment(), View.OnClickListener, AdapterView.OnItemSe | |
super.onViewCreated(view, savedInstanceState) | ||
val ctx = view.context | ||
|
||
view.findViewById<Button>(R.id.offline_game_button).setOnClickListener(this) | ||
view.findViewById<Button>(R.id.local_buzzer_game_button).setOnClickListener(this) | ||
view.findViewById<Button>(R.id.local_typing_game_button).setOnClickListener(this) | ||
view.findViewById<Button>(R.id.local_lyrics_game_button).setOnClickListener(this) | ||
|
@@ -182,16 +184,15 @@ class GameSetupFragment : Fragment(), View.OnClickListener, AdapterView.OnItemSe | |
* Fetch data from Lastfm and set song list in a GameManager | ||
* @param uri: contains all Lastfm query parameters (method, artist, tag) | ||
*/ | ||
private fun setGameSongList(uri: LastfmUri) { | ||
private fun setGameSongList(uri: LastfmUri, playOffline: Boolean = false) { | ||
val service = LastfmApiInterface.createLastfmService() | ||
val call = service.getSongList(uri.convertToHashmap()) | ||
call.enqueue(object : Callback<Any> { | ||
override fun onFailure(call: Call<Any>, t: Throwable?) {} | ||
override fun onResponse(call: Call<Any>, response: Response<Any>) { | ||
// TODO: OFFLINE | ||
val external = context?.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) | ||
if (external != null) { | ||
gameManager.setOffline(external, hasInternet) | ||
gameManager.setOffline(external, !playOffline) | ||
} | ||
gameManager.setGameSongList(Gson().toJson(response.body()), uri.method) | ||
} | ||
|
@@ -201,12 +202,16 @@ class GameSetupFragment : Fragment(), View.OnClickListener, AdapterView.OnItemSe | |
/** | ||
* Set game mode. Set appropriate GameManager. | ||
*/ | ||
private fun chooseGame(gameMode: String, gameManager: GameManager){ | ||
private fun chooseGame(gameMode: String, gameManager: GameManager, playOffline: Boolean = false){ | ||
this.game = gameMode | ||
this.gameManager = gameManager | ||
|
||
toggleViewsVisibility(goneView = requireView().findViewById<LinearLayout>(R.id.chooseGame), | ||
visibleView = requireView().findViewById<ConstraintLayout>(R.id.genrePerScoreboard)) | ||
if (playOffline) { | ||
chooseGenre(method = LastfmMethod.BY_ARTIST.method, artist = "Personalized", mode = R.string.gameGenre_byArtistSearch, playOffline = playOffline) | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 2 locations. Consider refactoring. |
||
toggleViewsVisibility(goneView = requireView().findViewById<LinearLayout>(R.id.chooseGame), | ||
visibleView = requireView().findViewById<ConstraintLayout>(R.id.genrePerScoreboard)) | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -216,22 +221,30 @@ class GameSetupFragment : Fragment(), View.OnClickListener, AdapterView.OnItemSe | |
* @param tag: tag (genre) to fetch songs from: used in BY_TAG method | ||
* @param mode: official game mode name | ||
*/ | ||
private fun chooseGenre(method: String = "", artist: String = "", tag: String = "", mode: Int = 0) { | ||
private fun chooseGenre(method: String = "", artist: String = "", tag: String = "", mode: Int = 0, playOffline: Boolean = false) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be more useful to make playOffline a private field with a setter function to avoid this refactoring issue? |
||
if (artist != "") { | ||
val uri = LastfmUri() | ||
|
||
uri.method = method | ||
uri.artist = artist | ||
uri.tag = tag | ||
|
||
toggleViewsVisibility( | ||
goneView = requireView().findViewById<LinearLayout>(R.id.genrePerScoreboard), | ||
visibleView = requireView().findViewById<ConstraintLayout>(R.id.chooseSetting) | ||
) | ||
if (playOffline) { | ||
toggleViewsVisibility( | ||
goneView = requireView().findViewById<LinearLayout>(R.id.chooseGame), | ||
visibleView = requireView().findViewById<ConstraintLayout>(R.id.chooseSetting) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it testable? |
||
) | ||
} else{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 2 locations. Consider refactoring. |
||
toggleViewsVisibility( | ||
goneView = requireView().findViewById<LinearLayout>(R.id.genrePerScoreboard), | ||
visibleView = requireView().findViewById<ConstraintLayout>(R.id.chooseSetting) | ||
Comment on lines
+244
to
+245
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the CodeClimate issue, we can make the toggleViewsVisibility function take just a single boolean if the two arguments are always the same but switched around(i.e instead of tVV(a, b) you call tVV(b, a)) so the duplication can be fixed. Not an urgent thing, but could be useful if we want to refactor in the future |
||
) | ||
} | ||
|
||
|
||
gameManager.gameMode = getString(mode) | ||
AppPreferences.setStr(getString(R.string.preferences_game_genre), getString(mode)) | ||
setGameSongList(uri) | ||
setGameSongList(uri, playOffline) | ||
} | ||
} | ||
|
||
|
@@ -241,6 +254,7 @@ class GameSetupFragment : Fragment(), View.OnClickListener, AdapterView.OnItemSe | |
R.id.local_typing_game_button -> chooseGame("local_typing", GameManager()) | ||
R.id.local_lyrics_game_button -> chooseGame("local_lyrics", GameManager()) | ||
R.id.online_buzzer_game_button -> switchToGameNoParameters(ChoosePartyRoomActivity()) | ||
R.id.offline_game_button -> chooseGame("local_buzzer", GameManager(), true) | ||
|
||
R.id.btsButton -> chooseGenre(method = LastfmMethod.BY_ARTIST.method, artist = "BTS", mode = R.string.gameGenre_bts) | ||
R.id.kpopButton -> chooseGenre(method = LastfmMethod.BY_TAG.method, tag = "kpop", mode = R.string.kpop) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it testable?