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,6 +105,10 @@ public class BottomTabsLayout extends RelativeLayout implements Layout, AHBottom
105 105
 
106 106
     @Override
107 107
     public boolean onBackPressed() {
108
+        if (getCurrentScreenStack().handleBackPressInJs()) {
109
+            return true;
110
+        }
111
+
108 112
         if (getCurrentScreenStack().canPop()) {
109 113
             getCurrentScreenStack().pop(true);
110 114
             setBottomTabsStyleFromCurrentScreen();

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

@@ -68,6 +68,10 @@ public class SingleScreenLayout extends RelativeLayout implements Layout {
68 68
 
69 69
     @Override
70 70
     public boolean onBackPressed() {
71
+        if (stack.handleBackPressInJs()) {
72
+            return true;
73
+        }
74
+
71 75
         if (stack.canPop()) {
72 76
             stack.pop(true);
73 77
             return true;

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

@@ -9,6 +9,7 @@ public class ScreenParams {
9 9
     public String screenId;
10 10
     public List<TitleBarButtonParams> rightButtons;
11 11
     public TitleBarLeftButtonParams leftButton;
12
+    public boolean overrideBackPressInJs;
12 13
     public String title;
13 14
     public StyleParams styleParams;
14 15
     public List<TopTabParams> topTabParams;

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

@@ -4,6 +4,7 @@ import com.balysv.materialmenu.MaterialMenuDrawable;
4 4
 
5 5
 public class TitleBarLeftButtonParams extends TitleBarButtonParams {
6 6
     public MaterialMenuDrawable.IconState iconState;
7
+    public boolean overrideBackPressInJs;
7 8
 
8 9
     public TitleBarLeftButtonParams(TitleBarButtonParams params) {
9 10
         icon = params.icon;
@@ -15,4 +16,8 @@ public class TitleBarLeftButtonParams extends TitleBarButtonParams {
15 16
     public boolean isBackButton() {
16 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,6 +26,7 @@ public class ScreenParamsParser extends Parser {
26 26
     private static final String TOP_TABS = "topTabs";
27 27
     private static final String FRAGMENT_CREATOR_CLASS_NAME = "fragmentCreatorClassName";
28 28
     private static final String FRAGMENT_CREATOR_PASS_PROPS = "fragmentCreatorPassProps";
29
+    public static final String OVERRIDE_BACK_PRESS = "overrideBackPress";
29 30
 
30 31
     @SuppressWarnings("ConstantConditions")
31 32
     public static ScreenParams parse(Bundle params) {
@@ -42,6 +43,7 @@ public class ScreenParamsParser extends Parser {
42 43
 
43 44
         result.title = params.getString(KEY_TITLE);
44 45
         result.rightButtons = parseRightButton(params);
46
+        result.overrideBackPressInJs = params.getBoolean(OVERRIDE_BACK_PRESS, false);
45 47
         result.leftButton = parseLeftButton(params);
46 48
 
47 49
         result.topTabParams = parseTopTabs(params);

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

@@ -66,8 +66,11 @@ public abstract class Screen extends RelativeLayout {
66 66
         if (screenParams.leftButton != null) {
67 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 76
     private void createTopBar() {
@@ -140,7 +143,10 @@ public abstract class Screen extends RelativeLayout {
140 143
 
141 144
     public void setTitleBarLeftButton(String navigatorEventId, LeftButtonOnClickListener backButtonListener,
142 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 152
     public StyleParams getStyleParams() {

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

@@ -5,6 +5,7 @@ import android.support.v7.app.AppCompatActivity;
5 5
 import android.view.View;
6 6
 import android.widget.RelativeLayout;
7 7
 
8
+import com.reactnativenavigation.NavigationApplication;
8 9
 import com.reactnativenavigation.params.ScreenParams;
9 10
 import com.reactnativenavigation.params.StyleParams;
10 11
 import com.reactnativenavigation.params.TitleBarButtonParams;
@@ -201,6 +202,15 @@ public class ScreenStack {
201 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 214
     private void performOnScreen(String screenInstanceId, Task<Screen> task) {
205 215
         if (stack.isEmpty()) {
206 216
             return;

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

@@ -21,15 +21,18 @@ public class LeftButton extends MaterialMenuDrawable implements View.OnClickList
21 21
     private TitleBarLeftButtonParams params;
22 22
     private final LeftButtonOnClickListener onClickListener;
23 23
     private final String navigatorEventId;
24
+    private final boolean overrideBackPressInJs;
24 25
 
25 26
     public LeftButton(Context context,
26 27
                       TitleBarLeftButtonParams params,
27 28
                       LeftButtonOnClickListener onClickListener,
28
-                      String navigatorEventId) {
29
+                      String navigatorEventId,
30
+                      boolean overrideBackPressInJs) {
29 31
         super(context, getColor(params), Stroke.THIN);
30 32
         this.params = params;
31 33
         this.onClickListener = onClickListener;
32 34
         this.navigatorEventId = navigatorEventId;
35
+        this.overrideBackPressInJs = overrideBackPressInJs;
33 36
         setInitialState();
34 37
     }
35 38
 
@@ -44,7 +47,7 @@ public class LeftButton extends MaterialMenuDrawable implements View.OnClickList
44 47
     @Override
45 48
     public void onClick(View v) {
46 49
         if (isBackButton()) {
47
-            onClickListener.onTitleBarBackButtonClick();
50
+            handleBackButtonClick();
48 51
         } else if (isSideMenuButton()) {
49 52
             onClickListener.onSideMenuButtonClick();
50 53
         } else {
@@ -52,6 +55,14 @@ public class LeftButton extends MaterialMenuDrawable implements View.OnClickList
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 66
     private void setInitialState() {
56 67
         if (params != null) {
57 68
             setIconState(params.iconState);

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

@@ -46,9 +46,9 @@ public class TitleBar extends Toolbar {
46 46
     }
47 47
 
48 48
     public void setLeftButton(TitleBarLeftButtonParams leftButtonParams,
49
-                              LeftButtonOnClickListener leftButtonOnClickListener, String navigatorEventId) {
49
+                              LeftButtonOnClickListener leftButtonOnClickListener, String navigatorEventId, boolean overrideBackPressInJs) {
50 50
         if (shouldSetLeftButton(leftButtonParams)) {
51
-            createAndSetLeftButton(leftButtonParams, leftButtonOnClickListener, navigatorEventId);
51
+            createAndSetLeftButton(leftButtonParams, leftButtonOnClickListener, navigatorEventId, overrideBackPressInJs);
52 52
         } else if (hasLeftButton()) {
53 53
             updateLeftButton(leftButtonParams);
54 54
         }
@@ -101,8 +101,12 @@ public class TitleBar extends Toolbar {
101 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 110
         setNavigationOnClickListener(leftButton);
107 111
         setNavigationIcon(leftButton);
108 112
     }

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

@@ -25,11 +25,11 @@ public class TopBar extends AppBarLayout {
25 25
     public void addTitleBarAndSetButtons(List<TitleBarButtonParams> rightButtons,
26 26
                                          TitleBarLeftButtonParams leftButton,
27 27
                                          LeftButtonOnClickListener leftButtonOnClickListener,
28
-                                         String navigatorEventId) {
28
+                                         String navigatorEventId, boolean overrideBackPressInJs) {
29 29
         titleBar = new TitleBar(getContext());
30 30
         addView(titleBar);
31 31
         titleBar.setRightButtons(rightButtons, navigatorEventId);
32
-        titleBar.setLeftButton(leftButton, leftButtonOnClickListener, navigatorEventId);
32
+        titleBar.setLeftButton(leftButton, leftButtonOnClickListener, navigatorEventId, overrideBackPressInJs);
33 33
     }
34 34
 
35 35
     public void setTitle(String title) {
@@ -55,10 +55,12 @@ public class TopBar extends AppBarLayout {
55 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 66
     private void setTopTabsStyle(StyleParams style) {

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

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