Browse Source

destroy TitleBar component in titleBar.clear

Guy Carmeli 6 years ago
parent
commit
cb43c9800c

+ 10
- 4
lib/android/app/src/main/java/com/reactnativenavigation/parse/TitleOptions.java View File

47
         options.component = TextParser.parse(json, "component");
47
         options.component = TextParser.parse(json, "component");
48
         options.alignment = Alignment.fromString(TextParser.parse(json, "alignment").get(""));
48
         options.alignment = Alignment.fromString(TextParser.parse(json, "alignment").get(""));
49
 
49
 
50
-        if (options.component.hasValue() && options.text.hasValue()) {
51
-            if (BuildConfig.DEBUG) Log.w("RNN", "A screen can't use both text and component - clearing text.");
52
-            options.text = new NullText();
53
-        }
50
+        validate(options);
54
 
51
 
55
         return options;
52
         return options;
56
     }
53
     }
69
         if (other.fontFamily != null) fontFamily = other.fontFamily;
66
         if (other.fontFamily != null) fontFamily = other.fontFamily;
70
         if (other.component.hasValue()) component = other.component;
67
         if (other.component.hasValue()) component = other.component;
71
         if (other.alignment != Alignment.Default) alignment = other.alignment;
68
         if (other.alignment != Alignment.Default) alignment = other.alignment;
69
+        validate(this);
72
     }
70
     }
73
 
71
 
74
     void mergeWithDefault(TitleOptions defaultOptions) {
72
     void mergeWithDefault(TitleOptions defaultOptions) {
78
         if (fontFamily == null) fontFamily = defaultOptions.fontFamily;
76
         if (fontFamily == null) fontFamily = defaultOptions.fontFamily;
79
         if (!component.hasValue()) component = defaultOptions.component;
77
         if (!component.hasValue()) component = defaultOptions.component;
80
         if (alignment == Alignment.Default) alignment = defaultOptions.alignment;
78
         if (alignment == Alignment.Default) alignment = defaultOptions.alignment;
79
+        validate(this);
80
+    }
81
+
82
+    private static void validate(TitleOptions options) {
83
+        if (options.component.hasValue() && options.text.hasValue()) {
84
+            if (BuildConfig.DEBUG) Log.w("RNN", "A screen can't use both text and component - clearing text.");
85
+            options.text = new Text("");
86
+        }
81
     }
87
     }
82
 }
88
 }

+ 5
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/TitleBarReactViewController.java View File

17
         this.reactViewCreator = reactViewCreator;
17
         this.reactViewCreator = reactViewCreator;
18
     }
18
     }
19
 
19
 
20
+    public TitleBarReactViewController(TitleBarReactViewController reactViewController) {
21
+        super(reactViewController.getActivity(), CompatUtils.generateViewId() + "", new Options());
22
+        this.reactViewCreator = reactViewController.reactViewCreator;
23
+    }
24
+
20
     @Override
25
     @Override
21
     protected TitleBarReactView createView() {
26
     protected TitleBarReactView createView() {
22
         return reactViewCreator.create(getActivity(), getId(), componentName);
27
         return reactViewCreator.create(getActivity(), getId(), componentName);

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

82
         setTitle(null);
82
         setTitle(null);
83
         clearRightButtons();
83
         clearRightButtons();
84
         clearLeftButton();
84
         clearLeftButton();
85
+        clearComponent();
86
+    }
87
+
88
+    private void clearComponent() {
89
+        reactViewController.destroy();
90
+        reactViewController = new TitleBarReactViewController(reactViewController);
85
     }
91
     }
86
 
92
 
87
     private void clearLeftButton() {
93
     private void clearLeftButton() {

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

38
     private Button textButton;
38
     private Button textButton;
39
     private Button customButton;
39
     private Button customButton;
40
     private Map<String, TopBarButtonController> buttonControllers;
40
     private Map<String, TopBarButtonController> buttonControllers;
41
+    private TitleBarReactViewController reactViewController;
41
 
42
 
42
     @Override
43
     @Override
43
     public void beforeEach() {
44
     public void beforeEach() {
44
         final TopBarButtonCreatorMock buttonCreator = new TopBarButtonCreatorMock();
45
         final TopBarButtonCreatorMock buttonCreator = new TopBarButtonCreatorMock();
45
         final Activity activity = newActivity();
46
         final Activity activity = newActivity();
46
-        TitleBarReactViewController reactViewController = new TitleBarReactViewController(activity, new TitleBarReactViewCreatorMock());
47
+        reactViewController = spy(new TitleBarReactViewController(activity, new TitleBarReactViewCreatorMock()));
47
         createButtons();
48
         createButtons();
48
         buttonControllers = new HashMap<>();
49
         buttonControllers = new HashMap<>();
49
         uut = spy(new TitleBar(activity, buttonCreator, reactViewController, (buttonId -> {})) {
50
         uut = spy(new TitleBar(activity, buttonCreator, reactViewController, (buttonId -> {})) {
132
     @Test
133
     @Test
133
     public void setComponent_addsComponentToTitleBar() throws Exception {
134
     public void setComponent_addsComponentToTitleBar() throws Exception {
134
         uut.setComponent("com.rnn.CustomView", TitleOptions.Alignment.Center);
135
         uut.setComponent("com.rnn.CustomView", TitleOptions.Alignment.Center);
135
-        verify(uut, times(1)).addView(any(TitleBarReactView.class));
136
+        verify(uut, times(1)).addView(any(TitleBarReactView.class), any(Toolbar.LayoutParams.class));
136
     }
137
     }
137
 
138
 
138
     @Test
139
     @Test
154
         assertThat(lpCaptor.getValue().gravity == Gravity.CENTER);
155
         assertThat(lpCaptor.getValue().gravity == Gravity.CENTER);
155
     }
156
     }
156
 
157
 
158
+    @Test
159
+    public void clear() throws Exception {
160
+        uut.clear();
161
+        assertThat(uut.getTitle()).isNullOrEmpty();
162
+        assertThat(uut.getMenu().size()).isZero();
163
+        assertThat(uut.getNavigationIcon()).isNull();
164
+        verify(reactViewController, times(1)).destroy();
165
+    }
166
+
157
     private List<Button> leftButton(Button leftButton) {
167
     private List<Button> leftButton(Button leftButton) {
158
         return Collections.singletonList(leftButton);
168
         return Collections.singletonList(leftButton);
159
     }
169
     }

+ 2
- 4
playground/src/screens/OptionsScreen.js View File

22
           color: 'black',
22
           color: 'black',
23
           fontSize: 16,
23
           fontSize: 16,
24
           fontFamily: 'HelveticaNeue-Italic',
24
           fontFamily: 'HelveticaNeue-Italic',
25
-          largeTitle: false,
26
-          component: 'navigation.playground.CustomTopBar',
27
-          alignment: 'center'
25
+          largeTitle: false
28
         },
26
         },
29
         ...Platform.select({
27
         ...Platform.select({
30
           android: { drawBehind: true },
28
           android: { drawBehind: true },
268
       topBar: {
266
       topBar: {
269
         title: {
267
         title: {
270
           component: 'navigation.playground.CustomTopBar',
268
           component: 'navigation.playground.CustomTopBar',
271
-          alignment: 'fill'
269
+          alignment: 'center'
272
         }
270
         }
273
       }
271
       }
274
     });
272
     });