Selaa lähdekoodia

Refactor orientation logic to separate class (#811)

closes #661
Guy Carmeli 7 vuotta sitten
vanhempi
commit
08dbe4dc35

+ 1
- 21
android/app/src/main/java/com/reactnativenavigation/bridge/NavigationReactModule.java Näytä tiedosto

@@ -1,8 +1,5 @@
1 1
 package com.reactnativenavigation.bridge;
2 2
 
3
-import android.app.Activity;
4
-import android.content.res.Configuration;
5
-
6 3
 import com.facebook.react.bridge.Callback;
7 4
 import com.facebook.react.bridge.Promise;
8 5
 import com.facebook.react.bridge.ReactApplicationContext;
@@ -227,23 +224,6 @@ public class NavigationReactModule extends ReactContextBaseJavaModule {
227 224
 
228 225
     @ReactMethod
229 226
     public void getOrientation(Promise promise) {
230
-        Activity activity = getCurrentActivity();
231
-        if (activity != null) {
232
-            int orientation = getCurrentActivity().getResources().getConfiguration().orientation;
233
-            switch (orientation) {
234
-                case Configuration.ORIENTATION_PORTRAIT:
235
-                    promise.resolve("PORTRAIT");
236
-                    return;
237
-                case Configuration.ORIENTATION_LANDSCAPE:
238
-                    promise.resolve("LANDSCAPE");
239
-                    return;
240
-                case Configuration.ORIENTATION_UNDEFINED:
241
-                    promise.resolve("UNDEFINED");
242
-                    return;
243
-            }
244
-        } else {
245
-            promise.resolve("UNDEFINED");
246
-        }
227
+        NavigationCommandsHandler.getOrientation(promise);
247 228
     }
248
-
249 229
 }

+ 2
- 24
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java Näytä tiedosto

@@ -9,22 +9,15 @@ import android.support.annotation.Nullable;
9 9
 import android.support.v7.app.AppCompatActivity;
10 10
 import android.view.KeyEvent;
11 11
 
12
-import com.facebook.react.bridge.Arguments;
13 12
 import com.facebook.react.bridge.Callback;
14
-import com.facebook.react.bridge.ReactContext;
15
-import com.facebook.react.bridge.WritableMap;
16 13
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
17
-import com.facebook.react.modules.core.DeviceEventManagerModule;
18 14
 import com.facebook.react.modules.core.PermissionAwareActivity;
19 15
 import com.facebook.react.modules.core.PermissionListener;
20 16
 import com.reactnativenavigation.NavigationApplication;
21
-import com.reactnativenavigation.bridge.EventEmitter;
22
-import com.reactnativenavigation.bridge.NavigationReactEventEmitter;
23 17
 import com.reactnativenavigation.events.Event;
24 18
 import com.reactnativenavigation.events.EventBus;
25 19
 import com.reactnativenavigation.events.JsDevReloadEvent;
26 20
 import com.reactnativenavigation.events.ModalDismissedEvent;
27
-import com.reactnativenavigation.events.OrientationChangedEvent;
28 21
 import com.reactnativenavigation.events.Subscriber;
29 22
 import com.reactnativenavigation.layouts.BottomTabsLayout;
30 23
 import com.reactnativenavigation.layouts.Layout;
@@ -39,6 +32,7 @@ import com.reactnativenavigation.params.SnackbarParams;
39 32
 import com.reactnativenavigation.params.TitleBarButtonParams;
40 33
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
41 34
 import com.reactnativenavigation.react.ReactGateway;
35
+import com.reactnativenavigation.utils.Orientation;
42 36
 import com.reactnativenavigation.views.SideMenu.Side;
43 37
 
44 38
 import java.util.List;
@@ -197,23 +191,7 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
197 191
 
198 192
     @Override
199 193
     public void onConfigurationChanged(Configuration newConfig) {
200
-        EventEmitter eventEmitter = NavigationApplication.instance.getEventEmitter();
201
-        OrientationChangedEvent event = new OrientationChangedEvent();
202
-        WritableMap params = Arguments.createMap();
203
-
204
-        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
205
-            params.putString("orientation", "LANDSCAPE");
206
-        }
207
-
208
-        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
209
-            params.putString("orientation", "PORTRAIT");
210
-        }
211
-
212
-        if (newConfig.orientation == Configuration.ORIENTATION_UNDEFINED) {
213
-            params.putString("orientation", "UNDEFINED");
214
-        }
215
-
216
-        eventEmitter.sendNavigatorEvent(event.getType(), params);
194
+        Orientation.onConfigurationChanged(newConfig);
217 195
         NavigationApplication.instance.getActivityCallbacks().onConfigurationChanged(newConfig);
