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

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximeZmt committed May 26, 2022
2 parents 3703e90 + 4e7d888 commit 1efcd80
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 167 deletions.
153 changes: 68 additions & 85 deletions app/src/main/java/ch/sdp/vibester/fragment/GameSetupFragment.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package ch.sdp.vibester.fragment

import android.content.Context
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
import android.view.View.VISIBLE
import android.view.ViewGroup
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
Expand All @@ -26,6 +22,7 @@ import ch.sdp.vibester.api.LastfmMethod
import ch.sdp.vibester.api.LastfmUri
import ch.sdp.vibester.database.AppPreferences
import ch.sdp.vibester.helper.GameManager
import ch.sdp.vibester.helper.ViewModel
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -37,78 +34,85 @@ import retrofit2.Response
* Game Setup fragment with a button in the bottom navigation.
*/
@AndroidEntryPoint
class GameSetupFragment : Fragment(), View.OnClickListener, AdapterView.OnItemSelectedListener {
class GameSetupFragment : Fragment(R.layout.fragment_layout_game_setup), AdapterView.OnItemSelectedListener {
var difficulty = R.string.GameSetup_easy.toString()
var game = "local_buzzer"
var gameSize = R.string.one.toString()
var searchArtistEditable: Editable? = null
lateinit var gameManager: GameManager
// TODO: OFFLINE
private var hasInternet: Boolean = true
var viewfrag: View? = null

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
viewfrag = inflater.inflate(R.layout.fragment_game_setup, container, false)
return viewfrag!!.rootView
}
private var vmGameSetup = ViewModel()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
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)
view.findViewById<Button>(R.id.online_buzzer_game_button).setOnClickListener(this)
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)
view.findViewById<Button>(R.id.difficulty_proceed).setOnClickListener(this)
view.findViewById<Button>(R.id.game_setup_has_internet).setOnClickListener(this)
updateInternet(view.findViewById<Button>(R.id.game_setup_has_internet))

searchArtistEditable = view.findViewById<EditText>(R.id.searchArtist).text
view.findViewById<Button>(R.id.validateSearch).setOnClickListener(this)

setReturnBtnListener(view)
setSpinnerListener(view, ctx, R.id.difficulty_spinner, R.array.difficulties_name)
setSpinnerListener(view, ctx, R.id.size_spinner, R.array.game_size_options)

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

setGenreListeners()
setGameModeListeners()

vmGameSetup.view.findViewById<Button>(R.id.difficulty_proceed).setOnClickListener{ proceedGame() }
vmGameSetup.view.findViewById<Button>(R.id.game_setup_has_internet).setOnClickListener { updateInternet(vmGameSetup.view.findViewById(R.id.game_setup_has_internet)) }

updateInternet(vmGameSetup.view.findViewById<Button>(R.id.game_setup_has_internet))
searchArtistEditable = vmGameSetup.view.findViewById<EditText>(R.id.searchArtist).text

setReturnBtnListener()
setSpinnerListener(R.id.difficulty_spinner, R.array.difficulties_name)
setSpinnerListener(R.id.size_spinner, R.array.game_size_options)
}

override fun onResume() {
super.onResume()
updateInternet(viewfrag!!.findViewById<Button>(R.id.game_setup_has_internet))
updateInternet(vmGameSetup.view.findViewById<Button>(R.id.game_setup_has_internet))
}

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) }
vmGameSetup.view.findViewById<Button>(R.id.topTracksButton).setOnClickListener { chooseGenre(method = LastfmMethod.BY_CHART.method, mode = R.string.top_tracks) }
vmGameSetup.view.findViewById<Button>(R.id.imagDragonsButton).setOnClickListener{ chooseGenre(method = LastfmMethod.BY_ARTIST.method, artist = "Imagine Dragons", mode = R.string.gameGenre_imagine_dragons) }
vmGameSetup.view.findViewById<Button>(R.id.billieEilishButton).setOnClickListener { chooseGenre(method = LastfmMethod.BY_ARTIST.method, artist = "Billie Eilish", mode = R.string.gameGenre_billie_eilish) }
vmGameSetup.view.findViewById<Button>(R.id.validateSearch).setOnClickListener{ chooseGenre(method = LastfmMethod.BY_ARTIST.method, artist = searchArtistEditable.toString(), mode = R.string.gameGenre_byArtistSearch) }
}

