소스 검색

disabledColor android

Guy Carmeli 6 년 전
부모
커밋
911adb141b

+ 4
- 2
lib/android/app/src/main/java/com/reactnativenavigation/parse/params/Button.java 파일 보기

@@ -19,7 +19,8 @@ public class Button {
19 19
     public Bool enabled = new NullBool();
20 20
     public Bool disableIconTint = new NullBool();
21 21
     public int showAsAction;
22
-    public Color buttonColor = new NullColor();
22
+    public Color color = new NullColor();
23
+    public Color disabledColor = new NullColor();
23 24
     public Number buttonFontSize = new NullNumber();
24 25
     private Text buttonFontWeight = new NullText();
25 26
     public Text icon = new NullText();
@@ -33,7 +34,8 @@ public class Button {
33 34
         button.enabled = BoolParser.parse(json, "enabled");
34 35
         button.disableIconTint = BoolParser.parse(json, "disableIconTint");
35 36
         button.showAsAction = parseShowAsAction(json);
36
-        button.buttonColor = ColorParser.parse(json, "buttonColor");
37
+        button.color = ColorParser.parse(json, "buttonColor");
38
+        button.disabledColor = ColorParser.parse(json, "disabledColor");
37 39
         button.buttonFontSize = NumberParser.parse(json, "buttonFontSize");
38 40
         button.buttonFontWeight = TextParser.parse(json, "buttonFontWeight");
39 41
         button.testId = TextParser.parse(json, "testID");

+ 11
- 0
lib/android/app/src/main/java/com/reactnativenavigation/utils/DrawableTinter.java 파일 보기

@@ -0,0 +1,11 @@
1
+package com.reactnativenavigation.utils;
2
+
3
+import android.graphics.PorterDuff;
4
+import android.graphics.PorterDuffColorFilter;
5
+import android.graphics.drawable.Drawable;
6
+
7
+public class DrawableTinter {
8
+    public void tint(Drawable drawable, int tint) {
9
+        drawable.setColorFilter(new PorterDuffColorFilter(tint, PorterDuff.Mode.SRC_IN));
10
+    }
11
+}

+ 0
- 7
lib/android/app/src/main/java/com/reactnativenavigation/utils/UiUtils.java 파일 보기

@@ -2,9 +2,6 @@ package com.reactnativenavigation.utils;
2 2
 
3 3
 import android.content.Context;
4 4
 import android.content.res.Resources;
5
-import android.graphics.PorterDuff;
6
-import android.graphics.PorterDuffColorFilter;
7
-import android.graphics.drawable.Drawable;
8 5
 import android.os.Build;
9 6
 import android.os.Handler;
10 7
 import android.os.Looper;
@@ -30,10 +27,6 @@ public class UiUtils {
30 27
         });
31 28
     }
32 29
 
33
-	public static void tintDrawable(Drawable drawable, int tint) {
34
-		drawable.setColorFilter(new PorterDuffColorFilter(tint, PorterDuff.Mode.SRC_IN));
35
-	}
36
-
37 30
 	public static void runOnMainThread(Runnable runnable) {
38 31
 		new Handler(Looper.getMainLooper()).post(runnable);
39 32
 	}

+ 19
- 14
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/TopBarButtonController.java 파일 보기

@@ -19,6 +19,7 @@ import com.reactnativenavigation.parse.Options;
19 19
 import com.reactnativenavigation.parse.params.Button;
20 20
 import com.reactnativenavigation.parse.params.Text;
21 21
 import com.reactnativenavigation.utils.ArrayUtils;
22
+import com.reactnativenavigation.utils.DrawableTinter;
22 23
 import com.reactnativenavigation.utils.ImageLoader;
23 24
 import com.reactnativenavigation.utils.ImageLoadingListenerAdapter;
24 25
 import com.reactnativenavigation.utils.UiUtils;
@@ -33,13 +34,17 @@ public class TopBarButtonController extends ViewController<TitleBarReactButtonVi
33 34
         void onPress(String buttonId);
34 35
     }
35 36
 
37
+    private final ImageLoader imageLoader;
38
+    private DrawableTinter drawableTinter;
36 39
     private final Button button;
37 40
     private final ReactViewCreator viewCreator;
