Browse Source

Add infra for adding Snackbar to Layouts.

Guy Carmeli 8 years ago
parent
commit
af4e990660

+ 8
- 0
android/app/src/main/java/com/reactnativenavigation/bridge/NavigationReactModule.java View File

@@ -6,8 +6,10 @@ import com.facebook.react.bridge.ReactMethod;
6 6
 import com.facebook.react.bridge.ReadableArray;
7 7
 import com.facebook.react.bridge.ReadableMap;
8 8
 import com.reactnativenavigation.controllers.NavigationCommandsHandler;
9
+import com.reactnativenavigation.params.SnackbarParams;
9 10
 import com.reactnativenavigation.params.TitleBarButtonParams;
10 11
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
12
+import com.reactnativenavigation.params.parsers.SnackbarParamsParser;
11 13
 import com.reactnativenavigation.params.parsers.TitleBarButtonParamsParser;
12 14
 import com.reactnativenavigation.params.parsers.TitleBarLeftButtonParamsParser;
13 15
 
@@ -153,4 +155,10 @@ public class NavigationReactModule extends ReactContextBaseJavaModule {
153 155
     public void dismissTopModal() {
154 156
         NavigationCommandsHandler.dismissTopModal();
155 157
     }
158
+
159
+    @ReactMethod
160
+    public void showSnackbar(final ReadableMap params) {
161
+        SnackbarParams snackbarParams = new SnackbarParamsParser().parse(BundleConverter.toBundle(params));
162
+        NavigationCommandsHandler.showSnackbar(snackbarParams);
163
+    }
156 164
 }

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java View File

@@ -12,6 +12,7 @@ import com.reactnativenavigation.layouts.Layout;
12 12
 import com.reactnativenavigation.layouts.LayoutFactory;
13 13
 import com.reactnativenavigation.params.ActivityParams;
14 14
 import com.reactnativenavigation.params.ScreenParams;
15
+import com.reactnativenavigation.params.SnackbarParams;
15 16
 import com.reactnativenavigation.params.TitleBarButtonParams;
16 17
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
17 18
 import com.reactnativenavigation.react.JsDevReloadHandler;
@@ -239,4 +240,8 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
239 240
             ((BottomTabsLayout) layout).setBottomTabBadgeByNavigatorId(navigatorId, badge);
240 241
         }
241 242
     }
243
+
244
+    public void showSnackbar(SnackbarParams params) {
245
+        layout.showSnackbar(params);
246
+    }
242 247
 }

+ 15
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationCommandsHandler.java View File

@@ -6,6 +6,7 @@ import android.os.Bundle;
6 6
 import com.reactnativenavigation.NavigationApplication;
7 7
 import com.reactnativenavigation.params.ActivityParams;
8 8
 import com.reactnativenavigation.params.ScreenParams;
9
+import com.reactnativenavigation.params.SnackbarParams;
9 10
 import com.reactnativenavigation.params.TitleBarButtonParams;
10 11
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
11 12
 import com.reactnativenavigation.params.parsers.ActivityParamsParser;
@@ -294,4 +295,18 @@ public class NavigationCommandsHandler {
294 295
             }
295 296
         });
296 297
     }
298
+
299
+    public static void showSnackbar(final SnackbarParams params) {
300
+        final NavigationActivity currentActivity = NavigationActivity.currentActivity;
301
+        if (currentActivity == null) {
302
+            return;
303
+        }
304
+
305
+        NavigationApplication.instance.runOnMainThread(new Runnable() {
306
+            @Override
307
+            public void run() {
308
+                currentActivity.showSnackbar(params);
309
+            }
310
+        });
311
+    }
297 312
 }

+ 16
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java View File

@@ -10,11 +10,13 @@ import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
10 10
 import com.reactnativenavigation.params.ActivityParams;
11 11
 import com.reactnativenavigation.params.ScreenParams;
12 12
 import com.reactnativenavigation.params.SideMenuParams;
13
+import com.reactnativenavigation.params.SnackbarParams;
13 14
 import com.reactnativenavigation.params.TitleBarButtonParams;
14 15
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
15 16
 import com.reactnativenavigation.screens.ScreenStack;
16 17
 import com.reactnativenavigation.views.BottomTabs;
17 18
 import com.reactnativenavigation.views.SideMenu;
19
+import com.reactnativenavigation.views.SnackbarContainer;
18 20
 
19 21
 import java.util.List;
20 22
 
@@ -25,6 +27,7 @@ public class BottomTabsLayout extends RelativeLayout implements Layout, AHBottom
25 27
 
26 28
     private final AppCompatActivity activity;
27 29
     private ActivityParams params;
30
+    private SnackbarContainer snackbarContainer;
28 31
     private BottomTabs bottomTabs;
29 32
     private ScreenStack[] screenStacks;
30 33
     private final SideMenuParams sideMenuParams;
@@ -45,6 +48,7 @@ public class BottomTabsLayout extends RelativeLayout implements Layout, AHBottom
45 48
         createBottomTabs();
