22
33import android .content .Intent ;
44import android .content .pm .ActivityInfo ;
5- import android .content .res .Resources ;
65import android .net .Uri ;
76import android .os .Bundle ;
87import android .support .design .widget .AppBarLayout ;
98import android .support .design .widget .CollapsingToolbarLayout ;
109import android .support .v4 .app .DialogFragment ;
10+ import android .support .v4 .widget .DrawerLayout ;
1111import android .support .v7 .app .AppCompatActivity ;
1212import android .support .v7 .widget .Toolbar ;
1313import android .util .Log ;
1414import android .view .Menu ;
1515import android .view .MenuItem ;
16+ import android .view .View ;
1617import android .widget .ImageView ;
17- import android .widget .Toast ;
18+ import android .widget .ProgressBar ;
1819
1920import com .bumptech .glide .Glide ;
2021import com .google .android .gms .ads .AdListener ;
21- import com .google .android .gms .ads .AdRequest ;
2222import com .google .android .gms .ads .AdView ;
2323
2424import org .app .txema .ghiblifilms .di .App ;
25+ import org .app .txema .ghiblifilms .model .NavDrawerItem ;
2526import org .app .txema .ghiblifilms .view .fragment .AboutUsFragment ;
27+ import org .app .txema .ghiblifilms .view .fragment .CharacterDetailsFragment ;
28+ import org .app .txema .ghiblifilms .view .fragment .CharacterListFragment ;
29+ import org .app .txema .ghiblifilms .view .fragment .FilmDetailsFragment ;
30+ import org .app .txema .ghiblifilms .view .fragment .FilmListFragment ;
31+ import org .app .txema .ghiblifilms .view .fragment .LocationDetailsFragment ;
32+ import org .app .txema .ghiblifilms .view .fragment .LocationListFragment ;
33+ import org .app .txema .ghiblifilms .view .fragment .NavigationDrawerFragment ;
2634
27- public class MainActivity extends AppCompatActivity {
28- private static final String TAG = MainActivity . class . getSimpleName () ;
35+ import static org . app . txema . ghiblifilms . view . util . Tag . TAG_DETAILS_FRAGMENT ;
36+ import static org . app . txema . ghiblifilms . view . util . Tag . TAG_LIST_FRAGMENT ;
2937
30- private AdView mAdView ;
38+ public class MainActivity extends AppCompatActivity implements NavigationDrawerFragment . FragmentDrawerListener {
3139
40+ private AdView mAdView ;
41+ protected ProgressBar progressBar ;
3242
3343 @ Override
3444 protected void onCreate (Bundle savedInstanceState ) {
3545 super .onCreate (savedInstanceState );
3646 setContentView (R .layout .activity_main );
3747
38- //AdMobs
39- mAdView = (AdView ) findViewById (R .id .adView );
40- AdRequest adRequest = new AdRequest .Builder ().build ();
41- mAdView .loadAd (adRequest );
42- adMobListener ();
48+ progressBar = (ProgressBar )findViewById (R .id .progressBar );
49+
50+ //AdMobs TODO
51+ //mAdView = (AdView) findViewById(R.id.adView);
52+ //AdRequest adRequest = new AdRequest.Builder().build();
53+ //mAdView.loadAd(adRequest);
54+ //adMobListener();
4355
4456 //DI with Dagger
4557 ((App ) getApplication ()).getAppComponent ().inject (this );
@@ -56,14 +68,183 @@ protected void onCreate(Bundle savedInstanceState) {
5668 Toolbar toolbar = (Toolbar ) findViewById (R .id .toolbar );
5769 setSupportActionBar (toolbar );
5870
71+ //NavigationDrawerFragment declaration
72+ NavigationDrawerFragment navDrawerFragment = (NavigationDrawerFragment )
73+ getSupportFragmentManager ().findFragmentById (R .id .fragment_navigation_drawer );
74+ navDrawerFragment .setUp (R .id .fragment_navigation_drawer , (DrawerLayout ) findViewById (R .id .drawer_layout ), toolbar );
75+ navDrawerFragment .setDrawerListener (this );
76+
5977 //Set image cover for collapsingLayout (Using Glide)
6078 try {
61- Glide .with (this ).load (R .drawable .cover ).into ((ImageView ) findViewById (R .id .backdrop ));
79+ Glide .with (this ).load (R .drawable .cover ).centerCrop (). into ((ImageView )findViewById (R .id .backdrop ));
6280 } catch (Exception e ) {
6381 e .printStackTrace ();
6482 }
6583
6684 setScreenOrientation ();
85+
86+ //insert fragment at right pane if layout is two panes
87+ if (getResources ().getBoolean (R .bool .two_panes )) {
88+ insertDefaultDetailsFragment ();
89+ }
90+
91+ //insert fragment list
92+ insertFilmListFragment ();
93+ }
94+
95+ @ Override
96+ public boolean onCreateOptionsMenu (Menu menu ) {
97+ // Inflate the menu; this adds items to the action bar if it is present.
98+ getMenuInflater ().inflate (R .menu .menu_main , menu );
99+ return true ;
100+ }
101+
102+ @ Override
103+ public boolean onOptionsItemSelected (MenuItem item ) {
104+ // Handle action bar item clicks.
105+ int id = item .getItemId ();
106+
107+ //noinspection SimplifiableIfStatement
108+ if (id == R .id .action_about_us ) {
109+ DialogFragment newFragment = new AboutUsFragment ();
110+ newFragment .show (getSupportFragmentManager (), DialogFragment .class .getSimpleName ());
111+ return true ;
112+ }
113+
114+ if (id == R .id .action_rate_app ) {
115+ Intent browserIntent = new Intent (Intent .ACTION_VIEW , Uri .parse ("https://play.google.com/store/apps/details?id=org.app.txema.ghiblifilms" ));
116+ startActivity (browserIntent );
117+ }
118+
119+ return super .onOptionsItemSelected (item );
120+ }
121+
122+ private void setScreenOrientation () {
123+ if (getResources ().getBoolean (R .bool .two_panes )) {
124+ this .setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_LANDSCAPE );
125+ } else {
126+ this .setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_PORTRAIT );
127+ }
128+ }
129+
130+ @ Override
131+ public void onDrawerItemSelected (View view , int position ) {
132+ if (position != NavDrawerItem .SELECTED ) {
133+ switch (position ) {
134+ case NavDrawerItem .FILMS :
135+ if (getResources ().getBoolean (R .bool .two_panes )) {
136+ insertFilmDetailsFragment ();
137+ }
138+ insertFilmListFragment ();
139+ break ;
140+ case NavDrawerItem .CHARACTERS :
141+ if (getResources ().getBoolean (R .bool .two_panes )) {
142+ insertCharacterDetailsFragment ();
143+ }
144+ insertCharacterListFragment ();
145+ break ;
146+ case NavDrawerItem .LOCATIONS :
147+ if (getResources ().getBoolean (R .bool .two_panes )) {
148+ insertLocationDetailsFragment ();
149+ }
150+ insertLocationsListFragment ();
151+ break ;
152+ }
153+ NavDrawerItem .SELECTED = position ;
154+ }
155+ }
156+
157+ // *****************************************
158+ // FRAGMENTS TO INSERT: 1) DetailsFragment
159+ // *****************************************
160+
161+ //default details fragment
162+ private void insertDefaultDetailsFragment () {
163+ // Create a new Fragment to be placed in the activity layout
164+ FilmDetailsFragment fragment = new FilmDetailsFragment ();
165+
166+ // Add the fragment to the 'fragment_details' FrameLayout
167+ getSupportFragmentManager ().beginTransaction ()
168+ .add (R .id .details_fragment , fragment , TAG_DETAILS_FRAGMENT )
169+ .addToBackStack (null )
170+ .commit ();
171+ }
172+
173+ //film details fragment
174+ private void insertFilmDetailsFragment () {
175+ // Create a new Fragment to be placed in the activity layout
176+ FilmDetailsFragment fragment = new FilmDetailsFragment ();
177+
178+ // Replace the fragment to the 'fragment_details' FrameLayout
179+ getSupportFragmentManager ().beginTransaction ()
180+ .replace (R .id .details_fragment , fragment , TAG_DETAILS_FRAGMENT )
181+ .commit ();
182+
183+ }
184+
185+ //character details fragment
186+ private void insertCharacterDetailsFragment () {
187+ // Create a new Fragment to be placed in the activity layout
188+ CharacterDetailsFragment fragment = new CharacterDetailsFragment ();
189+
190+ // Replace the fragment to the 'fragment_details' FrameLayout
191+ getSupportFragmentManager ().beginTransaction ()
192+ .replace (R .id .details_fragment , fragment , TAG_DETAILS_FRAGMENT )
193+ .commit ();
194+ }
195+
196+ //locations details fragment
197+ private void insertLocationDetailsFragment () {
198+ // Create a new Fragment to be placed in the activity layout
199+ LocationDetailsFragment fragment = new LocationDetailsFragment ();
200+
201+ // Replace the fragment to the 'fragment_details' FrameLayout
202+ getSupportFragmentManager ().beginTransaction ()
203+ .replace (R .id .details_fragment , fragment , TAG_DETAILS_FRAGMENT )
204+ .commit ();
205+ }
206+
207+ // *****************************************
208+ // FRAGMENTS TO INSERT: 2) ListFragment
209+ // *****************************************
210+
211+ //films list fragment
212+ private void insertFilmListFragment () {
213+ // Create a new Fragment to be placed in the activity layout
214+ FilmListFragment fragment = new FilmListFragment ();
215+
216+ // Add the fragment to the 'fragment_list' FrameLayout
217+ getSupportFragmentManager ().beginTransaction ()
218+ .add (R .id .list_fragment , fragment , TAG_LIST_FRAGMENT )
219+ .commit ();
220+ }
221+
222+ //characters list fragment
223+ private void insertCharacterListFragment () {
224+ // Create a new Fragment to be placed in the activity layout
225+ CharacterListFragment fragment = new CharacterListFragment ();
226+
227+ // Add the fragment to the 'fragment_list' FrameLayout
228+ getSupportFragmentManager ().beginTransaction ()
229+ .replace (R .id .list_fragment , fragment , TAG_LIST_FRAGMENT ).commit ();
230+ }
231+
232+ //locations list fragment
233+ private void insertLocationsListFragment () {
234+ // Create a new Fragment to be placed in the activity layout
235+ LocationListFragment fragment = new LocationListFragment ();
236+
237+ // Add the fragment to the 'fragment_list' FrameLayout
238+ getSupportFragmentManager ().beginTransaction ()
239+ .replace (R .id .list_fragment , fragment , TAG_LIST_FRAGMENT ).commit ();
240+ }
241+
242+ public void showProgress () {
243+ progressBar .setVisibility (View .VISIBLE );
244+ }
245+
246+ public void hideProgress () {
247+ progressBar .setVisibility (View .GONE );
67248 }
68249
69250 private void adMobListener () {
@@ -101,43 +282,4 @@ public void onAdClosed() {
101282 }
102283 });
103284 }
104-
105- @ Override
106- public boolean onCreateOptionsMenu (Menu menu ) {
107- // Inflate the menu; this adds items to the action bar if it is present.
108- getMenuInflater ().inflate (R .menu .menu_main , menu );
109- return true ;
110- }
111-
112- @ Override
113- public boolean onOptionsItemSelected (MenuItem item ) {
114- // Handle action bar item clicks here. The action bar will
115- // automatically handle clicks on the Home/Up button, so long
116- // as you specify a parent activity in AndroidManifest.xml.
117- int id = item .getItemId ();
118-
119- //noinspection SimplifiableIfStatement
120- if (id == R .id .action_about_us ) {
121- DialogFragment newFragment = new AboutUsFragment ();
122- newFragment .show (getSupportFragmentManager (), "aboutUs" );
123- return true ;
124- }
125-
126- if (id == R .id .action_rate_app ) {
127- Intent browserIntent = new Intent (Intent .ACTION_VIEW , Uri .parse ("https://play.google.com/store/apps/details?id=org.app.txema.ghiblifilms" ));
128- startActivity (browserIntent );
129- }
130-
131- return super .onOptionsItemSelected (item );
132- }
133-
134- private void setScreenOrientation () {
135- Resources res = getResources ();
136- boolean twoPanes = res .getBoolean (R .bool .two_panes );
137- if (twoPanes ) {
138- this .setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_LANDSCAPE );
139- } else {
140- this .setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_PORTRAIT );
141- }
142- }
143285}
0 commit comments