38 41
     private TopBarButtonController.OnClickListener onPressListener;
39 42
     private Drawable icon;
40 43
 
41
-    public TopBarButtonController(Activity activity, Button button, ReactViewCreator viewCreator, TopBarButtonController.OnClickListener onClickListener) {
44
+    public TopBarButtonController(Activity activity, ImageLoader imageLoader, DrawableTinter drawableTinter, Button button, ReactViewCreator viewCreator, OnClickListener onClickListener) {
42 45
         super(activity, button.id, new Options());
46
+        this.imageLoader = imageLoader;
47
+        this.drawableTinter = drawableTinter;
43 48
         this.button = button;
44 49
         this.viewCreator = viewCreator;
45 50
         this.onPressListener = onClickListener;
@@ -79,7 +84,7 @@ public class TopBarButtonController extends ViewController<TitleBarReactButtonVi
79 84
             return;
80 85
         }
81 86
 
82
-        new ImageLoader().loadIcon(toolbar.getContext(), button.icon.get(), new ImageLoader.ImageLoadingListener() {
87
+        imageLoader.loadIcon(toolbar.getContext(), button.icon.get(), new ImageLoader.ImageLoadingListener() {
83 88
             @Override
84 89
             public void onComplete(@NonNull Drawable drawable) {
85 90
                 icon = drawable;
@@ -132,18 +137,15 @@ public class TopBarButtonController extends ViewController<TitleBarReactButtonVi
132 137
     }
133 138
 
134 139
     private void loadIcon(ImageLoader.ImageLoadingListener callbacks) {
135
-        new ImageLoader().loadIcon(getActivity(), button.icon.get(), callbacks);
140
+        imageLoader.loadIcon(getActivity(), button.icon.get(), callbacks);
136 141
     }
137 142
 
138 143
     private void setIconColor(Drawable icon) {
139
-        if (button.enabled.isTrueOrUndefined() && button.buttonColor.hasValue()) {
140
-            UiUtils.tintDrawable(icon, button.buttonColor.get());
141
-            return;
142
-        }
143
-        if (button.disableIconTint.isTrue() && button.buttonColor.hasValue()) {
144
-            UiUtils.tintDrawable(icon, button.buttonColor.get());
145
-        } else {
146
-            UiUtils.tintDrawable(icon, Color.LTGRAY);
144
+        if (button.disableIconTint.isTrue()) return;
145
+        if (button.enabled.isTrueOrUndefined() && button.color.hasValue()) {
146
+            drawableTinter.tint(icon, button.color.get());
147
+        } else if (button.enabled.isFalse()) {
148
+            drawableTinter.tint(icon, button.disabledColor.get(Color.LTGRAY));
147 149
         }
148 150
     }
149 151
 
@@ -162,9 +164,12 @@ public class TopBarButtonController extends ViewController<TitleBarReactButtonVi
162 164
     }
163 165
 
164 166
     private void setTextColorForFoundButtonViews(ArrayList<View> buttons) {
165
-        for (View button : buttons) {
166
-            if (this.button.buttonColor.hasValue())
167
-                ((TextView) button).setTextColor(this.button.buttonColor.get());
167
+        for (View btn : buttons) {
168
+            if (button.enabled.isTrueOrUndefined() && button.color.hasValue()) {
169
+                ((TextView) btn).setTextColor(this.button.color.get());
170
+            } else if (button.enabled.isFalse()) {
171
+                ((TextView) btn).setTextColor(button.disabledColor.get(Color.LTGRAY));
172
+            }
168 173
         }
169 174
     }
170 175
 

+ 3
- 1
lib/android/app/src/main/java/com/reactnativenavigation/views/titlebar/TitleBar.java 파일 보기

@@ -13,6 +13,8 @@ import com.reactnativenavigation.parse.Alignment;
13 13
 import com.reactnativenavigation.parse.Component;
14 14
 import com.reactnativenavigation.parse.params.Button;
15 15
 import com.reactnativenavigation.parse.params.Color;
16
+import com.reactnativenavigation.utils.DrawableTinter;
17
+import com.reactnativenavigation.utils.ImageLoader;
16 18
 import com.reactnativenavigation.utils.UiUtils;
17 19
 import com.reactnativenavigation.utils.ViewUtils;
18 20
 import com.reactnativenavigation.viewcontrollers.ReactViewCreator;
@@ -190,7 +192,7 @@ public class TitleBar extends Toolbar {
190 192
     }
191 193
 
192 194
     public TopBarButtonController createButtonController(Button button) {
193
-        return new TopBarButtonController((Activity) getContext(), button, buttonCreator, onClickListener);
195
+        return new TopBarButtonController((Activity) getContext(), new ImageLoader(), new DrawableTinter(), button, buttonCreator, onClickListener);
194 196
     }
195 197
 
196 198
     public Toolbar.LayoutParams getComponentLayoutParams(Component component) {

+ 2
- 2
lib/android/app/src/test/java/com/reactnativenavigation/utils/TitleBarHelper.java 파일 보기

@@ -1,11 +1,11 @@
1 1
 package com.reactnativenavigation.utils;
2 2
 
3
+import android.support.v7.view.menu.ActionMenuItemView;
3 4
 import android.support.v7.widget.Toolbar;
4 5
 import android.view.View;
5
-import android.widget.TextView;
6 6
 
7 7
 public class TitleBarHelper {
8 8
     public static View getRightButton(Toolbar toolbar, int index) {
9
-        return (View) ViewUtils.findChildrenByClassRecursive(toolbar, TextView.class).get(toolbar.getMenu().size() - index - 1);
9
+        return (View) ViewUtils.findChildrenByClassRecursive(toolbar, ActionMenuItemView.class).get(toolbar.getMenu().size() - index - 1);
10 10
     }
11 11
 }

+ 79
- 16
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/TopBarButtonControllerTest.java 파일 보기

@@ -1,62 +1,125 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Activity;
4
-import android.support.annotation.NonNull;
4
+import android.support.v7.widget.Toolbar;
5
+import android.view.MenuItem;
5 6
 
6 7
 import com.reactnativenavigation.BaseTest;
8
+import com.reactnativenavigation.mocks.ImageLoaderMock;
7 9
 import com.reactnativenavigation.mocks.TitleBarReactViewCreatorMock;
8 10
 import com.reactnativenavigation.mocks.TopBarBackgroundViewCreatorMock;
9 11
 import com.reactnativenavigation.mocks.TopBarButtonCreatorMock;
10 12
 import com.reactnativenavigation.parse.Options;
13
+import com.reactnativenavigation.parse.params.Bool;
11 14
 import com.reactnativenavigation.parse.params.Button;
15
+import com.reactnativenavigation.parse.params.Color;
16
+import com.reactnativenavigation.parse.params.NullText;
12 17
 import com.reactnativenavigation.parse.params.Text;
18
+import com.reactnativenavigation.utils.DrawableTinter;
13 19
 import com.reactnativenavigation.viewcontrollers.topbar.TopBarBackgroundViewController;
14 20
 import com.reactnativenavigation.viewcontrollers.topbar.TopBarController;
15 21
 
16
-import org.junit.Ignore;
17 22
 import org.junit.Test;
18 23
 
24
+import static org.assertj.core.api.Java6Assertions.assertThat;
25
+import static org.mockito.ArgumentMatchers.any;
26
+import static org.mockito.ArgumentMatchers.eq;
19 27
 import static org.mockito.Mockito.spy;
20 28
 import static org.mockito.Mockito.times;
21 29
 import static org.mockito.Mockito.verify;
30
+import static org.mockito.Mockito.verifyZeroInteractions;
22 31
 
23 32
 public class TopBarButtonControllerTest extends BaseTest {
24 33
 
25 34
     private TopBarButtonController uut;
26 35
     private StackController stackController;
36
+    private Button button;
37
+    private DrawableTinter drawableTinter = spy(new DrawableTinter());
27 38
 
28 39
     @Override
29 40
     public void beforeEach() {
30
-        Button button = createButton();
41
+        button = new Button();
31 42
         final Activity activity = newActivity();
32 43
 
33 44
         TopBarButtonCreatorMock buttonCreatorMock = new TopBarButtonCreatorMock();
34
-        uut = spy(new TopBarButtonController(activity, button, buttonCreatorMock, (buttonId) -> {}));
35
-        stackController = spy(new StackController(activity, buttonCreatorMock, new TitleBarReactViewCreatorMock(), new TopBarBackgroundViewController(activity, new TopBarBackgroundViewCreatorMock()), new TopBarController(), "stack", new Options()));
36
-
45
+        uut = new TopBarButtonController(activity, ImageLoaderMock.mock(), drawableTinter, button, buttonCreatorMock, (buttonId) -> {});
46
+        stackController = spy(new StackController(activity,
47
+                buttonCreatorMock,
48
+                new TitleBarReactViewCreatorMock(),
49
+                new TopBarBackgroundViewController(activity, new TopBarBackgroundViewCreatorMock()),
50
+                new TopBarController(),
51
+                "stack",
52
+                new Options())
53
+        );
54
+        stackController.getView().layout(0, 0, 1080, 1920);
55
+        stackController.getTopBar().layout(0, 0, 1080, 200);
56
+        stackController.getTopBar().getTitleBar().layout(0, 0, 1080, 200);
37 57
     }
38 58
 
39 59
     @Test
40 60
     public void buttonDoesNotClearStackOptionsOnAppear() {
61
+        setReactComponentButton();
41 62
         uut.ensureViewIsCreated();
42 63
         uut.onViewAppeared();
43 64
         verify(stackController, times(0)).clearOptions();
44 65
     }
45 66
 
46
-    @Test @Ignore
47
-    public void destroy_buttonIsDestroyedWhenStackIsDestroyed() {
48
-        uut.ensureViewIsCreated();
49
-        uut.onViewAppeared();
50
-        stackController.destroy();
51
-        verify(uut, times(1)).destroy();
67
+    @Test
68
+    public void setIconColor_enabled() {
69
+        stackController.ensureViewIsCreated();
70
+
71
+        setIconButton(true);
72
+        Toolbar titleBar = stackController.getTopBar().getTitleBar();
73
+        uut.addToMenu(titleBar, 0);
74
+
75
+        assertThat(titleBar.getMenu().size()).isOne();
76
+        verify(drawableTinter, times(1)).tint(any(), eq(android.graphics.Color.RED));
77
+    }
78
+
79
+    @Test
80
+    public void setIconColor_disabled() {
81
+        stackController.ensureViewIsCreated();
82
+
83
+        setIconButton(false);
84
+        uut.addToMenu(stackController.getTopBar().getTitleBar(), 0);
85
+
86
+        verify(drawableTinter, times(1)).tint(any(), eq(android.graphics.Color.LTGRAY));
87
+    }
88
+
89
+    @Test
90
+    public void setIconColor_disabledColor() {
91
+        stackController.ensureViewIsCreated();
92
+
93
+        setIconButton(false);
94
+        button.disabledColor = new Color(android.graphics.Color.BLACK);
95
+        uut.addToMenu(stackController.getTopBar().getTitleBar(), 0);
96
+
97
+        verify(drawableTinter, times(1)).tint(any(), eq(android.graphics.Color.BLACK));
98
+    }
99
+
100
+    @Test
101
+    public void disableIconTint() {
102
+        stackController.ensureViewIsCreated();
103
+
104
+        setIconButton(false);
105
+        button.disableIconTint = new Bool(true);
106
+        uut.addToMenu(stackController.getTopBar().getTitleBar(), 0);
107
+        verifyZeroInteractions(drawableTinter);
108
+    }
109
+
110
+    private void setIconButton(boolean enabled) {
111
+        button.id = "btn1";
112
+        button.icon = new Text("someIcon");
113
+        button.color = new Color(android.graphics.Color.RED);
114
+        button.component.name = new NullText();
115
+        button.component.componentId = new NullText();
116
+        button.enabled = new Bool(enabled);
117
+        button.showAsAction = MenuItem.SHOW_AS_ACTION_ALWAYS;
52 118
     }
53 119
 
54
-    @NonNull
55
-    private Button createButton() {
56
-        Button button = new Button();
120
+    private void setReactComponentButton() {
57 121
         button.id = "btnId";
58 122
         button.component.name = new Text("com.example.customBtn");
59 123
         button.component.componentId = new Text("component666");
60
-        return button;
61 124
     }
62 125
 }