46 49
         addBottomTabs();
47 50
         addScreenStacks();
51
+        createSnackbarContainer();
48 52
         showInitialScreenStack();
49 53
     }
50 54
 
@@ -94,6 +98,13 @@ public class BottomTabsLayout extends RelativeLayout implements Layout, AHBottom
94 98
         getScreenStackParent().addView(bottomTabs, lp);
95 99
     }
96 100
 
101
+    private void createSnackbarContainer() {
102
+        snackbarContainer = new SnackbarContainer(getContext());
103
+        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
104
+        lp.addRule(ABOVE, bottomTabs.getId());
105
+        snackbarContainer.setLayoutParams(lp);
106
+    }
107
+
97 108
     private void showInitialScreenStack() {
98 109
         showStackAndUpdateStyle(screenStacks[0]);
99 110
     }
@@ -164,6 +175,11 @@ public class BottomTabsLayout extends RelativeLayout implements Layout, AHBottom
164 175
         }
165 176
     }
166 177
 
178
+    @Override
179
+    public void showSnackbar(SnackbarParams params) {
180
+        // TODO: implement me
181
+    }
182
+
167 183
     public void selectBottomTabByTabIndex(Integer index) {
168 184
         bottomTabs.setCurrentItem(index);
169 185
     }

+ 3
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/Layout.java View File

@@ -2,6 +2,7 @@ package com.reactnativenavigation.layouts;
2 2
 
3 3
 import android.view.View;
4 4
 
5
+import com.reactnativenavigation.params.SnackbarParams;
5 6
 import com.reactnativenavigation.params.TitleBarButtonParams;
6 7
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
7 8
 
@@ -23,4 +24,6 @@ public interface Layout extends ScreenStackContainer {
23 24
     void toggleSideMenuVisible(boolean animated);
24 25
 
25 26
     void setSideMenuVisible(boolean animated, boolean visible);
27
+
28
+    void showSnackbar(SnackbarParams params);
26 29
 }

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

@@ -7,6 +7,7 @@ import android.widget.RelativeLayout;
7 7
 
8 8
 import com.reactnativenavigation.params.ScreenParams;
9 9
 import com.reactnativenavigation.params.SideMenuParams;
10
+import com.reactnativenavigation.params.SnackbarParams;
10 11
 import com.reactnativenavigation.params.TitleBarButtonParams;
11 12
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
12 13
 import com.reactnativenavigation.screens.ScreenStack;
@@ -148,6 +149,11 @@ public class SingleScreenLayout extends RelativeLayout implements Layout {
148 149
         }
149 150
     }
150 151
 
152
+    @Override
153
+    public void showSnackbar(SnackbarParams params) {
154
+        // TODO: implement me
155
+    }
156
+
151 157
     @Override
