custom register-handler & response icon animation

Former-commit-id: e7b328aa27
This commit is contained in:
Roman Volosovskyi 2016-06-27 12:07:14 +03:00
parent 21fb6064de
commit eedd1c1186
11 changed files with 51 additions and 77 deletions

View File

@ -1,5 +1,5 @@
(ns status-im.chat.handlers (ns status-im.chat.handlers
(:require [re-frame.core :refer [register-handler enrich after debug dispatch]] (:require [re-frame.core :refer [enrich after debug dispatch]]
[status-im.models.commands :as commands] [status-im.models.commands :as commands]
[clojure.string :as str] [clojure.string :as str]
[status-im.components.styles :refer [default-chat-color]] [status-im.components.styles :refer [default-chat-color]]
@ -12,7 +12,7 @@
[status-im.chat.sign-up :as sign-up-service] [status-im.chat.sign-up :as sign-up-service]
[status-im.models.chats :as chats] [status-im.models.chats :as chats]
[status-im.navigation.handlers :as nav] [status-im.navigation.handlers :as nav]
[status-im.utils.handlers :as u] [status-im.utils.handlers :refer [register-handler] :as u]
[status-im.persistence.realm :as r] [status-im.persistence.realm :as r]
[status-im.handlers.server :as server] [status-im.handlers.server :as server]
[status-im.handlers.content-suggestions :refer [get-content-suggestions]] [status-im.handlers.content-suggestions :refer [get-content-suggestions]]

View File

@ -1,5 +1,6 @@
(ns status-im.chat.handlers.animation (ns status-im.chat.handlers.animation
(:require [re-frame.core :refer [register-handler after dispatch debug]] (:require [re-frame.core :refer [after dispatch debug]]
[status-im.utils.handlers :refer [register-handler]]
[re-frame.middleware :refer [path]] [re-frame.middleware :refer [path]]
[status-im.handlers.content-suggestions :refer [get-content-suggestions]] [status-im.handlers.content-suggestions :refer [get-content-suggestions]]
[status-im.chat.constants :refer [input-height request-info-height [status-im.chat.constants :refer [input-height request-info-height

View File

@ -153,15 +153,13 @@
:backgroundColor (:color command) :backgroundColor (:color command)
:transform [{:scale scale}]}) :transform [{:scale scale}]})
(defn command-image-view (def command-image-view
[command]
{:position :absolute {:position :absolute
:top 0 :top 0
:right 0 :right 0
:width 24 :width 24
:height 24 :height 24
:borderRadius 50 :borderRadius 50
:backgroundColor (:color command)
:alignItems :center}) :alignItems :center})
(def command-request-image (def command-request-image
@ -198,7 +196,9 @@
(def command-image (def command-image
{:margin-top 5 {:margin-top 5
:width 12 :width 12
:height 13}) :height 13
:tintColor :#a9a9a9cc})
(def command-text (def command-text
(merge style-message-text (merge style-message-text
{:marginTop 8 {:marginTop 8

View File

@ -69,7 +69,7 @@
(str "!" (:name command))]]] (str "!" (:name command))]]]
;; todo doesn't reflect design ;; todo doesn't reflect design
(when-let [icon (:icon command)] (when-let [icon (:icon command)]
[view (st/command-image-view command) [view st/command-image-view
[image {:source {:uri icon} [image {:source {:uri icon}
:style st/command-image}]]) :style st/command-image}]])
(if preview (if preview
@ -84,25 +84,6 @@
(->> (when command (name command)) (->> (when command (name command))
(str "request-"))) (str "request-")))
;; todo remove (merging leftover)
#_(defview message-content-command-request
[{:keys [msg-id content from incoming-group]}]
[commands [:get-responses]]
(let [{:keys [command content]} (parse-command-request commands content)]
[touchable-highlight {:onPress #(set-chat-command msg-id command)
:accessibility-label (label command)}
[view st/comand-request-view
[view st/command-request-message-view
(when incoming-group
[text {:style st/command-request-from-text} from])
[text {:style st/style-message-text} content]]
[view (st/command-request-image-view command)
[image {:source {:uri (:icon command)}
:style st/command-request-image}]]
(when-let [request-text (:request-text command)]
[view st/command-request-text-view
[text {:style st/style-sub-text} request-text]])]]))
(defn message-view (defn message-view
[message content] [message content]
[view (st/message-view message) [view (st/message-view message)

View File

@ -19,49 +19,37 @@
(->> (name command) (->> (name command)
(str "request-"))) (str "request-")))
(defn request-button-animation-logic [{:keys [to-value val loop?]}] (def min-scale 1)
(fn [_] (def max-scale 1.3)
(let [loop? @loop?
minimum 1 (defn request-button-animation-logic
maximum 1.3 [{:keys [to-value val loop?] :as context}]
to-scale (if loop?
(or @to-value maximum)
minimum)]
(anim/start (anim/start
(anim/anim-sequence (anim/anim-sequence
[(anim/anim-delay (if loop? request-message-icon-scale-delay 0)) [(anim/anim-delay (if @loop? request-message-icon-scale-delay 0))
(anim/spring val {:toValue to-scale})]) (anim/spring val {:toValue to-value})])
(fn [arg] #(when @loop?
(when (.-finished arg) (let [new-value (if (= to-value min-scale) max-scale min-scale)
(dispatch [:set-animation ::request-button-scale-current to-scale]) context' (assoc context :to-value new-value)]
(when loop? (request-button-animation-logic context')))))
(dispatch [:set-animation ::request-button-scale (if (= to-scale minimum)
maximum
minimum)]))))))))
(defn request-button [msg-id command] (defn request-button [msg-id command]
(let [to-scale (subscribe [:animations ::request-button-scale]) (let [scale-anim-val (anim/create-value min-scale)
cur-scale (subscribe [:animations ::request-button-scale-current])
scale-anim-val (anim/create-value (or @cur-scale 1))
loop? (r/atom true) loop? (r/atom true)
context {:to-value to-scale context {:to-value max-scale
:val scale-anim-val :val scale-anim-val
:loop? loop?} :loop? loop?}]
on-update (request-button-animation-logic context)]
(r/create-class (r/create-class
{:component-did-mount {:component-did-mount
on-update #(request-button-animation-logic context)
:component-did-update :component-will-unmount
on-update #(reset! loop? false)
:reagent-render :reagent-render
(fn [msg-id command] (fn [msg-id command]
@to-scale
[touchable-highlight {:on-press (fn [] [touchable-highlight {:on-press (fn []
(reset! loop? false) (reset! loop? false)
(set-chat-command msg-id command)) (set-chat-command msg-id command))
:style st/command-request-image-touchable} :style st/command-request-image-touchable}
;:accessibility-label (label command)
[animated-view {:style (st/command-request-image-view command scale-anim-val)} [animated-view {:style (st/command-request-image-view command scale-anim-val)}
[image {:source {:uri (:icon command)} [image {:source {:uri (:icon command)}
:style st/command-request-image}]]])}))) :style st/command-request-image}]]])})))

View File

@ -83,11 +83,10 @@
[icon :close-white st/cancel-icon]]]]]))) [icon :close-white st/cancel-icon]]]]])))
(defn container-animation-logic [{:keys [to-value val]}] (defn container-animation-logic [{:keys [to-value val]}]
(fn [_]
(let [to-value @to-value] (let [to-value @to-value]
(anim/start (anim/spring val {:toValue to-value (anim/start (anim/spring val {:toValue to-value
:tension 50 :tension 50
:friction 10}))))) :friction 10}))))
(defn container [response-height & children] (defn container [response-height & children]
(let [;; todo to-response-height, cur-response-height must be specific (let [;; todo to-response-height, cur-response-height must be specific
@ -96,7 +95,7 @@
changed (subscribe [:animations :response-height-changed]) changed (subscribe [:animations :response-height-changed])
context {:to-value to-response-height context {:to-value to-response-height
:val response-height} :val response-height}
on-update (container-animation-logic context)] on-update #(container-animation-logic context)]
(r/create-class (r/create-class
{:component-did-mount {:component-did-mount
on-update on-update

View File

@ -93,11 +93,10 @@
[icon :drag_down st/drag-down-icon]]))) [icon :drag_down st/drag-down-icon]])))
(defn container-animation-logic [{:keys [to-value val]}] (defn container-animation-logic [{:keys [to-value val]}]
(fn [_]
(let [to-value @to-value] (let [to-value @to-value]
(anim/start (anim/spring val {:toValue to-value (anim/start (anim/spring val {:toValue to-value
:tension 50 :tension 50
:friction 10}))))) :friction 10}))))
(defn container [h & elements] (defn container [h & elements]
(let [;; todo to-response-height, cur-response-height must be specific (let [;; todo to-response-height, cur-response-height must be specific
@ -106,7 +105,7 @@
changed (subscribe [:animations :commands-height-changed]) changed (subscribe [:animations :commands-height-changed])
context {:to-value to-response-height context {:to-value to-response-height
:val h} :val h}
on-update (container-animation-logic context)] on-update #(container-animation-logic context)]
(r/create-class (r/create-class
{:component-did-mount {:component-did-mount
on-update on-update

View File

@ -1,6 +1,5 @@
(ns status-im.commands.handlers.jail (ns status-im.commands.handlers.jail
(:require [re-frame.core :refer [register-handler after dispatch subscribe (:require [re-frame.core :refer [after dispatch subscribe trim-v debug]]
trim-v debug]]
[status-im.utils.handlers :as u] [status-im.utils.handlers :as u]
[status-im.utils.utils :refer [http-get toast]] [status-im.utils.utils :refer [http-get toast]]
[status-im.components.jail :as j] [status-im.components.jail :as j]

View File

@ -3,7 +3,8 @@
[clojure.walk :as w] [clojure.walk :as w]
[status-im.components.react :refer [text scroll-view view [status-im.components.react :refer [text scroll-view view
image touchable-highlight]] image touchable-highlight]]
[re-frame.core :refer [register-handler dispatch trim-v debug]])) [re-frame.core :refer [register-handler dispatch trim-v debug]]
[status-im.utils.handlers :refer [register-handler]]))
(defn json->clj [json] (defn json->clj [json]
(if (= json "undefined") (if (= json "undefined")
@ -44,4 +45,4 @@
(defn reg-handler (defn reg-handler
([name handler] (reg-handler name nil handler)) ([name handler] (reg-handler name nil handler))
([name middleware handler] ([name middleware handler]
(register-handler name [#_debug trim-v middleware] handler))) (register-handler name [trim-v middleware] handler)))

View File

@ -1,13 +1,13 @@
(ns status-im.handlers (ns status-im.handlers
(:require (:require
[re-frame.core :refer [register-handler after dispatch debug]] [re-frame.core :refer [after dispatch debug]]
[schema.core :as s :include-macros true] [schema.core :as s :include-macros true]
[status-im.db :refer [app-db schema]] [status-im.db :refer [app-db schema]]
[status-im.persistence.simple-kv-store :as kv] [status-im.persistence.simple-kv-store :as kv]
[status-im.protocol.state.storage :as storage] [status-im.protocol.state.storage :as storage]
[status-im.utils.logging :as log] [status-im.utils.logging :as log]
[status-im.utils.crypt :refer [gen-random-bytes]] [status-im.utils.crypt :refer [gen-random-bytes]]
[status-im.utils.handlers :as u] [status-im.utils.handlers :refer [register-handler] :as u]
status-im.chat.handlers status-im.chat.handlers
status-im.chat.handlers.animation status-im.chat.handlers.animation
status-im.group-settings.handlers status-im.group-settings.handlers

View File

@ -1,4 +1,5 @@
(ns status-im.utils.handlers) (ns status-im.utils.handlers
(:require [re-frame.core :refer [after dispatch debug] :as re-core]))
(defn side-effect! (defn side-effect!
"Middleware for handlers that will not affect db." "Middleware for handlers that will not affect db."
@ -6,3 +7,8 @@
(fn [db params] (fn [db params]
(handler db params) (handler db params)
db)) db))
(defn register-handler
([name handler] (register-handler name nil handler))
([name middleware handler]
(re-core/register-handler name [#_debug middleware] handler)))