Quellcode durchsuchen

Null check tabTestId

Guy Carmeli vor 6 Jahren
Ursprung
Commit
770a001ba9

+ 13
- 0
lib/android/app/src/main/java/com/reactnativenavigation/utils/ObjectUtils.java Datei anzeigen

@@ -0,0 +1,13 @@
1
+package com.reactnativenavigation.utils;
2
+
3
+import android.support.annotation.Nullable;
4
+
5
+public class ObjectUtils {
6
+    public interface Action<T> {
7
+        void performOn(T obj);
8
+    }
9
+
10
+    public static <T> void perform(@Nullable T obj, Action<T> action) {
11
+        if (obj != null) action.performOn(obj);
12
+    }
13
+}

+ 7
- 5
lib/android/app/src/main/java/com/reactnativenavigation/views/BottomTabs.java Datei anzeigen

@@ -4,13 +4,14 @@ import android.annotation.SuppressLint;
4 4
 import android.content.Context;
5 5
 import android.support.annotation.ColorInt;
6 6
 import android.support.annotation.IntRange;
7
-import android.view.View;
8 7
 
9 8
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
10 9
 import com.reactnativenavigation.BuildConfig;
11 10
 import com.reactnativenavigation.parse.params.Text;
12 11
 import com.reactnativenavigation.utils.CompatUtils;
13 12
 
13
+import static com.reactnativenavigation.utils.ObjectUtils.perform;
14
+
14 15
 @SuppressLint("ViewConstructor")
15 16
 public class BottomTabs extends AHBottomNavigation {
16 17
     public BottomTabs(Context context) {
@@ -20,10 +21,11 @@ public class BottomTabs extends AHBottomNavigation {
20 21
     }
21 22
 
22 23
     public void setTabTestId(int index, Text testId) {
23
-        if (!testId.hasValue()) return;
24
-        View view = getViewAtPosition(index);
25
-        view.setTag(testId.get());
26
-        if (BuildConfig.DEBUG) view.setContentDescription(testId.get());
24
+        if (!testId.hasValue() ) return;
25
+        perform(getViewAtPosition(index), view -> {
26
+            view.setTag(testId.get());
27
+            if (BuildConfig.DEBUG) view.setContentDescription(testId.get());
28
+        });
27 29
     }
28 30
 
29 31
     public void setBadge(int bottomTabIndex, String badge) {