Browse Source

Set button testID (#2660)

Guy Carmeli 6 years ago
parent
commit
e64e4f0486
No account linked to committer's email address

+ 3
- 1
lib/android/app/src/main/java/com/reactnativenavigation/parse/Button.java View File

@@ -19,7 +19,8 @@ public class Button {
19 19
 	@ColorInt public int buttonColor;
20 20
 	public int buttonFontSize;
21 21
 	public Text buttonFontWeight;
22
-	public Text icon;
22
+	public Text icon = new NullText();
23
+	public Text testId;
23 24
 
24 25
 	private static Button parseJson(JSONObject json)  {
25 26
 		Button button = new Button();
@@ -31,6 +32,7 @@ public class Button {
31 32
 		button.buttonColor = json.optInt("buttonColor", NO_INT_VALUE);
32 33
 		button.buttonFontSize = json.optInt("buttonFontSize", NO_INT_VALUE);
33 34
 		button.buttonFontWeight = TextParser.parse(json, "buttonFontWeight");
35
+        button.testId = TextParser.parse(json, "testID");
34 36
 
35 37
 		if (json.has("icon")) {
36 38
 			button.icon = TextParser.parse(json.optJSONObject("icon"), "uri");

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

@@ -0,0 +1,15 @@
1
+package com.reactnativenavigation.utils;
2
+
3
+public class ArraryUtils {
4
+    public static boolean isNullOrEmpty(Object[] array) {
5
+        return array == null || array.length == 0;
6
+    }
7
+
8
+    public static boolean containes(Object[] array, Object item) {
9
+        if (isNullOrEmpty(array)) return false;
10
+        for (Object o : array) {
11
+            if (o == item) return true;
12
+        }
13
+        return false;
14
+    }
15
+}

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

@@ -4,6 +4,9 @@ import android.support.annotation.Nullable;
4 4
 import android.view.View;
5 5
 import android.view.ViewGroup;
6 6
 
7
+import java.util.ArrayList;
8
+import java.util.List;
9
+
7 10
 public class ViewUtils {
8 11
     @Nullable
9 12
     public static <T> T findChildByClass(ViewGroup root, Class clazz) {
@@ -22,4 +25,15 @@ public class ViewUtils {
22 25
         }
23 26
         return null;
24 27
     }
28
+
29
+    public static <T> List<T> findChildrenByClass(ViewGroup root, Class clazz) {
30
+        List<T> ret = new ArrayList<>();
31
+        for (int i = 0; i < root.getChildCount(); i++) {
32
+            View view = root.getChildAt(i);
33
+            if (clazz.isAssignableFrom(view.getClass())) {
34
+                ret.add((T) view);
35
+            }
36
+        }
37
+        return ret;
38
+    }
25 39
 }

+ 6
- 1
lib/android/app/src/main/java/com/reactnativenavigation/views/BottomTabs.java View File

@@ -5,6 +5,7 @@ import android.content.Context;
5 5
 
6 6
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
7 7
 import com.reactnativenavigation.parse.BottomTabsOptions;
8
+import com.reactnativenavigation.parse.Text;
8 9
 import com.reactnativenavigation.utils.CompatUtils;
9 10
 
10 11
 @SuppressLint("ViewConstructor")
@@ -12,6 +13,10 @@ public class BottomTabs extends AHBottomNavigation {
12 13
     public BottomTabs(Context context, BottomTabsOptions bottomTabsOptions) {
13 14
         super(context);
14 15
         setId(CompatUtils.generateViewId());
15
-        setTag(bottomTabsOptions.testId.get(""));
16
+        setTestId(bottomTabsOptions.testId);
17
+    }
18
+
19
+    private void setTestId(Text testId) {
20
+        if (testId.hasValue()) setTag(testId.get());
16 21
     }
17 22
 }

+ 47
- 13
lib/android/app/src/main/java/com/reactnativenavigation/views/TitleBarButton.java View File

@@ -4,6 +4,7 @@ import android.content.Context;
4 4
 import android.graphics.Color;
5 5
 import android.graphics.drawable.Drawable;
6 6
 import android.support.annotation.NonNull;
7
+import android.support.v7.widget.ActionMenuView;
7 8
 import android.support.v7.widget.Toolbar;
8 9
 import android.text.Spannable;
9 10
 import android.text.SpannableString;
@@ -12,14 +13,19 @@ import android.util.Log;
12 13
 import android.view.Menu;
13 14
 import android.view.MenuItem;
14 15
 import android.view.View;
16
+import android.widget.ImageButton;
15 17
 import android.widget.TextView;
16 18
 
17 19
 import com.reactnativenavigation.parse.Button;
18 20
 import com.reactnativenavigation.parse.Options;
21
+import com.reactnativenavigation.parse.Text;
22
+import com.reactnativenavigation.utils.ArraryUtils;
19 23
 import com.reactnativenavigation.utils.ImageLoader;
20 24
 import com.reactnativenavigation.utils.UiUtils;
25
+import com.reactnativenavigation.utils.ViewUtils;
21 26
 
22 27
 import java.util.ArrayList;
28
+import java.util.List;
23 29
 
24 30
 public class TitleBarButton implements MenuItem.OnMenuItemClickListener {
25 31
     public interface OnClickListener {
@@ -49,9 +55,11 @@ public class TitleBarButton implements MenuItem.OnMenuItemClickListener {
49 55
 			setTextColor();
50 56
 			setFontSize(menuItem);
51 57
 		}
52
-	}
53 58
 
54
-	void applyNavigationIcon(Context context) {
59
+        setTestId(button.testId);
60
+    }
61
+
62
+    void applyNavigationIcon(Context context) {
55 63
 		if (!hasIcon()) {
56 64
 			Log.w("RNN", "Left button needs to have an icon");
57 65
 			return;
@@ -64,7 +72,8 @@ public class TitleBarButton implements MenuItem.OnMenuItemClickListener {
64 72
                 setIconColor();
65 73
                 setNavigationClickListener();
66 74
                 toolbar.setNavigationIcon(icon);
67
-			}
75
+                setLeftButtonTestId();
76
+            }
68 77
 
69 78
 			@Override
70 79
 			public void onError(Throwable error) {
@@ -73,7 +82,17 @@ public class TitleBarButton implements MenuItem.OnMenuItemClickListener {
73 82
 		});
74 83
 	}
75 84
 
76
-	private void applyIcon(Context context, final MenuItem menuItem) {
85
+    private void setLeftButtonTestId() {
86
+        if (!button.testId.hasValue()) return;
87
+        toolbar.post(() -> {
88
+            ImageButton leftButton = ViewUtils.findChildByClass(toolbar, ImageButton.class);
89
+            if (leftButton != null) {
90
+                leftButton.setTag(button.testId.get());
91
+            }
92
+        });
93
+    }
94
+
95
+    private void applyIcon(Context context, final MenuItem menuItem) {
77 96
         new ImageLoader().loadIcon(context, button.icon.get(), new ImageLoader.ImageLoadingListener() {
78 97
 			@Override
79 98
 			public void onComplete(@NonNull Drawable drawable) {
@@ -104,7 +123,7 @@ public class TitleBarButton implements MenuItem.OnMenuItemClickListener {
104 123
 
105 124
 	private void setTextColor() {
106 125
 		UiUtils.runOnPreDrawOnce(this.toolbar, () -> {
107
-            ArrayList<View> outViews = findActualTextViewInMenuByLabel();
126
+            ArrayList<View> outViews = findActualTextViewInMenuByText();
108 127
             setTextColorForFoundButtonViews(outViews);
109 128
         });
110 129
 	}
@@ -125,13 +144,6 @@ public class TitleBarButton implements MenuItem.OnMenuItemClickListener {
125 144
 		return true;
126 145
 	}
127 146
 
128
-	@NonNull
129
-	private ArrayList<View> findActualTextViewInMenuByLabel() {
130
-		ArrayList<View> outViews = new ArrayList<>();
131
-		this.toolbar.findViewsWithText(outViews, button.title.get(), View.FIND_VIEWS_WITH_TEXT);
132
-		return outViews;
133
-	}
134
-
135 147
 	private void setTextColorForFoundButtonViews(ArrayList<View> buttons) {
136 148
 		for (View button : buttons) {
137 149
 			((TextView) button).setTextColor(this.button.buttonColor);
@@ -139,6 +151,28 @@ public class TitleBarButton implements MenuItem.OnMenuItemClickListener {
139 151
 	}
140 152
 
141 153
 	private boolean hasIcon() {
142
-		return button.icon != null;
154
+		return button.icon.hasValue();
143 155
 	}
156
+
157
+    private void setTestId(Text testId) {
158
+        if (!testId.hasValue()) return;
159
+        UiUtils.runOnPreDrawOnce(this.toolbar, () -> {
160
+            ActionMenuView buttonsLayout = ViewUtils.findChildByClass(toolbar, ActionMenuView.class);
161
+            List<TextView> buttons = ViewUtils.findChildrenByClass(buttonsLayout, TextView.class);
162
+            for (TextView view : buttons) {
163
+                if (button.title.hasValue() && button.title.get().equals(view.getText())) {
164
+                    view.setTag(testId.get());
165
+                } else if (button.icon.hasValue() && ArraryUtils.containes(view.getCompoundDrawables(), icon)) {
166
+                    view.setTag(testId.get());
167
+                }
168
+            }
169
+        });
170
+    }
171
+
172
+    @NonNull
173
+    private ArrayList<View> findActualTextViewInMenuByText() {
174
+        ArrayList<View> outViews = new ArrayList<>();
175
+        this.toolbar.findViewsWithText(outViews, button.title.get(), View.FIND_VIEWS_WITH_TEXT);
176
+        return outViews;
177
+    }
144 178
 }