ソースを参照

Add support for hiding the Toolbar on Android

Matt Davies 8 年 前
コミット
337b1b9d25

+ 4
- 4
android/app/src/main/java/com/reactnativenavigation/activities/BottomTabActivity.java ファイルの表示

@@ -64,10 +64,9 @@ public class BottomTabActivity extends BaseReactActivity implements AHBottomNavi
64 64
 
65 65
     private void setupToolbar(ArrayList<Screen> screens) {
66 66
         Screen initialScreen = screens.get(0);
67
-        setNavigationStyle(initialScreen);
68 67
         mToolbar.setScreens(screens);
69
-        mToolbar.setTitle(initialScreen.title);
70
-        setSupportActionBar(mToolbar);
68
+        mToolbar.updateToolbar(initialScreen);
69
+        setNavigationStyle(initialScreen);
71 70
     }
72 71
 
73 72
     @Override
@@ -104,6 +103,7 @@ public class BottomTabActivity extends BaseReactActivity implements AHBottomNavi
104 103
     @Override
105 104
     public void push(Screen screen) {
106 105
         super.push(screen);
106
+        setNavigationStyle(screen);
107 107
         mScreenStacks.get(mCurrentStackPosition).push(screen);
108 108
     }
109 109
 
@@ -111,7 +111,7 @@ public class BottomTabActivity extends BaseReactActivity implements AHBottomNavi
111 111
     public Screen pop(String navigatorId) {
112 112
         super.pop(navigatorId);
113 113
         Screen screen = mScreenStacks.get(mCurrentStackPosition).pop();
114
-        setNavigationStyle(screen);
114
+        setNavigationStyle(getCurrentScreen());
115 115
         return screen;
116 116
     }
117 117
 

+ 2
- 3
android/app/src/main/java/com/reactnativenavigation/activities/SingleScreenActivity.java ファイルの表示

@@ -36,9 +36,8 @@ public class SingleScreenActivity extends BaseReactActivity {
36 36
     }
37 37
 
38 38
     protected void setupToolbar(Screen screen) {
39
+        mToolbar.updateToolbar(screen);
39 40
         setNavigationStyle(screen);
40
-        mToolbar.setTitle(screen.title);
41
-        setSupportActionBar(mToolbar);
42 41
     }
43 42
     
44 43
     @Override
