Skip to content

Commit

Permalink
No browsing history kept on iOS [#5078]
Browse files Browse the repository at this point in the history
  • Loading branch information
flexsurfer committed Jul 6, 2018
1 parent 6a17f93 commit 7a2a093
Show file tree
Hide file tree
Showing 19 changed files with 206 additions and 306 deletions.
11 changes: 11 additions & 0 deletions resources/js/webview.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
(function () {
var history = window.history;
var pushState = history.pushState;
history.pushState = function(state) {
setTimeout(function () {
WebViewBridge.send(JSON.stringify({
type: 'navStateChange',
navState: { url: location.href, title: document.title }
}))
}, 100)
return pushState.apply(history, arguments)
};

WebViewBridge.onMessage = function (messageString) {
console.log("received from react-native: " + messageString);
Expand Down
8 changes: 6 additions & 2 deletions src/status_im/data_store/realm/schemas/account/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
[status-im.data-store.realm.schemas.account.v4.core :as v4]
[status-im.data-store.realm.schemas.account.v5.core :as v5]
[status-im.data-store.realm.schemas.account.v6.core :as v6]
[status-im.data-store.realm.schemas.account.v7.core :as v7]))
[status-im.data-store.realm.schemas.account.v7.core :as v7]
[status-im.data-store.realm.schemas.account.v8.core :as v8]))

;; TODO(oskarth): Add failing test if directory vXX exists but isn't in schemas.

Expand All @@ -31,4 +32,7 @@
:migration v6/migration}
{:schema v7/schema
:schemaVersion 7
:migration v7/migration}])
:migration v7/migration}
{:schema v8/schema
:schemaVersion 8
:migration v8/migration}])
22 changes: 9 additions & 13 deletions src/status_im/data_store/realm/schemas/account/v8/browser.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

(def schema {:name :browser
:primaryKey :browser-id
:properties {:browser-id :string
:name :string
:timestamp :int
:dapp? {:type :bool
:default false}
:url {:type :string
:optional true}
:contact {:type :string
:optional true}
:history-index {:type :int
:optional true}
:history {:type :vector
:optional true}}})
:properties {:browser-id :string
:name :string
:timestamp :int
:dapp? {:type :bool
:default false}
:history-index {:type :int
:optional true}
:history {:type "string[]"
:optional true}}})
25 changes: 25 additions & 0 deletions src/status_im/data_store/realm/schemas/account/v8/core.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(ns status-im.data-store.realm.schemas.account.v8.core
(:require [status-im.data-store.realm.schemas.account.v5.chat :as chat]
[status-im.data-store.realm.schemas.account.v6.transport :as transport]
[status-im.data-store.realm.schemas.account.v1.contact :as contact]
[status-im.data-store.realm.schemas.account.v7.message :as message]
[status-im.data-store.realm.schemas.account.v1.request :as request]
[status-im.data-store.realm.schemas.account.v1.user-status :as user-status]
[status-im.data-store.realm.schemas.account.v1.local-storage :as local-storage]
[status-im.data-store.realm.schemas.account.v2.mailserver :as mailserver]
[status-im.data-store.realm.schemas.account.v8.browser :as browser]
[taoensso.timbre :as log]))

(def schema [chat/schema
transport/schema
contact/schema
message/schema
request/schema
mailserver/schema
user-status/schema
local-storage/schema
browser/schema])

(defn migration [old-realm new-realm]
(log/debug "migrating v8 account database: " old-realm new-realm)
(message/migration old-realm new-realm))
56 changes: 0 additions & 56 deletions src/status_im/models/browser_history.cljs

This file was deleted.

