Parcourir la source

basic FAB support

Daniel Zlotin il y a 8 ans
Parent
révision
7ce30d9f3d

+ 3
- 0
android/app/src/main/java/com/reactnativenavigation/activities/BaseReactActivity.java Voir le fichier

@@ -464,4 +464,7 @@ public abstract class BaseReactActivity extends AppCompatActivity implements Def
464 464
                 break;
465 465
         }
466 466
     }
467
+
468
+    public void showFAB(ReadableMap params) {
469
+    }
467 470
 }

+ 15
- 0
android/app/src/main/java/com/reactnativenavigation/activities/SingleScreenActivity.java Voir le fichier

@@ -2,6 +2,7 @@ package com.reactnativenavigation.activities;
2 2
 
3 3
 import android.support.design.widget.CoordinatorLayout;
4 4
 
5
+import com.facebook.react.bridge.ReadableMap;
5 6
 import com.reactnativenavigation.R;
6 7
 import com.reactnativenavigation.core.RctManager;
7 8
 import com.reactnativenavigation.core.objects.Drawer;
@@ -107,4 +108,18 @@ public class SingleScreenActivity extends BaseReactActivity {
107 108
     protected void removeAllReactViews() {
108 109
         mScreenStack.removeAllReactViews();
109 110
     }
111
+
112
+    @Override
113
+    public void showFAB(ReadableMap params) {
114
+//        FloatingActionButton fab = new FloatingActionButton(this);
115
+//        fab.setImageDrawable(IconUtils.getIcon(this, params.getString("icon")));
116
+//        fab.setBackgroundColor(Color.parseColor(params.getString("backgroundColor")));
117
+//        fab.setImageResource(R.drawable.notification_background);
118
+//        CoordinatorLayout content = (CoordinatorLayout) findViewById(R.id.contentFrame);
119
+//        CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
120
+//        layoutParams.gravity = Gravity.BOTTOM | Gravity.END;
121
+//        int m = (int) ImageUtils.convertDpToPixel(16, this);
122
+//        layoutParams.setMargins(m, m, m, m);
123
+//        content.addView(fab, layoutParams);
124
+    }
110 125
 }

+ 26
- 11
android/app/src/main/java/com/reactnativenavigation/modules/RctActivityModule.java Voir le fichier