152 158
     public boolean onTitleBarBackButtonClick() {
153 159
         if (leftButtonOnClickListener != null) {

+ 8
- 0
android/app/src/main/java/com/reactnativenavigation/params/SnackbarParams.java View File

@@ -0,0 +1,8 @@
1
+package com.reactnativenavigation.params;
2
+
3
+public class SnackbarParams {
4
+    public String text;
5
+    public StyleParams.Color textColor;
6
+    public String buttonText;
7
+    public StyleParams.Color buttonColor;
8
+}

+ 3
- 0
android/app/src/main/java/com/reactnativenavigation/params/StyleParams.java View File

@@ -55,6 +55,9 @@ public class StyleParams {
55 55
 
56 56
     public boolean drawScreenAboveBottomTabs;
57 57
 
58
+    public Color snackbarTextColor;
59
+    public Color snackbarButtonColor;
60
+
58 61
     public boolean bottomTabsHidden;
59 62
     public boolean bottomTabsHiddenOnScroll;
60 63
     public Color bottomTabsColor;

+ 7
- 1
android/app/src/main/java/com/reactnativenavigation/params/parsers/Parser.java View File

@@ -2,6 +2,8 @@ package com.reactnativenavigation.params.parsers;
2 2
 
3 3
 import android.os.Bundle;
4 4
 
5
+import com.reactnativenavigation.params.StyleParams;
6
+
5 7
 import java.util.ArrayList;
6 8
 import java.util.Collections;
7 9
 import java.util.List;
@@ -11,7 +13,6 @@ public class Parser {
11 13
         return bundle.keySet().contains(key);
12 14
     }
13 15
 
14
-
15 16
     protected static void assertKeyExists(Bundle bundle, String key) {
16 17
         if (!hasKey(bundle, key)) {
17 18
             throw new KeyDoesNotExistsException(key);
@@ -35,4 +36,9 @@ public class Parser {
35 36
         }
36 37
         return result;
37 38
     }
39
+
40
+    protected StyleParams.Color getColor(Bundle bundle, String key, StyleParams.Color defaultColor) {
41
+        StyleParams.Color color = StyleParams.Color.parse(bundle.getString(key));
42
+        return color.hasColor() || defaultColor == null ? color : defaultColor;
43
+    }
38 44
 }

+ 18
- 0
android/app/src/main/java/com/reactnativenavigation/params/parsers/SnackbarParamsParser.java View File

@@ -0,0 +1,18 @@
1
+package com.reactnativenavigation.params.parsers;
2
+
3
+import android.os.Bundle;
4
+
5
+import com.reactnativenavigation.params.AppStyle;
6
+import com.reactnativenavigation.params.SnackbarParams;
7
+import com.reactnativenavigation.params.StyleParams;
8
+
9
+public class SnackbarParamsParser extends Parser {
10
+    public SnackbarParams parse(Bundle params) {
11
+        SnackbarParams result = new SnackbarParams();
12
+        result.text = params.getString("text");
13
+        result.textColor = getColor(params, "textColor", AppStyle.appStyle.snackbarTextColor);
14
+        result.buttonText = params.getString("buttonText");
15
+        result.buttonColor = getColor(params, "buttonColor", AppStyle.appStyle.snackbarButtonColor);
16
+        return result;
17
+    }
18
+}

+ 0
- 6
android/app/src/main/java/com/reactnativenavigation/params/parsers/TitleBarButtonParamsParser.java View File

@@ -3,7 +3,6 @@ package com.reactnativenavigation.params.parsers;
3 3
 import android.os.Bundle;
4 4
 
5 5
 import com.reactnativenavigation.params.AppStyle;
6
-import com.reactnativenavigation.params.StyleParams;
7 6
 import com.reactnativenavigation.params.TitleBarButtonParams;
8 7
 import com.reactnativenavigation.react.ImageLoader;
9 8
 
@@ -33,11 +32,6 @@ public class TitleBarButtonParamsParser extends Parser {
33 32
         return result;
34 33
     }
35 34
 
36
-    private StyleParams.Color getColor(Bundle bundle, String key, StyleParams.Color defaultColor) {
37
-        StyleParams.Color color = StyleParams.Color.parse(bundle.getString(key));
38
-        return color.hasColor() || defaultColor == null ? color : defaultColor;
39
-    }
40
-
41 35
     private static TitleBarButtonParams.ShowAsAction parseShowAsAction(String showAsAction) {
42 36
         if (showAsAction == null) {
43 37
             return TitleBarButtonParams.ShowAsAction.IfRoom;

+ 12
- 0
android/app/src/main/java/com/reactnativenavigation/views/SnackbarContainer.java View File

@@ -0,0 +1,12 @@
1
+package com.reactnativenavigation.views;
2
+
3
+import android.content.Context;
4
+import android.widget.LinearLayout;
5
+
6
+public class SnackbarContainer extends LinearLayout {
7
+
8
+    public SnackbarContainer(Context context) {
9
+        super(context);
10
+        setOrientation(LinearLayout.VERTICAL);
11
+    }
12
+}

+ 4
- 0
src/Screen.js View File

@@ -101,6 +101,10 @@ class Navigator {
101 101
     return platformSpecific.navigatorSwitchToTab(this, params);
102 102
   }
103 103
 
104
+  showSnackbar(params = {}) {
105
+    return platformSpecific.showSnackbar(this, params);
106
+  }
107
+
104 108
   setOnNavigatorEvent(callback) {
105 109
     this.navigatorEventHandler = callback;
106 110
     if (!this.navigatorEventSubscription) {

+ 6
- 1
src/deprecated/platformSpecificDeprecated.android.js View File

@@ -418,6 +418,10 @@ function addNavigationStyleParams(screen) {
418 418
   screen.navigatorStyle = Object.assign({}, screen.navigatorStyle, Screen.navigatorStyle);
419 419
 }
420 420
 
421
+function showSnackbar(navigator, params) {
422
+  return newPlatformSpecific.showSnackbar(params.text);
423
+}
424
+
421 425
 export default {
422 426
   startTabBasedApp,
423 427
   startSingleScreenApp,
@@ -434,5 +438,6 @@ export default {
434 438
   navigatorSwitchToTab,
435 439
   navigatorToggleDrawer,
436 440
   navigatorToggleTabs,
437
-  navigatorToggleNavBar
441
+  navigatorToggleNavBar,
442
+  showSnackbar
438 443
 };

+ 6
- 1
src/platformSpecific.android.js View File

@@ -108,6 +108,10 @@ function setBottomTabBadgeByNavigatorId(navigatorId, badge) {
108 108
   NativeReactModule.setBottomTabBadgeByNavigatorId(navigatorId, badge);
109 109
 }
110 110
 
111
+function showSnackbar(text) {
112
+  NativeReactModule.showSnackbar(text);
113
+}
114
+
111 115
 module.exports = {
112 116
   startApp,
113 117
   push,
@@ -126,5 +130,6 @@ module.exports = {
126 130
   selectBottomTabByNavigatorId,
127 131
   selectBottomTabByTabIndex,
128 132
   setBottomTabBadgeByNavigatorId,
129
-  setBottomTabBadgeByIndex
133
+  setBottomTabBadgeByIndex,
134
+  showSnackbar
130 135
 };