Browse Source

Implement overrideBackPress

Guy Carmeli 8 years ago
parent
commit
b58d9c4f67

+ 4
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java View File

105
 
105
 
106
     @Override
106
     @Override
107
     public boolean onBackPressed() {
107
     public boolean onBackPressed() {
108
+        if (getCurrentScreenStack().handleBackPressInJs()) {
109
+            return true;
110
+        }
111
+
108
         if (getCurrentScreenStack().canPop()) {
112
         if (getCurrentScreenStack().canPop()) {
109
             getCurrentScreenStack().pop(true);
113
             getCurrentScreenStack().pop(true);
110
             setBottomTabsStyleFromCurrentScreen();
114
             setBottomTabsStyleFromCurrentScreen();

+ 4
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/SingleScreenLayout.java View File

68
 
68
 
69
     @Override
69
     @Override
70
     public boolean onBackPressed() {
70
     public boolean onBackPressed() {
71
+        if (stack.handleBackPressInJs()) {
72
+            return true;
73
+        }
74
+
71
         if (stack.canPop()) {
75
         if (stack.canPop()) {
72
             stack.pop(true);
76
             stack.pop(true);
73
             return true;
77
             return true;

+ 1
- 0
android/app/src/main/java/com/reactnativenavigation/params/ScreenParams.java View File

9
     public String screenId;
9
     public String screenId;
10
     public List<TitleBarButtonParams> rightButtons;
10
     public List<TitleBarButtonParams> rightButtons;
11
     public TitleBarLeftButtonParams leftButton;
11
     public TitleBarLeftButtonParams leftButton;
12
+    public boolean overrideBackPressInJs;
12
     public String title;
13
     public String title;
13
     public StyleParams styleParams;
14
     public StyleParams styleParams;
14
     public List<TopTabParams> topTabParams;
15
     public List<TopTabParams> topTabParams;

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/params/TitleBarLeftButtonParams.java View File

4
 
4
 
5
 public class TitleBarLeftButtonParams extends TitleBarButtonParams {
5
 public class TitleBarLeftButtonParams extends TitleBarButtonParams {
6
     public MaterialMenuDrawable.IconState iconState;
6
     public MaterialMenuDrawable.IconState iconState;
7
+    public boolean overrideBackPressInJs;
7
 
8
 
8
     public TitleBarLeftButtonParams(TitleBarButtonParams params) {
9
     public TitleBarLeftButtonParams(TitleBarButtonParams params) {
9
         icon = params.icon;
10
         icon = params.icon;
15
     public boolean isBackButton() {
16
     public boolean isBackButton() {
16
         return eventId.equals("back");
17
         return eventId.equals("back");
17
     }
18
     }
19
+
20
+    public void setOverrideBackPressInJs(boolean overrideBackPressInJs) {
21
+        this.overrideBackPressInJs = overrideBackPressInJs;
22
+    }
18
 }
23
 }

+ 2
- 0
android/app/src/main/java/com/reactnativenavigation/params/parsers/ScreenParamsParser.java View File

26
     private static final String TOP_TABS = "topTabs";
26
     private static final String TOP_TABS = "topTabs";
27
     private static final String FRAGMENT_CREATOR_CLASS_NAME = "fragmentCreatorClassName";
27
     private static final String FRAGMENT_CREATOR_CLASS_NAME = "fragmentCreatorClassName";
28
     private static final String FRAGMENT_CREATOR_PASS_PROPS = "fragmentCreatorPassProps";
28
     private static final String FRAGMENT_CREATOR_PASS_PROPS = "fragmentCreatorPassProps";
29
+    public static final String OVERRIDE_BACK_PRESS = "overrideBackPress";
29
 
30
 
30
     @SuppressWarnings("ConstantConditions")
31
     @SuppressWarnings("ConstantConditions")
31
     public static ScreenParams parse(Bundle params) {
32
     public static ScreenParams parse(Bundle params) {
42
 
43
 
43
         result.title = params.getString(KEY_TITLE);
44
         result.title = params.getString(KEY_TITLE);
44
         result.rightButtons = parseRightButton(params);
45
         result.rightButtons = parseRightButton(params);
46
+        result.overrideBackPressInJs = params.getBoolean(OVERRIDE_BACK_PRESS, false);
45
         result.leftButton = parseLeftButton(params);
47
         result.leftButton = parseLeftButton(params);
46
 
48
 
47
         result.topTabParams = parseTopTabs(params);
49
         result.topTabParams = parseTopTabs(params);

+ 9
- 3
android/app/src/main/java/com/reactnativenavigation/screens/Screen.java View File

66
         if (screenParams.leftButton != null) {
66
         if (screenParams.leftButton != null) {
67
             screenParams.leftButton.setColorFromScreenStyle(screenParams.styleParams.titleBarButtonColor);
67
             screenParams.leftButton.setColorFromScreenStyle(screenParams.styleParams.titleBarButtonColor);
68
         }
68
         }
69
-        topBar.addTitleBarAndSetButtons(screenParams.rightButtons, screenParams.leftButton,
70
-                leftButtonOnClickListener, screenParams.navigatorEventId);
69
+        topBar.addTitleBarAndSetButtons(screenParams.rightButtons,
70
+                screenParams.leftButton,
71
+                leftButtonOnClickListener,
72
+                screenParams.navigatorEventId,
73
+                screenParams.overrideBackPressInJs);
71
     }
74
     }
72
 
75
 
73
     private void createTopBar() {
76
     private void createTopBar() {
140
 
143
 
141
     public void setTitleBarLeftButton(String navigatorEventId, LeftButtonOnClickListener backButtonListener,
144
     public void setTitleBarLeftButton(String navigatorEventId, LeftButtonOnClickListener backButtonListener,
142
                                       TitleBarLeftButtonParams titleBarLeftButtonParams) {
145
                                       TitleBarLeftButtonParams titleBarLeftButtonParams) {
143
-        topBar.setTitleBarRightButton(navigatorEventId, backButtonListener, titleBarLeftButtonParams);
146
+        topBar.setTitleBarLeftButton(navigatorEventId,
147
+                backButtonListener,
148
+                titleBarLeftButtonParams,
149
+                screenParams.overrideBackPressInJs);
144
     }
150
     }
145
 
151
 
146
     public StyleParams getStyleParams() {
152
     public StyleParams getStyleParams() {

+ 10
- 0
android/app/src/main/java/com/reactnativenavigation/screens/ScreenStack.java View File

5
 import android.view.View;
5
 import android.view.View;
6
 import android.widget.RelativeLayout;
6
 import android.widget.RelativeLayout;
7
 
7
 
8
+import com.reactnativenavigation.NavigationApplication;
8
 import com.reactnativenavigation.params.ScreenParams;
9
 import com.reactnativenavigation.params.ScreenParams;
9
 import com.reactnativenavigation.params.StyleParams;
10
 import com.reactnativenavigation.params.StyleParams;
10
 import com.reactnativenavigation.params.TitleBarButtonParams;
11
 import com.reactnativenavigation.params.TitleBarButtonParams;
201
         return stack.peek().getStyleParams();
202
         return stack.peek().getStyleParams();
202
     }
203
     }
203
 
204
 
205
+    public boolean handleBackPressInJs() {
206
+        ScreenParams currentScreen = stack.peek().screenParams;
207
+        if (currentScreen.overrideBackPressInJs) {
208
+            NavigationApplication.instance.sendNavigatorEvent("backPress", currentScreen.navigatorEventId);
209
+            return true;
210
+        }
211
+        return false;
212
+    }
213
+
204
     private void performOnScreen(String screenInstanceId, Task<Screen> task) {
214
     private void performOnScreen(String screenInstanceId, Task<Screen> task) {
205
         if (stack.isEmpty()) {
215
         if (stack.isEmpty()) {
206
             return;
216
             return;

+ 13
- 2
android/app/src/main/java/com/reactnativenavigation/views/LeftButton.java View File

21
     private TitleBarLeftButtonParams params;
21
     private TitleBarLeftButtonParams params;
22
     private final LeftButtonOnClickListener onClickListener;
22
     private final LeftButtonOnClickListener onClickListener;
23
     private final String navigatorEventId;
23
     private final String navigatorEventId;
24
+    private final boolean overrideBackPressInJs;
24
 
25
 
25
     public LeftButton(Context context,
26
     public LeftButton(Context context,
26
                       TitleBarLeftButtonParams params,
27
                       TitleBarLeftButtonParams params,
27
                       LeftButtonOnClickListener onClickListener,
28
                       LeftButtonOnClickListener onClickListener,
28
-                      String navigatorEventId) {
29
+                      String navigatorEventId,
30
+                      boolean overrideBackPressInJs) {
29
         super(context, getColor(params), Stroke.THIN);
31
         super(context, getColor(params), Stroke.THIN);
30
         this.params = params;
32
         this.params = params;
31
         this.onClickListener = onClickListener;
33
         this.onClickListener = onClickListener;
32
         this.navigatorEventId = navigatorEventId;
34
         this.navigatorEventId = navigatorEventId;
35
+        this.overrideBackPressInJs = overrideBackPressInJs;
33
         setInitialState();
36
         setInitialState();
34
     }
37
     }
35
 
38
 
44
     @Override
47
     @Override
45
     public void onClick(View v) {
48
     public void onClick(View v) {
46
         if (isBackButton()) {
49
         if (isBackButton()) {
47
-            onClickListener.onTitleBarBackButtonClick();
50
+            handleBackButtonClick();
48
         } else if (isSideMenuButton()) {
51
         } else if (isSideMenuButton()) {
49
             onClickListener.onSideMenuButtonClick();
52
             onClickListener.onSideMenuButtonClick();
50
         } else {
53
         } else {
52
         }
55
         }
53
     }
56
     }
54
 
57
 
58
+    private void handleBackButtonClick() {
59
+        if (overrideBackPressInJs) {
60
+            NavigationApplication.instance.sendNavigatorEvent("backPress", navigatorEventId);
61
+        } else {
62
+            onClickListener.onTitleBarBackButtonClick();
63
+        }
64
+    }
65
+
55
     private void setInitialState() {
66
     private void setInitialState() {
56
         if (params != null) {
67
         if (params != null) {
57
             setIconState(params.iconState);
68
             setIconState(params.iconState);

+ 8
- 4
android/app/src/main/java/com/reactnativenavigation/views/TitleBar.java View File

46
     }
46
     }
47
 
47
 
48
     public void setLeftButton(TitleBarLeftButtonParams leftButtonParams,
48
     public void setLeftButton(TitleBarLeftButtonParams leftButtonParams,
49
-                              LeftButtonOnClickListener leftButtonOnClickListener, String navigatorEventId) {
49
+                              LeftButtonOnClickListener leftButtonOnClickListener, String navigatorEventId, boolean overrideBackPressInJs) {
50
         if (shouldSetLeftButton(leftButtonParams)) {
50
         if (shouldSetLeftButton(leftButtonParams)) {
51
-            createAndSetLeftButton(leftButtonParams, leftButtonOnClickListener, navigatorEventId);
51
+            createAndSetLeftButton(leftButtonParams, leftButtonOnClickListener, navigatorEventId, overrideBackPressInJs);
52
         } else if (hasLeftButton()) {
52
         } else if (hasLeftButton()) {
53
             updateLeftButton(leftButtonParams);
53
             updateLeftButton(leftButtonParams);
54
         }
54
         }
101
         return leftButton == null && leftButtonParams != null;
101
         return leftButton == null && leftButtonParams != null;
102
     }
102
     }
103
 
103
 
104
-    private void createAndSetLeftButton(TitleBarLeftButtonParams leftButtonParams, LeftButtonOnClickListener leftButtonOnClickListener, String navigatorEventId) {
105
-        leftButton = new LeftButton(getContext(), leftButtonParams, leftButtonOnClickListener, navigatorEventId);
104
+    private void createAndSetLeftButton(TitleBarLeftButtonParams leftButtonParams,
105
+                                        LeftButtonOnClickListener leftButtonOnClickListener,
106
+                                        String navigatorEventId,
107
+                                        boolean overrideBackPressInJs) {
108
+        leftButton = new LeftButton(getContext(), leftButtonParams, leftButtonOnClickListener, navigatorEventId,
109
+                overrideBackPressInJs);
106
         setNavigationOnClickListener(leftButton);
110
         setNavigationOnClickListener(leftButton);
107
         setNavigationIcon(leftButton);
111
         setNavigationIcon(leftButton);
108
     }
112
     }

+ 8
- 6
android/app/src/main/java/com/reactnativenavigation/views/TopBar.java View File

25
     public void addTitleBarAndSetButtons(List<TitleBarButtonParams> rightButtons,
25
     public void addTitleBarAndSetButtons(List<TitleBarButtonParams> rightButtons,
26
                                          TitleBarLeftButtonParams leftButton,
26
                                          TitleBarLeftButtonParams leftButton,
27
                                          LeftButtonOnClickListener leftButtonOnClickListener,
27
                                          LeftButtonOnClickListener leftButtonOnClickListener,
28
-                                         String navigatorEventId) {
28
+                                         String navigatorEventId, boolean overrideBackPressInJs) {
29
         titleBar = new TitleBar(getContext());
29
         titleBar = new TitleBar(getContext());
30
         addView(titleBar);
30
         addView(titleBar);
31
         titleBar.setRightButtons(rightButtons, navigatorEventId);
31
         titleBar.setRightButtons(rightButtons, navigatorEventId);
32
-        titleBar.setLeftButton(leftButton, leftButtonOnClickListener, navigatorEventId);
32
+        titleBar.setLeftButton(leftButton, leftButtonOnClickListener, navigatorEventId, overrideBackPressInJs);
33
     }
33
     }
34
 
34
 
35
     public void setTitle(String title) {
35
     public void setTitle(String title) {
55
         return topTabs;
55
         return topTabs;
56
     }
56
     }
57
 
57
 
58
-    public void setTitleBarRightButton(String navigatorEventId,
59
-                                       LeftButtonOnClickListener leftButtonOnClickListener,
60
-                                       TitleBarLeftButtonParams titleBarLeftButtonParams) {
61
-        titleBar.setLeftButton(titleBarLeftButtonParams, leftButtonOnClickListener, navigatorEventId);
58
+    public void setTitleBarLeftButton(String navigatorEventId,
59
+                                      LeftButtonOnClickListener leftButtonOnClickListener,
60
+                                      TitleBarLeftButtonParams titleBarLeftButtonParams,
61
+                                      boolean overrideBackPressInJs) {
62
+        titleBar.setLeftButton(titleBarLeftButtonParams, leftButtonOnClickListener, navigatorEventId,
63
+                overrideBackPressInJs);
62
     }
64
     }
63
 
65
 
64
     private void setTopTabsStyle(StyleParams style) {
66
     private void setTopTabsStyle(StyleParams style) {

+ 5
- 1
src/deprecated/platformSpecificDeprecated.android.js View File

28
   params.screen = adaptNavigationParams(screen);
28
   params.screen = adaptNavigationParams(screen);
29
   params.appStyle = convertStyleParams(params.appStyle);
29
   params.appStyle = convertStyleParams(params.appStyle);
30
   params.sideMenu = convertDrawerParamsToSideMenuParams(params.drawer);
30
   params.sideMenu = convertDrawerParamsToSideMenuParams(params.drawer);
31
+  params.overrideBackPress = screen.overrideBackPress;
31
 
32
 
32
   newPlatformSpecific.startApp(params);
33
   newPlatformSpecific.startApp(params);
33
 }
34
 }
50
   params.screenId = params.screen;
51
   params.screenId = params.screen;
51
   let adapted = adaptNavigationStyleToScreenStyle(params);
52
   let adapted = adaptNavigationStyleToScreenStyle(params);
52
   adapted = adaptNavigationParams(adapted);
53
   adapted = adaptNavigationParams(adapted);
53
-
54
+  adapted.overrideBackPress = params.overrideBackPress;
55
+  
54
   newPlatformSpecific.push(adapted);
56
   newPlatformSpecific.push(adapted);
55
 }
57
 }
56
 
58
 
178
 
180
 
179
     let newtab = adaptNavigationStyleToScreenStyle(tab);
181
     let newtab = adaptNavigationStyleToScreenStyle(tab);
180
     newtab = adaptNavigationParams(tab);
182
     newtab = adaptNavigationParams(tab);
183
+    newtab.overrideBackPress = tab.overrideBackPress;
181
     newTabs.push(newtab);
184
     newTabs.push(newtab);
182
   });
185
   });
183
   params.tabs = newTabs;
186
   params.tabs = newTabs;
282
   params.screenId = params.screen;
285
   params.screenId = params.screen;
283
   let adapted = adaptNavigationStyleToScreenStyle(params);
286
   let adapted = adaptNavigationStyleToScreenStyle(params);
284
   adapted = adaptNavigationParams(adapted);
287
   adapted = adaptNavigationParams(adapted);
288
+  adapted.overrideBackPress = params.overrideBackPress;
285
 
289
 
286
   newPlatformSpecific.showModal(adapted);
290
   newPlatformSpecific.showModal(adapted);
287
 }
291
 }