From 2fab8bf43f503886a0b4feaf3ad88d429f19b573 Mon Sep 17 00:00:00 2001 From: Jamie Caprani Date: Mon, 26 Jun 2023 11:04:32 +0100 Subject: [PATCH] chore: small refactor to remove some uses of status-im in status-im2 --- src/status_im/chat/models/input.cljs | 17 ++-- .../ui/screens/wallet/recipient/views.cljs | 5 +- src/status_im/utils/utils.cljs | 10 --- src/status_im/wallet/recipient/core.cljs | 5 +- src/status_im2/common/toasts/view.cljs | 85 ++++++++----------- .../contexts/add_new_contact/events.cljs | 6 +- src/utils/string.cljs | 13 ++- 7 files changed, 65 insertions(+), 76 deletions(-) diff --git a/src/status_im/chat/models/input.cljs b/src/status_im/chat/models/input.cljs index f70a72c7c639..e47910bc1bca 100644 --- a/src/status_im/chat/models/input.cljs +++ b/src/status_im/chat/models/input.cljs @@ -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 @@ -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 diff --git a/src/status_im/ui/screens/wallet/recipient/views.cljs b/src/status_im/ui/screens/wallet/recipient/views.cljs index bf895c52d6f1..0ce8fc68d5b5 100644 --- a/src/status_im/ui/screens/wallet/recipient/views.cljs +++ b/src/status_im/ui/screens/wallet/recipient/views.cljs @@ -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 @@ -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} diff --git a/src/status_im/utils/utils.cljs b/src/status_im/utils/utils.cljs index c7d861f0422c..ac5bae52a91d 100644 --- a/src/status_im/utils/utils.cljs +++ b/src/status_im/utils/utils.cljs @@ -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) diff --git a/src/status_im/wallet/recipient/core.cljs b/src/status_im/wallet/recipient/core.cljs index 582159232059..97f6b0ef6741 100644 --- a/src/status_im/wallet/recipient/core.cljs +++ b/src/status_im/wallet/recipient/core.cljs @@ -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)) @@ -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)] diff --git a/src/status_im2/common/toasts/view.cljs b/src/status_im2/common/toasts/view.cljs index 309c5402f124..24f9ab6988ad 100644 --- a/src/status_im2/common/toasts/view.cljs +++ b/src/status_im2/common/toasts/view.cljs @@ -4,28 +4,10 @@ [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])] @@ -33,49 +15,52 @@ [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)] - (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? 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)))))] ;; 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)) diff --git a/src/status_im2/contexts/add_new_contact/events.cljs b/src/status_im2/contexts/add_new_contact/events.cljs index bead662a60b5..a012f78a800c 100644 --- a/src/status_im2/contexts/add_new_contact/events.cljs +++ b/src/status_im2/contexts/add_new_contact/events.cljs @@ -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]). @@ -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)] diff --git a/src/utils/string.cljs b/src/utils/string.cljs index c100fe7c6f9d..f0750648bdc3 100644 --- a/src/utils/string.cljs +++ b/src/utils/string.cljs @@ -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 `...` @@ -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)))