Browse Source

Send fab press events and collapse fab on action click

Guy Carmeli 8 years ago
parent
commit
4b756ce0b1

+ 1
- 0
android/app/src/main/java/com/reactnativenavigation/params/FabActionParams.java View File

4
 
4
 
5
 public class FabActionParams {
5
 public class FabActionParams {
6
     public String id;
6
     public String id;
7
+    public String navigatorEventId;
7
     public Drawable icon;
8
     public Drawable icon;
8
     public StyleParams.Color backgroundColor;
9
     public StyleParams.Color backgroundColor;
9
 }
10
 }

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/params/FabParams.java View File

9
     public Drawable expendedIcon;
9
     public Drawable expendedIcon;
10
     public StyleParams.Color backgroundColor;
10
     public StyleParams.Color backgroundColor;
11
     public String id;
11
     public String id;
12
+    public String navigatorEventId;
12
     public List<FabActionParams> actions;
13
     public List<FabActionParams> actions;
14
+
15
+    public boolean hasExpendedState() {
16
+        return actions != null && actions.size() > 0;
17
+    }
13
 }
18
 }

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/params/parsers/ButtonParser.java View File

35
         return leftButton;
35
         return leftButton;
36
     }
36
     }
37
 
37
 
38
-    public static FabParams parseFab(Bundle params) {
38
+    public static FabParams parseFab(Bundle params, String navigatorEventId) {
39
         FabParams fabParams = null;
39
         FabParams fabParams = null;
40
         if (hasKey(params, KEY_FAB)) {
40
         if (hasKey(params, KEY_FAB)) {
41
-            fabParams = new FabParamsParser().parse(params.getBundle(KEY_FAB));
41
+            fabParams = new FabParamsParser().parse(params.getBundle(KEY_FAB), navigatorEventId);
42
         }
42
         }
43
         return fabParams;
43
         return fabParams;
44
     }
44
     }

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

7
 import com.reactnativenavigation.react.ImageLoader;
7
 import com.reactnativenavigation.react.ImageLoader;
8
 
8
 
9
 public class FabActionParamsParser extends Parser {
9
 public class FabActionParamsParser extends Parser {
10
-    public FabActionParams parse(Bundle params) {
10
+    public FabActionParams parse(Bundle params, String navigatorEventId) {
11
         FabActionParams fabActionParams = new FabActionParams();
11
         FabActionParams fabActionParams = new FabActionParams();
12
         fabActionParams.id = params.getString("id");
12
         fabActionParams.id = params.getString("id");
13
+        fabActionParams.navigatorEventId = navigatorEventId;
13
         fabActionParams.icon = ImageLoader.loadImage(params.getString("icon"));
14
         fabActionParams.icon = ImageLoader.loadImage(params.getString("icon"));
14
         fabActionParams.backgroundColor = StyleParams.Color.parse(params.getString("backgroundColor"));
15
         fabActionParams.backgroundColor = StyleParams.Color.parse(params.getString("backgroundColor"));
15
         return fabActionParams;
16
         return fabActionParams;

+ 3
- 3
android/app/src/main/java/com/reactnativenavigation/params/parsers/FabParamsParser.java View File

8
 import com.reactnativenavigation.react.ImageLoader;
8
 import com.reactnativenavigation.react.ImageLoader;
9
 
9
 
10
 public class FabParamsParser extends Parser {
10
 public class FabParamsParser extends Parser {
11
-    public FabParams parse(Bundle params) {
11
+    public FabParams parse(Bundle params, final String navigatorEventId) {
12
         FabParams fabParams = new FabParams();
12
         FabParams fabParams = new FabParams();
13
         fabParams.id = params.getString("id");
13
         fabParams.id = params.getString("id");
14
+        fabParams.navigatorEventId = navigatorEventId;
14
         fabParams.backgroundColor = getColor(params, "backgroundColor", new StyleParams.Color());
15
         fabParams.backgroundColor = getColor(params, "backgroundColor", new StyleParams.Color());
15
 
16
 
16
         if (hasKey(params, "collapsedIcon")) {
17
         if (hasKey(params, "collapsedIcon")) {
23
             fabParams.actions = parseBundle(params.getBundle("actions"), new ParseStrategy<FabActionParams>() {
24
             fabParams.actions = parseBundle(params.getBundle("actions"), new ParseStrategy<FabActionParams>() {
24
                 @Override
25
                 @Override
25
                 public FabActionParams parse(Bundle params) {
26
                 public FabActionParams parse(Bundle params) {
26
-                    return new FabActionParamsParser().parse(params);
27
+                    return new FabActionParamsParser().parse(params, navigatorEventId);
27
                 }
28
                 }
28
             });
29
             });
29
         }
30
         }
30
-
31
         return fabParams;
31
         return fabParams;
32
     }
32
     }
33
 }
33
 }

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

41
             result.fragmentCreatorPassProps = params.getBundle(FRAGMENT_CREATOR_PASS_PROPS);
41
             result.fragmentCreatorPassProps = params.getBundle(FRAGMENT_CREATOR_PASS_PROPS);
42
         }
42
         }
43
 
43
 
44
-        result.fabParams = ButtonParser.parseFab(params);
44
+        result.fabParams = ButtonParser.parseFab(params, result.navigationParams.navigatorEventId);
45
 
45
 
46
         result.tabLabel = getTabLabel(params);
46
         result.tabLabel = getTabLabel(params);
47
         result.tabIcon = getTabIcon(params);
47
         result.tabIcon = getTabIcon(params);

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

31
         result.navigationParams = new NavigationParams(params.getBundle(NAVIGATION_PARAMS));
31
         result.navigationParams = new NavigationParams(params.getBundle(NAVIGATION_PARAMS));
32
         result.leftButton = ButtonParser.parseLeftButton(params);
32
         result.leftButton = ButtonParser.parseLeftButton(params);
33
         result.rightButtons = ButtonParser.parseRightButton(params);
33
         result.rightButtons = ButtonParser.parseRightButton(params);
34
-        result.fabParams = ButtonParser.parseFab(params);
34
+        result.fabParams = ButtonParser.parseFab(params, result.navigationParams.navigatorEventId);
35
         return result;
35
         return result;
36
     }
36
     }