@@ -52,7 +51,7 @@ public class SingleScreenActivity extends BaseReactActivity {
52 51
     public Screen pop(String navigatorId) {
53 52
         super.pop(navigatorId);
54 53
         Screen screen = mScreenStack.pop();
55
-        setNavigationStyle(screen);
54
+        setNavigationStyle(getCurrentScreen());
56 55
         return screen;
57 56
     }
58 57
 

+ 4
- 5
android/app/src/main/java/com/reactnativenavigation/activities/TabActivity.java ファイルの表示

@@ -39,11 +39,9 @@ public class TabActivity extends BaseReactActivity {
39 39
 
40 40
     private void setupToolbar(ArrayList<Screen> screens) {
41 41
         Screen initialScreen = screens.get(0);
42
-        setNavigationStyle(initialScreen);
43 42
         mToolbar.setScreens(screens);
44
-        mToolbar.setTitle(initialScreen.title);
45
-        mToolbar.setupToolbarButtonsAsync(initialScreen);
46
-        setSupportActionBar(mToolbar);
43
+        mToolbar.updateToolbar(initialScreen);
44
+        setNavigationStyle(initialScreen);
47 45
     }
48 46
 
49 47
     @Override
@@ -70,6 +68,7 @@ public class TabActivity extends BaseReactActivity {
70 68
     @Override
71 69
     public void push(Screen screen) {
72 70
         super.push(screen);
71
+        setNavigationStyle(screen);
73 72
         mAdapter.push(screen);
74 73
     }
75 74
 
@@ -77,7 +76,7 @@ public class TabActivity extends BaseReactActivity {
77 76
     public Screen pop(String navigatorId) {
78 77
         super.pop(navigatorId);
79 78
         Screen screen = mAdapter.pop(navigatorId);
80
-        setNavigationStyle(screen);
79
+        setNavigationStyle(getCurrentScreen());
81 80
         return screen;
82 81
     }
83 82
 

+ 4
- 0
android/app/src/main/java/com/reactnativenavigation/core/objects/JsonObject.java ファイルの表示

@@ -28,4 +28,8 @@ public class JsonObject {
28 28
     protected Integer getColor(ReadableMap map, String key) {
29 29
         return map.hasKey(key) ? Color.parseColor(map.getString(key)) : null;
30 30
     }
31
+    
32
+    protected Boolean getBoolean(ReadableMap map, String key) {
33
+        return map.hasKey(key) ? map.getBoolean(key) : null;
34
+    }
31 35
 }

+ 3
- 0
android/app/src/main/java/com/reactnativenavigation/core/objects/Screen.java ファイルの表示

@@ -34,6 +34,7 @@ public class Screen extends JsonObject implements Serializable {
34 34
     private static final String KEY_TOOL_BAR_STYLE = "navigatorStyle";
35 35
     private static final String KEY_STATUS_BAR_COLOR = "statusBarColor";
36 36
     private static final String KEY_TOOL_BAR_COLOR = "toolBarColor";
37
+    private static final String KEY_TOOL_BAR_HIDDEN = "navBarHidden";
37 38
     private static final String KEY_NAVIGATION_BAR_COLOR = "navigationBarColor";
38 39
     private static final String KEY_BUTTONS_TINT_COLOR = "buttonsTint";
39 40
     private static final String KEY_TITLE_COLOR = "titleColor";
@@ -54,6 +55,7 @@ public class Screen extends JsonObject implements Serializable {
54 55
 
55 56
     // Navigation styling
56 57
     @Nullable @ColorInt public Integer toolBarColor;
58
+    @Nullable public Boolean toolBarHidden;
57 59
     @Nullable @ColorInt public Integer statusBarColor;
58 60
     @Nullable @ColorInt public Integer navigationBarColor;
59 61
     @Nullable @ColorInt public Integer buttonsTintColor;
@@ -105,6 +107,7 @@ public class Screen extends JsonObject implements Serializable {
105 107
         ReadableMap style = getMap(screen, KEY_TOOL_BAR_STYLE);
106 108
         if (style != null) {
107 109
             toolBarColor = getColor(style, KEY_TOOL_BAR_COLOR);
110
+            toolBarHidden = getBoolean(style, KEY_TOOL_BAR_HIDDEN);
108 111
             statusBarColor = getColor(style, KEY_STATUS_BAR_COLOR);
109 112
             navigationBarColor = getColor(style, KEY_NAVIGATION_BAR_COLOR);
110 113
             buttonsTintColor = getColor(style, KEY_BUTTONS_TINT_COLOR);

+ 11
- 6
android/app/src/main/java/com/reactnativenavigation/modal/RnnModal.java ファイルの表示

@@ -29,6 +29,7 @@ public class RnnModal extends Dialog implements DialogInterface.OnDismissListene
29 29
     private ScreenStack mScreenStack;
30 30
     private View mContentView;
31 31
     private Screen mScreen;
32
+    private RnnToolBar mToolBar;
32 33
 
33 34
     public RnnModal(BaseReactActivity context, Screen screen) {
34 35
         super(context, R.style.Modal);
@@ -41,13 +42,10 @@ public class RnnModal extends Dialog implements DialogInterface.OnDismissListene
41 42
     private void init(final Context context) {
42 43
         requestWindowFeature(Window.FEATURE_NO_TITLE);
43 44
         mContentView = LayoutInflater.from(context).inflate(R.layout.modal_layout, null, false);
44
-        RnnToolBar toolBar = (RnnToolBar) mContentView.findViewById(R.id.toolbar);
45
+        mToolBar = (RnnToolBar) mContentView.findViewById(R.id.toolbar);
45 46
         mScreenStack = (ScreenStack) mContentView.findViewById(R.id.screenStack);
46
-
47 47
         setContentView(mContentView);
48
-        toolBar.setStyle(mScreen);
49
-        toolBar.setTitle(mScreen.title);
50
-        toolBar.setupToolbarButtonsAsync(mScreen);
48
+        mToolBar.updateToolbar(mScreen);
51 49
         mScreenStack.push(mScreen, new RctView.OnDisplayedListener() {
52 50
             @Override
53 51
             public void onDisplayed() {
@@ -67,10 +65,17 @@ public class RnnModal extends Dialog implements DialogInterface.OnDismissListene
67 65
 
68 66
     public void push(Screen screen) {
69 67
         mScreenStack.push(screen);
68
+        mToolBar.updateToolbar(mScreen);
70 69
     }
71 70
 
72 71
     public Screen pop() {
73
-        return mScreenStack.pop();
72
+        if (mScreenStack.getStackSize() > 1) {
73
+            return mScreenStack.pop();
74
+        } else {
75
+            ModalController.getInstance().remove();
76
+            super.onBackPressed();
77
+            return null;
78
+        }
74 79
     }
75 80
 
76 81
     @Override

+ 34
- 0
android/app/src/main/java/com/reactnativenavigation/views/RnnToolBar.java ファイルの表示

@@ -5,9 +5,11 @@ import android.content.Context;
5 5
 import android.content.res.Resources;
6 6
 import android.graphics.drawable.Drawable;
7 7
 import android.os.AsyncTask;
8
+import android.support.annotation.UiThread;
8 9
 import android.support.v4.content.ContextCompat;
9 10
 import android.support.v4.content.res.ResourcesCompat;
10 11
 import android.support.v7.app.ActionBar;
12
+import android.support.v7.app.AppCompatActivity;
11 13
 import android.support.v7.widget.Toolbar;
12 14
 import android.util.AttributeSet;
13 15
 import android.view.Menu;
@@ -70,6 +72,12 @@ public class RnnToolBar extends Toolbar {
70 72
         } else {
71 73
             resetTitleTextColor();
72 74
         }
75
+        
76
+        if (screen.toolBarHidden != null && screen.toolBarHidden) {
77
+            hideToolbar();
78
+        } else {
79
+            showToolbar();
80
+        }
73 81
     }
74 82
 
75 83
     private void resetBackground() {
@@ -95,6 +103,20 @@ public class RnnToolBar extends Toolbar {
95 103
         }
96 104
     }
97 105
 
106
+    private void showToolbar() {
107
+        ActionBar actionBar = ContextProvider.getActivityContext().getSupportActionBar();
108
+        if (actionBar != null) {
109
+            actionBar.show();
110
+        }
111
+    }
112
+
113
+    private void hideToolbar() {
114
+        ActionBar actionBar = ContextProvider.getActivityContext().getSupportActionBar();
115
+        if (actionBar != null) {
116
+            actionBar.hide();
117
+        }
118
+    }
119
+
98 120
     @SuppressWarnings({"ConstantConditions"})
99 121
     public void showBackButton(Screen screen) {
100 122
         ActionBar actionBar = ContextProvider.getActivityContext().getSupportActionBar();
@@ -126,6 +148,18 @@ public class RnnToolBar extends Toolbar {
126 148
         ContextProvider.getActivityContext().getSupportActionBar().setDisplayHomeAsUpEnabled(false);
127 149
     }
128 150
 
151
+    @UiThread
152
+    public void updateToolbar(Screen screen) {
153
+        setSupportActionBar();
154
+        setTitle(screen.title);
155
+        setStyle(screen);
156
+        setupToolbarButtonsAsync(screen);
157
+    }
158
+
159
+    private void setSupportActionBar() {
160
+        ((AppCompatActivity) getContext()).setSupportActionBar(this);
161
+    }
162
+
129 163
     private static class SetupToolbarButtonsTask extends AsyncTask<Void, Void, Map<String, Drawable>> {
130 164
         private final List<Button> mOldButtons;
131 165
         private final List<Button> mNewButtons;