/** * Handles button clicks * * @return v Clicked view */ @Override public void onClick(View v) { if (tournament != null) { switch (v.getId()) { case R.id.plusButton: if (activeRound < tournament.getMaxRounds()) { activeRound++; clearUI(); containerIDs.clear(); awayScoreIDs.clear(); homeScoreIDs.clear(); createMatchTable(activeRound); roundTextView.setText("Round: " + activeRound); } break; case R.id.minusButton: if (activeRound > 1) { activeRound--; clearUI(); awayScoreIDs.clear(); homeScoreIDs.clear(); containerIDs.clear(); createMatchTable(activeRound); roundTextView.setText("Round: " + activeRound); } break; case R.id.saveRoundButton: tournament.getRounds()[activeRound - 1].setGames(saveRound(activeRound)); comm.respond(tournament, 2, false); break; } } }
@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (savedInstanceState != null) { DataSource ds = new DataSource(getActivity()); try { ds.open(); } catch (SQLException e) { e.printStackTrace(); } // activeRound = savedInstanceState.getInt("activeRound"); long tournamentId = savedInstanceState.getLong("tournamentId"); Log.d("ScoresFragment", "onActivityCreated " + tournamentId + ""); if (tournamentId != TEMP_ID) { Loader loader = new Loader(getActivity().getApplicationContext()); this.tournament = loader.getTournamentAndRounds(tournamentId); if (tournament.getRounds().length > 0) { createMatchTable(1); roundTextView.setText("Round: " + 1); } else { Log.d("ScoresFragment", "getRounds = 0"); } } else { Log.d("ScoresFragment", "temp id"); } } }
/** * Saves the current active round to the database. * * @param activeRound Current active round * @return List of Game objects */ public Game[] saveRound(int activeRound) { Game[] games = tournament.getRounds()[(activeRound) - 1].getGames(); Round round = tournament.getRounds()[activeRound - 1]; round.setRoundSaved(true); DataSource ds = new DataSource(getActivity()); for (int i = 0; i < games.length; i++) { EditText homeScore = (EditText) rootView.findViewById(homeScoreIDs.get(i)); EditText awayScore = (EditText) rootView.findViewById(awayScoreIDs.get(i)); games[i].setHomeScore(Integer.parseInt(homeScore.getText().toString())); games[i].setAwayScore(Integer.parseInt(awayScore.getText().toString())); try { ds.open(); } catch (SQLException e) { e.printStackTrace(); } Log.d("ScoresFragment", games[i].getId() + " GAME ID"); ds.updateMatch(games[i]); ds.close(); } return games; }
/** * Method communicates with MainActivity and is used to initialize and create a match table for * the tournament * * @param newTournament Tournament to use for match table generation. * @param isNew True if a tournament is new, false if not */ public void updateUI(Tournament newTournament, boolean isNew) { this.tournament = newTournament; activeRound = 1; rounds = tournament.getRounds(); if (!isNew) { clearUI(); containerIDs.clear(); awayScoreIDs.clear(); homeScoreIDs.clear(); saveAndInitAllRounds(); } clearUI(); containerIDs.clear(); awayScoreIDs.clear(); homeScoreIDs.clear(); createMatchTable(activeRound); roundTextView.setText("Round: " + activeRound); }
/** * Generates match tables of the currently active tournament * * @param roundNumber Current round number */ public void createMatchTable(int roundNumber) { rounds = tournament.getRounds(); Log.d("ScoresFragment", rounds[0].getGames()[0].getAwayPlayer().getName()); Game[] games = rounds[roundNumber - 1].getGames(); LinearLayout linearContainerTop = new LinearLayout(getActivity().getBaseContext()); LinearLayout.LayoutParams paramsContainerTop = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); linearContainerTop.setLayoutParams(paramsContainerTop); containerId = containerId + multiplier; containerIDs.add(containerId); linearContainerTop.setId(containerId); linearContainerTop.setBackgroundColor(getResources().getColor(R.color.darkgray)); linearContainerTop.setOrientation(LinearLayout.VERTICAL); for (int i = 0; i < games.length; i++) { int id = i; Player homePlayer = games[i].getHomePlayer(); Player awayPlayer = games[i].getAwayPlayer(); LinearLayout linearContainer = new LinearLayout(getActivity().getBaseContext()); LinearLayout.LayoutParams paramsContainer = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); linearContainer.setLayoutParams(paramsContainer); linearContainer.setId(6); linearContainer.setBackgroundColor(getResources().getColor(R.color.darkgray)); linearContainer.setOrientation(LinearLayout.HORIZONTAL); TextView homeText = new TextView(getActivity().getBaseContext()); homeText.setId(1); LinearLayout.LayoutParams paramsHomeText = new LinearLayout.LayoutParams(0, 130, 0.5f); homeText.setGravity(Gravity.CENTER); paramsHomeText.setMargins(10, 10, 10, 0); homeText.setLayoutParams(paramsHomeText); homeText.setText(homePlayer.getName()); homeText.setTextSize(18); TextView midText = new TextView(getActivity().getBaseContext()); LinearLayout.LayoutParams paramsMidText = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.1f); paramsMidText.setMargins(10, 10, 10, 0); midText.setGravity(Gravity.CENTER); midText.setLayoutParams(paramsMidText); midText.setId(3); midText.setText("-"); LinearLayout.LayoutParams paramsAwayText = new LinearLayout.LayoutParams(0, 130, 0.5f); TextView awayText = new TextView(getActivity().getBaseContext()); awayText.setId(2); awayText.setText(awayPlayer.getName()); awayText.setTextSize(18); paramsAwayText.setMargins(10, 10, 10, 0); awayText.setLayoutParams(paramsAwayText); awayText.setGravity(Gravity.CENTER); EditText homeScore = new EditText(getActivity().getBaseContext()); homeScoreId = homeScoreId + multiplier; homeScoreIDs.add(homeScoreId); LinearLayout.LayoutParams paramsHomeScore = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.2f); homeScore.setGravity(Gravity.CENTER); paramsHomeScore.setMargins(10, 10, 10, 0); homeScore.setLayoutParams(paramsHomeScore); homeScore.setText(Integer.toString(games[i].getHomeScore())); homeScore.setInputType(InputType.TYPE_CLASS_NUMBER); homeScore.setId(homeScoreId); EditText awayScore = new EditText(getActivity().getBaseContext()); awayScoreId = awayScoreId + multiplier; awayScoreIDs.add(awayScoreId); LinearLayout.LayoutParams paramsAwayScore = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.2f); awayScore.setGravity(Gravity.CENTER); paramsAwayScore.setMargins(10, 10, 10, 0); awayScore.setLayoutParams(paramsAwayScore); awayScore.setInputType(InputType.TYPE_CLASS_NUMBER); awayScore.setText(Integer.toString(games[i].getAwayScore())); awayScore.setId(awayScoreId); linearContainer.addView(homeText); linearContainer.addView(homeScore); linearContainer.addView(midText); linearContainer.addView(awayScore); linearContainer.addView(awayText); linearContainerTop.addView(linearContainer); } layout.addView(linearContainerTop); }
/** Saves status of all rounds to be saved */ public void saveAndInitAllRounds() { for (int i = 0; i < tournament.getRounds().length; i++) { tournament.getRounds()[i].setRoundSaved(true); } }