4 changes: 2 additions & 2 deletions src/status_im/ui/components/list_selection.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
(defn browse [link]
(show {:title (i18n/label :t/browsing-title)
:options [{:label (i18n/label :t/browsing-open-in-browser)
:action #(re-frame/dispatch [:open-browser {:url link}])}
:action #(re-frame/dispatch [:open-url-in-browser link])}
{:label (i18n/label :t/browsing-open-in-web-browser)
:action #(.openURL react/linking (http/normalize-url link))}]
:cancel-text (i18n/label :t/browsing-cancel)}))

(defn browse-dapp [link]
(show {:title (i18n/label :t/browsing-title)
:options [{:label (i18n/label :t/browsing-open-in-browser)
:action #(re-frame/dispatch [:open-browser {:url link}])}]
:action #(re-frame/dispatch [:open-url-in-browser link])}]
:cancel-text (i18n/label :t/browsing-cancel)}))
2 changes: 1 addition & 1 deletion src/status_im/ui/screens/add_new/open_dapp/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
[react/text-input {:on-change-text #(reset! url-text %)
:on-submit-editing #(do
(re-frame/dispatch [:navigate-to-clean :home])
(re-frame/dispatch [:open-browser {:url @url-text}]))
(re-frame/dispatch [:open-url-in-browser @url-text]))
:placeholder (i18n/label :t/enter-url)
:auto-capitalize :none
:auto-correct false
Expand Down
4 changes: 0 additions & 4 deletions src/status_im/ui/screens/browser/db.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
(spec/def :browser/photo-path (spec/nilable string?))
(spec/def :browser/name (spec/nilable string?))
(spec/def :browser/dapp? (spec/nilable boolean?))
(spec/def :browser/fullscreen? (spec/nilable boolean?))
(spec/def :browser/error? (spec/nilable boolean?))
(spec/def :browser/history (spec/nilable vector?))
(spec/def :browser/history-index (spec/nilable int?))
Expand All @@ -18,7 +17,6 @@
(spec/def :browser/options
(allowed-keys
:opt-un [:browser/browser-id
:browser/fullscreen?
:browser/error?
:browser/dont-store-history-on-nav-change?]))

Expand All @@ -28,8 +26,6 @@
:browser/timestamp]
:opt-un [:browser/name
:browser/dapp?
:browser/url
:browser/contact
:browser/history
:browser/history-index]))

Expand Down
122 changes: 78 additions & 44 deletions src/status_im/ui/screens/browser/events.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(ns status-im.ui.screens.browser.events
(:require status-im.ui.screens.browser.navigation
[status-im.models.browser-history :as browser-history]
[status-im.utils.handlers :as handlers]
[re-frame.core :as re-frame]
[status-im.utils.random :as random]
Expand All @@ -10,78 +9,95 @@
[status-im.data-store.browser :as browser-store]
[status-im.utils.http :as http]))

(re-frame/reg-fx
:browse
(fn [link]
(if (utils.universal-links/universal-link? link)
(utils.universal-links/open! link)
(list-selection/browse link))))

(handlers/register-handler-fx
:initialize-browsers
[(re-frame/inject-cofx :data-store/all-browsers)]
(fn [{:keys [db all-stored-browsers]} _]
(let [browsers (into {} (map #(vector (:browser-id %) %) all-stored-browsers))]
{:db (assoc db :browser/browsers browsers)})))

(re-frame/reg-fx
:browse
(fn [link]
(if (utils.universal-links/universal-link? link)
(do
(utils.universal-links/open! link))
(list-selection/browse link))))

(handlers/register-handler-fx
:browse-link-from-message
(fn [_ [_ link]]
{:browse link}))

(defn get-new-browser [browser now]
(cond-> browser
true
(assoc :timestamp now)
(not (:browser-id browser))
(assoc :browser-id (random/id))
(not (:name browser))
(assoc :name (i18n/label :t/browser))
(:url browser)
(update :url (comp js/decodeURI http/normalize-url))))

(defn add-browser-fx [{:keys [db now]} browser]
(let [new-browser (get-new-browser browser now)]
{:db (update-in db [:browser/browsers (:browser-id new-browser)]
merge new-browser)
:data-store/tx [(browser-store/save-browser-tx new-browser)]}))
(defn update-browser-fx [{:keys [db now]} browser]
(let [updated-browser (assoc browser :timestamp now)]
{:db (update-in db [:browser/browsers (:browser-id updated-browser)]
merge updated-browser)
:data-store/tx [(browser-store/save-browser-tx updated-browser)]}))

(defn update-browser-history-fx [cofx browser url loading]
(cond
loading
nil

(get-in cofx [:db :browser/options :dont-store-history-on-nav-change?])
(assoc-in (update-browser-fx cofx browser)
[:db :browser/options :dont-store-history-on-nav-change?]
false)

:else
(let [history-index (:history-index browser)
history (:history browser)
history-url (try (nth history history-index) (catch js/Error _))
new-history (if (not= history-url url)
(conj (subvec history 0 (inc history-index))
url)
history)
new-index (dec (count new-history))]
(update-browser-fx cofx
(assoc browser :history new-history :history-index new-index)))))

(defn update-browser-and-navigate [cofx browser]
(merge (update-browser-fx cofx browser)
{:dispatch [:navigate-to :browser (:browser-id browser)]}))

(handlers/register-handler-fx
:open-dapp-in-browser
[re-frame/trim-v]
(fn [cofx [{:keys [name dapp-url]}]]
(let [browser {:browser-id name
:name name
:dapp? true
:url dapp-url}]
(merge (add-browser-fx cofx browser)
{:dispatch [:navigate-to :browser {:browser/browser-id (:browser-id browser)}]}))))
(let [browser {:browser-id name
:name name
:dapp? true
:history-index 0
:history [(http/normalize-and-decode-url dapp-url)]}]
(update-browser-and-navigate cofx browser))))

(handlers/register-handler-fx
:open-url-in-browser
[re-frame/trim-v]
(fn [cofx [url]]
(let [browser {:browser-id (random/id)
:name (i18n/label :t/browser)
:history-index 0
:history [(http/normalize-and-decode-url url)]}]
(update-browser-and-navigate cofx browser))))

(handlers/register-handler-fx
:open-browser
[re-frame/trim-v]
(fn [{:keys [now] :as cofx} [browser]]
(let [new-browser (get-new-browser browser now)]
(merge (add-browser-fx cofx new-browser)
{:dispatch [:navigate-to :browser {:browser/browser-id (:browser-id new-browser)}]}))))
(fn [cofx [browser]]
(update-browser-and-navigate cofx browser)))

(handlers/register-handler-fx
:update-browser
[re-frame/trim-v]
(fn [{:keys [now] :as cofx} [browser]]
(let [new-browser (get-new-browser browser now)]
(-> (add-browser-fx cofx new-browser)
(update-in [:db :browser/options] #(assoc % :browser-id (:browser-id new-browser)))))))
(fn [cofx [browser]]
(update-browser-fx cofx browser)))

(handlers/register-handler-fx
:update-browser-on-nav-change
[re-frame/trim-v]
(fn [{:keys [db now] :as cofx} [browser url loading]]
(let [new-browser (get-new-browser browser now)
new-browser-with-history-updated (browser-history/record-history-in-browser-if-needed db new-browser url loading)]
(-> (add-browser-fx cofx new-browser-with-history-updated)
(update-in [:db :browser/options] assoc :browser-id (:browser-id new-browser-with-history-updated))))))
(fn [cofx [browser url loading]]
(update-browser-history-fx cofx browser url loading)))

(handlers/register-handler-fx
:update-browser-options
Expand All @@ -95,3 +111,21 @@
(fn [{:keys [db]} [browser-id]]
{:db (update-in db [:browser/browsers] dissoc browser-id)
:data-store/tx [(browser-store/remove-browser-tx browser-id)]}))

(handlers/register-handler-fx
:browser-nav-back
[re-frame/trim-v]
(fn [cofx [browser]]
(let [back-index (:history-index browser)]
(when (not (zero? back-index))
(assoc-in (update-browser-fx cofx (assoc browser :history-index (dec back-index)))
[:db :browser/options :dont-store-history-on-nav-change?] true)))))

(handlers/register-handler-fx
:browser-nav-forward
[re-frame/trim-v]
(fn [cofx [browser]]
(let [forward-index (:history-index browser)]
(when (< forward-index (dec (count (:history browser))))
(assoc-in (update-browser-fx cofx (assoc browser :history-index (inc forward-index)))
[:db :browser/options :dont-store-history-on-nav-change?] true)))))
8 changes: 3 additions & 5 deletions src/status_im/ui/screens/browser/navigation.cljs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
(ns status-im.ui.screens.browser.navigation
(:require [status-im.ui.screens.navigation :as navigation]
[status-im.models.browser-history :as browser-history]))
(:require [status-im.ui.screens.navigation :as navigation]))

(defmethod navigation/preload-data! :browser
[db [_ _ {:keys [browser/browser-id]}]]
(let [dont-store (browser-history/dont-store-history-on-nav-change-if-history-exists db browser-id)]
(assoc db :browser/options (assoc dont-store :browser-id browser-id))))
[db [_ _ browser-id]]
(assoc db :browser/options {:browser-id browser-id}))
Loading

0 comments on commit 7a2a093

Please sign in to comment.