Skip to content

Commit

Permalink
chore: small refactor to remove some uses of status-im in status-im2
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Son89 committed Jun 22, 2023
1 parent d300e88 commit ffbda98
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 93 deletions.
5 changes: 3 additions & 2 deletions src/status_im/ui/screens/wallet/recipient/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
[status-im.ui.components.topbar :as topbar]
[status-im.ui.screens.wallet.components.views :as components]
[status-im.utils.utils :as utils]
[utils.debounce :as debounce])
[utils.debounce :as debounce]
[utils.string :as utils.string])
(:require-macros [status-im.utils.views :as views]))

(defn- recipient-topbar
Expand Down Expand Up @@ -304,7 +305,7 @@
(re-frame/dispatch [:set-in [:wallet/recipient :searching]
:searching])
(debounce/debounce-and-dispatch [:wallet.recipient/address-changed
(utils/safe-trim %)]
(utils.string/safe-trim %)]
600))
:accessibility-label :recipient-address-input}]]
[react/view {:align-items :center :height 30 :padding-bottom 8}
Expand Down
4 changes: 0 additions & 4 deletions src/status_im/utils/http.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,3 @@
[url]
(string/lower-case (apply str (map filter-letters-numbers-and-replace-dot-on-dash (url-host url)))))

(defn replace-port
[url new-port]
(when url
(string/replace url #"(:\d+)" (str ":" new-port))))
10 changes: 0 additions & 10 deletions src/status_im/utils/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,6 @@
(gstring/format (str "%." places "f") amount)
(or (str amount) 0))))

(defn safe-trim
[s]
(when (string? s)
(string/trim s)))

(defn safe-replace
[s m r]
(when (string? s)
(string/replace s m r)))

(defn safe-nth
[coll index]
(when (number? index)
Expand Down
5 changes: 3 additions & 2 deletions src/status_im/wallet/recipient/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
[status-im.utils.random :as random]
[status-im.utils.utils :as utils]
[status-im2.common.json-rpc.events :as json-rpc]
[status-im2.navigation.events :as navigation]))
[status-im2.navigation.events :as navigation]
[utils.string :as utils.string]))

