Browse Source

refactored

Daniel Zlotin 8 years ago
parent
commit
0268c95fab

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

@@ -69,4 +69,7 @@ public abstract class NavigationApplication extends Application {
69 69
     @NonNull
70 70
     public abstract List<ReactPackage> createAdditionalReactPackages();
71 71
 
72
+    public void sendEvent(String eventId, String navigatorEventId) {
73
+        navigationReactInstance.getReactEventEmitter().sendEvent(eventId, navigatorEventId);
74
+    }
72 75
 }

+ 5
- 2
android/app/src/main/java/com/reactnativenavigation/layouts/SingleScreenLayout.java View File

@@ -5,6 +5,8 @@ import android.widget.FrameLayout;
5 5
 
6 6
 import com.reactnativenavigation.params.ScreenParams;
7 7
 
8
+import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
9
+
8 10
 public class SingleScreenLayout extends FrameLayout implements Layout {
9 11
 
10 12
     private final ScreenParams screenParams;
@@ -13,11 +15,12 @@ public class SingleScreenLayout extends FrameLayout implements Layout {
13 15
     public SingleScreenLayout(Context context, ScreenParams screenParams) {
14 16
         super(context);
15 17
         this.screenParams = screenParams;
18
+        createLayout();
16 19
     }
17 20
 
18
-    public void createLayout() {
21
+    private void createLayout() {
19 22
         screenLayout = new ScreenLayout(getContext(), screenParams);
20
-        addView(screenLayout);
23
+        addView(screenLayout, new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
21 24
     }
22 25
 
23 26
     @Override

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

@@ -8,4 +8,16 @@ public class Parser {
8 8
         return bundle.keySet().contains(key);
9 9
     }
10 10
 
11
+
12
+    protected static void assertKeyExists(Bundle bundle, String key) {
13
+        if (!hasKey(bundle, key)) {
14
+            throw new KeyDoesNotExistsException(key);
15
+        }
16
+    }
17
+
18
+    public static class KeyDoesNotExistsException extends RuntimeException {
19
+        public KeyDoesNotExistsException(String key) {
20
+            super(key);
21
+        }
22
+    }
11 23
 }

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

@@ -4,7 +4,7 @@ import android.os.Bundle;
4 4
 
5 5
 import com.reactnativenavigation.params.ScreenParams;
6 6
 
7
-public class ScreenParamsParser {
7
+public class ScreenParamsParser extends Parser {
8 8
     private static final String KEY_TITLE = "title";
9 9
     private static final String KEY_SCREEN_ID = "screenId";
10 10
     private static final String KEY_SCREEN_INSTANCE_ID = "screenInstanceID";
@@ -14,13 +14,14 @@ public class ScreenParamsParser {
14 14
     private static final String KEY_BUTTONS = "titleBarButtons";
15 15
     private static final String STYLE_PARAMS = "styleParams";
16 16
 
17
+    @SuppressWarnings("ConstantConditions")
17 18
     public static ScreenParams parse(Bundle params) {
18 19
         ScreenParams result = new ScreenParams();
19 20
         result.screenId = params.getString(KEY_SCREEN_ID);
20 21
         result.screenInstanceId = params.getString(KEY_SCREEN_INSTANCE_ID);
21 22
         result.passProps = params.getBundle(KEY_PROPS);
23
+        assertKeyExists(params, KEY_NAVIGATION_PARAMS);
22 24
         result.navigationParams = params.getBundle(KEY_NAVIGATION_PARAMS);
23
-        assert result.navigationParams != null;
24 25
         result.navigatorEventId = result.navigationParams.getString(KEY_NAVIGATOR_EVENT_ID);
25 26
         result.buttons = TitleBarButtonParamsParser.parse(params.getBundle(KEY_BUTTONS));
26 27
         result.title = params.getString(KEY_TITLE);

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/react/JsDevImageLoader.java View File

@@ -8,7 +8,7 @@ import android.os.StrictMode;
8 8
 import android.support.annotation.NonNull;
9 9
 
10 10
 import com.reactnativenavigation.NavigationApplication;
11
-import com.reactnativenavigation.utils.ImageUtils;
11
+import com.reactnativenavigation.utils.ViewUtils;
12 12
 
13 13
 import java.io.IOException;
14 14
 import java.net.URL;
@@ -33,7 +33,7 @@ public class JsDevImageLoader {
33 33
     private static Drawable tryLoadIcon(String iconDevUri) throws IOException {
34 34
         URL url = new URL(iconDevUri);
35 35
         Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
36
-        final int dimensions = (int) ImageUtils.convertDpToPixel(48);
36
+        final int dimensions = (int) ViewUtils.convertDpToPixel(48);
37 37
         // TODO: fix hard coded dimensions -add options to decodeStream
38 38
         bitmap = Bitmap.createScaledBitmap(bitmap, dimensions, dimensions, false);
39 39
         return new BitmapDrawable(NavigationApplication.instance.getResources(), bitmap);

+ 1
- 4
android/app/src/main/java/com/reactnativenavigation/react/NavigationReactInstance.java View File

@@ -48,6 +48,7 @@ public class NavigationReactInstance {
48 48
     public void onResume(NavigationActivity activity, DefaultHardwareBackBtnHandler defaultHardwareBackBtnHandler, OnJsDevReloadListener onJsDevReloadListener) {
49 49
         this.onJsDevReloadListener = onJsDevReloadListener;
50 50
         reactInstanceManager.onHostResume(activity, defaultHardwareBackBtnHandler);
51
+        reactEventEmitter = new NavigationReactEventEmitter(reactInstanceManager.getCurrentReactContext());
51 52
     }
52 53
 
53 54
     public void onPause() {
@@ -86,10 +87,6 @@ public class NavigationReactInstance {
86 87
     }
87 88
 
88 89
     public NavigationReactEventEmitter getReactEventEmitter() {
89
-        if (reactEventEmitter == null) {
90
-            reactEventEmitter = new NavigationReactEventEmitter(reactInstanceManager.getCurrentReactContext());
91
-        }
92
-
93 90
         return reactEventEmitter;
94 91
     }
95 92
 }

+ 0
- 121
android/app/src/main/java/com/reactnativenavigation/utils/BridgeUtils.java View File

@@ -1,121 +0,0 @@
1
-package com.reactnativenavigation.utils;
2
-
3
-import android.os.Bundle;
4
-import android.util.Log;
5
-
6
-import java.util.ArrayList;
7
-import java.util.HashMap;
8
-
9
-/**
10
- * Created by yedidyak on 26/05/2016.
11
- */
12
-public class BridgeUtils {
13
-
14
-    @SuppressWarnings("unchecked")
15
-    public static Bundle addMapToBundle(HashMap<String, ?> map, Bundle bundle) {
16
-        for (String key : map.keySet()) {
17
-            Object value = map.get(key);
18
-            if (value instanceof String) {
19
-                bundle.putString(key, (String) value);
20
-            } else if (value instanceof Integer) {
21
-                bundle.putInt(key, (Integer) value);
22
-            } else if (value instanceof Double) {
23
-                bundle.putDouble(key, ((Double) value));
24
-            } else if (value instanceof Boolean) {
25
-                bundle.putBoolean(key, (Boolean) value);
26
-            } else if (value instanceof HashMap) {
27
-                bundle.putBundle(key, addMapToBundle((HashMap<String, Object>) value, new Bundle()));
28
-            } else if (value instanceof ArrayList) {
29
-                putArray(key, (ArrayList) value, bundle);
30
-            }
31
-        }
32
-        return bundle;
33
-    }
34
-
35
-    @SuppressWarnings("unchecked")
36
-    private static void putArray(String key, ArrayList arrayList, Bundle bundle) {
37
-        if (arrayList.size() == 0) {
38
-            bundle.putBooleanArray(key, new boolean[]{});
39
-        } else {
40
-            verifyArrayListIsSingleType(arrayList);
41
-            if (arrayList.get(0) instanceof String) {
42
-                bundle.putStringArray(key, toStringArray((ArrayList<String>) arrayList));
43
-            } else if (arrayList.get(0) instanceof Integer) {
44
-                bundle.putIntArray(key, toIntArray((ArrayList<Integer>) arrayList));
45
-            } else if (arrayList.get(0) instanceof Float) {
46
-                bundle.putFloatArray(key, toFloatArray((ArrayList<Float>) arrayList));
47
-            } else if (arrayList.get(0) instanceof Double) {
48
-                bundle.putDoubleArray(key, toDoubleArray((ArrayList<Double>) arrayList));
49
-            } else if (arrayList.get(0) instanceof Boolean) {
50
-                bundle.putBooleanArray(key, toBooleanArray((ArrayList<Boolean>) arrayList));
51
-            } else if (arrayList.get(0) instanceof HashMap) {
52
-                bundle.putParcelableArray(key, toBundleArray((ArrayList<HashMap>) arrayList));
53
-            } else if (arrayList.get(0) instanceof ArrayList) {
54
-                Log.w("RNNavigation", "Arrays of arrays passed in props are converted to dictionaries with indexes as keys");
55
-                Bundle innerArray = new Bundle();
56
-                for (int i = 0; i < arrayList.size(); i++) {
57
-                    putArray(String.valueOf(i), (ArrayList) arrayList.get(i), innerArray);
58
-                }
59
-                bundle.putParcelable(key, innerArray);
60
-            }
61
-        }
62
-    }
63
-
64
-    private static void verifyArrayListIsSingleType(ArrayList arrayList) {
65
-        for (int i = 1; i < arrayList.size(); i++) {
66
-            if (!arrayList.get(i - 1).getClass().isInstance(arrayList.get(i))) {
67
-                throw new IllegalArgumentException("Cannot pass array of multiple types via props");
68
-            }
69
-        }
70
-    }
71
-
72
-    @SuppressWarnings("unchecked")
73
-    private static Bundle[] toBundleArray(ArrayList<HashMap> arrayList) {
74
-        Bundle[] ret = new Bundle[arrayList.size()];
75
-        for (int i=0; i < ret.length; i++) {
76
-            ret[i] = addMapToBundle(arrayList.get(i), new Bundle());
77
-        }
78
-        return ret;
79
-    }
80
-
81
-    private static int[] toIntArray(ArrayList<Integer> arrayList) {
82
-        int[] ret = new int[arrayList.size()];
83
-        for (int i=0; i < ret.length; i++) {
84
-            ret[i] = arrayList.get(i);
85
-        }
86
-        return ret;
87
-    }
88
-
89
-    private static float[] toFloatArray(ArrayList<Float> arrayList) {
90
-        float[] ret = new float[arrayList.size()];
91
-        for (int i=0; i < ret.length; i++) {
92
-            ret[i] = arrayList.get(i);
93
-        }
94
-        return ret;
95
-    }
96
-
97
-    private static double[] toDoubleArray(ArrayList<Double> arrayList) {
98
-        double[] ret = new double[arrayList.size()];
99
-        for (int i=0; i < ret.length; i++) {
100
-            ret[i] = arrayList.get(i);
101
-        }
102
-        return ret;
103
-    }
104
-
105
-    private static boolean[] toBooleanArray(ArrayList<Boolean> arrayList) {
106
-        boolean[] ret = new boolean[arrayList.size()];
107
-        for (int i=0; i < ret.length; i++) {
108
-            ret[i] = arrayList.get(i);
109
-        }
110
-        return ret;
111
-    }
112
-
113
-    private static String[] toStringArray(ArrayList<String> arrayList) {
114
-        String[] ret = new String[arrayList.size()];
115
-        for (int i=0; i < ret.length; i++) {
116
-            ret[i] = arrayList.get(i);
117
-        }
118
-        return ret;
119
-    }
120
-
121
-}

+ 0
- 14
android/app/src/main/java/com/reactnativenavigation/utils/CollectionUtils.java View File

@@ -1,14 +0,0 @@
1
-package com.reactnativenavigation.utils;
2
-
3
-import java.util.Collection;
4
-
5
-/**
6
- * Created by guyc on 11/04/16.
7
- */
8
-public class CollectionUtils {
9
-
10
-    public static boolean isNullOrEmpty(Collection collection) {
11
-        return collection == null || collection.size() == 0;
12
-    }
13
-
14
-}

+ 0
- 19
android/app/src/main/java/com/reactnativenavigation/utils/ImageUtils.java View File

@@ -1,19 +0,0 @@
1
-package com.reactnativenavigation.utils;
2
-
3
-import android.graphics.PorterDuff;
4
-import android.graphics.PorterDuffColorFilter;
5
-import android.graphics.drawable.Drawable;
6
-
7
-import com.reactnativenavigation.NavigationApplication;
8
-
9
-public class ImageUtils {
10
-
11
-    public static void tint(Drawable drawable, int tint) {
12
-        drawable.setColorFilter(new PorterDuffColorFilter(tint, PorterDuff.Mode.SRC_IN));
13
-    }
14
-
15
-    public static float convertDpToPixel(float dp) {
16
-        float scale = NavigationApplication.instance.getResources().getDisplayMetrics().density;
17
-        return dp * scale + 0.5f;
18
-    }
19
-}

+ 0
- 20
android/app/src/main/java/com/reactnativenavigation/utils/RefUtils.java View File

@@ -1,20 +0,0 @@
1
-package com.reactnativenavigation.utils;
2
-
3
-import java.lang.ref.WeakReference;
4
-
5
-/**
6
- * Created by guyc on 06/05/16.
7
- */
8
-public class RefUtils {
9
-    /**
10
-     * Extract the object within a WeakReference object
11
-     * @param wr the WeakReference to extract
12
-     * @return the object within the WR or null when object not available.
13
-     */
14
-    public static <T> T get(WeakReference<T> wr) {
15
-        if (wr != null) {
16
-            return wr.get();
17
-        }
18
-        return null;
19
-    }
20
-}

+ 21
- 27
android/app/src/main/java/com/reactnativenavigation/utils/ReflectionUtils.java View File

@@ -1,41 +1,25 @@
1 1
 package com.reactnativenavigation.utils;
2 2
 
3
+import android.support.annotation.Nullable;
4
+
3 5
 import java.lang.reflect.Field;
4
-import java.lang.reflect.Method;
5 6
 
6
-/**
7
- * Created by guyc on 14/04/16.
8
- */
9 7
 public class ReflectionUtils {
10 8
 
11
-    public static boolean setField(Object obj, String name, Object value) {
9
+    public static void setField(Object obj, String name, Object value) {
12 10
         try {
13 11
             Field field = getField(obj.getClass(), name);
14 12
             if (field == null) {
15
-                return false;
13
+                return;
16 14
             }
17 15
             field.setAccessible(true);
18 16
             field.set(obj, value);
19
-            return true;
20 17
         } catch (Exception e) {
21 18
             e.printStackTrace();
22 19
         }
23
-        return false;
24 20
     }
25 21
 
26
-    private static Field getField(Class clazz, String name) {
27
-        try {
28
-            return clazz.getDeclaredField(name);
29
-        } catch (NoSuchFieldException nsfe) {
30
-            return getField(clazz.getSuperclass(), name);
31
-        } catch (Exception e) {
32
-            return null;
33
-        }
34
-    }
35
-
36
-    /**
37
-     * Returns the value of the field
38
-     */
22
+    @Nullable
39 23
     public static Object getDeclaredField(Object obj, String fieldName) {
40 24
         try {
41 25
             Field f = getField(obj.getClass(), fieldName);
@@ -50,14 +34,24 @@ public class ReflectionUtils {
50 34
         return null;
51 35
     }
52 36
 
53
-    public static Object invoke(Object object, String methodName) {
37
+    private static Field getField(Class clazz, String name) {
54 38
         try {
55
-            Method method = object.getClass().getDeclaredMethod(methodName);
56
-            method.setAccessible(true);
57
-            return method.invoke(object);
39
+            return clazz.getDeclaredField(name);
40
+        } catch (NoSuchFieldException nsfe) {
41
+            return getField(clazz.getSuperclass(), name);
58 42
         } catch (Exception e) {
59
-            e.printStackTrace();
43
+            return null;
60 44
         }
61
-        return null;
62 45
     }
46
+
47
+//    public static Object invoke(Object object, String methodName) {
48
+//        try {
49
+//            Method method = object.getClass().getDeclaredMethod(methodName);
50
+//            method.setAccessible(true);
51
+//            return method.invoke(object);
52
+//        } catch (Exception e) {
53
+//            e.printStackTrace();
54
+//        }
55
+//        return null;
56
+//    }
63 57
 }

+ 0
- 3
android/app/src/main/java/com/reactnativenavigation/utils/SdkSupports.java View File

@@ -2,9 +2,6 @@ package com.reactnativenavigation.utils;
2 2
 
3 3
 import android.os.Build;
4 4
 
5
-/**
6
- * Created by guyc on 23/04/16.
7
- */
8 5
 public class SdkSupports {
9 6
 
10 7
     public static boolean lollipop() {

+ 0
- 41
android/app/src/main/java/com/reactnativenavigation/utils/StyleHelper.java View File

@@ -1,41 +0,0 @@
1
-package com.reactnativenavigation.utils;
2
-
3
-/**
4
- * Created by guyc on 07/05/16.
5
- */
6
-public class StyleHelper {
7
-//
8
-//    public static void updateStyles(RnnToolBar toolBar, _Screen screen) {
9
-//        try {
10
-//            toolBar.updateAndSetButtons(screen);
11
-//            setWindowStyle(screen);
12
-//        } catch (Exception e) {
13
-//            Log.w("RNNavigation", "Tried to update styles with no screen!");
14
-//        }
15
-//    }
16
-//
17
-//    private static void setWindowStyle(_Screen screen) {
18
-//        BaseReactActivity context = ContextProvider.getActivityContext();
19
-//        if (context != null) {
20
-//            StyleHelper.setWindowStyle(context.getWindow(), context, screen);
21
-//        }
22
-//    }
23
-//
24
-//    public static void setWindowStyle(Window window, Context context, _Screen screen) {
25
-//        if (SdkSupports.lollipop()) {
26
-//            final int black = ContextCompat.getColor(context, android.R.color.black);
27
-//            if (screen.statusBarColor != null) {
28
-//                window.setStatusBarColor(screen.statusBarColor);
29
-//            } else {
30
-//                window.setStatusBarColor(black);
31
-//            }
32
-//
33
-//            if (screen.navigationBarColor != null) {
34
-//                window.setNavigationBarColor(screen.navigationBarColor);
35
-//            } else {
36
-//                window.setNavigationBarColor(black);
37
-//            }
38
-//        }
39
-//
40
-//    }
41
-}

+ 14
- 0
android/app/src/main/java/com/reactnativenavigation/utils/ViewUtils.java View File

@@ -1,8 +1,13 @@
1 1
 package com.reactnativenavigation.utils;
2 2
 
3
+import android.graphics.PorterDuff;
4
+import android.graphics.PorterDuffColorFilter;
5
+import android.graphics.drawable.Drawable;
3 6
 import android.view.View;
4 7
 import android.view.ViewTreeObserver;
5 8
 
9
+import com.reactnativenavigation.NavigationApplication;
10
+
6 11
 public class ViewUtils {
7 12
     public static void runOnPreDraw(final View view, final Runnable task) {
8 13
         view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@@ -17,5 +22,14 @@ public class ViewUtils {
17 22
             }
18 23
         });
19 24
     }
25
+
26
+    public static void tintDrawable(Drawable drawable, int tint) {
27
+        drawable.setColorFilter(new PorterDuffColorFilter(tint, PorterDuff.Mode.SRC_IN));
28
+    }
29
+
30
+    public static float convertDpToPixel(float dp) {
31
+        float scale = NavigationApplication.instance.getResources().getDisplayMetrics().density;
32
+        return dp * scale + 0.5f;
33
+    }
20 34
 }
21 35
 

+ 2
- 7
android/app/src/main/java/com/reactnativenavigation/views/TitleBarButton.java View File

@@ -8,7 +8,6 @@ import android.widget.TextView;
8 8
 
9 9
 import com.reactnativenavigation.NavigationApplication;
10 10
 import com.reactnativenavigation.params.TitleBarButtonParams;
11
-import com.reactnativenavigation.utils.ImageUtils;
12 11
 import com.reactnativenavigation.utils.ViewUtils;
13 12
 
14 13
 import java.util.ArrayList;
@@ -57,7 +56,7 @@ public class TitleBarButton implements MenuItem.OnMenuItemClickListener {
57 56
     }
58 57
 
59 58
     private void setIconColor() {
60
-        ImageUtils.tint(buttonParams.icon, buttonParams.color.getColor());
59
+        ViewUtils.tintDrawable(buttonParams.icon, buttonParams.color.getColor());
61 60
     }
62 61
 
63 62
     private void setTextColor() {
@@ -93,11 +92,7 @@ public class TitleBarButton implements MenuItem.OnMenuItemClickListener {
93 92
 
94 93
     @Override
95 94
     public boolean onMenuItemClick(MenuItem item) {
96
-        NavigationApplication.
97
-                instance.
98
-                getNavigationReactInstance().
99
-                getReactEventEmitter().
100
-                sendEvent(buttonParams.eventId, navigatorEventId);
95
+        NavigationApplication.instance.sendEvent(buttonParams.eventId, navigatorEventId);
101 96
         return true;
102 97
     }
103 98
 }