Browse Source

Catch ScreenStackNotFoundException

Perform all stack related commands in performOnStack which
doesn't crash when requested stack is no found
Guy Carmeli 7 years ago
parent
commit
a8c8c9068d

+ 43
- 24
android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java View File

32
 import com.reactnativenavigation.screens.NavigationType;
32
 import com.reactnativenavigation.screens.NavigationType;
33
 import com.reactnativenavigation.screens.Screen;
33
 import com.reactnativenavigation.screens.Screen;
34
 import com.reactnativenavigation.screens.ScreenStack;
34
 import com.reactnativenavigation.screens.ScreenStack;
35
+import com.reactnativenavigation.utils.Task;
35
 import com.reactnativenavigation.utils.ViewUtils;
36
 import com.reactnativenavigation.utils.ViewUtils;
36
 import com.reactnativenavigation.views.BottomTabs;
37
 import com.reactnativenavigation.views.BottomTabs;
37
 import com.reactnativenavigation.views.LightBox;
38
 import com.reactnativenavigation.views.LightBox;
333
         bottomTabs.setCurrentItem(index);
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
     private boolean hasBackgroundColor(StyleParams params) {
346
     private boolean hasBackgroundColor(StyleParams params) {
358
     }
358
     }
359
 
359
 
360
     @Override
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
     @Override
375
     @Override
394
 
398
 
395
     @Override
399
     @Override
396
     public void newStack(final ScreenParams params) {
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
     private void alignSnackbarContainerWithBottomTabs(LayoutParams lp, StyleParams styleParams) {
414
     private void alignSnackbarContainerWithBottomTabs(LayoutParams lp, StyleParams styleParams) {
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
     @Override
433
     @Override
415
     public void destroy() {
434
     public void destroy() {
416
         snackbarAndFabContainer.destroy();
435
         snackbarAndFabContainer.destroy();
485
 
504
 
486
     private
505
     private
487
     @NonNull
506
     @NonNull
488
-    ScreenStack getScreenStack(String navigatorId) {
507
+    ScreenStack getScreenStack(String navigatorId) throws ScreenStackNotFoundException {
489
         int index = getScreenStackIndex(navigatorId);
508
         int index = getScreenStackIndex(navigatorId);
490
         return screenStacks[index];
509
         return screenStacks[index];
491
     }
510
     }