Ver código fonte

Fab gravity (#2926)

* WIP

* Apply default options when screen options are null

* modal animations

* fix tests

* refactor

* refactor

* refactor

* refactor

* test fix

* topBar anim

* child disappear bug

* Update app.js

* Update WelcomeScreen.js

* fix tests

* bottom tabs

* stack id

* fix test

* refactoring

* merge fix

* Update app.js

* merge

* comment to fix test

* merge options
Roman Kozlov 6 anos atrás
pai
commit
1c829ad638

+ 158
- 2
lib/android/app/src/main/java/com/reactnativenavigation/presentation/FabOptionsPresenter.java Ver arquivo

@@ -34,13 +34,13 @@ public class FabOptionsPresenter {
34 34
 
35 35
         if (options.id.hasValue()) {
36 36
             if (fabMenu != null && fabMenu.getFabId().equals(options.id.get())) {
37
-                setParams(fabMenu, options);
38 37
                 fabMenu.bringToFront();
39 38
                 applyFabMenuOptions(fabMenu, options);
39
+                setParams(fabMenu, options);
40 40
             } else if (fab != null && fab.getFabId().equals(options.id.get())) {
41
-                setParams(fab, options);
42 41
                 fab.bringToFront();
43 42
                 applyFabOptions(fab, options);
43
+                setParams(fab, options);
44 44
                 fab.setOnClickListener(v -> component.sendOnNavigationButtonPressed(options.id.get()));
45 45
             } else {
46 46
                 createFab(options);
@@ -51,6 +51,26 @@ public class FabOptionsPresenter {
51 51
         }
52 52
     }
53 53
 
54
+    public void mergeOptions(FabOptions options, @NonNull ReactComponent component, @NonNull ViewGroup viewGroup) {
55
+        this.viewGroup = viewGroup;
56
+        this.component = component;
57
+
58
+        if (options.id.hasValue()) {
59
+            if (fabMenu != null && fabMenu.getFabId().equals(options.id.get())) {
60
+                mergeParams(fabMenu, options);
61
+                fabMenu.bringToFront();
62
+                mergeFabMenuOptions(fabMenu, options);
63
+            } else if (fab != null && fab.getFabId().equals(options.id.get())) {
64
+                mergeParams(fab, options);
65
+                fab.bringToFront();
66
+                mergeFabOptions(fab, options);
67
+                fab.setOnClickListener(v -> component.sendOnNavigationButtonPressed(options.id.get()));
68
+            } else {
69
+                createFab(options);
70
+            }
71
+        }
72
+    }
73
+
54 74
     private void createFab(FabOptions options) {
55 75
         if (options.actionsArray.size() > 0) {
56 76
             fabMenu = new FabMenu(viewGroup.getContext(), options.id.get());
@@ -151,6 +171,73 @@ public class FabOptionsPresenter {
151 171
         fab.setLayoutParams(layoutParams);
152 172
     }
153 173
 
174
+    private void mergeParams(View fab, FabOptions options) {
175
+        ViewGroup.LayoutParams layoutParams = fab.getLayoutParams();
176
+        if (viewGroup instanceof RelativeLayout) {
177
+            RelativeLayout.LayoutParams layoutParamsRelative = (RelativeLayout.LayoutParams) fab.getLayoutParams();
178
+            if (layoutParamsRelative == null) {
179
+                layoutParamsRelative = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
180
+                layoutParamsRelative.bottomMargin = (int) viewGroup.getContext().getResources().getDimension(R.dimen.margin);
181
+                layoutParamsRelative.rightMargin = (int) viewGroup.getContext().getResources().getDimension(R.dimen.margin);
182
+                layoutParamsRelative.leftMargin = (int) viewGroup.getContext().getResources().getDimension(R.dimen.margin);
183
+                layoutParamsRelative.topMargin = (int) viewGroup.getContext().getResources().getDimension(R.dimen.margin);
184
+            }
185
+            if (options.alignHorizontally.hasValue()) {
186
+                if ("right".equals(options.alignHorizontally.get())) {
187
+                    layoutParamsRelative.removeRule(ALIGN_PARENT_LEFT);
188
+                    layoutParamsRelative.addRule(ALIGN_PARENT_RIGHT);
189
+                }
190
+                if ("left".equals(options.alignHorizontally.get())) {
191
+                    layoutParamsRelative.removeRule(ALIGN_PARENT_RIGHT);
192
+                    layoutParamsRelative.addRule(ALIGN_PARENT_LEFT);
193
+                }
194
+            }
195
+            if (options.alignVertically.hasValue()) {
196
+                if ("top".equals(options.alignVertically.get())) {
197
+                    layoutParamsRelative.removeRule(ALIGN_PARENT_BOTTOM);
198
+                    layoutParamsRelative.addRule(ALIGN_PARENT_TOP);
199
+                }
200
+                if ("bottom".equals(options.alignVertically.get())) {
201
+                    layoutParamsRelative.removeRule(ALIGN_PARENT_TOP);
202
+                    layoutParamsRelative.addRule(ALIGN_PARENT_BOTTOM);
203
+                }
204
+            }
205
+            layoutParams = layoutParamsRelative;
206
+        }
207
+        if (viewGroup instanceof FrameLayout) {
208
+            FrameLayout.LayoutParams layoutParamsFrame = (FrameLayout.LayoutParams) fab.getLayoutParams();
209
+            if (layoutParamsFrame == null) {
210
+                layoutParamsFrame = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
211
+                layoutParamsFrame.bottomMargin = (int) viewGroup.getContext().getResources().getDimension(R.dimen.margin);
212
+                layoutParamsFrame.rightMargin = (int) viewGroup.getContext().getResources().getDimension(R.dimen.margin);
213
+                layoutParamsFrame.leftMargin = (int) viewGroup.getContext().getResources().getDimension(R.dimen.margin);
214
+                layoutParamsFrame.topMargin = (int) viewGroup.getContext().getResources().getDimension(R.dimen.margin);
215
+            }
216
+            if (options.alignHorizontally.hasValue()) {
217
+                if ("right".equals(options.alignHorizontally.get())) {
218
+                    removeGravityParam(layoutParamsFrame, Gravity.LEFT);
219
+                    setGravityParam(layoutParamsFrame, Gravity.RIGHT);
220
+                }
221
+                if ("left".equals(options.alignHorizontally.get())) {
222
+                    removeGravityParam(layoutParamsFrame, Gravity.RIGHT);
223
+                    setGravityParam(layoutParamsFrame, Gravity.LEFT);
224
+                }
225
+            }
226
+            if (options.alignVertically.hasValue()) {
227
+                if ("top".equals(options.alignVertically.get())) {
228
+                    removeGravityParam(layoutParamsFrame, Gravity.BOTTOM);
229
+                    setGravityParam(layoutParamsFrame, Gravity.TOP);
230
+                }
231
+                if ("bottom".equals(options.alignVertically.get())) {
232
+                    removeGravityParam(layoutParamsFrame, Gravity.TOP);
233
+                    setGravityParam(layoutParamsFrame, Gravity.BOTTOM);
234
+                }
235
+            }
236
+            layoutParams = layoutParamsFrame;
237
+        }
238
+        fab.setLayoutParams(layoutParams);
239
+    }
240
+
154 241
     private void applyFabOptions(Fab fab, FabOptions options) {
155 242
         if (options.visible.isTrueOrUndefined()) {
156 243
             fab.show(true);
@@ -181,6 +268,36 @@ public class FabOptionsPresenter {
181 268
         }
182 269
     }
183 270
 
271
+    private void mergeFabOptions(Fab fab, FabOptions options) {
272
+        if (options.visible.isTrue()) {
273
+            fab.show(true);
274
+        }
275
+        if (options.visible.isFalse()) {
276
+            fab.hide(true);
277
+        }
278
+        if (options.backgroundColor.hasValue()) {
279
+            fab.setColorNormal(options.backgroundColor.get());
280
+        }
281
+        if (options.clickColor.hasValue()) {
282
+            fab.setColorPressed(options.clickColor.get());
283
+        }
284
+        if (options.rippleColor.hasValue()) {
285
+            fab.setColorRipple(options.rippleColor.get());
286
+        }
287
+        if (options.icon.hasValue()) {
288
+            fab.applyIcon(options.icon.get());
289
+        }
290
+        if (options.size.hasValue()) {
291
+            fab.setButtonSize("mini".equals(options.size.get()) ? SIZE_MINI : SIZE_NORMAL);
292
+        }
293
+        if (options.hideOnScroll.isTrue()) {
294
+            fab.enableCollapse(component.getScrollEventListener());
295
+        }
296
+        if (options.hideOnScroll.isFalse()) {
297
+            fab.disableCollapse();
298
+        }
299
+    }
300
+
184 301
     private void applyFabMenuOptions(FabMenu fabMenu, FabOptions options) {
185 302
         if (options.visible.isTrueOrUndefined()) {
186 303
             fabMenu.showMenuButton(true);
@@ -218,6 +335,45 @@ public class FabOptionsPresenter {
218 335
         }
219 336
     }
220 337
 
338
+    private void mergeFabMenuOptions(FabMenu fabMenu, FabOptions options) {
339
+        if (options.visible.isTrue()) {
340
+            fabMenu.showMenuButton(true);
341
+        }
342
+        if (options.visible.isFalse()) {
343
+            fabMenu.hideMenuButton(true);
344
+        }
345
+
346
+        if (options.backgroundColor.hasValue()) {
347
+            fabMenu.setMenuButtonColorNormal(options.backgroundColor.get());
348
+        }
349
+        if (options.clickColor.hasValue()) {
350
+            fabMenu.setMenuButtonColorPressed(options.clickColor.get());
351
+        }
352
+        if (options.rippleColor.hasValue()) {
353
+            fabMenu.setMenuButtonColorRipple(options.rippleColor.get());
354
+        }
355
+        if (options.actionsArray.size() > 0) {
356
+            for (Fab fabStored : fabMenu.getActions()) {
357
+                fabMenu.removeMenuButton(fabStored);
358
+            }
359
+            fabMenu.getActions().clear();
360
+            for (FabOptions fabOption : options.actionsArray) {
361
+                Fab fab = new Fab(viewGroup.getContext(), fabOption.id.get());
362
+                applyFabOptions(fab, fabOption);
363
+                fab.setOnClickListener(v -> component.sendOnNavigationButtonPressed(options.id.get()));
364
+
365
+                fabMenu.getActions().add(fab);
366
+                fabMenu.addMenuButton(fab);
367
+            }
368
+        }
369
+        if (options.hideOnScroll.isTrue()) {
370
+            fabMenu.enableCollapse(component.getScrollEventListener());
371
+        }
372
+        if (options.hideOnScroll.isFalse()) {
373
+            fabMenu.disableCollapse();
374
+        }
375
+    }
376
+
221 377
     private void setGravityParam(FrameLayout.LayoutParams params, int gravityParam) {
222 378
         params.gravity = params.gravity | gravityParam;
223 379
     }

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/StackController.java Ver arquivo

@@ -66,7 +66,7 @@ public class StackController extends ParentController<StackLayout> {
66 66
         getView().mergeChildOptions(options, child);
67 67
         animator.mergeOptions(options.animationsOptions);
68 68
         if (options.fabOptions.hasValue() && child instanceof ReactComponent) {
69
-            fabOptionsPresenter.applyOptions(options.fabOptions, (ReactComponent) child, getView());
69
+            fabOptionsPresenter.mergeOptions(options.fabOptions, (ReactComponent) child, getView());
70 70
         }
71 71
         applyOnParentController(parentController ->
72 72
                 ((ParentController) parentController).mergeChildOptions(