Skip to content

Commit 6a12b07

Browse files
committed
Implement sending data to MainMenuFragment
1 parent b81e4ca commit 6a12b07

File tree

4 files changed

+68
-29
lines changed

4 files changed

+68
-29
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.app.Activity;
1111
import android.content.Intent;
1212
import android.os.Bundle;
13+
import android.os.Parcelable;
1314

1415
import com.google.android.gms.tasks.OnSuccessListener;
1516
import com.google.firebase.auth.FirebaseAuth;
@@ -25,13 +26,16 @@
2526
import com.tiansirk.countryquiz.utils.MyDebugTree;
2627
import com.tiansirk.countryquiz.utils.MyReleaseTree;
2728

29+
import java.util.ArrayList;
2830
import java.util.List;
2931

3032
public class MainActivity extends AppCompatActivity implements Repository.EntityChangeListener,
3133
WelcomeFragment.WelcomeFragmentListener, MainMenuFragment.MainMenuFragmentListener {
3234

3335
public static final String USER_PREFERENCES = MainActivity.class.getPackage().getName().concat("_userPrefs");
3436
public static final String KEY_SAVED_USER_NAME = "userName";
37+
public static final String KEY_USER = "user";
38+
public static final String KEY_LEVELS = "levels";
3539
public static final String TAG_WELCOME_FRAGMENT = "welcome_fragment";
3640
public static final String TAG_MAIN_MENU_FRAGMENT = "main_menu_fragment";
3741
private static final String COLLECTION_NAME = "users";
@@ -74,10 +78,6 @@ private void initFireStore(){
7478
mRepository = new Repository(this, User.class, COLLECTION_NAME);
7579
}
7680

77-
78-
79-
80-
8181
private void initWelcomeFragment(){
8282
Timber.i("Initializing WelcomeFragment");
8383
FragmentManager fragmentManager = getSupportFragmentManager();
@@ -90,9 +90,13 @@ private void initWelcomeFragment(){
9090

9191
private void initMainMenuFragment(){
9292
Timber.i("Initializing MainMenuFragment");
93+
Bundle bundle = new Bundle();
94+
bundle.putParcelable(KEY_USER, mUser);
95+
bundle.putParcelableArrayList(KEY_LEVELS, (ArrayList<? extends Parcelable>) mLevels);
9396
FragmentManager fragmentManager = getSupportFragmentManager();
9497
FragmentTransaction ft = fragmentManager.beginTransaction();
9598
mainMenuFragment = new MainMenuFragment();
99+
mainMenuFragment.setArguments(bundle);
96100
ft.replace(R.id.container_main_menu, mainMenuFragment, TAG_MAIN_MENU_FRAGMENT);
97101
ft.addToBackStack(TAG_MAIN_MENU_FRAGMENT);
98102
ft.commit();
@@ -105,6 +109,7 @@ public void onSetupFinished(User user, List<Level> levels) {
105109
Timber.d("User received from WelcomeFragment: %s", user.toString());
106110
mUser = user;
107111
mLevels = levels;
112+
if(!welcomeFragment.isHidden()) showHideFragment(welcomeFragment);
108113
initMainMenuFragment();
109114
}
110115

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

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import androidx.annotation.NonNull;
77
import androidx.annotation.Nullable;
88
import androidx.fragment.app.Fragment;
9+
import timber.log.Timber;
910

1011
import android.view.LayoutInflater;
1112
import android.view.View;
@@ -16,35 +17,31 @@
1617
import com.tiansirk.countryquiz.model.Question;
1718
import com.tiansirk.countryquiz.model.User;
1819

20+
import java.util.ArrayList;
1921
import java.util.List;
2022

23+
import static com.tiansirk.countryquiz.ui.MainActivity.KEY_LEVELS;
24+
import static com.tiansirk.countryquiz.ui.MainActivity.KEY_USER;
25+
2126
/** A simple {@link Fragment} subclass.
2227
* Use the {@link MainMenuFragment.MainMenuFragmentListener} interface for to communicate with this fragment.
2328
*/
2429
public class MainMenuFragment extends Fragment {
2530

26-
/**
27-
* Member vars for views
28-
*/
31+
/** Member vars for views */
2932
private FragmentMainMenuBinding binding;
3033

31-
/**
32-
* Member var for own custom communication listener
33-
*/
34+
/** Member var for own custom communication listener */
3435
private MainMenuFragmentListener listener;
3536

36-
/**
37-
* The interface for communication
38-
*/
37+
/** The interface for communication */
3938
public interface MainMenuFragmentListener {
4039
void onStartGameClicked();
4140
}
4241

43-
/**
44-
* Member vars for game
45-
*/
42+
/** Member vars for game */
4643
private User mUser;
47-
private Level mLevel;
44+
private ArrayList<Level> mLevels;
4845
private List<Question> mQuestions;
4946

5047
// Required empty public constructor
@@ -62,21 +59,23 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
6259
// Inflate the layout for this fragment
6360
binding = FragmentMainMenuBinding.inflate(inflater, container, false);
6461
View rootView = binding.getRoot();
65-
62+
Timber.i("Receiving User and ArrayList<Level> from MainActivity");
63+
Bundle bundle = getArguments();
64+
mUser = bundle.getParcelable(KEY_USER);
65+
mLevels = bundle.getParcelableArrayList(KEY_LEVELS);
66+
Timber.i("User: %s. Levels: %s", mUser.toString(), mLevels.size());
6667
return rootView;
6768
}
6869

6970
@Override
7071
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
7172
super.onViewCreated(view, savedInstanceState);
73+
setDataToViews();
74+
showDataView();
7275
}
7376

74-
/**
75-
* When this fragment is attached to its host activity, ie {@link MainActivity} the listener interface is connected
76-
* If not then an error exception is thrown to notify the developer.
77-
*
78-
* @param context
79-
*/
77+
/** When this fragment is attached to its host activity, ie {@link MainActivity} the listener interface is connected
78+
* If not then an error exception is thrown to notify the developer. */
8079
@Override
8180
public void onAttach(@NonNull Context context) {
8281
super.onAttach(context);
@@ -88,13 +87,47 @@ public void onAttach(@NonNull Context context) {
8887
}
8988
}
9089

91-
/**
92-
* When this fragment is detached from the host, the listeners is set to null, to decouple.
93-
*/
90+
/** When this fragment is detached from the host, the listeners is set to null, to decouple. */
9491
@Override
9592
public void onDetach() {
9693
super.onDetach();
9794
listener = null;
9895
}
9996

97+
/** This method will set the data in member fields to the views */
98+
private void setDataToViews(){
99+
binding.tvName.setText(mUser.getUsername());
100+
binding.tvHighScore.setText(mUser.getTotalPoints());
101+
}
102+
103+
/** This method will show the progressbar */
104+
private void showProgressBar() {
105+
binding.pbMainMenuFragment.setVisibility(View.VISIBLE);
106+
}
107+
/** This method will hide the progressbar */
108+
private void hideProgressBar() {
109+
binding.pbMainMenuFragment.setVisibility(View.INVISIBLE);
110+
}
111+
/** This method will make the Welcome view visible and hide the error message */
112+
private void showDataView() {
113+
// First, make sure the error is invisible
114+
binding.tvErrorMessageMainMenuFragment.setVisibility(View.INVISIBLE);
115+
// Then hide loading indicator
116+
binding.pbMainMenuFragment.setVisibility(View.INVISIBLE);
117+
// Then, make sure the data is visible
118+
binding.tvTitle.setVisibility(View.VISIBLE);
119+
binding.tvName.setVisibility(View.VISIBLE);
120+
binding.tvHighScore.setVisibility(View.VISIBLE);
121+
}
122+
/** This method will make the error message visible and hide the Welcome view */
123+
private void showErrorMessage() {
124+
// First, hide the currently visible data
125+
binding.tvTitle.setVisibility(View.INVISIBLE);
126+
binding.tvName.setVisibility(View.INVISIBLE);
127+
binding.tvHighScore.setVisibility(View.INVISIBLE);
128+
// Then hide loading indicator
129+
binding.pbMainMenuFragment.setVisibility(View.INVISIBLE);
130+
// Then, show the error
131+
binding.tvErrorMessageMainMenuFragment.setVisibility(View.VISIBLE);
132+
}
100133
}

app/src/main/res/layout/fragment_main_menu.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
android:id="@+id/tv_title"
3838
android:layout_width="wrap_content"
3939
android:layout_height="wrap_content"
40-
android:hint="@string/title_main_menu_fragment"
40+
android:text="@string/title_main_menu_fragment"
4141
app:layout_constraintBottom_toTopOf="@+id/tv_name"
4242
app:layout_constraintEnd_toEndOf="parent"
4343
app:layout_constraintHorizontal_bias="0.5"
@@ -59,7 +59,7 @@
5959
android:id="@+id/tv_high_score"
6060
android:layout_width="wrap_content"
6161
android:layout_height="wrap_content"
62-
android:text="@string/high_score_main_menu_fragment"
62+
tools:text="@string/high_score_main_menu_fragment"
6363

6464
app:layout_constraintBottom_toTopOf="@+id/btn_new_game"
6565
app:layout_constraintEnd_toEndOf="parent"

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<string name="high_score_main_menu_fragment">High Score: %d</string>
1919
<string name="name_main_menu_fragment">Player 1</string>
2020
<string name="start_main_menu_fragment">Start Game</string>
21+
<string name="continue_main_menu_fragment">Continue Game</string>
2122
<string name="leaderboard_main_menu_fragment">Leader Board</string>
2223
<string name="question_topLevelDomain">What is the domain of %s used on the internet?</string>
2324
<string name="question_capital">What is the %s of %s?</string>

0 commit comments

Comments
 (0)