6
6
import androidx .annotation .NonNull ;
7
7
import androidx .annotation .Nullable ;
8
8
import androidx .fragment .app .Fragment ;
9
+ import timber .log .Timber ;
9
10
10
11
import android .view .LayoutInflater ;
11
12
import android .view .View ;
16
17
import com .tiansirk .countryquiz .model .Question ;
17
18
import com .tiansirk .countryquiz .model .User ;
18
19
20
+ import java .util .ArrayList ;
19
21
import java .util .List ;
20
22
23
+ import static com .tiansirk .countryquiz .ui .MainActivity .KEY_LEVELS ;
24
+ import static com .tiansirk .countryquiz .ui .MainActivity .KEY_USER ;
25
+
21
26
/** A simple {@link Fragment} subclass.
22
27
* Use the {@link MainMenuFragment.MainMenuFragmentListener} interface for to communicate with this fragment.
23
28
*/
24
29
public class MainMenuFragment extends Fragment {
25
30
26
- /**
27
- * Member vars for views
28
- */
31
+ /** Member vars for views */
29
32
private FragmentMainMenuBinding binding ;
30
33
31
- /**
32
- * Member var for own custom communication listener
33
- */
34
+ /** Member var for own custom communication listener */
34
35
private MainMenuFragmentListener listener ;
35
36
36
- /**
37
- * The interface for communication
38
- */
37
+ /** The interface for communication */
39
38
public interface MainMenuFragmentListener {
40
39
void onStartGameClicked ();
41
40
}
42
41
43
- /**
44
- * Member vars for game
45
- */
42
+ /** Member vars for game */
46
43
private User mUser ;
47
- private Level mLevel ;
44
+ private ArrayList < Level > mLevels ;
48
45
private List <Question > mQuestions ;
49
46
50
47
// Required empty public constructor
@@ -62,21 +59,23 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
62
59
// Inflate the layout for this fragment
63
60
binding = FragmentMainMenuBinding .inflate (inflater , container , false );
64
61
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 ());
66
67
return rootView ;
67
68
}
68
69
69
70
@ Override
70
71
public void onViewCreated (@ NonNull View view , @ Nullable Bundle savedInstanceState ) {
71
72
super .onViewCreated (view , savedInstanceState );
73
+ setDataToViews ();
74
+ showDataView ();
72
75
}
73
76
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. */
80
79
@ Override
81
80
public void onAttach (@ NonNull Context context ) {
82
81
super .onAttach (context );
@@ -88,13 +87,47 @@ public void onAttach(@NonNull Context context) {
88
87
}
89
88
}
90
89
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. */
94
91
@ Override
95
92
public void onDetach () {
96
93
super .onDetach ();
97
94
listener = null ;
98
95
}
99
96
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
+ }
100
133
}
0 commit comments