Browse Source

Only set elevation values from Options

This fixes a bug which occurred when returning from background - sometimes the system would set
an unexpected elevation value which seems like the default elevation.
This commit checks if the new elevation value was specified by RNN before setting it.
Guy Carmeli 5 years ago
parent
commit
487c1da9dc

+ 10
- 4
lib/android/app/src/main/java/com/reactnativenavigation/views/topbar/TopBar.java View File

@@ -50,6 +50,7 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
50 50
     private FrameLayout root;
51 51
     private View border;
52 52
     private View component;
53
+    private float elevation = -1;
53 54
 
54 55
     public TopBar(final Context context, StackLayout parentView) {
55 56
         super(context);
@@ -195,7 +196,7 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
195 196
 
196 197
     public void setTopTabsHeight(int height) {
197 198
         if (topTabs.getLayoutParams().height == height) return;
198
-        topTabs.getLayoutParams().height = height > 0 ? (int) UiUtils.dpToPx(getContext(), height) : height;
199
+        topTabs.getLayoutParams().height = height > 0 ? UiUtils.dpToPx(getContext(), height) : height;
199 200
         topTabs.setLayoutParams(topTabs.getLayoutParams());
200 201
     }
201 202
 
@@ -212,12 +213,17 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
212 213
     }
213 214
 
214 215
     public void setElevation(Double elevation) {
215
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP &&
216
-                getElevation() != elevation.floatValue()) {
217
-            setElevation(UiUtils.dpToPx(getContext(), elevation.floatValue()));
216
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && getElevation() != elevation.floatValue()) {
217
+            this.elevation = UiUtils.dpToPx(getContext(), elevation.floatValue());
218
+            setElevation(this.elevation);
218 219
         }
219 220
     }
220 221
 
222
+    @Override
223
+    public void setElevation(float elevation) {
224
+        if (elevation == this.elevation) super.setElevation(elevation);
225
+    }
226
+
221 227
     public Toolbar getTitleBar() {
222 228
         return titleBar;
223 229
     }

+ 15
- 1
lib/android/app/src/test/java/com/reactnativenavigation/views/TopBarTest.java View File

@@ -5,10 +5,12 @@ import android.app.Activity;
5 5
 import com.reactnativenavigation.BaseTest;
6 6
 import com.reactnativenavigation.anim.TopBarAnimator;
7 7
 import com.reactnativenavigation.parse.AnimationOptions;
8
+import com.reactnativenavigation.utils.UiUtils;
8 9
 import com.reactnativenavigation.viewcontrollers.topbar.TopBarController;
9 10
 import com.reactnativenavigation.views.topbar.TopBar;
10 11
 
11 12
 import org.junit.Test;
13
+import org.robolectric.annotation.Config;
12 14
 
13 15
 import static org.assertj.core.api.Java6Assertions.assertThat;
14 16
 import static org.mockito.ArgumentMatchers.any;
@@ -17,15 +19,17 @@ import static org.mockito.Mockito.spy;
17 19
 import static org.mockito.Mockito.times;
18 20
 import static org.mockito.Mockito.verify;
19 21
 
22
+@Config(qualifiers = "xxhdpi")
20 23
 public class TopBarTest extends BaseTest {
21 24
 
22 25
     private TopBar uut;
23 26
     private TopBarAnimator animator;
27
+    private Activity activity;
24 28
 
25 29
     @SuppressWarnings("Convert2Lambda")
26 30
     @Override
27 31
     public void beforeEach() {
28
-        Activity activity = newActivity();
32
+        activity = newActivity();
29 33
         StackLayout parent = new StackLayout(activity, new TopBarController(), null);
30 34
         uut = new TopBar(activity, parent);
31 35
         animator = spy(new TopBarAnimator(uut));
@@ -54,4 +58,14 @@ public class TopBarTest extends BaseTest {
54 58
         uut.showAnimate(options);
55 59
         verify(animator, times(1)).show(options);
56 60
     }
61
+
62
+    @Test
63
+    public void setElevation_ignoreValuesNotSetByNavigation() {
64
+        float initialElevation = uut.getElevation();
65
+        uut.setElevation(1f);
66
+        assertThat(uut.getElevation()).isEqualTo(initialElevation);
67
+
68
+        uut.setElevation(Double.valueOf(2));
69
+        assertThat(uut.getElevation()).isEqualTo(UiUtils.dpToPx(activity, 2));
70
+    }
57 71
 }