123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- package com.reactnativenavigation.views;
-
- import android.content.Context;
- import android.support.design.widget.CoordinatorLayout;
-
- import com.reactnativenavigation.events.Event;
- import com.reactnativenavigation.events.EventBus;
- import com.reactnativenavigation.events.FabSetEvent;
- import com.reactnativenavigation.events.ScreenChangedEvent;
- import com.reactnativenavigation.events.Subscriber;
- import com.reactnativenavigation.layouts.Layout;
- import com.reactnativenavigation.params.FabParams;
- import com.reactnativenavigation.params.SnackbarParams;
-
- public class SnackbarAndFabContainer extends CoordinatorLayout implements Snakbar.OnDismissListener, Subscriber{
- private Snakbar snakbar;
- private FloatingActionButtonCoordinator fabCoordinator;
- private Layout layout;
-
- public SnackbarAndFabContainer(Context context, Layout layout) {
- super(context);
- this.layout = layout;
- fabCoordinator = new FloatingActionButtonCoordinator(this);
- EventBus.instance.register(this);
- }
-
- public void showSnackbar(final String navigatorEventId, final SnackbarParams params) {
- snakbar = new Snakbar(this, navigatorEventId, params);
- snakbar.show();
- }
-
- @Override
- public void onDismiss(Snakbar snakbar) {
- if (this.snakbar == snakbar) {
- this.snakbar = null;
- }
- }
-
- public void destroy() {
- EventBus.instance.unregister(this);
- }
-
- @Override
- public void onEvent(Event event) {
- if (ScreenChangedEvent.TYPE.equals(event.getType())) {
- onScreenChange(((ScreenChangedEvent) event).fabParams);
- }
- if (FabSetEvent.TYPE.equals(event.getType())) {
- updateFab(((FabSetEvent) event).fabParams);
- }
- }
-
- private void onScreenChange(FabParams fabParams) {
- dismissSnackbar();
- updateFab(fabParams);
- }
-
- private void dismissSnackbar() {
- if (snakbar != null) {
- snakbar.dismiss();
- snakbar = null;
- }
- }
-
- private void updateFab(final FabParams fabParams) {
- fabCoordinator.remove(new Runnable() {
- @Override
- public void run() {
- if (fabParams != null) {
- if (layout.getCurrentScreen().getScreenInstanceId().equals(fabParams.screenInstanceId)) {
- fabCoordinator.add(fabParams);
- }
- }
- }
- });
- }
- }
|