Browse Source

Align title after left button is set (#5129)

When setting left button and the title was centred, it got an undesired offset.
Now whenever left button is set we make sure to realign the title.
Guy Carmeli 5 years ago
parent
commit
9a2a921ab2
No account linked to committer's email address

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

@@ -1,5 +1,7 @@
1 1
 package com.reactnativenavigation.utils;
2 2
 
3
+import android.support.annotation.Nullable;
4
+
3 5
 public class StringUtils {
4 6
 
5 7
 	@SuppressWarnings("StringEquality")
@@ -9,4 +11,8 @@ public class StringUtils {
9 11
 		}
10 12
 		return s1.equals(s2);
11 13
 	}
14
+
15
+    public static boolean isEmpty(@Nullable CharSequence s) {
16
+        return s == null || s.length() == 0;
17
+    }
12 18
 }

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

@@ -6,6 +6,7 @@ import android.os.Build;
6 6
 import android.os.Handler;
7 7
 import android.os.Looper;
8 8
 import android.support.annotation.NonNull;
9
+import android.support.annotation.Nullable;
9 10
 import android.util.DisplayMetrics;
10 11
 import android.view.View;
11 12
 import android.view.ViewTreeObserver;
@@ -19,7 +20,13 @@ public class UiUtils {
19 20
     private static int statusBarHeight = -1;
20 21
     private static int topBarHeight = -1;
21 22
 
22
-    public static void runOnPreDrawOnce(final View view, final Runnable task) {
23
+    public static <T extends View> void runOnPreDrawOnce(@Nullable final T view, final Functions.Func1<T> task) {
24
+        if (view == null) return;
25
+        runOnPreDrawOnce(view, () -> task.run(view));
26
+    }
27
+
28
+    public static void runOnPreDrawOnce(@Nullable final View view, final Runnable task) {
29
+        if (view == null) return;
23 30
         view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
24 31
             @Override
25 32
             public boolean onPreDraw() {

+ 7
- 6
lib/android/app/src/main/java/com/reactnativenavigation/views/titlebar/TitleBar.java View File

@@ -15,6 +15,7 @@ import android.widget.TextView;
15 15
 
16 16
 import com.reactnativenavigation.parse.Alignment;
17 17
 import com.reactnativenavigation.parse.params.Colour;
18
+import com.reactnativenavigation.utils.StringUtils;
18 19
 import com.reactnativenavigation.utils.UiUtils;
19 20
 import com.reactnativenavigation.utils.ViewUtils;
20 21
 import com.reactnativenavigation.viewcontrollers.TitleBarButtonController;
@@ -23,6 +24,8 @@ import java.util.List;
23 24
 
24 25
 import javax.annotation.Nullable;
25 26
 
27
+import static com.reactnativenavigation.utils.UiUtils.runOnPreDrawOnce;
28
+
26 29
 @SuppressLint("ViewConstructor")
27 30
 public class TitleBar extends Toolbar {
28 31
     public static final int DEFAULT_LEFT_MARGIN = 16;
@@ -63,10 +66,6 @@ public class TitleBar extends Toolbar {
63 66
         return super.getTitle() == null ? "" : (String) super.getTitle();
64 67
     }
65 68
 
66
-    public void setTitleTextColor(Colour color) {
67
-        if (color.hasValue()) setTitleTextColor(color.get());
68
-    }
69
-
70 69
     public void setComponent(View component) {
71 70
         clearTitle();
72 71
         clearSubtitle();
@@ -106,9 +105,10 @@ public class TitleBar extends Toolbar {
106 105
         subtitleAlignment = alignment;
107 106
     }
108 107
 
109
-    private void alignTextView(Alignment alignment, TextView view) {
108
+    public void alignTextView(Alignment alignment, TextView view) {
109
+        if (StringUtils.isEmpty(view.getText())) return;
110 110
         Integer direction = view.getParent().getLayoutDirection();
111
-        Boolean isRTL = direction == View.LAYOUT_DIRECTION_RTL;
111
+        boolean isRTL = direction == View.LAYOUT_DIRECTION_RTL;
112 112
 
113 113
         if (alignment == Alignment.Center) {
114 114
             //noinspection IntegerDivisionInFloatingPointContext
@@ -206,6 +206,7 @@ public class TitleBar extends Toolbar {
206 206
 
207 207
     private void setLeftButton(TitleBarButtonController button) {
208 208
         leftButtonController = button;
209
+        runOnPreDrawOnce(findTitleTextView(), title -> alignTextView(titleAlignment, title));
209 210
         button.applyNavigationIcon(this);
210 211
     }
211 212
 

+ 41
- 17
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/TitleBarTest.java View File

@@ -1,22 +1,34 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3
-import android.app.*;
4
-import android.support.v7.widget.*;
5
-import android.view.*;
6
-
7
-import com.reactnativenavigation.*;
8
-import com.reactnativenavigation.parse.params.*;
9
-import com.reactnativenavigation.react.*;
10
-import com.reactnativenavigation.utils.*;
11
-import com.reactnativenavigation.views.titlebar.*;
12
-
13
-import org.junit.*;
14
-
15
-import java.util.*;
16
-
17
-import static com.reactnativenavigation.utils.TitleBarHelper.*;
18
-import static org.assertj.core.api.Java6Assertions.*;
19
-import static org.mockito.Mockito.*;
3
+import android.app.Activity;
4
+import android.support.v7.widget.ActionMenuView;
5
+import android.view.View;
6
+import android.widget.TextView;
7
+
8
+import com.reactnativenavigation.BaseTest;
9
+import com.reactnativenavigation.parse.params.Button;
10
+import com.reactnativenavigation.parse.params.Text;
11
+import com.reactnativenavigation.react.Constants;
12
+import com.reactnativenavigation.react.ReactView;
13
+import com.reactnativenavigation.utils.CollectionUtils;
14
+import com.reactnativenavigation.views.titlebar.TitleBar;
15
+
16
+import org.junit.Test;
17
+import org.mockito.Mockito;
18
+
19
+import java.util.ArrayList;
20
+import java.util.Arrays;
21
+import java.util.Collections;
22
+import java.util.List;
23
+
24
+import static com.reactnativenavigation.utils.TitleBarHelper.createButtonController;
25
+import static org.assertj.core.api.Java6Assertions.assertThat;
26
+import static org.mockito.Mockito.any;
27
+import static org.mockito.Mockito.eq;
28
+import static org.mockito.Mockito.mock;
29
+import static org.mockito.Mockito.spy;
30
+import static org.mockito.Mockito.verify;
31
+import static org.mockito.Mockito.when;
20 32
 
21 33
 public class TitleBarTest extends BaseTest {
22 34
 
@@ -82,6 +94,18 @@ public class TitleBarTest extends BaseTest {
82 94
         assertThat(uut.getNavigationIcon()).isNull();
83 95
     }
84 96
 
97
+    @Test
98
+    public void setLeftButton_titleIsAligned() {
99
+        uut.setTitle("Title");
100
+        TextView title = new TextView(activity);
101
+        uut.addView(title);
102
+        when(uut.findTitleTextView()).thenReturn(title);
103
+
104
+        uut.setLeftButtons(Collections.singletonList(Mockito.mock(TitleBarButtonController.class)));
105
+        dispatchPreDraw(title);
106
+        verify(uut).alignTextView(any(), eq(title));
107
+    }
108
+
85 109
     @Test
86 110
     public void setRightButtons_buttonsAreAddedInReverseOrderToMatchOrderOnIOs() {
87 111
         uut.setLeftButtons(new ArrayList<>());