Bladeren bron

Catch ScreenStackNotFoundException

Perform all stack related commands in performOnStack which
doesn't crash when requested stack is no found
Guy Carmeli 7 jaren geleden
bovenliggende
commit
a8c8c9068d
1 gewijzigde bestanden met toevoegingen van 43 en 24 verwijderingen
  1. 43
    24
      android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java

+ 43
- 24
android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java Bestand weergeven

@@ -32,6 +32,7 @@ import com.reactnativenavigation.params.TitleBarLeftButtonParams;
32 32
 import com.reactnativenavigation.screens.NavigationType;
33 33
 import com.reactnativenavigation.screens.Screen;
34 34
 import com.reactnativenavigation.screens.ScreenStack;
35
+import com.reactnativenavigation.utils.Task;
35 36
 import com.reactnativenavigation.utils.ViewUtils;
36 37
 import com.reactnativenavigation.views.BottomTabs;
37 38
 import com.reactnativenavigation.views.LightBox;
@@ -333,14 +334,13 @@ public class BottomTabsLayout extends BaseLayout implements AHBottomNavigation.O
333 334
         bottomTabs.setCurrentItem(index);
334 335
     }
335 336
 
336
-    public void selectBottomTabByNavigatorId(String navigatorId) {
337
-        try {
338
-            bottomTabs.setCurrentItem(getScreenStackIndex(navigatorId));
339
-        } catch (ScreenStackNotFoundException e) {
340
-            Log.e("BottomTabsLayout", "Could not select bottom tab by navigatorId [" + navigatorId + "]." +
341
-                                      "This should not have happened, it probably means navigator.switchToTab()" +
342
-                                      "was called from an unmounted tab.");
343
-        }
337
+    public void selectBottomTabByNavigatorId(final String navigatorId) {
338
+        performOnStack(navigatorId, new Task<ScreenStack>() {
339
+            @Override
340
+            public void run(ScreenStack param) {
341
+                bottomTabs.setCurrentItem(getScreenStackIndex(navigatorId));
342
+            }
343
+        });
344 344
     }
345 345
 
346 346
     private boolean hasBackgroundColor(StyleParams params) {
@@ -358,14 +358,18 @@ public class BottomTabsLayout extends BaseLayout implements AHBottomNavigation.O
358 358
     }
359 359
 
360 360
     @Override
361
-    public void push(ScreenParams params) {
362
-        ScreenStack screenStack = getScreenStack(params.getNavigatorId());
363
-        screenStack.push(params, createScreenLayoutParams(params));
364
-        setStyleFromScreen(params.styleParams);
365
-        if (isCurrentStack(screenStack)) {
366
-            alignSnackbarContainerWithBottomTabs((LayoutParams) snackbarAndFabContainer.getLayoutParams(), params.styleParams);
367
-            EventBus.instance.post(new ScreenChangedEvent(params));
368
-        }
361
+    public void push(final ScreenParams params) {
362
+        performOnStack(params.getNavigatorId(), new Task<ScreenStack>() {
363
+            @Override
364
+            public void run(ScreenStack screenStack) {
365
+                screenStack.push(params, createScreenLayoutParams(params));
366
+                setStyleFromScreen(params.styleParams);
367
+                if (isCurrentStack(screenStack)) {
368
+                    alignSnackbarContainerWithBottomTabs((LayoutParams) snackbarAndFabContainer.getLayoutParams(), params.styleParams);
369
+                    EventBus.instance.post(new ScreenChangedEvent(params));
370
+                }
371
+            }
372
+        });
369 373
     }
370 374
 
371 375
     @Override
@@ -394,13 +398,17 @@ public class BottomTabsLayout extends BaseLayout implements AHBottomNavigation.O
394 398
 
395 399
     @Override
396 400
     public void newStack(final ScreenParams params) {
397
-        ScreenStack screenStack = getScreenStack(params.getNavigatorId());
398
-        screenStack.newStack(params, createScreenLayoutParams(params));
399
-        if (isCurrentStack(screenStack)) {
400
-            setStyleFromScreen(params.styleParams);
401
-            alignSnackbarContainerWithBottomTabs((LayoutParams) snackbarAndFabContainer.getLayoutParams(), params.styleParams);
402
-            EventBus.instance.post(new ScreenChangedEvent(params));
403
-        }
401
+        performOnStack(params.getNavigatorId(), new Task<ScreenStack>() {
402
+            @Override
403
+            public void run(ScreenStack screenStack) {
404
+                screenStack.newStack(params, createScreenLayoutParams(params));
405
+                if (isCurrentStack(screenStack)) {
406
+                    setStyleFromScreen(params.styleParams);
407
+                    alignSnackbarContainerWithBottomTabs((LayoutParams) snackbarAndFabContainer.getLayoutParams(), params.styleParams);
408
+                    EventBus.instance.post(new ScreenChangedEvent(params));
409
+                }
410
+            }
411
+        });
404 412
     }
405 413
 
406 414
     private void alignSnackbarContainerWithBottomTabs(LayoutParams lp, StyleParams styleParams) {
@@ -411,6 +419,17 @@ public class BottomTabsLayout extends BaseLayout implements AHBottomNavigation.O
411 419
         }
412 420
     }
413 421
 
422
+    private void performOnStack(String navigatorId, Task<ScreenStack> task) {
423
+        try {
424
+            ScreenStack screenStack = getScreenStack(navigatorId);
425
+            task.run(screenStack);
426
+        } catch (ScreenStackNotFoundException e) {
427
+            Log.e("Navigation", "Could not perform action on stack [" + navigatorId + "]." +
428
+                                      "This should not have happened, it probably means a navigator action" +
429
+                                      "was called from an unmounted tab.");
430
+        }
431
+    }
432
+
414 433
     @Override
415 434
     public void destroy() {
416 435
         snackbarAndFabContainer.destroy();
@@ -485,7 +504,7 @@ public class BottomTabsLayout extends BaseLayout implements AHBottomNavigation.O
485 504
 
486 505
     private
487 506
     @NonNull
488
-    ScreenStack getScreenStack(String navigatorId) {
507
+    ScreenStack getScreenStack(String navigatorId) throws ScreenStackNotFoundException {
489 508
         int index = getScreenStackIndex(navigatorId);
490 509
         return screenStacks[index];
491 510
     }