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
 package com.reactnativenavigation.utils;
1
 package com.reactnativenavigation.utils;
2
 
2
 
3
+import android.support.annotation.Nullable;
4
+
3
 public class StringUtils {
5
 public class StringUtils {
4
 
6
 
5
 	@SuppressWarnings("StringEquality")
7
 	@SuppressWarnings("StringEquality")
9
 		}
11
 		}
10
 		return s1.equals(s2);
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
 import android.os.Handler;
6
 import android.os.Handler;
7
 import android.os.Looper;
7
 import android.os.Looper;
8
 import android.support.annotation.NonNull;
8
 import android.support.annotation.NonNull;
9
+import android.support.annotation.Nullable;
9
 import android.util.DisplayMetrics;
10
 import android.util.DisplayMetrics;
10
 import android.view.View;
11
 import android.view.View;
11
 import android.view.ViewTreeObserver;
12
 import android.view.ViewTreeObserver;
19
     private static int statusBarHeight = -1;
20
     private static int statusBarHeight = -1;
20
     private static int topBarHeight = -1;
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
         view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
30
         view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
24
             @Override
31
             @Override
25
             public boolean onPreDraw() {
32
             public boolean onPreDraw() {

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

15
 
15
 
16
 import com.reactnativenavigation.parse.Alignment;
16
 import com.reactnativenavigation.parse.Alignment;
17
 import com.reactnativenavigation.parse.params.Colour;
17
 import com.reactnativenavigation.parse.params.Colour;
18
+import com.reactnativenavigation.utils.StringUtils;
18
 import com.reactnativenavigation.utils.UiUtils;
19
 import com.reactnativenavigation.utils.UiUtils;
19
 import com.reactnativenavigation.utils.ViewUtils;
20
 import com.reactnativenavigation.utils.ViewUtils;
20
 import com.reactnativenavigation.viewcontrollers.TitleBarButtonController;
21
 import com.reactnativenavigation.viewcontrollers.TitleBarButtonController;
23
 
24
 
24
 import javax.annotation.Nullable;
25
 import javax.annotation.Nullable;
25
 
26
 
27
+import static com.reactnativenavigation.utils.UiUtils.runOnPreDrawOnce;
28
+
26
 @SuppressLint("ViewConstructor")
29
 @SuppressLint("ViewConstructor")
27
 public class TitleBar extends Toolbar {
30
 public class TitleBar extends Toolbar {
28
     public static final int DEFAULT_LEFT_MARGIN = 16;
31
     public static final int DEFAULT_LEFT_MARGIN = 16;
63
         return super.getTitle() == null ? "" : (String) super.getTitle();
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
     public void setComponent(View component) {
69
     public void setComponent(View component) {
71
         clearTitle();
70
         clearTitle();
72
         clearSubtitle();
71
         clearSubtitle();
106
         subtitleAlignment = alignment;
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
         Integer direction = view.getParent().getLayoutDirection();
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
         if (alignment == Alignment.Center) {
113
         if (alignment == Alignment.Center) {
114
             //noinspection IntegerDivisionInFloatingPointContext
114
             //noinspection IntegerDivisionInFloatingPointContext
206
 
206
 
207
     private void setLeftButton(TitleBarButtonController button) {
207
     private void setLeftButton(TitleBarButtonController button) {
208
         leftButtonController = button;
208
         leftButtonController = button;
209
+        runOnPreDrawOnce(findTitleTextView(), title -> alignTextView(titleAlignment, title));
209
         button.applyNavigationIcon(this);
210
         button.applyNavigationIcon(this);
210
     }
211
     }
211
 
212
 

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

1
 package com.reactnativenavigation.viewcontrollers;
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
 public class TitleBarTest extends BaseTest {
33
 public class TitleBarTest extends BaseTest {
22
 
34
 
82
         assertThat(uut.getNavigationIcon()).isNull();
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
     @Test
109
     @Test
86
     public void setRightButtons_buttonsAreAddedInReverseOrderToMatchOrderOnIOs() {
110
     public void setRightButtons_buttonsAreAddedInReverseOrderToMatchOrderOnIOs() {
87
         uut.setLeftButtons(new ArrayList<>());
111
         uut.setLeftButtons(new ArrayList<>());