Quellcode durchsuchen

destroy TitleBar component in titleBar.clear

Guy Carmeli vor 6 Jahren
Ursprung
Commit
cb43c9800c

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

@@ -47,10 +47,7 @@ public class TitleOptions {
47 47
         options.component = TextParser.parse(json, "component");
48 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 52
         return options;
56 53
     }
@@ -69,6 +66,7 @@ public class TitleOptions {
69 66
         if (other.fontFamily != null) fontFamily = other.fontFamily;
70 67
         if (other.component.hasValue()) component = other.component;
71 68
         if (other.alignment != Alignment.Default) alignment = other.alignment;
69
+        validate(this);
72 70
     }
73 71
 
74 72
     void mergeWithDefault(TitleOptions defaultOptions) {
@@ -78,5 +76,13 @@ public class TitleOptions {
78 76
         if (fontFamily == null) fontFamily = defaultOptions.fontFamily;
79 77
         if (!component.hasValue()) component = defaultOptions.component;
80 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 Datei anzeigen

@@ -17,6 +17,11 @@ public class TitleBarReactViewController extends ViewController<TitleBarReactVie
17 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 25
     @Override
21 26
     protected TitleBarReactView createView() {
22 27
         return reactViewCreator.create(getActivity(), getId(), componentName);

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

@@ -82,6 +82,12 @@ public class TitleBar extends Toolbar {
82 82
         setTitle(null);
83 83
         clearRightButtons();
84 84
         clearLeftButton();
85
+        clearComponent();
86
+    }
87
+
88
+    private void clearComponent() {
89
+        reactViewController.destroy();
90
+        reactViewController = new TitleBarReactViewController(reactViewController);
85 91
     }
86 92
 
87 93
     private void clearLeftButton() {

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

@@ -38,12 +38,13 @@ public class TitleBarTest extends BaseTest {
38 38
     private Button textButton;
39 39
     private Button customButton;
40 40
     private Map<String, TopBarButtonController> buttonControllers;
41
+    private TitleBarReactViewController reactViewController;
41 42
 
42 43
     @Override
43 44
     public void beforeEach() {
44 45
         final TopBarButtonCreatorMock buttonCreator = new TopBarButtonCreatorMock();
45 46
         final Activity activity = newActivity();
46
-        TitleBarReactViewController reactViewController = new TitleBarReactViewController(activity, new TitleBarReactViewCreatorMock());
47
+        reactViewController = spy(new TitleBarReactViewController(activity, new TitleBarReactViewCreatorMock()));
47 48
         createButtons();
48 49
         buttonControllers = new HashMap<>();
49 50
         uut = spy(new TitleBar(activity, buttonCreator, reactViewController, (buttonId -> {})) {
@@ -132,7 +133,7 @@ public class TitleBarTest extends BaseTest {
132 133
     @Test
133 134
     public void setComponent_addsComponentToTitleBar() throws Exception {
134 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 139
     @Test
@@ -154,6 +155,15 @@ public class TitleBarTest extends BaseTest {
154 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 167
     private List<Button> leftButton(Button leftButton) {
158 168
         return Collections.singletonList(leftButton);
159 169
     }

+ 2
- 4
playground/src/screens/OptionsScreen.js Datei anzeigen

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