Browse Source

Merge pull request #52 from MattDavies/master

Add support for hiding the Toolbar on Android
Guy Carmeli 8 years ago
parent
commit
75114aa2a8

+ 4
- 4
android/app/src/main/java/com/reactnativenavigation/activities/BottomTabActivity.java View File

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

+ 2
- 3
android/app/src/main/java/com/reactnativenavigation/activities/SingleScreenActivity.java View File

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

+ 4
- 5
android/app/src/main/java/com/reactnativenavigation/activities/TabActivity.java View File

39
 
39
 
40
     private void setupToolbar(ArrayList<Screen> screens) {
40
     private void setupToolbar(ArrayList<Screen> screens) {
41
         Screen initialScreen = screens.get(0);
41
         Screen initialScreen = screens.get(0);
42
-        setNavigationStyle(initialScreen);
43
         mToolbar.setScreens(screens);
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
     @Override
47
     @Override
70
     @Override
68
     @Override
71
     public void push(Screen screen) {
69
     public void push(Screen screen) {
72
         super.push(screen);
70
         super.push(screen);
71
+        setNavigationStyle(screen);
73
         mAdapter.push(screen);
72
         mAdapter.push(screen);
74
     }
73
     }
75
 
74
 
77
     public Screen pop(String navigatorId) {
76
     public Screen pop(String navigatorId) {
78
         super.pop(navigatorId);
77
         super.pop(navigatorId);
79
         Screen screen = mAdapter.pop(navigatorId);
78
         Screen screen = mAdapter.pop(navigatorId);
80
-        setNavigationStyle(screen);
79
+        setNavigationStyle(getCurrentScreen());
81
         return screen;
80
         return screen;
82
     }
81
     }
83
 
82
 

+ 4
- 0
android/app/src/main/java/com/reactnativenavigation/core/objects/JsonObject.java View File

28
     protected Integer getColor(ReadableMap map, String key) {
28
     protected Integer getColor(ReadableMap map, String key) {
29
         return map.hasKey(key) ? Color.parseColor(map.getString(key)) : null;
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 View File

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

+ 11
- 6
android/app/src/main/java/com/reactnativenavigation/modal/RnnModal.java View File

29
     private ScreenStack mScreenStack;
29
     private ScreenStack mScreenStack;
30
     private View mContentView;
30
     private View mContentView;
31
     private Screen mScreen;
31
     private Screen mScreen;
32
+    private RnnToolBar mToolBar;
32
 
33
 
33
     public RnnModal(BaseReactActivity context, Screen screen) {
34
     public RnnModal(BaseReactActivity context, Screen screen) {
34
         super(context, R.style.Modal);
35
         super(context, R.style.Modal);
41
     private void init(final Context context) {
42
     private void init(final Context context) {
42
         requestWindowFeature(Window.FEATURE_NO_TITLE);
43
         requestWindowFeature(Window.FEATURE_NO_TITLE);
43
         mContentView = LayoutInflater.from(context).inflate(R.layout.modal_layout, null, false);
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
         mScreenStack = (ScreenStack) mContentView.findViewById(R.id.screenStack);
46
         mScreenStack = (ScreenStack) mContentView.findViewById(R.id.screenStack);
46
-
47
         setContentView(mContentView);
47
         setContentView(mContentView);
48
-        toolBar.setStyle(mScreen);
49
-        toolBar.setTitle(mScreen.title);
50
-        toolBar.setupToolbarButtonsAsync(mScreen);
48
+        mToolBar.updateToolbar(mScreen);
51
         mScreenStack.push(mScreen, new RctView.OnDisplayedListener() {
49
         mScreenStack.push(mScreen, new RctView.OnDisplayedListener() {
52
             @Override
50
             @Override
53
             public void onDisplayed() {
51
             public void onDisplayed() {
67
 
65
 
68
     public void push(Screen screen) {
66
     public void push(Screen screen) {
69
         mScreenStack.push(screen);
67
         mScreenStack.push(screen);
68
+        mToolBar.updateToolbar(mScreen);
70
     }
69
     }
71
 
70
 
72
     public Screen pop() {
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
     @Override
81
     @Override

+ 34
- 0
android/app/src/main/java/com/reactnativenavigation/views/RnnToolBar.java View File

5
 import android.content.res.Resources;
5
 import android.content.res.Resources;
6
 import android.graphics.drawable.Drawable;
6
 import android.graphics.drawable.Drawable;
7
 import android.os.AsyncTask;
7
 import android.os.AsyncTask;
8
+import android.support.annotation.UiThread;
8
 import android.support.v4.content.ContextCompat;
9
 import android.support.v4.content.ContextCompat;
9
 import android.support.v4.content.res.ResourcesCompat;
10
 import android.support.v4.content.res.ResourcesCompat;
10
 import android.support.v7.app.ActionBar;
11
 import android.support.v7.app.ActionBar;
12
+import android.support.v7.app.AppCompatActivity;
11
 import android.support.v7.widget.Toolbar;
13
 import android.support.v7.widget.Toolbar;
12
 import android.util.AttributeSet;
14
 import android.util.AttributeSet;
13
 import android.view.Menu;
15
 import android.view.Menu;
70
         } else {
72
         } else {
71
             resetTitleTextColor();
73
             resetTitleTextColor();
72
         }
74
         }
75
+        
76
+        if (screen.toolBarHidden != null && screen.toolBarHidden) {
77
+            hideToolbar();
78
+        } else {
79
+            showToolbar();
80
+        }
73
     }
81
     }
74
 
82
 
75
     private void resetBackground() {
83
     private void resetBackground() {
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
     @SuppressWarnings({"ConstantConditions"})
120
     @SuppressWarnings({"ConstantConditions"})
99
     public void showBackButton(Screen screen) {
121
     public void showBackButton(Screen screen) {
100
         ActionBar actionBar = ContextProvider.getActivityContext().getSupportActionBar();
122
         ActionBar actionBar = ContextProvider.getActivityContext().getSupportActionBar();
126
         ContextProvider.getActivityContext().getSupportActionBar().setDisplayHomeAsUpEnabled(false);
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
     private static class SetupToolbarButtonsTask extends AsyncTask<Void, Void, Map<String, Drawable>> {
163
     private static class SetupToolbarButtonsTask extends AsyncTask<Void, Void, Map<String, Drawable>> {
130
         private final List<Button> mOldButtons;
164
         private final List<Button> mOldButtons;
131
         private final List<Button> mNewButtons;
165
         private final List<Button> mNewButtons;