Browse Source

Enable reloading from cmd (#775)

Guy Carmeli 8 years ago
parent
commit
791011e0d7

+ 12
- 8
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java View File

@@ -31,7 +31,7 @@ import com.reactnativenavigation.params.SlidingOverlayParams;
31 31
 import com.reactnativenavigation.params.SnackbarParams;
32 32
 import com.reactnativenavigation.params.TitleBarButtonParams;
33 33
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
34
-import com.reactnativenavigation.react.JsDevReloadHandler;
34
+import com.reactnativenavigation.react.ReactGateway;
35 35
 import com.reactnativenavigation.views.SideMenu.Side;
36 36
 
37 37
 import java.util.List;
@@ -108,7 +108,7 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
108 108
 
109 109
         currentActivity = this;
110 110
         IntentDataHandler.onResume(getIntent());
111
-        NavigationApplication.instance.getReactGateway().onResumeActivity(this, this);
111
+        getReactGateway().onResumeActivity(this, this);
112 112
         NavigationApplication.instance.getActivityCallbacks().onActivityResumed(this);
113 113
         EventBus.instance.register(this);
114 114
         IntentDataHandler.onPostResume(getIntent());
@@ -117,7 +117,7 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
117 117
     @Override
118 118
     protected void onNewIntent(Intent intent) {
119 119
         super.onNewIntent(intent);
120
-        NavigationApplication.instance.getReactGateway().onNewIntent(intent);
120
+        getReactGateway().onNewIntent(intent);
121 121
         NavigationApplication.instance.getActivityCallbacks().onNewIntent(intent);
122 122
     }
123 123
 
@@ -132,7 +132,7 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
132 132
         super.onPause();
133 133
         currentActivity = null;
134 134
         IntentDataHandler.onPause(getIntent());
135
-        NavigationApplication.instance.getReactGateway().onPauseActivity();
135
+        getReactGateway().onPauseActivity();
136 136
         NavigationApplication.instance.getActivityCallbacks().onActivityPaused(this);
137 137
         EventBus.instance.unregister(this);
138 138
     }
@@ -163,7 +163,7 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
163 163
 
164 164
     private void destroyJsIfNeeded() {
165 165
         if (currentActivity == null || currentActivity.isFinishing()) {
166
-            NavigationApplication.instance.getReactGateway().onDestroyApp();
166
+            getReactGateway().onDestroyApp();
167 167
         }
168 168
     }
169 169
 
@@ -175,19 +175,23 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
175 175
     @Override
176 176
     public void onBackPressed() {
177 177
         if (layout != null && !layout.onBackPressed()) {
178
-            NavigationApplication.instance.getReactGateway().onBackPressed();
178
+            getReactGateway().onBackPressed();
179 179
         }
180 180
     }
181 181
 
182 182
     @Override
183 183
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
184
-        NavigationApplication.instance.getReactGateway().onActivityResult(requestCode, resultCode, data);
184
+        getReactGateway().onActivityResult(requestCode, resultCode, data);
185 185
         NavigationApplication.instance.getActivityCallbacks().onActivityResult(requestCode, resultCode, data);
186 186
     }
187 187
 
188 188
     @Override
189 189
     public boolean onKeyUp(int keyCode, KeyEvent event) {
190
-        return JsDevReloadHandler.onKeyUp(getCurrentFocus(), keyCode) || super.onKeyUp(keyCode, event);
190
+        return getReactGateway().onKeyUp(getCurrentFocus(), keyCode) || super.onKeyUp(keyCode, event);
191
+    }
192
+
193
+    private ReactGateway getReactGateway() {
194
+        return NavigationApplication.instance.getReactGateway();
191 195
     }
192 196
 
