Skip to content

Commit 6eec502

Browse files
committed
Moving navigation drawer into fragment to prepare for automatic responsive layout.
1 parent 258b635 commit 6eec502

18 files changed

+395
-135
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
android:debuggable="true">
2424

2525
<activity
26-
android:name=".ui.HomeActivity"
26+
android:name=".ui.MainActivity"
2727
android:configChanges="orientation|keyboardHidden|screenSize"
2828
android:label="@string/app_name">
2929
<intent-filter>
@@ -39,12 +39,12 @@
3939
android:configChanges="orientation|keyboardHidden|screenSize"
4040
android:label="@string/app_name"
4141
android:launchMode="singleTop"
42-
android:parentActivityName="com.donnfelker.android.bootstrap.ui.CarouselActivity" >
42+
android:parentActivityName="com.donnfelker.android.bootstrap.ui.MainActivity" >
4343
<!-- Parent activity meta-data to support 4.0 and lower -->
4444
<!-- Both parent activity attributes have to contain the full package name -->
4545
<meta-data
4646
android:name="android.support.PARENT_ACTIVITY"
47-
android:value="com.donnfelker.android.bootstrap.ui.CarouselActivity" />
47+
android:value="com.donnfelker.android.bootstrap.ui.MainActivity" />
4848
</activity>
4949

5050
<service android:name=".core.TimerService" android:enabled="true" android:exported="false" />

