From cd41cefa9636d91a2a114d2284ee14ccbb28a837 Mon Sep 17 00:00:00 2001 From: Dmitry Novotochinov Date: Fri, 11 May 2018 14:00:02 +0300 Subject: [PATCH] Handle mailserver connection error * Do not try to reconnect when offline * If online, but mailserver is disconnected, show notification * Tap on notification starts reconnecting process Signed-off-by: Eric Dvorsak --- src/status_im/translations/en.cljs | 1 + src/status_im/transport/inbox.cljs | 33 ++++++++++++------- .../ui/components/connectivity/styles.cljs | 4 +-- .../ui/components/connectivity/view.cljs | 15 +++++---- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/status_im/translations/en.cljs b/src/status_im/translations/en.cljs index 22506731a3..a385ed3bbf 100644 --- a/src/status_im/translations/en.cljs +++ b/src/status_im/translations/en.cljs @@ -8,6 +8,7 @@ :notifications-title "Notifications and sounds" :offline "Offline" :connection-problem "Messages connection problem" + :mailserver-reconnect "Could not connect to mailserver. Tap to reconnect" :search-for "Search for..." :cancel "Cancel" :next "Next" diff --git a/src/status_im/transport/inbox.cljs b/src/status_im/transport/inbox.cljs index 17a147f0dd..ebb5abcbf9 100644 --- a/src/status_im/transport/inbox.cljs +++ b/src/status_im/transport/inbox.cljs @@ -173,25 +173,31 @@ (fn [_ [_ retries]] {::fetch-peers (or retries 0)})) +(def ^:private ^:const short-retry-delay-ms 300) +(def ^:private ^:const long-retry-delay-ms 5000) +(def ^:private ^:const max-retries 10) +(def ^:private ^:const retries-interval-change-threshold 3) + (handlers/register-handler-fx :inbox/check-peer-added - ;; We check if the wnode is part of the peers list - ;; if not we dispatch a new fetch-peer event for later - (fn [{:keys [db]} [_ peers retries]] - (let [web3 (:web3 db) - wnode (get-current-wnode-address db)] + ;; We check if the wnode is part of the peers list + ;; if not we dispatch a new fetch-peer event for later + (fn [{{:keys [web3 network-status] :as db} :db} [_ peers retries]] + (let [wnode (get-current-wnode-address db)] (log/info "offline inbox: fetch-peers response" peers) (if (registered-peer? peers wnode) {::mark-trusted-peer {:web3 web3 :wnode wnode}} (do (log/info "Peer" wnode "is not registered. Retrying fetch peers.") - (let [delay (if (< retries 3) 300 5000)] - (if (> retries 10) - (do (log/error :mailserver-connection-error) - (utils/show-popup (i18n/label :t/error) - (i18n/label :t/mailserver-connection-error))) - {:dispatch-later [{:ms delay :dispatch [:inbox/fetch-peers (inc retries)]}]}))))))) + (let [delay-ms (if (< retries retries-interval-change-threshold) + short-retry-delay-ms + long-retry-delay-ms)] + (if (or (= network-status :offline) + (> retries max-retries)) + (do (log/info :mailserver-connection-error) + {:db (assoc db :mailserver-status :disconnected)}) + {:dispatch-later [{:ms delay-ms :dispatch [:inbox/fetch-peers (inc retries)]}]}))))))) (handlers/register-handler-fx :inbox/get-sym-key @@ -234,3 +240,8 @@ :sym-key-id sym-key-id :web3 web3} :db (assoc db :inbox/last-request (quot now 1000))}))) + +(handlers/register-handler-fx + :inbox/reconnect + (fn [cofx _] + (recover-offline-inbox true cofx))) diff --git a/src/status_im/ui/components/connectivity/styles.cljs b/src/status_im/ui/components/connectivity/styles.cljs index 753a8bec4f..0ef4d16f5a 100644 --- a/src/status_im/ui/components/connectivity/styles.cljs +++ b/src/status_im/ui/components/connectivity/styles.cljs @@ -1,7 +1,7 @@ (ns status-im.ui.components.connectivity.styles (:require-macros [status-im.utils.styles :refer [defnstyle]])) -(defnstyle offline-wrapper [top opacity window-width pending?] +(defnstyle text-wrapper [top opacity window-width pending?] {:ios {:z-index 0} :opacity opacity :width window-width @@ -10,7 +10,7 @@ :background-color "#828b92cc" :height 35}) -(def offline-text +(def text {:text-align :center :color :white :font-size 14 diff --git a/src/status_im/ui/components/connectivity/view.cljs b/src/status_im/ui/components/connectivity/view.cljs index 8ccafec893..75d8a0015a 100644 --- a/src/status_im/ui/components/connectivity/view.cljs +++ b/src/status_im/ui/components/connectivity/view.cljs @@ -31,11 +31,14 @@ :display-name "connectivity-error-view" :reagent-render (fn [{:keys [top]}] - (when (or @offline? @connection-problem?) + (when-let [label (cond + @offline? :t/offline + @connection-problem? :t/mailserver-reconnect + :else nil)] (let [pending? (and (:pending @current-chat-contact) (= :chat @view-id))] - [react/animated-view {:style (styles/offline-wrapper top offline-opacity window-width pending?)} + [react/animated-view {:style (styles/text-wrapper top offline-opacity window-width pending?)} [react/view - [react/text {:style styles/offline-text} - (i18n/label (if @connection-problem? - :t/connection-problem - :t/offline))]]])))}))) + [react/text {:style styles/text + :on-press (when @connection-problem? + #(re-frame/dispatch [:inbox/reconnect]))} + (i18n/label label)]]])))})))