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 authored Jun 27, 2023
1 parent cebc50d commit e0e6937
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 64 deletions.
17 changes: 9 additions & 8 deletions src/status_im/chat/models/input.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
[status-im2.contexts.chat.composer.link-preview.events :as link-preview]
[taoensso.timbre :as log]
[utils.i18n :as i18n]
[utils.re-frame :as rf]))
[utils.re-frame :as rf]
[utils.string :as utils.string]))

(defn text->emoji
"Replaces emojis in a specified `text`"
[text]
(utils/safe-replace text
#":([a-z_\-+0-9]*):"
(fn [[original emoji-id]]
(if-let [emoji-map (object/get (.-lib emojis) emoji-id)]
(.-char ^js emoji-map)
original))))
(utils.string/safe-replace text
#":([a-z_\-+0-9]*):"
(fn [[original emoji-id]]
(if-let [emoji-map (object/get (.-lib emojis) emoji-id)]
(.-char ^js emoji-map)
original))))

;; effects
(re-frame/reg-fx
Expand Down Expand Up @@ -169,7 +170,7 @@
{:chat-id chat-id
:album-id album-id
:content-type constants/content-type-image
:image-path (utils/safe-replace resized-uri #"file://" "")
:image-path (utils.string/safe-replace resized-uri #"file://" "")
:image-width width
:image-height height
:text input-text
Expand Down
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
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
63 changes: 25 additions & 38 deletions src/status_im2/common/toasts/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,58 @@
[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? close!]
(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)]
(let [evt-translation-y (.-translationY evt)
pan-down? (> evt-translation-y 100)
pan-up? (< evt-translation-y -30)]
(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!))
pan-down? (reset-translate-y translate-y)
pan-up? (dismiss translate-y dismissed-locally? close!)
:else
(reanimated/set-shared-value translate-y
evt-translation-y)))))
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
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 e0e6937

Please sign in to comment.