diff --git a/modules/react-native-status/android/build.gradle b/modules/react-native-status/android/build.gradle index d0c201b23d..7c001b28e1 100644 --- a/modules/react-native-status/android/build.gradle +++ b/modules/react-native-status/android/build.gradle @@ -17,7 +17,7 @@ dependencies { implementation 'com.github.ericwlange:AndroidJSCore:3.0.1' implementation 'status-im:function:0.0.1' - String statusGoVersion = 'develop-g228bda9f' + String statusGoVersion = 'develop-gabb5df88' final String statusGoGroup = 'status-im', statusGoName = 'status-go' // Check if the local status-go jar exists, and compile against that if it does diff --git a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java index 94b2845a2a..f504d2900a 100644 --- a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java +++ b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java @@ -699,4 +699,10 @@ class StatusModule extends ReactContextBaseJavaModule implements LifecycleEventL public void closeApplication() { System.exit(0); } + + @ReactMethod + public void connectionChange(final String type, final boolean isExpensive) { + Log.d(TAG, "ConnectionChange: " + type + ", is expensive " + isExpensive); + Statusgo.ConnectionChange(type, isExpensive ? 1 : 0); + } } diff --git a/modules/react-native-status/ios/RCTStatus/RCTStatus.m b/modules/react-native-status/ios/RCTStatus/RCTStatus.m index cc91bde073..1c3e1752ff 100644 --- a/modules/react-native-status/ios/RCTStatus/RCTStatus.m +++ b/modules/react-native-status/ios/RCTStatus/RCTStatus.m @@ -403,6 +403,15 @@ RCT_EXPORT_METHOD(closeApplication) { exit(0); } + +RCT_EXPORT_METHOD(connectionChange:(NSString *)type + isExpensive:(BOOL)isExpensive) { +#if DEBUG + NSLog(@"ConnectionChange() method called"); +#endif + ConnectionChange((char *) [type UTF8String], isExpensive? 1 : 0); +} + + (void)signalEvent:(const char *) signal { if(!signal){ diff --git a/modules/react-native-status/ios/RCTStatus/pom.xml b/modules/react-native-status/ios/RCTStatus/pom.xml index 762836723d..d902fd8e30 100644 --- a/modules/react-native-status/ios/RCTStatus/pom.xml +++ b/modules/react-native-status/ios/RCTStatus/pom.xml @@ -25,7 +25,7 @@ status-im status-go-ios-simulator - develop-g228bda9f + develop-gabb5df88 zip true ./ diff --git a/src/status_im/native_module/core.cljs b/src/status_im/native_module/core.cljs index 22a8098254..5a094df216 100644 --- a/src/status_im/native_module/core.cljs +++ b/src/status_im/native_module/core.cljs @@ -92,3 +92,6 @@ (defn close-application [] (module-interface/-close-application rns-module)) + +(defn connection-change [data] + (module-interface/-connection-change rns-module data)) diff --git a/src/status_im/native_module/impl/module.cljs b/src/status_im/native_module/impl/module.cljs index b773c86271..eba732773f 100644 --- a/src/status_im/native_module/impl/module.cljs +++ b/src/status_im/native_module/impl/module.cljs @@ -232,6 +232,9 @@ (defn close-application [] (.closeApplication status)) +(defn connection-change [{:keys [type expensive?]}] + (.connectionChange status type expensive?)) + (defrecord ReactNativeStatus [] module/IReactNativeStatus ;; status-go calls @@ -276,4 +279,6 @@ (-should-move-to-internal-storage? [this callback] (should-move-to-internal-storage? callback)) (-close-application [this] - (close-application))) + (close-application)) + (-connection-change [this data] + (connection-change data))) diff --git a/src/status_im/native_module/impl/non_status_go_module.cljs b/src/status_im/native_module/impl/non_status_go_module.cljs index 3622876450..8e2ae378ed 100644 --- a/src/status_im/native_module/impl/non_status_go_module.cljs +++ b/src/status_im/native_module/impl/non_status_go_module.cljs @@ -60,4 +60,5 @@ (impl/should-move-to-internal-storage? callback)) (-notify-users [this {:keys [message payload tokens] :as m} callback]) (-add-peer [this enode callback]) - (-close-application [this])) + (-close-application [this]) + (-connection-change [this data])) diff --git a/src/status_im/native_module/module.cljs b/src/status_im/native_module/module.cljs index a121fab123..a8bd8197c1 100644 --- a/src/status_im/native_module/module.cljs +++ b/src/status_im/native_module/module.cljs @@ -20,5 +20,6 @@ (-should-move-to-internal-storage? [this callback]) (-notify-users [this {:keys [message payload tokens] :as m} callback]) (-add-peer [this enode callback]) - (-close-application [this])) + (-close-application [this]) + (-connection-change [this data])) diff --git a/src/status_im/network/events.cljs b/src/status_im/network/events.cljs new file mode 100644 index 0000000000..a9a1d6c4ec --- /dev/null +++ b/src/status_im/network/events.cljs @@ -0,0 +1,36 @@ +(ns status-im.network.events + (:require [re-frame.core :as re-frame] + [status-im.utils.handlers :as handlers] + [status-im.network.net-info :as net-info] + [status-im.native-module.core :as status])) + +(re-frame/reg-fx + ::listen-to-network-status + (fn [[connection-listener net-info-listener]] + (net-info/is-connected? connection-listener) + (net-info/net-info net-info-listener) + (net-info/add-connection-listener connection-listener) + (net-info/add-net-info-listener net-info-listener))) + +(re-frame/reg-fx + ::notify-status-go + (fn [data] + (status/connection-change data))) + +(handlers/register-handler-fx + :listen-to-network-status + (fn [] + {::listen-to-network-status [#(re-frame/dispatch [::update-connection-status %]) + #(re-frame/dispatch [::update-network-status %])]})) + +(handlers/register-handler-db + ::update-connection-status + [re-frame/trim-v] + (fn [db [is-connected?]] + (assoc db :network-status (if is-connected? :online :offline)))) + +(handlers/register-handler-fx + ::update-network-status + [re-frame/trim-v] + (fn [_ [data]] + {::notify-status-go data})) diff --git a/src/status_im/network/handlers.cljs b/src/status_im/network/handlers.cljs deleted file mode 100644 index fd1f295ac4..0000000000 --- a/src/status_im/network/handlers.cljs +++ /dev/null @@ -1,17 +0,0 @@ -(ns status-im.network.handlers - (:require [re-frame.core :refer [dispatch debug enrich after]] - [status-im.utils.handlers :refer [register-handler]] - [status-im.utils.handlers :as u] - [status-im.network.net-info :as ni])) - -(register-handler :listen-to-network-status! - (u/side-effect! - (fn [] - (let [handler #(dispatch [:update-network-status %])] - (ni/init handler) - (ni/add-listener handler))))) - -(register-handler :update-network-status - (fn [db [_ is-connected?]] - (let [status (if is-connected? :online :offline)] - (assoc db :network-status status)))) diff --git a/src/status_im/network/net_info.cljs b/src/status_im/network/net_info.cljs index 4a56946ae7..9aa7bb85bc 100644 --- a/src/status_im/network/net_info.cljs +++ b/src/status_im/network/net_info.cljs @@ -2,13 +2,30 @@ (:require [taoensso.timbre :as log] [status-im.ui.components.react :as react-components])) -(defn init [callback] +(defn is-connected? [callback] (when react-components/net-info (.then (.fetch (.-isConnected react-components/net-info)) (fn [is-connected?] (log/debug "Is connected?" is-connected?) (callback is-connected?))))) -(defn add-listener [listener] +(defn- wrap-net-info [callback] + (fn [info-js] + (let [info (js->clj info-js :keywordize-keys true)] + (.then (.isConnectionExpensive react-components/net-info) + (fn [expensive?] + (callback {:type (:type info) :expensive? expensive?})))))) + +(defn net-info [callback] + (when react-components/net-info + (.then (.getConnectionInfo react-components/net-info) + (wrap-net-info callback)))) + +(defn add-connection-listener [listener] (when react-components/net-info (.addEventListener (.-isConnected react-components/net-info) "connectionChange" listener))) + +(defn add-net-info-listener [listener] + (when react-components/net-info + (.addEventListener react-components/net-info "connectionChange" + (wrap-net-info listener)))) diff --git a/src/status_im/ui/screens/events.cljs b/src/status_im/ui/screens/events.cljs index 94625095a4..540b880f4d 100644 --- a/src/status_im/ui/screens/events.cljs +++ b/src/status_im/ui/screens/events.cljs @@ -4,7 +4,7 @@ status-im.commands.handlers.jail status-im.commands.events.loading status-im.commands.handlers.debug - status-im.network.handlers + status-im.network.events status-im.protocol.handlers status-im.ui.screens.accounts.events status-im.ui.screens.accounts.login.events @@ -226,7 +226,7 @@ :dispatch-n [[:initialize-db] [:load-accounts] [:check-console-chat] - [:listen-to-network-status!] + [:listen-to-network-status] [:initialize-crypt] [:initialize-geth]]}))