;;NOTE we want to handle only last resolve
(def resolve-last-id (atom nil))
Expand All @@ -37,7 +38,7 @@
[{:keys [db] :as cofx} raw-recipient id]
(when (or (not id) (= id @resolve-last-id))
(reset! resolve-last-id nil)
(let [recipient (utils/safe-trim raw-recipient)]
(let [recipient (utils.string/safe-trim raw-recipient)]
(cond
(ethereum/address? recipient)
(let [checksum (eip55/address->checksum recipient)]
Expand Down
85 changes: 35 additions & 50 deletions src/status_im2/common/toasts/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,63 @@
[react-native.gesture :as gesture]
[react-native.reanimated :as reanimated]
[reagent.core :as reagent]
[status-im.utils.utils :as utils.utils]
[react-native.background-timer :as background-timer]
[status-im2.common.toasts.style :as style]
[utils.re-frame :as rf]))

;; (def ^:private slide-out-up-animation
;; (-> ^js reanimated/slide-out-up-animation
;; .springify
;; (.damping 20)
;; (.stiffness 300)))

;; (def ^:private slide-in-up-animation
;; (-> ^js reanimated/slide-in-up-animation
;; .springify
;; (.damping 20)
;; (.stiffness 300)))

;; (def ^:private linear-transition
;; (-> ^js reanimated/linear-transition
;; .springify
;; (.damping 20)
;; (.stiffness 300)))

(defn toast
[id]
(let [{:keys [type] :as toast-opts} (rf/sub [:toasts/toast id])]
(if (= type :notification)
[quo/notification toast-opts]
[quo/toast toast-opts])))

(defn reset-translate-y [translate-y]
(reanimated/animate-shared-value-with-spring translate-y
0
{:mass 1
:damping 20
:stiffness 300}))

(defn dismiss [translate-y dismissed-locally?]
(do (reanimated/animate-shared-value-with-spring
translate-y
-500
{:mass 1 :damping 20 :stiffness 300})
(reset! dismissed-locally? true)
(close!)))

(defn f-container
[id]
(let [dismissed-locally? (reagent/atom false)
close! #(rf/dispatch [:toasts/close id])
timer (reagent/atom nil)
clear-timer #(utils.utils/clear-timeout @timer)]
clear-timer #(background-timer/clear-timeout @timer)]
(fn []
(let [duration (or (rf/sub [:toasts/toast-cursor id :duration]) 3000)
on-dismissed (or (rf/sub [:toasts/toast-cursor id :on-dismissed]) identity)
create-timer (fn []
(reset! timer (utils.utils/set-timeout close! duration)))
(reset! timer (background-timer/set-timeout close! duration)))
translate-y (reanimated/use-shared-value 0)
pan
(->
(gesture/gesture-pan)
;; remove timer on pan start
(gesture/on-start clear-timer)
(gesture/on-update
(fn [^js evt]
(let [evt-translation-y (.-translationY evt)]
(cond
;; reset translate y on pan down
(> evt-translation-y 100)
(reanimated/animate-shared-value-with-spring translate-y
0
{:mass 1
:damping 20
:stiffness 300})
;; dismiss on pan up
(< evt-translation-y -30)
(do (reanimated/animate-shared-value-with-spring
translate-y
-500
{:mass 1 :damping 20 :stiffness 300})
(reset! dismissed-locally? true)
(close!))
:else
(reanimated/set-shared-value translate-y
evt-translation-y)))))
(gesture/on-end (fn [_]
(when-not @dismissed-locally?
(reanimated/set-shared-value translate-y 0)
(create-timer)))))]
(gesture/gesture-pan)
(gesture/on-start clear-timer)
(gesture/on-update
(fn [^js evt]
(let [evt-translation-y (.-translationY evt)
pan-down? (> evt-translation-y 100)
pan-up? (< evt-translation-y -30)]
(cond
pan-down? (reset-translate-y translate-y)
pan-up? (dismiss translate-y dismissed-locally?)
:else
(reanimated/set-shared-value translate-y
evt-translation-y)))))
(gesture/on-end (fn [_]
(when-not @dismissed-locally?
(reanimated/set-shared-value translate-y 0)
(create-timer)))))]
;; create auto dismiss timer, clear timer when unmount or duration changed
(rn/use-effect (fn [] (create-timer) clear-timer) [duration])
(rn/use-unmount #(on-dismissed id))
Expand Down
6 changes: 3 additions & 3 deletions src/status_im2/contexts/add_new_contact/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
[status-im2.navigation.events :as navigation]
[utils.validators :as validators]
[status-im2.contexts.contacts.events :as data-store.contacts]
[status-im.utils.utils :as utils]
[status-im2.constants :as constants]))
[status-im2.constants :as constants]
[utils.string :as utils.string]))

(defn init-contact
"Create a new contact (persisted to app-db as [:contacts/new-identity]).
Expand All @@ -37,7 +37,7 @@

(defn ->id
[{:keys [input] :as contact}]
(let [trimmed-input (utils/safe-trim input)]
(let [trimmed-input (utils.string/safe-trim input)]
(->> {:id (if (empty? trimmed-input)
nil
(if-some [[_ id] (re-matches url-regex trimmed-input)]
Expand Down
30 changes: 15 additions & 15 deletions src/status_im2/contexts/chat/lightbox/top_view.cljs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
(ns status-im2.contexts.chat.lightbox.top-view
(:require
[quo2.core :as quo]
[quo2.foundations.colors :as colors]
[react-native.core :as rn]
[react-native.orientation :as orientation]
[react-native.platform :as platform]
[react-native.reanimated :as reanimated]
[status-im.utils.http :as http]
[status-im2.contexts.chat.lightbox.animations :as anim]
[status-im2.contexts.chat.lightbox.style :as style]
[utils.datetime :as datetime]
[utils.re-frame :as rf]
[status-im2.contexts.chat.lightbox.constants :as c]
[status-im.chat.models.images :as images]))
[quo2.core :as quo]
[quo2.foundations.colors :as colors]
[react-native.core :as rn]
[react-native.orientation :as orientation]
[react-native.platform :as platform]
[react-native.reanimated :as reanimated]
[status-im2.contexts.chat.utils.core :as chat-utils]
[status-im2.contexts.chat.lightbox.animations :as anim]
[status-im2.contexts.chat.lightbox.style :as style]
[utils.datetime :as datetime]
[utils.re-frame :as rf]
[status-im2.contexts.chat.lightbox.constants :as c]
[status-im.chat.models.images :as images]))

(defn animate-rotation
[result screen-width screen-height insets
Expand Down Expand Up @@ -50,8 +50,8 @@
colors/neutral-100-opa-70
colors/neutral-100-opa-0)
{:keys [background-color opacity]} animations
uri (http/replace-port (:image content)
(rf/sub [:mediaserver/port]))]
uri (chat-utils/replace-port (:image content)
(rf/sub [:mediaserver/port]))]
[reanimated/view
{:style
(style/top-view-container (:top insets) screen-width bg-color landscape? animations derived)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[status-im2.contexts.chat.lightbox.zoomable-image.constants :as c]
[status-im2.contexts.chat.lightbox.zoomable-image.style :as style]
[status-im2.contexts.chat.lightbox.zoomable-image.utils :as utils]
[status-im.utils.http :as http]))
[status-im2.contexts.chat.utils.core :as chat-utils]))

(defn tap-gesture
[on-tap]
Expand Down Expand Up @@ -224,7 +224,7 @@
@set-full-height?
(= curr-orientation orientation/portrait))}
[reanimated/fast-image
{:source {:uri (http/replace-port (:image content) (rf/sub [:mediaserver/port]))}
{:source {:uri (chat-utils/replace-port (:image content) (rf/sub [:mediaserver/port]))}
:native-ID (when focused? :shared-element)
:style (style/image dimensions animations (:border-value render-data))}]]]))

Expand Down
4 changes: 2 additions & 2 deletions src/status_im2/contexts/chat/messages/content/album/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[status-im2.contexts.chat.messages.content.image.view :as image]
[status-im2.contexts.chat.messages.content.text.view :as text]
[utils.re-frame :as rf]
[status-im.utils.http :as http]))
[status-im2.contexts.chat.utils.core :as chat-utils]))