193 197
     void push(ScreenParams params) {

+ 38
- 9
android/app/src/main/java/com/reactnativenavigation/react/JsDevReloadHandler.java View File

@@ -1,5 +1,9 @@
1 1
 package com.reactnativenavigation.react;
2 2
 
3
+import android.content.BroadcastReceiver;
4
+import android.content.Context;
5
+import android.content.Intent;
6
+import android.content.IntentFilter;
3 7
 import android.view.KeyEvent;
4 8
 import android.view.View;
5 9
 import android.widget.EditText;
@@ -7,16 +11,30 @@ import android.widget.EditText;
7 11
 import com.facebook.react.ReactInstanceManager;
8 12
 import com.reactnativenavigation.NavigationApplication;
9 13
 
10
-public class JsDevReloadHandler {
14
+class JsDevReloadHandler {
11 15
 
12 16
     private static boolean shouldRefreshOnRR = false;
17
+    private final BroadcastReceiver reloadReceiver = new BroadcastReceiver() {
18
+        @Override
19
+        public void onReceive(Context context, Intent intent) {
20
+            reload();
21
+        }
22
+    };
13 23
 
14
-    //TODO yuck.
15
-    public static boolean onKeyUp(View currentFocus, int keyCode) {
16
-        ReactInstanceManager reactInstanceManager = NavigationApplication
17
-                .instance
18
-                .getReactGateway()
19
-                .getReactInstanceManager();
24
+    void onResumeActivity() {
25
+        if (getReactInstanceManager().getDevSupportManager().getDevSupportEnabled()) {
26
+            NavigationApplication.instance.registerReceiver(reloadReceiver, new IntentFilter("react.native.RELOAD"));
27
+        }
28
+    }
29
+
30
+    void onPauseActivity() {
31
+        if (getReactInstanceManager().getDevSupportManager().getDevSupportEnabled()) {
32
+            NavigationApplication.instance.unregisterReceiver(reloadReceiver);
33
+        }
34
+    }
35
+
36
+    boolean onKeyUp(View currentFocus, int keyCode) {
37
+        ReactInstanceManager reactInstanceManager = getReactInstanceManager();
20 38
 
21 39
         if (reactInstanceManager != null &&
22 40
                 reactInstanceManager.getDevSupportManager().getDevSupportEnabled()) {
@@ -27,8 +45,7 @@ public class JsDevReloadHandler {
27 45
             if (keyCode == KeyEvent.KEYCODE_R && !(currentFocus instanceof EditText)) {
28 46
                 // Enable double-tap-R-to-reload
29 47
                 if (shouldRefreshOnRR) {
30
-                    reactInstanceManager.getDevSupportManager().handleReloadJS();
31
-                    shouldRefreshOnRR = false;
48
+                    reload();
32 49
                     return true;
33 50
                 } else {
34 51
                     shouldRefreshOnRR = true;
@@ -45,4 +62,16 @@ public class JsDevReloadHandler {
45 62
         }
46 63
         return false;
47 64
     }
65
+
66
+    private void reload() {
67
+        getReactInstanceManager().getDevSupportManager().handleReloadJS();
68
+        shouldRefreshOnRR = false;
69
+    }
70
+
71
+    private ReactInstanceManager getReactInstanceManager() {
72
+        return NavigationApplication
73
+                .instance
74
+                .getReactGateway()
75
+                .getReactInstanceManager();
76
+    }
48 77
 }

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

@@ -2,6 +2,7 @@ package com.reactnativenavigation.react;
2 2
 
3 3
 import android.app.Activity;
4 4
 import android.content.Intent;
5
+import android.view.View;
5 6
 
6 7
 import com.facebook.react.ReactInstanceManager;
7 8
 import com.facebook.react.ReactNativeHost;
@@ -20,13 +21,17 @@ import java.util.List;
20 21
 
21 22
 import javax.annotation.Nullable;
22 23
 
24
+//import android.view.View;
25
+
23 26
 public class NavigationReactGateway implements ReactGateway {
24 27
 
25 28
     private final ReactNativeHost host;
26 29
     private NavigationReactEventEmitter reactEventEmitter;
30
+    private JsDevReloadHandler jsDevReloadHandler;
27 31
 
28 32
     public NavigationReactGateway() {
29 33
         host = new ReactNativeHostImpl();
34
+        jsDevReloadHandler = new JsDevReloadHandler();
30 35
     }
31 36
 
32 37
     @Override
@@ -67,14 +72,21 @@ public class NavigationReactGateway implements ReactGateway {
67 72
 
68 73
     public void onPauseActivity() {
69 74
         getReactInstanceManager().onHostPause();
75
+        jsDevReloadHandler.onPauseActivity();
70 76
     }
71 77
 
72 78
     public void onNewIntent(Intent intent) {
73 79
         getReactInstanceManager().onNewIntent(intent);
74 80
     }
75 81
 
82
+    @Override
83
+    public boolean onKeyUp(View currentFocus, int keyCode) {
84
+        return jsDevReloadHandler.onKeyUp(currentFocus, keyCode);
85
+    }
86
+
76 87
     public void onResumeActivity(Activity activity, DefaultHardwareBackBtnHandler defaultHardwareBackBtnHandler) {
77 88
         getReactInstanceManager().onHostResume(activity, defaultHardwareBackBtnHandler);
89
+        jsDevReloadHandler.onResumeActivity();
78 90
     }
79 91
 
80 92
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
@@ -87,13 +99,13 @@ public class NavigationReactGateway implements ReactGateway {
87 99
     }
88 100
 
89 101
     //TODO temp hack
90
-    void onReactContextInitialized() {
102
+    private void onReactContextInitialized() {
91 103
         reactEventEmitter = new NavigationReactEventEmitter(getReactContext());
92 104
     }
93 105
 
94 106
     private static class ReactNativeHostImpl extends ReactNativeHost implements ReactInstanceManager.ReactInstanceEventListener {
95 107
 
96
-        public ReactNativeHostImpl() {
108
+        ReactNativeHostImpl() {
97 109
             super(NavigationApplication.instance);
98 110
         }
99 111
 

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

@@ -2,6 +2,7 @@ package com.reactnativenavigation.react;
2 2
 
3 3
 import android.app.Activity;
4 4
 import android.content.Intent;
5
+import android.view.View;
5 6
 
6 7
 import com.facebook.react.ReactInstanceManager;
7 8
 import com.facebook.react.bridge.ReactContext;
@@ -33,4 +34,6 @@ public interface ReactGateway {
33 34
     boolean hasStartedCreatingContext();
34 35
 
35 36
     void onNewIntent(Intent intent);
37
+
38
+    boolean onKeyUp(View currentFocus, int keyCode);
36 39
 }