Browse Source

Hide Snacker when ViewPager screen changes

Guy Carmeli 8 years ago
parent
commit
5cd911c6f8

+ 16
- 0
android/app/src/main/java/com/reactnativenavigation/events/LocalBroadcastEvent.java View File

@@ -0,0 +1,16 @@
1
+package com.reactnativenavigation.events;
2
+
3
+import android.content.Intent;
4
+import android.support.v4.content.LocalBroadcastManager;
5
+
6
+import com.reactnativenavigation.NavigationApplication;
7
+
8
+public abstract class LocalBroadcastEvent {
9
+
10
+    public abstract Intent getIntent();
11
+
12
+    public void send() {
13
+        LocalBroadcastManager.getInstance(NavigationApplication.instance).sendBroadcast(getIntent());
14
+    }
15
+
16
+}

+ 12
- 0
android/app/src/main/java/com/reactnativenavigation/events/ScreenChangeBroadcast.java View File

@@ -0,0 +1,12 @@
1
+package com.reactnativenavigation.events;
2
+
3
+import android.content.Intent;
4
+
5
+public class ScreenChangeBroadcast extends LocalBroadcastEvent {
6
+    public static final String ACTION = "screenChange";
7
+
8
+    @Override
9
+    public Intent getIntent() {
10
+        return new Intent(ACTION);
11
+    }
12
+}

+ 40
- 0
android/app/src/main/java/com/reactnativenavigation/events/ScreenChangeBroadcastReceiver.java View File

@@ -0,0 +1,40 @@
1
+package com.reactnativenavigation.events;
2
+
3
+import android.content.BroadcastReceiver;
4
+import android.content.Context;
5
+import android.content.Intent;
6
+import android.content.IntentFilter;
7
+import android.support.v4.content.LocalBroadcastManager;
8
+
9
+import com.reactnativenavigation.NavigationApplication;
10
+
11
+public class ScreenChangeBroadcastReceiver extends BroadcastReceiver {
12
+    private OnScreenChangeListener onTabSelectedListener;
13
+
14
+    public interface OnScreenChangeListener {
15
+        void onScreenChangeListener();
16
+    }
17
+
18
+    public ScreenChangeBroadcastReceiver(OnScreenChangeListener onTabSelectedListener) {
19
+        this.onTabSelectedListener = onTabSelectedListener;
20
+    }
21
+
22
+    @Override
23
+    public void onReceive(Context context, Intent intent) {
24
+        onTabSelectedListener.onScreenChangeListener();
25
+    }
26
+
27
+    public void register() {
28
+        IntentFilter intentFilter = new IntentFilter();
29
+        intentFilter.addAction(ScreenChangeBroadcast.ACTION);
30
+        getBroadcastManager().registerReceiver(this, intentFilter);
31
+    }
32
+
33
+    public void unregister() {
34
+        getBroadcastManager().unregisterReceiver(this);
35
+    }
36
+
37
+    private LocalBroadcastManager getBroadcastManager() {
38
+        return LocalBroadcastManager.getInstance(NavigationApplication.instance);
39
+    }
40
+}

+ 6
- 4
android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java View File

@@ -243,12 +243,14 @@ public class BottomTabsLayout extends RelativeLayout implements Layout, AHBottom
243 243
     @Override
244 244
     public boolean onTabSelected(int position, boolean wasSelected) {
245 245
         hideCurrentStack();
246
+        showNewStack(position);
247
+        snackbarContainer.onScreenChange();
248
+        return true;
249
+    }
246 250
 
247
-        ScreenStack newStack = screenStacks[position];
248
-        showStackAndUpdateStyle(newStack);
251
+    private void showNewStack(int position) {
252
+        showStackAndUpdateStyle(screenStacks[position]);
249 253
         currentStackIndex = position;
250
-
251
-        return true;
252 254
     }
253 255
 