37
 }
37
 }

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/views/FloatingActionButtonAnimator.java View File

44
                 .start();
44
                 .start();
45
     }
45
     }
46
 
46
 
47
+    public void collapse() {
48
+        hideExpended();
49
+        showCollapsed();
50
+    }
51
+
47
     void hideCollapsed() {
52
     void hideCollapsed() {
48
         animateFab(collapsedFab, 0, 90);
53
         animateFab(collapsedFab, 0, 90);
49
     }
54
     }

+ 22
- 6
android/app/src/main/java/com/reactnativenavigation/views/FloatingActionButtonCoordinator.java View File

12
 import android.view.Gravity;
12
 import android.view.Gravity;
13
 import android.view.View;
13
 import android.view.View;
14
 
14
 
15
+import com.reactnativenavigation.NavigationApplication;
15
 import com.reactnativenavigation.params.FabActionParams;
16
 import com.reactnativenavigation.params.FabActionParams;
16
 import com.reactnativenavigation.params.FabParams;
17
 import com.reactnativenavigation.params.FabParams;
17
 import com.reactnativenavigation.utils.ViewUtils;
18
 import com.reactnativenavigation.utils.ViewUtils;
87
         collapsedFab.setOnClickListener(new View.OnClickListener() {
88
         collapsedFab.setOnClickListener(new View.OnClickListener() {
88
             @Override
89
             @Override
89
             public void onClick(View v) {
90
             public void onClick(View v) {
90
-                fabAnimator.hideCollapsed();
91
-                fabAnimator.showExpended();
92
-                showActions();
91
+                if (params.hasExpendedState()) {
92
+                    fabAnimator.hideCollapsed();
93
+                    fabAnimator.showExpended();
94
+                    showActions();
95
+                } else {
96
+                    NavigationApplication.instance.sendNavigatorEvent(params.id, params.navigatorEventId);
97
+                }
93
             }
98
             }
94
         });
99
         });
95
     }
100
     }
102
         expendedFab.setOnClickListener(new View.OnClickListener() {
107
         expendedFab.setOnClickListener(new View.OnClickListener() {
103
             @Override
108
             @Override
104
             public void onClick(View v) {
109
             public void onClick(View v) {
105
-                fabAnimator.hideExpended();
106
-                fabAnimator.showCollapsed();
110
+                fabAnimator.collapse();
107
             }
111
             }
108
         });
112
         });
109
     }
113
     }
142
     }
146
     }
143
 
147
 
144
     private FloatingActionButton createAction(int index) {
148
     private FloatingActionButton createAction(int index) {
145
-        FabActionParams actionParams = params.actions.get(index);
149
+        final FabActionParams actionParams = params.actions.get(index);
146
         FloatingActionButton action = createFab(actionParams.icon);
150
         FloatingActionButton action = createFab(actionParams.icon);
147
         action.setLayoutParams(createActionLayoutParams(index));
151
         action.setLayoutParams(createActionLayoutParams(index));
152
+        action.setOnClickListener(new View.OnClickListener() {
153
+            @Override
154
+            public void onClick(View v) {
155
+                NavigationApplication.instance.sendNavigatorEvent(actionParams.id, actionParams.navigatorEventId);
156
+                fabAnimator.collapse();
157
+            }
158
+        });
148
         if (actionParams.backgroundColor.hasColor()) {
159
         if (actionParams.backgroundColor.hasColor()) {
149
             action.setBackgroundTintList(ColorStateList.valueOf(actionParams.backgroundColor.getColor()));
160
             action.setBackgroundTintList(ColorStateList.valueOf(actionParams.backgroundColor.getColor()));
150
         }
161
         }
182
             float fraction = calculateTransitionFraction(dependentValue);
193
             float fraction = calculateTransitionFraction(dependentValue);
183
             child.setY(calculateY(parent, fraction));
194
             child.setY(calculateY(parent, fraction));
184
             child.setAlpha(calculateAlpha(fraction));
195
             child.setAlpha(calculateAlpha(fraction));
196
+            setVisibility(child);
185
             return true;
197
             return true;
186
         }
198
         }
187
 
199
 
200
+        private void setVisibility(FloatingActionButton child) {
201
+            child.setVisibility(child.getAlpha() == 0 ? View.GONE : View.VISIBLE);
202
+        }
203
+
188
         private float calculateAlpha(float fraction) {
204
         private float calculateAlpha(float fraction) {
189
             return 1 * fraction;
205
             return 1 * fraction;
190
         }
206
         }