private fun setGameModeListeners() {
vmGameSetup.view.findViewById<Button>(R.id.local_buzzer_game_button)
.setOnClickListener { chooseGame("local_buzzer", GameManager()) }
vmGameSetup.view.findViewById<Button>(R.id.local_typing_game_button)
.setOnClickListener { chooseGame("local_typing", GameManager()) }
vmGameSetup.view.findViewById<Button>(R.id.local_lyrics_game_button)
.setOnClickListener{ chooseGame("local_lyrics", GameManager()) }
vmGameSetup.view.findViewById<Button>(R.id.online_buzzer_game_button)
.setOnClickListener { switchToGameNoParameters(ChoosePartyRoomActivity()) }
}

private fun setReturnBtnListener(view:View) {
view.findViewById<FloatingActionButton>(R.id.gameSetup_returnToMain).setOnClickListener {
if (view.findViewById<LinearLayout>(R.id.genrePerScoreboard).visibility == View.VISIBLE) {
toggleViewsVisibility(goneView = view.findViewById<LinearLayout>(R.id.genrePerScoreboard),
visibleView = view.findViewById<LinearLayout>(R.id.chooseGame))
} else if (view.findViewById<RelativeLayout>(R.id.chooseSetting).visibility == View.VISIBLE) {
toggleViewsVisibility(goneView = view.findViewById<RelativeLayout>(R.id.chooseSetting),
visibleView = view.findViewById<LinearLayout>(R.id.genrePerScoreboard))

private fun setReturnBtnListener() {
vmGameSetup.view.findViewById<FloatingActionButton>(R.id.gameSetup_returnToMain).setOnClickListener {
if (vmGameSetup.view.findViewById<LinearLayout>(R.id.genrePerScoreboard).visibility == VISIBLE) {
toggleViewsVisibility(goneView = vmGameSetup.view.findViewById<LinearLayout>(R.id.genrePerScoreboard),
visibleView = vmGameSetup.view.findViewById<LinearLayout>(R.id.chooseGame))
} else if (vmGameSetup.view.findViewById<RelativeLayout>(R.id.chooseSetting).visibility == VISIBLE) {
toggleViewsVisibility(goneView = vmGameSetup.view.findViewById<RelativeLayout>(R.id.chooseSetting),
visibleView = vmGameSetup.view.findViewById<LinearLayout>(R.id.genrePerScoreboard))
}
}
}

private fun toggleViewsVisibility(goneView: View, visibleView:View) {
goneView.visibility = View.GONE
visibleView.visibility = View.VISIBLE
goneView.visibility = GONE
visibleView.visibility = VISIBLE
}

private fun setSpinnerListener(view: View, ctx: Context, spinnerId: Int, resourceId: Int) {
val spinner: Spinner = view.findViewById(spinnerId)
private fun setSpinnerListener(spinnerId: Int, resourceId: Int) {
val spinner: Spinner = vmGameSetup.view.findViewById(spinnerId)
val adapter = ArrayAdapter.createFromResource(
ctx, resourceId, android.R.layout.simple_spinner_item
vmGameSetup.ctx, resourceId, android.R.layout.simple_spinner_item
)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = adapter
Expand Down Expand Up @@ -190,7 +194,8 @@ class GameSetupFragment : Fragment(), View.OnClickListener, AdapterView.OnItemSe
call.enqueue(object : Callback<Any> {
override fun onFailure(call: Call<Any>, t: Throwable?) {}
override fun onResponse(call: Call<Any>, response: Response<Any>) {
val external = context?.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)

val external = vmGameSetup.ctx.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
if (external != null) {
gameManager.setOffline(external, !playOffline)
}
Expand Down Expand Up @@ -248,55 +253,33 @@ class GameSetupFragment : Fragment(), View.OnClickListener, AdapterView.OnItemSe
}
}

override fun onClick(v: View?) {
when(v!!.id) {
R.id.local_buzzer_game_button -> chooseGame("local_buzzer", GameManager())
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)
R.id.imagDragonsButton -> chooseGenre(method = LastfmMethod.BY_ARTIST.method, artist = "Imagine Dragons", mode = R.string.gameGenre_imagine_dragons)
R.id.rockButton-> chooseGenre(method = LastfmMethod.BY_TAG.method, tag = "rock", mode = R.string.rock)
R.id.topTracksButton -> chooseGenre(method = LastfmMethod.BY_CHART.method, mode = R.string.top_tracks)
R.id.billieEilishButton -> chooseGenre(method = LastfmMethod.BY_ARTIST.method, artist = "Billie Eilish", mode = R.string.gameGenre_billie_eilish)

R.id.validateSearch -> chooseGenre(method = LastfmMethod.BY_ARTIST.method, artist = searchArtistEditable.toString(), mode = R.string.gameGenre_byArtistSearch)

R.id.difficulty_proceed -> proceedGame()

R.id.game_setup_has_internet -> updateInternet(v.findViewById(R.id.game_setup_has_internet))
}
}

/**
* Switches between Internet On and Internet Off when the view button is pressed.
* @param view: The button responsible for internet toggling.
*/
private fun updateInternet(view: View) {
val btn: Button = view as Button
val isConncted = InternetState.getInternetStatus(requireContext())
val isConncted = InternetState.getInternetStatus(vmGameSetup.ctx)
if (isConncted) {
hasInternet = true
btn.text = getString(R.string.GameSetup_internetSwitchOn)
btn.setBackgroundColor(requireContext().getColor(R.color.maximum_yellow_red))
btn.setBackgroundColor(vmGameSetup.ctx.getColor(R.color.maximum_yellow_red))

viewfrag!!.findViewById<LinearLayout>(R.id.horilayer_multi).visibility = VISIBLE
viewfrag!!.findViewById<LinearLayout>(R.id.horilayer_single).visibility = VISIBLE
viewfrag!!.findViewById<TextView>(R.id.singleplayer_game_txt).visibility = VISIBLE
viewfrag!!.findViewById<TextView>(R.id.multiplayer_game_txt).visibility = VISIBLE
vmGameSetup.view.findViewById<LinearLayout>(R.id.horilayer_multi).visibility = VISIBLE
vmGameSetup.view.findViewById<LinearLayout>(R.id.horilayer_single).visibility = VISIBLE
vmGameSetup.view.findViewById<TextView>(R.id.singleplayer_game_txt).visibility = VISIBLE
vmGameSetup.view.findViewById<TextView>(R.id.multiplayer_game_txt).visibility = VISIBLE

} else {
hasInternet = false
btn.text = getString(R.string.GameSetup_internetSwitchOff)
btn.setBackgroundColor(requireContext().getColor(R.color.light_coral))
btn.setBackgroundColor(vmGameSetup.ctx.getColor(R.color.light_coral))

viewfrag!!.findViewById<LinearLayout>(R.id.horilayer_multi).visibility = GONE
viewfrag!!.findViewById<LinearLayout>(R.id.horilayer_single).visibility = GONE
viewfrag!!.findViewById<TextView>(R.id.singleplayer_game_txt).visibility = GONE
viewfrag!!.findViewById<TextView>(R.id.multiplayer_game_txt).visibility = GONE
vmGameSetup.view.findViewById<LinearLayout>(R.id.horilayer_multi).visibility = GONE
vmGameSetup.view.findViewById<LinearLayout>(R.id.horilayer_single).visibility = GONE
vmGameSetup.view.findViewById<TextView>(R.id.singleplayer_game_txt).visibility = GONE
vmGameSetup.view.findViewById<TextView>(R.id.multiplayer_game_txt).visibility = GONE
}
}
}
Loading

0 comments on commit 1efcd80

Please sign in to comment.