Pārlūkot izejas kodu

Refactor TitleBarReactView

Manage the view with controller which handles lifecycle
Guy Carmeli 6 gadus atpakaļ
vecāks
revīzija
a14d6f9636

+ 6
- 0
e2e/ScreenStyle.test.js Parādīt failu

@@ -127,4 +127,10 @@ describe('screen style', () => {
127 127
     await elementById(testIDs.TAB_BASED_APP_BUTTON).tap();
128 128
     await expect(elementById(testIDs.TOP_BAR_ELEMENT)).toBeVisible();
129 129
   });
130
+
131
+  it('set title component', async () => {
132
+    await elementById(testIDs.PUSH_OPTIONS_BUTTON).tap();
133
+    await elementById(testIDs.SHOW_TOPBAR_REACT_VIEW).tap();
134
+    await expect(elementByLabel('Press Me')).toBeVisible();
135
+  });
130 136
 });

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/presentation/OptionsPresenter.java Parādīt failu

@@ -38,7 +38,7 @@ public class OptionsPresenter {
38 38
 
39 39
     private void applyTopBarOptions(TopBarOptions options, AnimationsOptions animationOptions, Component component) {
40 40
         if (options.title.text.hasValue()) topBar.setTitle(options.title.text.get());
41
-        if (options.title.component.hasValue()) topBar.setComponent(options.title.component.get(), options.title.alignment);
41
+        if (options.title.component.hasValue()) topBar.setTitleComponent(options.title.component.get(), options.title.alignment);
42 42
         topBar.setBackgroundColor(options.background.color);
43 43
         topBar.setBackgroundComponent(options.background.component);
44 44
         topBar.setTitleTextColor(options.title.color);
@@ -121,7 +121,7 @@ public class OptionsPresenter {
121 121
 
122 122
     private void mergeTopBarOptions(TopBarOptions options, AnimationsOptions animationsOptions, Component component) {
123 123
         if (options.title.text.hasValue()) topBar.setTitle(options.title.text.get());
124
-        if (options.title.component.hasValue()) topBar.setComponent(options.title.component.get(), options.title.alignment);
124
+        if (options.title.component.hasValue()) topBar.setTitleComponent(options.title.component.get(), options.title.alignment);
125 125
         if (options.background.color.hasValue()) topBar.setBackgroundColor(options.background.color);
126 126
         if (options.title.color.hasValue()) topBar.setTitleTextColor(options.title.color);
127 127
         if (options.title.fontSize.hasValue()) topBar.setTitleFontSize(options.title.fontSize);

+ 6
- 4
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/TitleBarReactViewController.java Parādīt failu

@@ -12,15 +12,17 @@ public class TitleBarReactViewController extends ViewController<TitleBarReactVie
12 12
     private final TitleBarReactViewCreator reactViewCreator;
13 13
     private String componentName;
14 14
 
15
-    public TitleBarReactViewController(TitleBarReactViewController reactViewController) {
16
-        this(reactViewController.getActivity(), reactViewController.reactViewCreator);
17
-    }
18
-
19 15
     public TitleBarReactViewController(Activity activity, TitleBarReactViewCreator reactViewCreator) {
20 16
         super(activity, CompatUtils.generateViewId() + "", new Options());
21 17
         this.reactViewCreator = reactViewCreator;
22 18
     }
23 19
 
20
+    @Override
21
+    public void onViewAppeared() {
22
+        super.onViewAppeared();
23
+        runOnPreDraw(view -> view.setLayoutParams(view.getLayoutParams()));
24
+    }
25
+
24 26
     @Override
25 27
     protected TitleBarReactView createView() {
26 28
         return reactViewCreator.create(getActivity(), getId(), componentName);

+ 2
- 3
lib/android/app/src/main/java/com/reactnativenavigation/views/TopBar.java Parādīt failu

@@ -26,7 +26,6 @@ import com.reactnativenavigation.parse.params.Number;
26 26
 import com.reactnativenavigation.parse.params.Text;
27 27
 import com.reactnativenavigation.utils.CompatUtils;
28 28
 import com.reactnativenavigation.viewcontrollers.ReactViewCreator;
29
-import com.reactnativenavigation.viewcontrollers.TitleBarReactViewController;
30 29
 import com.reactnativenavigation.viewcontrollers.TopBarButtonController;
31 30
 import com.reactnativenavigation.views.titlebar.TitleBar;
32 31
 import com.reactnativenavigation.views.titlebar.TitleBarReactViewCreator;
@@ -71,7 +70,7 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
71 70
     protected TitleBar createTitleBar(Context context, ReactViewCreator buttonCreator, TitleBarReactViewCreator reactViewCreator, TopBarButtonController.OnClickListener onClickListener) {
72 71
         return new TitleBar(context,
73 72
                 buttonCreator,
74
-                new TitleBarReactViewController((Activity) context, reactViewCreator),
73
+                reactViewCreator,
75 74
                 onClickListener
76 75
         );
77 76
     }
@@ -100,7 +99,7 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
100 99
         titleBar.setTitleTypeface(typeface);
101 100
     }
102 101
 
103
-    public void setComponent(String componentName, TitleOptions.Alignment alignment) {
102
+    public void setTitleComponent(String componentName, TitleOptions.Alignment alignment) {
104 103
         titleBar.setComponent(componentName, alignment);
105 104
     }
106 105
 

+ 5
- 4
lib/android/app/src/main/java/com/reactnativenavigation/views/titlebar/TitleBar.java Parādīt failu

@@ -30,14 +30,16 @@ import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
30 30
 public class TitleBar extends Toolbar {
31 31
     private final ReactViewCreator buttonCreator;
32 32
     private TitleBarReactViewController reactViewController;
33
+    private final TitleBarReactViewCreator reactViewCreator;
33 34
     private final TopBarButtonController.OnClickListener onClickListener;
34 35
     private final List<TopBarButtonController> rightButtonControllers = new ArrayList<>();
35 36
     private TopBarButtonController leftButtonController;
36 37
 
37
-    public TitleBar(Context context, ReactViewCreator buttonCreator, TitleBarReactViewController reactViewController, TopBarButtonController.OnClickListener onClickListener) {
38
+    public TitleBar(Context context, ReactViewCreator buttonCreator, TitleBarReactViewCreator reactViewCreator, TopBarButtonController.OnClickListener onClickListener) {
38 39
         super(context);
39 40
         this.buttonCreator = buttonCreator;
40
-        this.reactViewController = reactViewController;
41
+        this.reactViewCreator = reactViewCreator;
42
+        reactViewController = new TitleBarReactViewController((Activity) context, reactViewCreator);
41 43
         this.onClickListener = onClickListener;
42 44
         getMenu();
43 45
         setContentDescription("titleBar");
@@ -61,7 +63,6 @@ public class TitleBar extends Toolbar {
61 63
         clearTitle();
62 64
         reactViewController.setComponent(componentName);
63 65
         addView(reactViewController.getView(), getComponentLayoutParams(alignment));
64
-        requestLayout();
65 66
     }
66 67
 
67 68
     public void setBackgroundColor(Color color) {
@@ -99,7 +100,7 @@ public class TitleBar extends Toolbar {
99 100
 
100 101
     private void clearComponent() {
101 102
         reactViewController.destroy();
102
-        reactViewController = new TitleBarReactViewController(reactViewController);
103
+        reactViewController = new TitleBarReactViewController((Activity) getContext(), reactViewCreator);
103 104
     }
104 105
 
105 106
     private void clearLeftButton() {

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/views/titlebar/TitleBarReactView.java Parādīt failu

@@ -16,7 +16,7 @@ public class TitleBarReactView extends ReactView {
16 16
     @Override
17 17
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
18 18
         super.onMeasure(
19
-                getChildCount() > 0 ? MeasureSpec.makeMeasureSpec(getChildAt(0).getWidth(), MeasureSpec.EXACTLY) : widthMeasureSpec,
19
+                (getChildCount() > 0 && getChildAt(0).getWidth() > 0) ? MeasureSpec.makeMeasureSpec(getChildAt(0).getWidth(), MeasureSpec.EXACTLY) : widthMeasureSpec,
20 20
                 heightMeasureSpec
21 21
         );
22 22
     }

+ 1
- 1
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/OptionsMergingTest.java Parādīt failu

@@ -137,7 +137,7 @@ public class OptionsMergingTest extends BaseTest {
137 137
 
138 138
     private void assertTopBarOptions(int t) {
139 139
         verify(topBar, times(t)).setTitle(any());
140
-        verify(topBar, times(t)).setComponent(any(), any());
140
+        verify(topBar, times(t)).setTitleComponent(any(), any());
141 141
         verify(topBar, times(t)).setBackgroundColor(any());
142 142
         verify(topBar, times(t)).setTitleTextColor(any());
143 143
         verify(topBar, times(t)).setTitleFontSize(any());

+ 5
- 4
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/TitleBarTest.java Parādīt failu

@@ -12,6 +12,7 @@ import com.reactnativenavigation.parse.TitleOptions;
12 12
 import com.reactnativenavigation.parse.params.Button;
13 13
 import com.reactnativenavigation.parse.params.Text;
14 14
 import com.reactnativenavigation.react.ReactView;
15
+import com.reactnativenavigation.utils.ViewUtils;
15 16
 import com.reactnativenavigation.views.titlebar.TitleBar;
16 17
 import com.reactnativenavigation.views.titlebar.TitleBarReactView;
17 18
 
@@ -38,16 +39,15 @@ public class TitleBarTest extends BaseTest {
38 39
     private Button textButton;
39 40
     private Button customButton;
40 41
     private Map<String, TopBarButtonController> buttonControllers;
41
-    private TitleBarReactViewController reactViewController;
42 42
 
43 43
     @Override
44 44
     public void beforeEach() {
45 45
         final TopBarButtonCreatorMock buttonCreator = new TopBarButtonCreatorMock();
46 46
         final Activity activity = newActivity();
47
-        reactViewController = spy(new TitleBarReactViewController(activity, new TitleBarReactViewCreatorMock()));
48 47
         createButtons();
49 48
         buttonControllers = new HashMap<>();
50
-        uut = spy(new TitleBar(activity, buttonCreator, reactViewController, (buttonId -> {})) {
49
+        TitleBarReactViewCreatorMock reactViewCreator = new TitleBarReactViewCreatorMock();
50
+        uut = spy(new TitleBar(activity, buttonCreator, reactViewCreator, (buttonId -> {})) {
51 51
             @Override
52 52
             public TopBarButtonController createButtonController(Button button) {
53 53
                 TopBarButtonController controller = spy(super.createButtonController(button));
@@ -169,11 +169,12 @@ public class TitleBarTest extends BaseTest {
169 169
 
170 170
     @Test
171 171
     public void clear() throws Exception {
172
+        uut.setComponent("someComponent", TitleOptions.Alignment.Center);
172 173
         uut.clear();
173 174
         assertThat(uut.getTitle()).isNullOrEmpty();
174 175
         assertThat(uut.getMenu().size()).isZero();
175 176
         assertThat(uut.getNavigationIcon()).isNull();
176
-        verify(reactViewController, times(1)).destroy();
177
+        assertThat(ViewUtils.findChildrenByClassRecursive(uut, TitleBarReactView.class).size()).isZero();
177 178
     }
178 179
 
179 180
     private List<Button> leftButton(Button leftButton) {

+ 0
- 1
playground/src/screens/OptionsScreen.js Parādīt failu

@@ -131,7 +131,6 @@ class OptionsScreen extends Component {
131 131
             title: 'Two',
132 132
             icon: require('../../img/navicon_add.png'),
133 133
             disableIconTint: true,
134
-            // disabled: true,
135 134
             showAsAction: 'ifRoom',
136 135
             buttonColor: 'green',
137 136
             buttonFontSize: 28,