@@ -55,7 +55,7 @@ public class RctActivityModule extends ReactContextBaseJavaModule {
55 55
 
56 56
             context.startActivity(intent);
57 57
             //TODO add abstract isRoot() instead of instanceof?
58
-            if(ContextProvider.getActivityContext() instanceof RootActivity) {
58
+            if (ContextProvider.getActivityContext() instanceof RootActivity) {
59 59
                 context.overridePendingTransition(0, 0);
60 60
             }
61 61
 
@@ -66,7 +66,7 @@ public class RctActivityModule extends ReactContextBaseJavaModule {
66 66
 
67 67
     private ArrayList<Screen> createScreens(ReadableArray screens) {
68 68
         ArrayList<Screen> ret = new ArrayList<>();
69
-        for(int i = 0; i < screens.size(); i++) {
69
+        for (int i = 0; i < screens.size(); i++) {
70 70
             ret.add(new Screen(screens.getMap(i)));
71 71
         }
72 72
         return ret;
@@ -87,7 +87,7 @@ public class RctActivityModule extends ReactContextBaseJavaModule {
87 87
             intent.putExtras(extras);
88 88
 
89 89
             context.startActivity(intent);
90
-            if(ContextProvider.getActivityContext() instanceof RootActivity) {
90
+            if (ContextProvider.getActivityContext() instanceof RootActivity) {
91 91
                 context.overridePendingTransition(0, 0);
92 92
             }
93 93
 
@@ -306,15 +306,15 @@ public class RctActivityModule extends ReactContextBaseJavaModule {
306 306
     public void dismissAllModals(final ReadableMap params) {
307 307
         final BaseReactActivity context = ContextProvider.getActivityContext();
308 308
         if (context != null && !context.isFinishing()) {
309
-        context.runOnUiThread(new Runnable() {
310
-            @Override
311
-            public void run() {
312
-                ModalController modalController = ModalController.getInstance();
313
-                if (modalController.isModalDisplayed()) {
314
-                    modalController.dismissAllModals();
309
+            context.runOnUiThread(new Runnable() {
310
+                @Override
311
+                public void run() {
312
+                    ModalController modalController = ModalController.getInstance();
313
+                    if (modalController.isModalDisplayed()) {
314
+                        modalController.dismissAllModals();
315
+                    }
315 316
                 }
316
-            }
317
-        });
317
+            });
318 318
         }
319 319
     }
320 320
 
@@ -328,4 +328,19 @@ public class RctActivityModule extends ReactContextBaseJavaModule {
328 328
             modalController.dismissModal();
329 329
         }
330 330
     }
331
+
332
+    @ReactMethod
333
+    public void showFAB(final ReadableMap params) {
334
+        final BaseReactActivity context = ContextProvider.getActivityContext();
335
+        if (context == null || context.isFinishing()) {
336
+            return;
337
+        }
338
+        context.runOnUiThread(new Runnable() {
339
+            @Override
340
+            public void run() {
341
+                context.showFAB(params);
342
+            }
343
+        });
344
+    }
345
+
331 346
 }

+ 4
- 8
android/app/src/main/java/com/reactnativenavigation/utils/ImageUtils.java Voir le fichier

@@ -1,11 +1,9 @@
1 1
 package com.reactnativenavigation.utils;
2 2
 
3 3
 import android.content.Context;
4
-import android.content.res.Resources;
5 4
 import android.graphics.PorterDuff;
6 5
 import android.graphics.PorterDuffColorFilter;
7 6
 import android.graphics.drawable.Drawable;
8
-import android.util.DisplayMetrics;
9 7
 
10 8
 /**
11 9
  * Created by guyc on 23/04/16.
@@ -19,14 +17,12 @@ public class ImageUtils {
19 17
     /**
20 18
      * This method converts dp unit to equivalent pixels, depending on device density.
21 19
      *
22
-     * @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
20
+     * @param dp      A value in dp (density independent pixels) unit. Which we need to convert into pixels
23 21
      * @param context Context to get resources and device specific display metrics
24 22
      * @return A float value to represent px equivalent to dp depending on device density
25 23
      */
26
-    public static float convertDpToPixel(float dp, Context context){
27
-        Resources resources = context.getResources();
28
-        DisplayMetrics metrics = resources.getDisplayMetrics();
29
-        float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
30
-        return px;
24
+    public static float convertDpToPixel(float dp, Context context) {
25
+        float scale = context.getResources().getDisplayMetrics().density;
26
+        return dp * scale + 0.5f;
31 27
     }
32 28
 }

+ 9
- 3
example-redux/src/screens/LoginScreen.js Voir le fichier

@@ -6,7 +6,7 @@ import {
6 6
   TouchableOpacity,
7 7
   StyleSheet
8 8
 } from 'react-native';
9
-import { connect } from 'react-redux';
9
+import {connect} from 'react-redux';
10 10
 import * as counterActions from '../reducers/counter/actions';
11 11
 import * as appActions from '../reducers/app/actions';
12 12
 
@@ -21,6 +21,10 @@ class LoginScreen extends Component {
21 21
 
22 22
   constructor(props) {
23 23
     super(props);
24
+    this.props.navigator.showFAB({
25
+      icon: require('../../img/navicon_edit@2x.png'),
26
+      backgroundColor: 'blue'
27
+    });
24 28
   }
25 29
 
26 30
   render() {
@@ -48,9 +52,11 @@ class LoginScreen extends Component {
48 52
       </View>
49 53
     );
50 54
   }
55
+
51 56
   onIncrementPress() {
52 57
     this.props.dispatch(counterActions.increment());
53 58
   }
59
+
54 60
   onLoginPress() {
55 61
     this.props.dispatch(appActions.login());
56 62
   }
@@ -61,13 +67,13 @@ const styles = StyleSheet.create({
61 67
     textAlign: 'center',
62 68
     fontSize: 18,
63 69
     marginBottom: 10,
64
-    marginTop:10,
70
+    marginTop: 10,
65 71
   },
66 72
   button: {
67 73
     textAlign: 'center',
68 74
     fontSize: 18,
69 75
     marginBottom: 10,
70
-    marginTop:10,
76
+    marginTop: 10,
71 77
     color: 'blue'
72 78
   }
73 79
 });

+ 6
- 1
src/Screen.js Voir le fichier

@@ -93,6 +93,10 @@ class Navigator {
93 93
     return platformSpecific.navigatorSwitchToTab(this, params);
94 94
   }
95 95
 
96
+  showFAB(params = {}) {
97
+    return platformSpecific.showFAB(params);
98
+  }
99
+
96 100
   setOnNavigatorEvent(callback) {
97 101
     this.navigatorEventHandler = callback;
98 102
     if (!this.navigatorEventSubscription) {
@@ -101,6 +105,7 @@ class Navigator {
101 105
       _allNavigatorEventHandlers[this.navigatorEventID] = (event) => this.onNavigatorEvent(event);
102 106
     }
103 107
   }
108
+
104 109
   handleDeepLink(params = {}) {
105 110
     if (!params.link) return;
106 111
     const event = {
@@ -129,7 +134,7 @@ class Navigator {
129 134
 export default class Screen extends Component {
130 135
   static navigatorStyle = {};
131 136
   static navigatorButtons = {};
132
-  
137
+
133 138
   constructor(props) {
134 139
     super(props);
135 140
     if (props.navigatorID) {

+ 6
- 0
src/platformSpecific.android.js Voir le fichier

@@ -197,6 +197,11 @@ function setupDrawer(drawerParams) {
197 197
   return drawer;
198 198
 }
199 199
 
200
+function showFAB(params) {
201
+  params.icon = resolveAssetSource(params.icon).uri;
202
+  RctActivity.showFAB(params);
203
+}
204
+
200 205
 export default {
201 206
   startTabBasedApp,
202 207
   startSingleScreenApp,
@@ -207,6 +212,7 @@ export default {
207 212
   showModal,
208 213
   dismissModal,
209 214
   dismissAllModals,
215
+  showFAB,
210 216
   navigatorSetButtons,
211 217
   navigatorSetTabBadge,
212 218
   navigatorSetTitle,