Преглед на файлове

Refactor OptionsPresenter

It's now created once and child component is passed as argument in applyChildOptions
Guy Carmeli преди 6 години
родител
ревизия
7f350758df

+ 3
- 10
lib/android/app/src/main/java/com/reactnativenavigation/presentation/OptionsPresenter.java Целия файл

@@ -16,21 +16,15 @@ import java.util.ArrayList;
16 16
 
17 17
 public class OptionsPresenter {
18 18
     private TopBar topBar;
19
-    private Component component;
20
-
21
-    public OptionsPresenter(TopBar topBar, Component component) {
22
-        this.topBar = topBar;
23
-        this.component = component;
24
-    }
25 19
 
26 20
     public OptionsPresenter(TopBar topBar) {
27 21
         this.topBar = topBar;
28 22
     }
29 23
 
30
-    public void applyOptions(Options options) {
24
+    public void applyChildOptions(Options options, Component child) {
31 25
         applyOrientation(options.orientationOptions);
32 26
         applyButtons(options.topBarOptions.leftButtons, options.topBarOptions.rightButtons);
33
-        applyTopBarOptions(options.topBarOptions);
27
+        applyTopBarOptions(options.topBarOptions, child);
34 28
         applyTopTabsOptions(options.topTabsOptions);
35 29
         applyTopTabOptions(options.topTabOptions);
36 30
     }
@@ -39,7 +33,7 @@ public class OptionsPresenter {
39 33
         ((Activity) topBar.getContext()).setRequestedOrientation(options.getValue());
40 34
     }
41 35
 
42
-    private void applyTopBarOptions(TopBarOptions options) {
36
+    private void applyTopBarOptions(TopBarOptions options, Component component) {
43 37
         if (options.title.text.hasValue()) topBar.setTitle(options.title.text.get());
44 38
         if (options.title.component.hasValue()) topBar.setComponent(options.title.component.get(), options.title.alignment);
45 39
         topBar.setBackgroundColor(options.background.color);
@@ -59,7 +53,6 @@ public class OptionsPresenter {
59 53
         } else if (options.drawBehind.isFalseOrUndefined()) {
60 54
             component.drawBelowTopBar(topBar);
61 55
         }
62
-
63 56
         if (options.hideOnScroll.isTrue()) {
64 57
             if (component instanceof IReactView) {
65 58
                 topBar.enableCollapse(((IReactView) component).getScrollEventListener());

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/StackController.java Целия файл

@@ -38,13 +38,13 @@ public class StackController extends ParentController <StackLayout> {
38 38
 
39 39
     public void applyOptions(Options options) {
40 40
         super.applyOptions(options);
41
-        getView().applyOptions(options);
41
+        getView().applyChildOptions(options);
42 42
     }
43 43
 
44 44
     @Override
45 45
     public void applyChildOptions(Options options, Component child) {
46 46
         super.applyChildOptions(options, child);
47
-        getView().applyOptions(this.options, child);
47
+        getView().applyChildOptions(this.options, child);
48 48
         applyOnParentController(parentController ->
49 49
                 ((ParentController) parentController).applyChildOptions(this.options.copy().clearTopBarOptions(), child)
50 50
         );

+ 12
- 11
lib/android/app/src/main/java/com/reactnativenavigation/views/StackLayout.java Целия файл

@@ -19,21 +19,27 @@ import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
19 19
 @SuppressLint("ViewConstructor")
20 20
 public class StackLayout extends RelativeLayout {
21 21
     private TopBar topBar;
22
+    private final OptionsPresenter optionsPresenter;
22 23
 
23 24
     public StackLayout(Context context, ReactViewCreator topBarButtonCreator, TitleBarReactViewCreator titleBarReactViewCreator, TopBarButtonController.OnClickListener topBarButtonClickListener) {
24 25
         super(context);
25
-        topBar = new TopBar(context, topBarButtonCreator, titleBarReactViewCreator, topBarButtonClickListener, this);
26
+        createLayout(topBarButtonCreator, titleBarReactViewCreator, topBarButtonClickListener);
27
+        optionsPresenter = new OptionsPresenter(topBar);
28
+        setContentDescription("StackLayout");
29
+    }
30
+
31
+    private void createLayout(ReactViewCreator topBarButtonCreator, TitleBarReactViewCreator titleBarReactViewCreator, TopBarButtonController.OnClickListener topBarButtonClickListener) {
32
+        topBar = new TopBar(getContext(), topBarButtonCreator, titleBarReactViewCreator, topBarButtonClickListener, this);
26 33
         topBar.setId(CompatUtils.generateViewId());
27 34
         addView(topBar, MATCH_PARENT, WRAP_CONTENT);
28
-        setContentDescription("StackLayout");
29 35
     }
30 36
 
31
-    public void applyOptions(Options options) {
32
-        new OptionsPresenter(topBar).applyOrientation(options.orientationOptions);
37
+    public void applyChildOptions(Options options) {
38
+        optionsPresenter.applyOrientation(options.orientationOptions);
33 39
     }
34 40
 
35
-    public void applyOptions(Options options, Component component) {
36
-        new OptionsPresenter(topBar, component).applyOptions(options);
41
+    public void applyChildOptions(Options options, Component child) {
42
+        optionsPresenter.applyChildOptions(options, child);
37 43
     }
38 44
 
39 45
     public void onChildWillDisappear(Options disappearing, Options appearing) {
@@ -56,9 +62,4 @@ public class StackLayout extends RelativeLayout {
56 62
     public TopBar getTopBar() {
57 63
         return topBar;
58 64
     }
59
-
60
-    @RestrictTo(RestrictTo.Scope.TESTS)
61
-    public void setTopBar(TopBar topBar) {
62
-        this.topBar = topBar;
63
-    }
64 65
 }

+ 0
- 1
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/StackControllerTest.java Целия файл

@@ -352,7 +352,6 @@ public class StackControllerTest extends BaseTest {
352 352
     @Test
353 353
     public void pop_animatesTopBarIfNeeded() throws Exception {
354 354
         uut.ensureViewIsCreated();
355
-        uut.getView().setTopBar(spy(uut.getTopBar()));
356 355
 
357 356
         child1.options.topBarOptions.visible = new Bool(false);
358 357
         child1.options.topBarOptions.animate = new Bool(false);