254 256
     private void showStackAndUpdateStyle(ScreenStack newStack) {

+ 1
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/SingleScreenLayout.java View File

@@ -98,6 +98,7 @@ public class SingleScreenLayout extends RelativeLayout implements Layout {
98 98
     @Override
99 99
     public void destroy() {
100 100
         stack.destroy();
101
+        snackbarContainer.destroy();
101 102
         if (sideMenu != null) {
102 103
             sideMenu.destroy();
103 104
         }

+ 8
- 2
android/app/src/main/java/com/reactnativenavigation/screens/ContentViewPagerAdapter.java View File

@@ -7,6 +7,7 @@ import android.view.ViewGroup;
7 7
 import com.facebook.react.bridge.Arguments;
8 8
 import com.facebook.react.bridge.WritableMap;
9 9
 import com.reactnativenavigation.NavigationApplication;
10
+import com.reactnativenavigation.events.ScreenChangeBroadcast;
10 11
 import com.reactnativenavigation.params.TopTabParams;
11 12
 import com.reactnativenavigation.views.ContentView;
12 13
 
@@ -33,11 +34,16 @@ public class ContentViewPagerAdapter extends PagerAdapter {
33 34
         super.setPrimaryItem(container, position, object);
34 35
         if (position != currentPosition) {
35 36
             currentPosition = position;
36
-            sendPageChangeEvent();
37
+            sendScreenChangeBroadcast();
38
+            sendTabSelectedEventToJs();
37 39
         }
38 40
     }
39 41
 
40
-    private void sendPageChangeEvent() {
42
+    private void sendScreenChangeBroadcast() {
43
+        new ScreenChangeBroadcast().send();
44
+    }
45
+
46
+    private void sendTabSelectedEventToJs() {
41 47
         WritableMap data = Arguments.createMap();
42 48
         String navigatorEventId = contentViews.get(currentPosition).getNavigatorEventId();
43 49
         NavigationApplication.instance.sendNavigatorEvent("tabSelected", navigatorEventId, data);

+ 34
- 2
android/app/src/main/java/com/reactnativenavigation/views/SnackbarContainer.java View File

@@ -3,15 +3,47 @@ package com.reactnativenavigation.views;
3 3
 import android.content.Context;
4 4
 import android.support.design.widget.CoordinatorLayout;
5 5
 
6
+import com.reactnativenavigation.events.ScreenChangeBroadcastReceiver;
6 7
 import com.reactnativenavigation.params.SnackbarParams;
7 8
 
8
-public class SnackbarContainer extends CoordinatorLayout {
9
+public class SnackbarContainer extends CoordinatorLayout implements Snakbar.OnDismissListener, ScreenChangeBroadcastReceiver.OnScreenChangeListener {
10
+
11
+    private Snakbar snakbar;
12
+    private ScreenChangeBroadcastReceiver screenChangeBroadcastReceiver;
9 13
 
10 14
     public SnackbarContainer(Context context) {
11 15
         super(context);
16
+        registerTabSelectedReceiver();
17
+    }
18
+
19
+    private void registerTabSelectedReceiver() {
20
+        screenChangeBroadcastReceiver = new ScreenChangeBroadcastReceiver(this);
21
+        screenChangeBroadcastReceiver.register();
12 22
     }
13 23
 
14 24
     public void showSnackbar(final String navigatorEventId, final SnackbarParams params) {
15
-        new Snakbar(this, navigatorEventId, params).show();
25
+        snakbar = new Snakbar(this, navigatorEventId, params);
26
+        snakbar.show();
27
+    }
28
+
29
+    public void onScreenChange() {
30
+        if (snakbar != null) {
31
+            snakbar.dismiss();
32
+            snakbar = null;
33
+        }
34
+    }
35
+
36
+    @Override
37
+    public void onDismiss() {
38
+        snakbar = null;
39
+    }
40
+
41
+    public void destroy() {
42
+        screenChangeBroadcastReceiver.unregister();
43
+    }
44
+
45
+    @Override
46
+    public void onScreenChangeListener() {
47
+        onScreenChange();
16 48
     }
17 49
 }

+ 33
- 5
android/app/src/main/java/com/reactnativenavigation/views/Snakbar.java View File

@@ -7,21 +7,27 @@ import com.reactnativenavigation.NavigationApplication;
7 7
 import com.reactnativenavigation.params.SnackbarParams;
8 8
 
9 9
 public class Snakbar {
10
-    private final View parent;
10
+    private final SnackbarContainer parent;
11 11
     private final String navigatorEventId;
12 12
     private final SnackbarParams params;
13
+    private Snackbar snackbar;
13 14
 
14
-    public Snakbar(View parent, String navigatorEventId, SnackbarParams params) {
15
+    public interface OnDismissListener {
16
+        void onDismiss();
17
+    }
18
+
19
+    public Snakbar(SnackbarContainer parent, String navigatorEventId, SnackbarParams params) {
15 20
         this.parent = parent;
16 21
         this.navigatorEventId = navigatorEventId;
17 22
         this.params = params;
23
+        create();
18 24
     }
19 25
 
20
-    public void show() {
21
-        Snackbar snackbar = Snackbar.make(parent, params.text, params.duration);
26
+    private void create() {
27
+        snackbar = Snackbar.make(parent, params.text, params.duration);
22 28
         setAction(navigatorEventId, params, snackbar);
23 29
         setStyle(snackbar, params);
24
-        snackbar.show();
30
+        setOnDismissListener();
25 31
     }
26 32
 
27 33
     private void setAction(final String navigatorEventId, final SnackbarParams params, Snackbar snackbar) {
@@ -40,4 +46,26 @@ public class Snakbar {
40 46
             snackbar.setActionTextColor(params.buttonColor.getColor());
41 47
         }
42 48
     }
49
+
50
+    private void setOnDismissListener() {
51
+        snackbar.getView().addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
52
+            @Override
53
+            public void onViewAttachedToWindow(View v) {
54
+
55
+            }
56
+
57
+            @Override
58
+            public void onViewDetachedFromWindow(View v) {
59
+                parent.onDismiss();
60
+            }
61
+        });
62
+    }
63
+
64
+    public void show() {
65
+        snackbar.show();
66
+    }
67
+
68
+    public void dismiss() {
69
+        snackbar.dismiss();
70
+    }
43 71
 }