app/src/main/java/com/donnfelker/android/bootstrap/BootstrapModule.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
import com.donnfelker.android.bootstrap.authenticator.LogoutService;
88
import com.donnfelker.android.bootstrap.core.TimerService;
99
import com.donnfelker.android.bootstrap.ui.BootstrapTimerActivity;
10-
import com.donnfelker.android.bootstrap.ui.HomeActivity;
10+
import com.donnfelker.android.bootstrap.ui.MainActivity;
1111
import com.donnfelker.android.bootstrap.ui.CheckInsListFragment;
12+
import com.donnfelker.android.bootstrap.ui.NavigationDrawerFragment;
1213
import com.donnfelker.android.bootstrap.ui.NewsActivity;
1314
import com.donnfelker.android.bootstrap.ui.NewsListFragment;
1415
import com.donnfelker.android.bootstrap.ui.UserActivity;
@@ -30,9 +31,10 @@
3031
injects = {
3132
BootstrapApplication.class,
3233
BootstrapAuthenticatorActivity.class,
33-
HomeActivity.class,
34+
MainActivity.class,
3435
BootstrapTimerActivity.class,
3536
CheckInsListFragment.class,
37+
NavigationDrawerFragment.class,
3638
NewsActivity.class,
3739
NewsListFragment.class,
3840
UserActivity.class,

app/src/main/java/com/donnfelker/android/bootstrap/authenticator/ApiKeyProvider.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,8 @@ public class ApiKeyProvider {
3131
* {@link com.donnfelker.android.bootstrap.authenticator.BootstrapAccountAuthenticator} will get started.
3232
* If you want to remove the authentication then you can comment out the code below and return a string such as
3333
* "foo" and the authentication process will not be kicked off. Alternatively, you can remove this class
34-
* completely and clean up any referecnes to the authenticator.
34+
* completely and clean up any references to the authenticator.
3535
*
36-
* @see com.donnfelker.android.bootstrap.authenticator.BootstrapAccountAuthenticator
37-
* @see com.donnfelker.android.bootstrap.authenticator.BootstrapAuthenticatorActivity
38-
* @see com.donnfelker.android.bootstrap.authenticator.AccountAuthenticatorService
39-
* @see com.donnfelker.android.bootstrap.authenticator.ApiKeyProvider
4036
*
4137
* @return API key to be used for authorization with a
4238
* {@link com.donnfelker.android.bootstrap.core.BootstrapService} instance
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.donnfelker.android.bootstrap.events;
2+
3+
/**
4+
* Pub/Sub event used to communicate between fragment and activity.
5+
* Subscription occurs in the {@link com.donnfelker.android.bootstrap.ui.MainActivity}
6+
*/
7+
public class NavItemSelectedEvent {
8+
private int itemPosition;
9+
10+
public NavItemSelectedEvent(int itemPosition) {
11+
this.itemPosition = itemPosition;
12+
}
13+
14+
public int getItemPosition() {
15+
return itemPosition;
16+
}
17+
}

app/src/main/java/com/donnfelker/android/bootstrap/ui/BootstrapActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public boolean onOptionsItemSelected(final MenuItem item) {
3939
case android.R.id.home:
4040
// Don't call finish! Because activity could have been started by an
4141
// outside activity and the home button would not operated as expected!
42-
final Intent homeIntent = new Intent(this, HomeActivity.class);
42+
final Intent homeIntent = new Intent(this, MainActivity.class);
4343
homeIntent.addFlags(FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP);
4444
startActivity(homeIntent);
4545
return true;

app/src/main/java/com/donnfelker/android/bootstrap/ui/BootstrapFragmentActivity.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@
44
import android.support.v7.app.ActionBarActivity;
55

66
import com.donnfelker.android.bootstrap.Injector;
7+
import com.squareup.otto.Bus;
78

9+
import javax.inject.Inject;
10+
11+
import butterknife.InjectView;
812
import butterknife.Views;
913

1014
/**
1115
* Base class for all Bootstrap Activities that need fragments.
1216
*/
1317
public class BootstrapFragmentActivity extends ActionBarActivity {
1418

19+
@Inject
20+
protected Bus eventBus;
21+
1522
@Override
1623
protected void onCreate(final Bundle savedInstanceState) {
1724
super.onCreate(savedInstanceState);
@@ -26,4 +33,15 @@ public void setContentView(final int layoutResId) {
2633
Views.inject(this);
2734
}
2835

36+
@Override
37+
protected void onResume() {
38+
super.onResume();
39+
eventBus.register(this);
40+
}
41+
42+
@Override
43+
protected void onPause() {
44+
super.onPause();
45+
eventBus.unregister(this);
46+
}
2947
}

app/src/main/java/com/donnfelker/android/bootstrap/ui/BootstrapTimerActivity.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected void onCreate(final Bundle savedInstanceState) {
4444

4545
setContentView(R.layout.bootstrap_timer);
4646

47-
setTitle(R.string.timer);
47+
setTitle(R.string.title_timer);
4848

4949
getSupportActionBar().setHomeButtonEnabled(true);
5050
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@@ -56,20 +56,6 @@ protected void onCreate(final Bundle savedInstanceState) {
5656

5757
}
5858

59-
@Override
60-
protected void onResume() {
61-
super.onResume();
62-
63-
eventBus.register(this);
64-
}
65-
66-
@Override
67-
protected void onPause() {
68-
super.onPause();
69-
70-
eventBus.unregister(this);
71-
}
72-
7359
@Override
7460
public void onClick(final View v) {
7561
switch (v.getId()) {

app/src/main/java/com/donnfelker/android/bootstrap/ui/HomeActivity.java renamed to app/src/main/java/com/donnfelker/android/bootstrap/ui/MainActivity.java

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
import com.donnfelker.android.bootstrap.BootstrapServiceProvider;
1717
import com.donnfelker.android.bootstrap.R;
1818
import com.donnfelker.android.bootstrap.core.BootstrapService;
19+
import com.donnfelker.android.bootstrap.events.NavItemSelectedEvent;
1920
import com.donnfelker.android.bootstrap.util.Ln;
2021
import com.donnfelker.android.bootstrap.util.SafeAsyncTask;
22+
import com.squareup.otto.Subscribe;
2123

2224
import javax.inject.Inject;
2325

@@ -30,7 +32,7 @@
3032
* If you need to remove the authentication from the application please see
3133
* {@link com.donnfelker.android.bootstrap.authenticator.ApiKeyProvider#getAuthKey(android.app.Activity)}
3234
*/
33-
public class HomeActivity extends BootstrapFragmentActivity {
35+
public class MainActivity extends BootstrapFragmentActivity {
3436

3537
@Inject protected BootstrapServiceProvider serviceProvider;
3638

@@ -40,6 +42,7 @@ public class HomeActivity extends BootstrapFragmentActivity {
4042
private ActionBarDrawerToggle drawerToggle;
4143
private CharSequence drawerTitle;
4244
private CharSequence title;
45+
private NavigationDrawerFragment navigationDrawerFragment;
4346

4447
@Override
4548
protected void onCreate(final Bundle savedInstanceState) {
@@ -48,7 +51,7 @@ protected void onCreate(final Bundle savedInstanceState) {
4851

4952
super.onCreate(savedInstanceState);
5053

51-
setContentView(R.layout.home_activity);
54+
setContentView(R.layout.main_activity);
5255

5356
// View injection with Butterknife
5457
Views.inject(this);
@@ -60,8 +63,8 @@ protected void onCreate(final Bundle savedInstanceState) {
6063
this, /* Host activity */
6164
drawerLayout, /* DrawerLayout object */
6265
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
63-
R.string.drawer_open, /* "open drawer" description */
64-
R.string.drawer_close) { /* "close drawer" description */
66+
R.string.navigation_drawer_open, /* "open drawer" description */
67+
R.string.navigation_drawer_close) { /* "close drawer" description */
6568

6669
/** Called when a drawer has settled in a completely closed state. */
6770
public void onDrawerClosed(View view) {
@@ -82,6 +85,14 @@ public void onDrawerOpened(View drawerView) {
8285
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
8386
getSupportActionBar().setHomeButtonEnabled(true);
8487

88+
navigationDrawerFragment = (NavigationDrawerFragment)
89+
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
90+
91+
// Set up the drawer.
92+
navigationDrawerFragment.setUp(
93+
R.id.navigation_drawer,
94+
(DrawerLayout) findViewById(R.id.drawer_layout));
95+
8596
checkAuth();
8697

8798
}
@@ -93,6 +104,7 @@ protected void onPostCreate(final Bundle savedInstanceState) {
93104
drawerToggle.syncState();
94105
}
95106

107+
96108
@Override
97109
public void onConfigurationChanged(final Configuration newConfig) {
98110
super.onConfigurationChanged(newConfig);
@@ -110,15 +122,14 @@ private void initScreen() {
110122
.commit();
111123
}
112124

113-
setNavListeners();
114125
}
115126

116127
private void checkAuth() {
117128
new SafeAsyncTask<Boolean>() {
118129

119130
@Override
120131
public Boolean call() throws Exception {
121-
final BootstrapService svc = serviceProvider.getService(HomeActivity.this);
132+
final BootstrapService svc = serviceProvider.getService(MainActivity.this);
122133
return svc != null;
123134
}
124135

@@ -141,26 +152,6 @@ protected void onSuccess(final Boolean hasAuthenticated) throws Exception {
141152
}.execute();
142153
}
143154

144-
145-
private void setNavListeners() {
146-
147-
findViewById(R.id.menu_item_home).setOnClickListener(new View.OnClickListener() {
148-
@Override
149-
public void onClick(final View v) {
150-
drawerLayout.closeDrawers();
151-
}
152-
});
153-
154-
findViewById(R.id.menu_item_timer).setOnClickListener(new View.OnClickListener() {
155-
@Override
156-
public void onClick(final View v) {
157-
drawerLayout.closeDrawers();
158-
navigateToTimer();
159-
}
160-
});
161-
162-
}
163-
164155
@Override
165156
public boolean onOptionsItemSelected(final MenuItem item) {
166157

@@ -184,4 +175,21 @@ private void navigateToTimer() {
184175
final Intent i = new Intent(this, BootstrapTimerActivity.class);
185176
startActivity(i);
186177
}
178+
179+
@Subscribe
180+
public void onNavigationItemSelected(NavItemSelectedEvent event) {
181+
182+
Ln.d("Selected: %1$s", event.getItemPosition());
183+
184+
switch(event.getItemPosition()) {
185+
case 0:
186+
// Home
187+
// do nothing as we're already on the home screen.
188+
break;
189+
case 1:
190+
// Timer
191+
navigateToTimer();
192+
break;
193+
}
194+
}
187195
}

0 commit comments

Comments
 (0)