Skip to content

Commit 59639fb

Browse files
committed
Add starting logic for GameFragment
1 parent 0c328c7 commit 59639fb

File tree

3 files changed

+74
-11
lines changed

3 files changed

+74
-11
lines changed

app/src/main/java/com/tiansirk/countryquiz/ui/GameFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class GameFragment extends Fragment {
3535

3636
/** The interface for communication */
3737
public interface GameFragmentListener {
38-
void onStartGameClicked();
38+
void onSubmitClicked();
3939
}
4040

4141
/** Member vars for game */

app/src/main/java/com/tiansirk/countryquiz/ui/MainActivity.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@
2525
import java.util.List;
2626

2727
public class MainActivity extends AppCompatActivity implements Repository.EntityChangeListener,
28-
WelcomeFragment.WelcomeFragmentListener, MainMenuFragment.MainMenuFragmentListener {
28+
WelcomeFragment.WelcomeFragmentListener, MainMenuFragment.MainMenuFragmentListener, GameFragment.GameFragmentListener {
2929

3030
public static final String USER_PREFERENCES = MainActivity.class.getPackage().getName().concat("_userPrefs");
3131
public static final String KEY_SAVED_USER_NAME = "userName";
3232
public static final String KEY_USER = "user";
3333
public static final String KEY_LEVELS = "levels";
3434
public static final String TAG_WELCOME_FRAGMENT = "welcome_fragment";
3535
public static final String TAG_MAIN_MENU_FRAGMENT = "main_menu_fragment";
36+
public static final String TAG_GAME_FRAGMENT = "game_fragment";
3637
private static final String COLLECTION_NAME = "users";
3738
public static final int LAUNCH_SECOND_ACTIVITY = 1;
3839

39-
4040
private ActivityMainBinding binding;
4141

4242
/** Member vars for the game */
@@ -47,10 +47,10 @@ public class MainActivity extends AppCompatActivity implements Repository.Entity
4747
/** Member vars for fragments of this activity */
4848
private WelcomeFragment welcomeFragment;
4949
private MainMenuFragment mainMenuFragment;
50+
private GameFragment gameFragment;
5051

5152
private Repository mRepository;
5253

53-
5454
@Override
5555
protected void onCreate(Bundle savedInstanceState) {
5656
super.onCreate(savedInstanceState);
@@ -98,6 +98,20 @@ private void initMainMenuFragment(){
9898

9999
}
100100

101+
private void initGameFragment(){
102+
Timber.i("Initializing GameFragment");
103+
Bundle bundle = new Bundle();
104+
bundle.putParcelable(KEY_USER, mUser);
105+
bundle.putParcelableArrayList(KEY_LEVELS, (ArrayList<? extends Parcelable>) mLevels);
106+
FragmentManager fragmentManager = getSupportFragmentManager();
107+
FragmentTransaction ft = fragmentManager.beginTransaction();
108+
gameFragment = new GameFragment();
109+
gameFragment.setArguments(bundle);
110+
ft.replace(R.id.container_game, gameFragment, TAG_GAME_FRAGMENT);
111+
ft.addToBackStack(TAG_GAME_FRAGMENT);
112+
ft.commit();
113+
}
114+
101115
/** This method is defined in the WelcomeFragment to let retrieve data from it */
102116
@Override
103117
public void onSetupFinished(User user, List<Level> levels) {
@@ -111,7 +125,21 @@ public void onSetupFinished(User user, List<Level> levels) {
111125
/** This method is defined in the MainMenuFragment to let retrieve data from it */
112126
@Override
113127
public void onStartGameClicked() {
128+
//todo: start gameFragment with fields
129+
if(!mainMenuFragment.isHidden()) showHideFragment(mainMenuFragment);
130+
initGameFragment();
131+
}
132+
/** This method is defined in the MainMenuFragment to let retrieve data from it */
133+
@Override
134+
public void onLeaderboardClicked() {
135+
//todo: start LeaderBoardActivity with fields
136+
137+
}
114138

139+
/** This method is defined in the GameFragment to let retrieve data from it */
140+
@Override
141+
public void onSubmitClicked() {
142+
//todo: get the Question: points, answered and Level:
115143
}
116144

117145
/** This ends the WelcomeFragment permanently */

app/src/main/java/com/tiansirk/countryquiz/ui/MainMenuFragment.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,27 @@
2828
* Use the {@link MainMenuFragmentListener} interface for communication. */
2929
public class MainMenuFragment extends Fragment {
3030

31-
/** Member vars for views */
31+
/**
32+
* Member vars for views
33+
*/
3234
private FragmentMainMenuBinding binding;
3335

34-
/** Member var for own custom communication listener */
36+
/**
37+
* Member var for own custom communication listener
38+
*/
3539
private MainMenuFragmentListener listener;
3640

37-
/** The interface for communication */
41+
/**
42+
* The interface for communication
43+
*/
3844
public interface MainMenuFragmentListener {
3945
void onStartGameClicked();
46+
void onLeaderboardClicked();
4047
}
4148

42-
/** Member vars for game */
49+
/**
50+
* Member vars for game
51+
*/
4352
private User mUser;
4453
private ArrayList<Level> mLevels;
4554
private List<Question> mQuestions;
@@ -73,10 +82,13 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
7382
setDataToViews();
7483
showHideButton();
7584
showDataView();
85+
setupClickListeners();
7686
}
7787

78-
/** When this fragment is attached to its host activity, ie {@link MainActivity} the listener interface is connected
79-
* If not then an error exception is thrown to notify the developer. */
88+
/**
89+
* When this fragment is attached to its host activity, ie {@link MainActivity} the listener interface is connected
90+
* If not then an error exception is thrown to notify the developer.
91+
*/
8092
@Override
8193
public void onAttach(@NonNull Context context) {
8294
super.onAttach(context);
@@ -88,13 +100,36 @@ public void onAttach(@NonNull Context context) {
88100
}
89101
}
90102

91-
/** When this fragment is detached from the host, the listeners is set to null, to decouple. */
103+
/**
104+
* When this fragment is detached from the host, the listeners is set to null, to decouple.
105+
*/
92106
@Override
93107
public void onDetach() {
94108
super.onDetach();
95109
listener = null;
96110
}
97111

112+
public void setupClickListeners(){
113+
binding.btnContinueGame.setOnClickListener(new View.OnClickListener() {
114+
@Override
115+
public void onClick(View view) {
116+
listener.onStartGameClicked();
117+
}
118+
});
119+
binding.btnNewGame.setOnClickListener(new View.OnClickListener() {
120+
@Override
121+
public void onClick(View view) {
122+
listener.onStartGameClicked();
123+
}
124+
});
125+
binding.btnLeaderboard.setOnClickListener(new View.OnClickListener() {
126+
@Override
127+
public void onClick(View view) {
128+
listener.onLeaderboardClicked();
129+
}
130+
});
131+
}
132+
98133
/** This method will set the data in member fields to the views */
99134
private void setDataToViews(){
100135
binding.tvName.setText(mUser.getUsername());

0 commit comments

Comments
 (0)