Commit b62bcedb authored by fpeterfalvi's avatar fpeterfalvi
Browse files

Server user can change question from a list in GameActivity.

QuestionListFragment added which displays the list of all questions in the database and the server user can select what question to be next in the game. In GameActivity the GameControlFragment and the QuestionListFragment are displayed by a ViewPager so the server user can alternate between them.
parent 4e0694cc
package onlab.kvizserver;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
......@@ -24,10 +25,12 @@ import java.util.List;
import onlab.kvizserver.model.PlayerAnswer;
import onlab.kvizserver.model.Question;
public class GameActivity extends AppCompatActivity implements GameControlFragment.OnFragmentInteractionListener {
public class GameActivity extends AppCompatActivity implements GameControlFragment.OnFragmentInteractionListener,
QuestionListFragment.OnFragmentInteractionListener {
private List<Question> questions;
private int questionIndex = 0;
//private List<Question> questions;
//private int questionIndex = 0;
private Question question = null;
private int numberOfPlayers;
private final List<String> answers = new ArrayList<>();
private PlayerAnswer[] playerAnswers;
......@@ -39,6 +42,7 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
private Handler updateConversationHandler;
private Gson gson;
private GameControlFragment gameControlFragment;
private QuestionListFragment questionListFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -53,12 +57,21 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
numberOfPlayers = ClientHolder.size();
FragmentManager fm = getFragmentManager();
/*
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
gameControlFragment = GameControlFragment.newInstance(numberOfPlayers);
fragmentTransaction.replace(R.id.RootLayout, gameControlFragment);
fragmentTransaction.commit();
*/
gameControlFragment = GameControlFragment.newInstance(numberOfPlayers);
questionListFragment = QuestionListFragment.newInstance(questionFileName);
ViewPager vpProfile = (ViewPager) findViewById(R.id.GameActivityViewPager);
vpProfile.setAdapter(new GameActivityPagerAdapter(getSupportFragmentManager(), gameControlFragment, questionListFragment));
/*
InputStream inputStream = null;
try {
if (questionFileName == null) {
......@@ -70,6 +83,7 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
e.printStackTrace();
}
questions = readQuestions(inputStream);
*/
try {
for (int i=0;i<ClientHolder.size();i++) {
......@@ -182,57 +196,60 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
@Override
public void nextQuestionButtonClicked() {
if (questionIndex < questions.size()) {
Question question = questions.get(questionIndex);
questionString = question.getQuestionText();
gameControlFragment.setCorrectAnswerButtonEnabled(true);
questionListFragment.selecetedQuestionDisplayed();
gameControlFragment.setNextQuestionButtonEnabled(false);
for (int i=0;i<numberOfPlayers;i++) {
playerAnswers[i].setValue(-1);
playerAnswers[i].setWinner(false);
}
questionString = question.getQuestionText();
answers.clear();
answers.addAll(question.getAnswers());
if (question.getType() == Question.MULTIPLE_CHOICE) {
gameControlFragment.displayMultipleChoiceQuestion(
questionString, answers, question.getCorrectAnswer());
} else {
gameControlFragment.displayGuessQuestion(questionString, question.getCorrectAnswer());
for (int i=0;i<numberOfPlayers;i++) {
playerAnswers[i].setValue(-1);
playerAnswers[i].setWinner(false);
}
answers.clear();
answers.addAll(question.getAnswers());
if (question.getType() == Question.MULTIPLE_CHOICE) {
gameControlFragment.displayMultipleChoiceQuestion(
questionString, answers, question.getCorrectAnswer());
} else {
gameControlFragment.displayGuessQuestion(questionString, question.getCorrectAnswer());
}
String correct = question.getCorrectAnswer();
if (question.getType() == Question.MULTIPLE_CHOICE) {
for (int i=0;i<4;i++) {
if (answers.get(i).equals(correct)) {
correctAnswer = i;
}
}
} else {
correctAnswer = Integer.parseInt(correct);
}
String correct = question.getCorrectAnswer();
String playerAnswersString = gson.toJson(playerAnswers);
for (int i=0;i<outputs.size();i++) {
gameControlFragment.displayAnswerOfPlayer(i,"The answer of " + playerAnswers[i].getPlayerName() + ": ");
if (question.getType() == Question.MULTIPLE_CHOICE) {
for (int i=0;i<4;i++) {
if (answers.get(i).equals(correct)) {
correctAnswer = i;
}
}
outputs.get(i).println("questionMC##" + questionString + "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + playerAnswersString
+ "##" + Integer.toString(i) + "##-1");
} else {
correctAnswer = Integer.parseInt(correct);
outputs.get(i).println("questionG##" + questionString + "##-1");
}
}
String playerAnswersString = gson.toJson(playerAnswers);
for (int i=0;i<outputs.size();i++) {
gameControlFragment.displayAnswerOfPlayer(i,"The answer of " + playerAnswers[i].getPlayerName() + ": ");
if (question.getType() == Question.MULTIPLE_CHOICE) {
outputs.get(i).println("questionMC##" + questionString + "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + playerAnswersString
+ "##" + Integer.toString(i) + "##-1");
} else {
outputs.get(i).println("questionG##" + questionString + "##-1");
}
}
questionIndex++;
} else {
/*
gameControlFragment.displayEndOfGame();
for (int i=0;i<outputs.size();i++) {
outputs.get(i).println("message##Játék vége");
}
}
*/
}
@Override
public void correctAnswerButtonClicked() {
gameControlFragment.setCorrectAnswerButtonEnabled(false);
for (int i=0;i<outputs.size();i++) {
if (answers.size() == 4) {
String playerAnswersString = gson.toJson(playerAnswers);
......@@ -248,4 +265,13 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
}
}
@Override
public void setQuestion(Question question) {
this.question = question;
if (question == null) {
gameControlFragment.setNextQuestionButtonEnabled(false);
} else {
gameControlFragment.setNextQuestionButtonEnabled(true);
}
}
}
package onlab.kvizserver;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
class GameActivityPagerAdapter extends FragmentPagerAdapter {
private static final int NUM_PAGES = 2;
private GameControlFragment gameControlFragment;
private QuestionListFragment questionListFragment;
public GameActivityPagerAdapter(FragmentManager fm,
GameControlFragment gameControlFragment, QuestionListFragment questionListFragment) {
super(fm);
this.gameControlFragment = gameControlFragment;
this.questionListFragment = questionListFragment;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return gameControlFragment;
case 1:
return questionListFragment;
default:
return gameControlFragment;
}
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
\ No newline at end of file
package onlab.kvizserver;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
......@@ -128,7 +128,7 @@ public class GameControlFragment extends Fragment {
}
@Override
public void onAttach(Activity context) {
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
......@@ -151,4 +151,12 @@ public class GameControlFragment extends Fragment {
public void correctAnswerButtonClicked();
}
public void setNextQuestionButtonEnabled(boolean enabled) {
nextQuestionBtn.setEnabled(enabled);
}
public void setCorrectAnswerButtonEnabled(boolean enabled) {
correctAnswerButton.setEnabled(enabled);
}
}
package onlab.kvizserver;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import onlab.kvizserver.model.Question;
public class QuestionListAdapter extends RecyclerView.Adapter<QuestionListAdapter.QuestionViewHolder> {
private List<Question> items = new ArrayList<>();
private QuestionListFragment.OnFragmentInteractionListener activity;
private int selected = -1;
private final int lightRed = Color.rgb(247, 203, 172);
private final int lightGreen = Color.rgb(197, 224, 179);
public QuestionListAdapter(QuestionListFragment.OnFragmentInteractionListener activity, List<Question> items) {
this.activity = activity;
this.items = items;
}
@Override
public QuestionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_question_list, parent, false);
QuestionViewHolder viewHolder = new QuestionViewHolder(itemView);
return viewHolder;
}
@Override
public void onBindViewHolder(final QuestionViewHolder holder, final int position) {
final Question item = items.get(position);
holder.indexOfQuestionTextView.setText(Integer.toString(position));
holder.questionTextTextView.setText(item.getQuestionText());
String answersString = item.getCorrectAnswer();
if (item.getType() == Question.MULTIPLE_CHOICE) {
List<String> otherAnswers = item.getOtherAnswers();
answersString += "\n" + otherAnswers.get(0) + "\n" + otherAnswers.get(1) + "\n" + otherAnswers.get(2);
}
holder.answerTextTextView.setText(answersString);
holder.questionItemMainLayout.setBackgroundColor(Color.LTGRAY);
if (item.isEnabled()) {
holder.enableButton.setText("Disable");
} else {
holder.enableButton.setText("Enable");
holder.questionItemMainLayout.setBackgroundColor(lightRed);
}
if (position == selected) {
holder.selectButton.setText("Deselect");
holder.questionItemMainLayout.setBackgroundColor(lightGreen);
} else {
holder.selectButton.setText("Select");
}
holder.selectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// deselect other question if there was selected
if (selected != -1 && selected != position) {
int index = selected;
selected = -1;
notifyItemChanged(index);
}
if (selected == position) {
selected = -1;
notifyItemChanged(position);
activity.setQuestion(null);
} else {
selected = position;
notifyItemChanged(position);
activity.setQuestion(item);
}
}
});
holder.enableButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
item.setEnabled(!item.isEnabled());
notifyItemChanged(position);
}
});
}
@Override
public int getItemCount() {
return items.size();
}
public class QuestionViewHolder extends RecyclerView.ViewHolder {
TextView indexOfQuestionTextView;
TextView questionTextTextView;
TextView answerTextTextView;
Button selectButton;
Button enableButton;
LinearLayout questionItemMainLayout;
public QuestionViewHolder(View itemView) {
super(itemView);
indexOfQuestionTextView = (TextView) itemView.findViewById(R.id.IndexOfQuestionTextView);
questionTextTextView = (TextView) itemView.findViewById(R.id.QuestionTextTextView);
answerTextTextView = (TextView) itemView.findViewById(R.id.AnswerTextTextView);
selectButton = (Button) itemView.findViewById(R.id.SelectButton);
enableButton = (Button) itemView.findViewById(R.id.EnableButton);
questionItemMainLayout = (LinearLayout) itemView.findViewById(R.id.QuestionItemMainLayout);
}
}
public void deselectActualQuestion() {
int index = selected;
if (index != -1) {
selected = -1;
items.get(index).setEnabled(false);
notifyItemChanged(index);
}
}
}
package onlab.kvizserver;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import onlab.kvizserver.model.Question;
import static onlab.kvizserver.GameActivity.readQuestions;
public class QuestionListFragment extends Fragment {
private static final String QUESTION_FILE_NAME = "QUESTION_FILE_NAME";
private String questionFileName;
private OnFragmentInteractionListener mListener;
private RecyclerView recyclerView;
private QuestionListAdapter adapter;
private List<Question> questions;
public QuestionListFragment() {
// Required empty public constructor
}
public static QuestionListFragment newInstance(String questionFileName) {
QuestionListFragment fragment = new QuestionListFragment();
Bundle args = new Bundle();
args.putString(QUESTION_FILE_NAME, questionFileName);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
questionFileName = getArguments().getString(QUESTION_FILE_NAME);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_question_list, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
InputStream inputStream = null;
try {
if (questionFileName == null) {
inputStream = getContext().getAssets().open("multiple.txt");
} else {
inputStream = new FileInputStream(new File(questionFileName));
}
} catch (IOException e) {
e.printStackTrace();
}
questions = readQuestions(inputStream);
initRecyclerView();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
public void setQuestion(Question question);
}
private void initRecyclerView() {
recyclerView = (RecyclerView) getActivity().findViewById(R.id.QuestionListRecyclerView);
adapter = new QuestionListAdapter(mListener, questions);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(adapter);
}
public void selecetedQuestionDisplayed() {
adapter.deselectActualQuestion();
}
}
......@@ -40,10 +40,18 @@ public class Question {
return list;
}
public List<String> getOtherAnswers() {
return otherAnswers;
}
public String getTopic() {
return topic;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public boolean isEnabled() {
return enabled;
}
......
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="onlab.kvizserver.GameActivity">
<LinearLayout
<android.support.v4.view.ViewPager
android:id="@+id/GameActivityViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/RootLayout">
android:layout_height="match_parent" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</LinearLayout>
\ No newline at end of file
......@@ -59,6 +59,7 @@
android:layout_height="wrap_content"
android:text="Next Question"
android:id="@+id/NextQuestion"
android:enabled="false"
android:textSize="20sp" />
<Button
......@@ -66,6 +67,7 @@
android:layout_height="wrap_content"
android:text="Show correct answer to players"
android:id="@+id/CorrectAnswer"
android:enabled="false"
android:textSize="20sp" />
</LinearLayout>
......
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".QuestionListFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/GenerateButton"
android:enabled="false"
android:text="Generate new list"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/ExportButton"
android:enabled="false"
android:text="Export database to file"/>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"