(def rectangular-style-count 3)

Expand Down Expand Up @@ -55,7 +55,7 @@
:index index}])}
[fast-image/fast-image
{:style (style/image dimensions index portrait? images-count)
:source {:uri (http/replace-port (:image (:content item))
:source {:uri (chat-utils/replace-port (:image (:content item))
(rf/sub [:mediaserver/port]))}
:native-ID (when (and (= shared-element-id (:message-id item))
(< index constants/max-album-photos))
Expand Down
4 changes: 2 additions & 2 deletions src/status_im2/contexts/chat/messages/content/image/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[status-im2.constants :as constants]
[utils.re-frame :as rf]
[status-im2.contexts.chat.messages.content.text.view :as text]
[status-im.utils.http :as http]))
[status-im2.contexts.chat.utils.core :as chat-utils]))

(defn calculate-dimensions
[width height]
Expand All @@ -19,7 +19,7 @@
(let [insets (safe-area/get-insets)
dimensions (calculate-dimensions (or image-width 1000) (or image-height 1000))
shared-element-id (rf/sub [:shared-element-id])
image-local-url (http/replace-port (:image content) (rf/sub [:mediaserver/port]))]
image-local-url (chat-utils/replace-port (:image content) (rf/sub [:mediaserver/port]))]
[:<>
(when (= index 0)
[text/text-content message])
Expand Down
7 changes: 7 additions & 0 deletions src/status_im2/contexts/chat/utils/core.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns status-im2.contexts.chat.utils.core
(:require [clojure.string :as string]))

(defn replace-port
[url new-port]
(when url
(string/replace url #"(:\d+)" (str ":" new-port))))
13 changes: 12 additions & 1 deletion src/utils/string.cljs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(ns utils.string)
(ns utils.string
(:require [clojure.string :as string]))

(defn truncate-str-memo
"Given string and max threshold, trims the string to threshold length with `...`
Expand Down Expand Up @@ -38,3 +39,13 @@
(defn at-least-n-chars?
[s n]
(>= (count s) n))

(defn safe-trim
[s]
(when (string? s)
(string/trim s)))

(defn safe-replace
[s m r]
(when (string? s)
(string/replace s m r)))

0 comments on commit ffbda98

Please sign in to comment.