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

Add logic for offline game - PART 2 #349

Merged
merged 2 commits into from
May 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions app/src/main/java/ch/sdp/vibester/fragment/GameSetupFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class GameSetupFragment : Fragment(R.layout.fragment_layout_game_setup), Adapter

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

vmGameSetup.view = view
vmGameSetup.ctx = view.context

Expand All @@ -69,6 +70,7 @@ class GameSetupFragment : Fragment(R.layout.fragment_layout_game_setup), Adapter
}

private fun setGenreListeners(){
vmGameSetup.view.findViewById<Button>(R.id.offline_game_button).setOnClickListener { chooseGame("local_buzzer", GameManager(), true) }
vmGameSetup.view.findViewById<Button>(R.id.kpopButton).setOnClickListener { chooseGenre(method = LastfmMethod.BY_TAG.method, tag = "kpop", mode = R.string.kpop) }
vmGameSetup.view.findViewById<Button>(R.id.rockButton).setOnClickListener { chooseGenre(method = LastfmMethod.BY_TAG.method, tag = "rock", mode = R.string.rock) }
vmGameSetup.view.findViewById<Button>(R.id.btsButton).setOnClickListener { chooseGenre(method = LastfmMethod.BY_ARTIST.method, artist = "BTS", mode = R.string.gameGenre_bts) }
Expand Down Expand Up @@ -186,16 +188,16 @@ class GameSetupFragment : Fragment(R.layout.fragment_layout_game_setup), Adapter
* 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 = vmGameSetup.ctx.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
if (external != null) {
gameManager.setOffline(external, hasInternet)
gameManager.setOffline(external, !playOffline)
}
gameManager.setGameSongList(Gson().toJson(response.body()), uri.method)
}
Expand All @@ -205,12 +207,16 @@ class GameSetupFragment : Fragment(R.layout.fragment_layout_game_setup), Adapter
/**
* 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 = vmGameSetup.view.findViewById<LinearLayout>(R.id.chooseGame),
visibleView = vmGameSetup.view.findViewById<ConstraintLayout>(R.id.genrePerScoreboard))
if (playOffline) {
chooseGenre(method = LastfmMethod.BY_ARTIST.method, artist = "Personalized", mode = R.string.gameGenre_byArtistSearch, playOffline = playOffline)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it testable?

} else {
Copy link

Choose a reason for hiding this comment

The 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))
}
}

/**
Expand All @@ -220,25 +226,34 @@ class GameSetupFragment : Fragment(R.layout.fragment_layout_game_setup), Adapter
* @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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method chooseGenre has 5 arguments (exceeds 4 allowed). Consider refactoring.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 = vmGameSetup.view.findViewById<LinearLayout>(R.id.genrePerScoreboard),
visibleView = vmGameSetup.view.findViewById<ConstraintLayout>(R.id.chooseSetting)
)
if (playOffline) {
toggleViewsVisibility(
goneView = requireView().findViewById<LinearLayout>(R.id.chooseGame),
visibleView = requireView().findViewById<ConstraintLayout>(R.id.chooseSetting)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it testable?

)
} else{
Copy link

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)
}
}


/**
* Switches between Internet On and Internet Off when the view button is pressed.
* @param view: The button responsible for internet toggling.
Expand Down