218 196
         super.onConfigurationChanged(newConfig);
219 197
     }

+ 10
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationCommandsHandler.java Näytä tiedosto

@@ -4,6 +4,7 @@ import android.content.Intent;
4 4
 import android.os.Bundle;
5 5
 
6 6
 import com.facebook.react.bridge.Callback;
7
+import com.facebook.react.bridge.Promise;
7 8
 import com.reactnativenavigation.NavigationApplication;
8 9
 import com.reactnativenavigation.params.ActivityParams;
9 10
 import com.reactnativenavigation.params.ContextualMenuParams;
@@ -15,6 +16,7 @@ import com.reactnativenavigation.params.TitleBarButtonParams;
15 16
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
16 17
 import com.reactnativenavigation.params.parsers.ActivityParamsParser;
17 18
 import com.reactnativenavigation.params.parsers.ScreenParamsParser;
19
+import com.reactnativenavigation.utils.Orientation;
18 20
 import com.reactnativenavigation.views.SideMenu.Side;
19 21
 
20 22
 import java.util.List;
@@ -420,4 +422,12 @@ public class NavigationCommandsHandler {
420 422
             }
421 423
         });
422 424
     }
425
+
426
+    public static void getOrientation(Promise promise) {
427
+        final NavigationActivity currentActivity = NavigationActivity.currentActivity;
428
+        if (currentActivity == null) {
429
+            return;
430
+        }
431
+        promise.resolve(Orientation.get(currentActivity));
432
+    }
423 433
 }

+ 0
- 9
android/app/src/main/java/com/reactnativenavigation/events/OrientationChangedEvent.java Näytä tiedosto

@@ -1,9 +0,0 @@
1
-package com.reactnativenavigation.events;
2
-
3
-public class OrientationChangedEvent implements Event {
4
-    public static final String TYPE = "OrientationChanged";
5
-    @Override
6
-    public String getType() {
7
-        return TYPE;
8
-    }
9
-}

+ 31
- 0
android/app/src/main/java/com/reactnativenavigation/utils/Orientation.java Näytä tiedosto

@@ -0,0 +1,31 @@
1
+package com.reactnativenavigation.utils;
2
+
3
+import android.content.res.Configuration;
4
+
5
+import com.facebook.react.bridge.Arguments;
6
+import com.facebook.react.bridge.WritableMap;
7
+import com.reactnativenavigation.NavigationApplication;
8
+import com.reactnativenavigation.controllers.NavigationActivity;
9
+
10
+public class Orientation {
11
+    public static String get(NavigationActivity currentActivity) {
12
+        return getOrientation(currentActivity.getResources().getConfiguration().orientation);
13
+    }
14
+
15
+    public static void onConfigurationChanged(Configuration newConfig) {
16
+        WritableMap params = Arguments.createMap();
17
+        params.putString("orientation", getOrientation(newConfig.orientation));
18
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("orientationChanged", params);
19
+    }
20
+
21
+    private static String getOrientation(int orientation) {
22
+        switch (orientation) {
23
+            case Configuration.ORIENTATION_PORTRAIT:
24
+                return "PORTRAIT";
25
+            case Configuration.ORIENTATION_LANDSCAPE:
26
+                return "LANDSCAPE";
27
+            default:
28
+                return "UNDEFINED";
29
+        }
30
+    }
31
+}