Browse Source

Unify enable/enabled options

In some places “enable” was used to control certain options. This commit adds backwards compatible support
for “enabled” property which should be used from now on.
Guy Carmeli 6 years ago
parent
commit
01b378df14

+ 5
- 0
docs/docs/styling.md View File

@@ -287,6 +287,7 @@ For example, changing the animation used when the app is first launched (Support
287 287
 Navigation.setDefaultOptions({
288 288
   animations: {
289 289
     setRoot: {
290
+      enabled: 'true' | 'false', // Optional, used to enable/disable the animation
290 291
       alpha: {
291 292
         from: 0,
292 293
         to: 1,
@@ -315,6 +316,7 @@ When *pushing* and *popping* screens to and from a stack, you can control the To
315 316
 ```js
316 317
 animations: {
317 318
   push: {
319
+    enabled: 'true' | 'false', // Optional, used to enable/disable the animation
318 320
     topBar: {
319 321
       id: 'TEST', // Optional, id of the TopBar we'd like to animate.
320 322
       alpha: {
@@ -334,6 +336,9 @@ animations: {
334 336
         to: 1
335 337
       }
336 338
     }
339
+  },
340
+  pop: {
341
+    ...
337 342
   }
338 343
 }
339 344
 ```

+ 6
- 5
lib/android/app/src/main/java/com/reactnativenavigation/parse/AnimationOptions.java View File

@@ -33,7 +33,8 @@ public class AnimationOptions {
33 33
                     options.id = TextParser.parse(json, key);
34 34
                     break;
35 35
                 case "enable":
36
-                    options.enable = BoolParser.parse(json, key);
36
+                case "enabled":
37
+                    options.enabled = BoolParser.parse(json, key);
37 38
                     break;
38 39
                 case "waitForRender":
39 40
                     options.waitForRender = BoolParser.parse(json, key);
@@ -47,26 +48,26 @@ public class AnimationOptions {
47 48
     }
48 49
 
49 50
     public Text id = new NullText();
50
-    public Bool enable = new NullBool();
51
+    public Bool enabled = new NullBool();
51 52
     public Bool waitForRender = new NullBool();
52 53
     private HashSet<ValueAnimationOptions> valueOptions = new HashSet<>();
53 54
 
54 55
     void mergeWith(AnimationOptions other) {
55 56
         if (other.id.hasValue()) id = other.id;
56
-        if (other.enable.hasValue()) enable = other.enable;
57
+        if (other.enabled.hasValue()) enabled = other.enabled;
57 58
         if (other.waitForRender.hasValue()) waitForRender = other.waitForRender;
58 59
         if (!other.valueOptions.isEmpty()) valueOptions = other.valueOptions;
59 60
     }
60 61
 
61 62
     void mergeWithDefault(AnimationOptions defaultOptions) {
62 63
         if (!id.hasValue()) id = defaultOptions.id;
63
-        if (!enable.hasValue()) enable = defaultOptions.enable;
64
+        if (!enabled.hasValue()) enabled = defaultOptions.enabled;
64 65
         if (!waitForRender.hasValue()) waitForRender = defaultOptions.waitForRender;
65 66
         if (valueOptions.isEmpty()) valueOptions = defaultOptions.valueOptions;
66 67
     }
67 68
 
68 69
     public boolean hasValue() {
69
-        return id.hasValue() || enable.hasValue() || waitForRender.hasValue();
70
+        return id.hasValue() || enabled.hasValue() || waitForRender.hasValue();
70 71
     }
71 72
 
72 73
     public AnimatorSet getAnimation(View view) {

+ 4
- 4
lib/android/app/src/main/java/com/reactnativenavigation/parse/NestedAnimationsOptions.java View File

@@ -15,13 +15,13 @@ public class NestedAnimationsOptions {
15 15
         options.content = AnimationOptions.parse(json.optJSONObject("content"));
16 16
         options.bottomTabs = AnimationOptions.parse(json.optJSONObject("bottomTabs"));
17 17
         options.topBar = AnimationOptions.parse(json.optJSONObject("topBar"));
18
-        options.enable = BoolParser.parse(json, "enable");
18
+        options.enabled = BoolParser.parseFirst(json, "enabled", "enable");
19 19
         options.waitForRender = BoolParser.parse(json, "waitForRender");
20 20
 
21 21
         return options;
22 22
     }
23 23
 
24
-    public Bool enable = new NullBool();
24
+    public Bool enabled = new NullBool();
25 25
     public Bool waitForRender = new NullBool();
26 26
     public AnimationOptions content = new AnimationOptions();
27 27
     public AnimationOptions bottomTabs = new AnimationOptions();
@@ -31,7 +31,7 @@ public class NestedAnimationsOptions {
31 31
         topBar.mergeWith(other.topBar);
32 32
         content.mergeWith(other.content);
33 33
         bottomTabs.mergeWith(other.bottomTabs);
34
-        if (other.enable.hasValue()) enable = other.enable;
34
+        if (other.enabled.hasValue()) enabled = other.enabled;
35 35
         if (other.waitForRender.hasValue()) waitForRender = other.waitForRender;
36 36
     }
37 37
 
@@ -39,7 +39,7 @@ public class NestedAnimationsOptions {
39 39
         content.mergeWithDefault(defaultOptions.content);
40 40
         bottomTabs.mergeWithDefault(defaultOptions.bottomTabs);
41 41
         topBar.mergeWithDefault(defaultOptions.topBar);
42
-        if (!enable.hasValue()) enable = defaultOptions.enable;
42
+        if (!enabled.hasValue()) enabled = defaultOptions.enabled;
43 43
         if (!waitForRender.hasValue()) waitForRender = defaultOptions.waitForRender;
44 44
     }
45 45
 

+ 11
- 2
lib/android/app/src/main/java/com/reactnativenavigation/parse/parsers/BoolParser.java View File

@@ -5,8 +5,17 @@ import com.reactnativenavigation.parse.params.NullBool;
5 5
 
6 6
 import org.json.JSONObject;
7 7
 
8
+import java.util.Arrays;
9
+
10
+import static com.reactnativenavigation.utils.CollectionUtils.first;
11
+
8 12
 public class BoolParser {
9
-    public static Bool parse(JSONObject json, String bool) {
10
-        return json.has(bool) ? new Bool(json.optBoolean(bool)) : new NullBool();
13
+    public static Bool parse(JSONObject json, String key) {
14
+        return json.has(key) ? new Bool(json.optBoolean(key)) : new NullBool();
15
+    }
16
+
17
+    public static Bool parseFirst(JSONObject json, String... keys) {
18
+        String first = first(Arrays.asList(keys), json::has);
19
+        return first != null ? new Bool(json.optBoolean(first)) : new NullBool();
11 20
     }
12 21
 }

+ 3
- 3
lib/android/app/src/main/java/com/reactnativenavigation/presentation/StackPresenter.java View File

@@ -226,14 +226,14 @@ public class StackPresenter {
226 226
 
227 227
     private void applyTopBarVisibility(TopBarOptions options, AnimationsOptions animationOptions, Options componentOptions) {
228 228
         if (options.visible.isFalse()) {
229
-            if (options.animate.isTrueOrUndefined() && componentOptions.animations.push.enable.isTrueOrUndefined()) {
229
+            if (options.animate.isTrueOrUndefined() && componentOptions.animations.push.enabled.isTrueOrUndefined()) {
230 230
                 topBar.hideAnimate(animationOptions.pop.topBar);
231 231
             } else {
232 232
                 topBar.hide();
233 233
             }
234 234
         }
235 235
         if (options.visible.isTrueOrUndefined()) {
236
-            if (options.animate.isTrueOrUndefined() && componentOptions.animations.push.enable.isTrueOrUndefined()) {
236
+            if (options.animate.isTrueOrUndefined() && componentOptions.animations.push.enabled.isTrueOrUndefined()) {
237 237
                 topBar.showAnimate(animationOptions.push.topBar);
238 238
             } else {
239 239
                 topBar.show();
@@ -301,7 +301,7 @@ public class StackPresenter {
301 301
 
302 302
     public void onChildWillAppear(Options appearing, Options disappearing) {
303 303
         if (disappearing.topBar.visible.isTrueOrUndefined() && appearing.topBar.visible.isFalse()) {
304
-            if (disappearing.topBar.animate.isTrueOrUndefined() && disappearing.animations.pop.enable.isTrueOrUndefined()) {
304
+            if (disappearing.topBar.animate.isTrueOrUndefined() && disappearing.animations.pop.enabled.isTrueOrUndefined()) {
305 305
                 topBar.hideAnimate(disappearing.animations.pop.topBar);
306 306
             } else {
307 307
                 topBar.hide();

+ 8
- 0
lib/android/app/src/main/java/com/reactnativenavigation/utils/CollectionUtils.java View File

@@ -75,6 +75,14 @@ public class CollectionUtils {
75 75
         }
76 76
     }
77 77
 
78
+    public static @Nullable <T> T first(@Nullable Collection<T> items, Filter<T> by) {
79
+        if (isNullOrEmpty(items)) return null;
80
+        for (T item : items) {
81
+            if (by.filter(item)) return item;
82
+        }
83
+        return null;
84
+    }
85
+
78 86
     private static @NonNull <T> Collection<T> get(@Nullable Collection<T> t) {
79 87
         return t == null ? Collections.EMPTY_LIST : t;
80 88
     }

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/modal/ModalPresenter.java View File

@@ -38,7 +38,7 @@ public class ModalPresenter {
38 38
         Options options = toAdd.resolveCurrentOptions(defaultOptions);
39 39
         toAdd.setWaitForRender(options.animations.showModal.waitForRender);
40 40
         modalsContainer.addView(toAdd.getView());
41
-        if (options.animations.showModal.enable.isTrueOrUndefined()) {
41
+        if (options.animations.showModal.enabled.isTrueOrUndefined()) {
42 42
             if (options.animations.showModal.waitForRender.isTrue()) {
43 43
                 toAdd.setOnAppearedListener(() -> animateShow(toAdd, toRemove, listener, options));
44 44
             } else {
@@ -83,7 +83,7 @@ public class ModalPresenter {
83 83
             listener.onError("Can not dismiss modal before activity is created");
84 84
             return;
85 85
         }
86
-        if (toDismiss.options.animations.dismissModal.enable.isTrueOrUndefined()) {
86
+        if (toDismiss.options.animations.dismissModal.enabled.isTrueOrUndefined()) {
87 87
             animator.dismiss(toDismiss.getView(), toDismiss.options.animations.dismissModal, new AnimatorListenerAdapter() {
88 88
                 @Override
89 89
                 public void onAnimationEnd(Animator animation) {

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackController.java View File

@@ -144,7 +144,7 @@ public class StackController extends ParentController<StackLayout> {
144 144
         addChildToStack(child, child.getView(), resolvedOptions);
145 145
 
146 146
         if (toRemove != null) {
147
-            if (resolvedOptions.animations.push.enable.isTrueOrUndefined()) {
147
+            if (resolvedOptions.animations.push.enabled.isTrueOrUndefined()) {
148 148
                 if (resolvedOptions.animations.push.waitForRender.isTrue()) {
149 149
                     child.getView().setAlpha(0);
150 150
                     child.setOnAppearedListener(() -> animator.push(child.getView(), resolvedOptions.animations.push, resolvedOptions.transitions, toRemove.getElements(), child.getElements(), () -> {
@@ -218,7 +218,7 @@ public class StackController extends ParentController<StackLayout> {
218 218
             getView().addView(appearingView, 0);
219 219
         }
220 220
         presenter.onChildWillAppear(appearing.options, disappearing.options);
221
-        if (disappearing.options.animations.pop.enable.isTrueOrUndefined()) {
221
+        if (disappearing.options.animations.pop.enabled.isTrueOrUndefined()) {
222 222
             animator.pop(disappearing.getView(), resolvedOptions.animations.pop, () -> finishPopping(disappearing, listener));
223 223
         } else {
224 224
             finishPopping(disappearing, listener);

+ 4
- 4
lib/android/app/src/test/java/com/reactnativenavigation/BaseTest.java View File

@@ -56,13 +56,13 @@ public abstract class BaseTest {
56 56
 
57 57
     protected void disablePushAnimation(ViewController... controllers) {
58 58
         for (ViewController controller : controllers) {
59
-            controller.options.animations.push.enable = new Bool(false);
59
+            controller.options.animations.push.enabled = new Bool(false);
60 60
         }
61 61
     }
62 62
 
63 63
     protected void disablePopAnimation(ViewController... controllers) {
64 64
         for (ViewController controller : controllers) {
65
-            controller.options.animations.pop.enable = new Bool(false);
65
+            controller.options.animations.pop.enabled = new Bool(false);
66 66
         }
67 67
     }
68 68
 
@@ -73,13 +73,13 @@ public abstract class BaseTest {
73 73
 
74 74
     protected void disableShowModalAnimation(ViewController... modals) {
75 75
         for (ViewController modal : modals) {
76
-            modal.options.animations.showModal.enable = new Bool(false);
76
+            modal.options.animations.showModal.enabled = new Bool(false);
77 77
         }
78 78
     }
79 79
 
80 80
     protected void disableDismissModalAnimation(ViewController... modals) {
81 81
         for (ViewController modal : modals) {
82
-            modal.options.animations.dismissModal.enable = new Bool(false);
82
+            modal.options.animations.dismissModal.enabled = new Bool(false);
83 83
         }
84 84
     }
85 85
 

+ 28
- 0
lib/android/app/src/test/java/com/reactnativenavigation/parse/parsers/BoolParserTest.java View File

@@ -0,0 +1,28 @@
1
+package com.reactnativenavigation.parse.parsers;
2
+
3
+import com.reactnativenavigation.BaseTest;
4
+
5
+import org.json.JSONException;
6
+import org.json.JSONObject;
7
+import org.junit.Test;
8
+
9
+import static org.assertj.core.api.Java6Assertions.assertThat;
10
+
11
+public class BoolParserTest extends BaseTest {
12
+
13
+    @Test
14
+    public void parse() throws JSONException {
15
+        JSONObject json = new JSONObject();
16
+        json.put("value", true);
17
+        assertThat(BoolParser.parse(json, "value").get()).isTrue();
18
+    }
19
+
20
+    @Test
21
+    public void parseFirst() throws JSONException {
22
+        JSONObject json = new JSONObject();
23
+        json.put("value1", true);
24
+        json.put("value2", false);
25
+        assertThat(BoolParser.parseFirst(json, "value1", "value2").get()).isTrue();
26
+        assertThat(BoolParser.parseFirst(json, "value2", "value1").get()).isFalse();
27
+    }
28
+}

+ 1
- 1
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/modal/ModalPresenterTest.java View File

@@ -89,7 +89,7 @@ public class ModalPresenterTest extends BaseTest {
89 89
     @Test
90 90
     public void showModal_resolvesDefaultOptions() throws JSONException {
91 91
         Options defaultOptions = new Options();
92
-        JSONObject disabledShowModalAnimation = new JSONObject().put("enable", false);
92
+        JSONObject disabledShowModalAnimation = new JSONObject().put("enabled", false);
93 93
         defaultOptions.animations.showModal = AnimationOptions.parse(disabledShowModalAnimation);
94 94
 
95 95
         uut.setDefaultOptions(defaultOptions);

+ 3
- 3
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/navigator/NavigatorTest.java View File

@@ -433,8 +433,8 @@ public class NavigatorTest extends BaseTest {
433 433
 
434 434
     @Test
435 435
     public void pushedStackCanBePopped() {
436
-        child1.options.animations.push.enable = new Bool(false);
437
-        child2.options.animations.push.enable = new Bool(false);
436
+        child1.options.animations.push.enabled = new Bool(false);
437
+        child2.options.animations.push.enabled = new Bool(false);
438 438
         StackController spy = spy(parentController);
439 439
         StackController parent = newStack();
440 440
         parent.ensureViewIsCreated();
@@ -577,7 +577,7 @@ public class NavigatorTest extends BaseTest {
577 577
         disablePushAnimation(child1);
578 578
 
579 579
         StackController spy = spy(parentController);
580
-        spy.options.animations.setRoot.enable = new Bool(false);
580
+        spy.options.animations.setRoot.enabled = new Bool(false);
581 581
         uut.setRoot(spy, new CommandListenerAdapter());
582 582
         spy.push(child1, new CommandListenerAdapter());
583 583
         activityController.destroy();

+ 14
- 14
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/stack/StackControllerTest.java View File

@@ -402,7 +402,7 @@ public class StackControllerTest extends BaseTest {
402 402
 
403 403
         child1.options.topBar.visible = new Bool(false);
404 404
         child1.options.topBar.animate = new Bool(false);
405
-        child1.options.animations.push.enable = new Bool(false);
405
+        child1.options.animations.push.enabled = new Bool(false);
406 406
 
407 407
         uut.push(child1, new CommandListenerAdapter());
408 408
         uut.push(child2, new CommandListenerAdapter() {
@@ -433,7 +433,7 @@ public class StackControllerTest extends BaseTest {
433 433
 
434 434
     @Test
435 435
     public void pop_appearingChildHasCorrectLayoutParams() {
436
-        child2.options.animations.pop.enable = new Bool(false);
436
+        child2.options.animations.pop.enabled = new Bool(false);
437 437
         child1.options.topBar.drawBehind = new Bool(false);
438 438
 
439 439
         StackController uut = createStack(Arrays.asList(child1, child2));
@@ -514,8 +514,8 @@ public class StackControllerTest extends BaseTest {
514 514
 
515 515
     @Test
516 516
     public void popToRoot_PopsEverythingAboveFirstController() {
517
-        child1.options.animations.push.enable = new Bool(false);
518
-        child2.options.animations.push.enable = new Bool(false);
517
+        child1.options.animations.push.enabled = new Bool(false);
518
+        child2.options.animations.push.enabled = new Bool(false);
519 519
 
520 520
         uut.push(child1, new CommandListenerAdapter());
521 521
         uut.push(child2, new CommandListenerAdapter());
@@ -538,8 +538,8 @@ public class StackControllerTest extends BaseTest {
538 538
 
539 539
     @Test
540 540
     public void popToRoot_onlyTopChildIsAnimated() {
541
-        child1.options.animations.push.enable = new Bool(false);
542
-        child2.options.animations.push.enable = new Bool(false);
541
+        child1.options.animations.push.enabled = new Bool(false);
542
+        child2.options.animations.push.enabled = new Bool(false);
543 543
 
544 544
         uut.push(child1, new CommandListenerAdapter());
545 545
         uut.push(child2, new CommandListenerAdapter());
@@ -558,9 +558,9 @@ public class StackControllerTest extends BaseTest {
558 558
 
559 559
     @Test
560 560
     public void popToRoot_topChildrenAreDestroyed() {
561
-        child1.options.animations.push.enable = new Bool(false);
562
-        child2.options.animations.push.enable = new Bool(false);
563
-        child3.options.animations.push.enable = new Bool(false);
561
+        child1.options.animations.push.enabled = new Bool(false);
562
+        child2.options.animations.push.enabled = new Bool(false);
563
+        child3.options.animations.push.enabled = new Bool(false);
564 564
 
565 565
         uut.push(child1, new CommandListenerAdapter());
566 566
         uut.push(child2, new CommandListenerAdapter());
@@ -633,8 +633,8 @@ public class StackControllerTest extends BaseTest {
633 633
 
634 634
     @Test
635 635
     public void pop_callWillAppearWillDisappear() {
636
-        child1.options.animations.push.enable = new Bool(false);
637
-        child2.options.animations.push.enable = new Bool(false);
636
+        child1.options.animations.push.enabled = new Bool(false);
637
+        child2.options.animations.push.enabled = new Bool(false);
638 638
         child1 = spy(child1);
639 639
         child2 = spy(child2);
640 640
         uut.push(child1, new CommandListenerAdapter());
@@ -649,8 +649,8 @@ public class StackControllerTest extends BaseTest {
649 649
         uut.ensureViewIsCreated();
650 650
 
651 651
         child1.options.topBar.visible = new Bool(false);
652
-        child1.options.animations.push.enable = new Bool(false);
653
-        child2.options.animations.push.enable = new Bool(true);
652
+        child1.options.animations.push.enabled = new Bool(false);
653
+        child2.options.animations.push.enabled = new Bool(true);
654 654
         uut.push(child1, new CommandListenerAdapter() {
655 655
             @Override
656 656
             public void onSuccess(String childId) {
@@ -678,7 +678,7 @@ public class StackControllerTest extends BaseTest {
678 678
 
679 679
         child1.options.topBar.visible = new Bool(false);
680 680
         child1.options.topBar.animate = new Bool(false);
681
-        child2.options.animations.push.enable = new Bool(false);
681
+        child2.options.animations.push.enabled = new Bool(false);
682 682
         child2.options.topBar.animate = new Bool(false);
683 683
 
684 684
         child1.ensureViewIsCreated();