remove request after answering & request icon animation

Former-commit-id: b7426f7eba106f10115b7beb909bd308a18b0efa
This commit is contained in:
Roman Volosovskyi 2016-06-30 13:52:04 +03:00
parent b763d5d9c9
commit d0e9eb04ea
6 changed files with 73 additions and 28 deletions

View File

@ -108,11 +108,14 @@
(after invoke-command-preview!) (after invoke-command-preview!)
(fn [{:keys [current-chat-id] :as db} _] (fn [{:keys [current-chat-id] :as db} _]
(let [db (update-input-text db nil) (let [db (update-input-text db nil)
{:keys [command content]} (get-in db [:chats current-chat-id :command-input]) {:keys [command content to-msg-id]}
(get-in db [:chats current-chat-id :command-input])
content' (if (= :command (:type command)) content' (if (= :command (:type command))
(subs content 2) (subs content 2)
content) content)
command-info {:command command :content content'}] command-info {:command command
:content content'
:to-message to-msg-id}]
(-> db (-> db
(commands/stage-command command-info) (commands/stage-command command-info)
(assoc :staged-command command-info))))) (assoc :staged-command command-info)))))
@ -222,7 +225,7 @@
(assoc db :new-message (when-not (str/blank? text) message))))) (assoc db :new-message (when-not (str/blank? text) message)))))
(defn prepare-command (defn prepare-command
[identity chat-id {:keys [preview preview-string content command]}] [identity chat-id {:keys [preview preview-string content command to-message]}]
(let [content {:command (command :name) (let [content {:command (command :name)
:content content}] :content content}]
{:msg-id (random/id) {:msg-id (random/id)
@ -232,7 +235,8 @@
:content-type content-type-command :content-type content-type-command
:outgoing true :outgoing true
:preview preview-string :preview preview-string
:rendered-preview preview})) :rendered-preview preview
:to-message to-message}))
(defn prepare-staged-commans (defn prepare-staged-commans
[{:keys [current-chat-id identity] :as db} _] [{:keys [current-chat-id identity] :as db} _]
@ -285,8 +289,15 @@
(defn save-commands-to-realm! (defn save-commands-to-realm!
[{:keys [new-commands current-chat-id]} _] [{:keys [new-commands current-chat-id]} _]
(doseq [new-command new-commands] (doseq [new-command new-commands]
(messages/save-message current-chat-id (messages/save-message
(dissoc new-command :rendered-preview)))) current-chat-id
(dissoc new-command :rendered-preview :to-message))))
(defn dispatch-responded-requests!
[{:keys [new-commands current-chat-id]} _]
(doseq [{:keys [to-message]} new-commands]
(when to-message
(dispatch [:request-answered! current-chat-id to-message]))))
(defn invoke-commands-handlers! (defn invoke-commands-handlers!
[{:keys [new-commands current-chat-id]}] [{:keys [new-commands current-chat-id]}]
@ -319,6 +330,7 @@
((after send-message!)) ((after send-message!))
((after save-message-to-realm!)) ((after save-message-to-realm!))
((after save-commands-to-realm!)) ((after save-commands-to-realm!))
((after dispatch-responded-requests!))
;; todo maybe it is better to track if it was handled or not ;; todo maybe it is better to track if it was handled or not
((after invoke-commands-handlers!)) ((after invoke-commands-handlers!))
((after handle-commands)))) ((after handle-commands))))

View File

@ -1,7 +1,8 @@
(ns status-im.chat.handlers.requests (ns status-im.chat.handlers.requests
(:require [re-frame.core :refer [after]] (:require [re-frame.core :refer [after dispatch enrich]]
[status-im.utils.handlers :refer [register-handler]] [status-im.utils.handlers :refer [register-handler]]
[status-im.persistence.realm :as realm])) [status-im.persistence.realm :as realm]
[status-im.utils.handlers :as u]))
(defn store-request! (defn store-request!
[{:keys [new-request]}] [{:keys [new-request]}]
@ -25,15 +26,32 @@
(let [chat-id' (or chat-id current-chat-id) (let [chat-id' (or chat-id current-chat-id)
requests (-> :requests requests (-> :requests
;; todo maybe limit is needed ;; todo maybe limit is needed
(realm/get-by-fieds {:chat-id chat-id' (realm/get-by-fields {:chat-id chat-id'
:status "open"}) :status "open"})
(realm/sorted :added :desc) (realm/sorted :added :desc)
(realm/collection->map)) (realm/collection->map))
requests' (map #(update % :type keyword) requests)] requests' (map #(update % :type keyword) requests)]
(assoc-in db [:chats chat-id' :requests] requests'))) (assoc-in db [:chats chat-id' :requests] requests')))
(defn mark-request-as-answered!
[_ [_ chat-id message-id]]
(realm/write
(fn []
(-> :requests
(realm/get-by-fields
{:chat-id chat-id
:message-id message-id})
(realm/single)
(.-status)
(set! "answered")))))
(register-handler :add-request (register-handler :add-request
(after store-request!) (after store-request!)
add-request) add-request)
(register-handler :load-requests! load-requests!) (register-handler :load-requests! load-requests!)
(register-handler :request-answered!
(after (fn [_ [_ chat-id]]
(dispatch [:load-requests! chat-id])))
(u/side-effect! mark-request-as-answered!))

View File

@ -167,3 +167,8 @@
(fn [db [_ n]] (fn [db [_ n]]
(let [chat-id (subscribe [:get-current-chat-id])] (let [chat-id (subscribe [:get-current-chat-id])]
(reaction (get-in @db [:chats @chat-id :responses n]))))) (reaction (get-in @db [:chats @chat-id :responses n])))))
(register-sub :is-request-answered?
(fn [_ [_ message-id]]
(let [requests (subscribe [:get-requests])]
(reaction (not (some #(= message-id (:message-id %)) @requests))))))

View File

@ -22,23 +22,33 @@
(def min-scale 1) (def min-scale 1)
(def max-scale 1.3) (def max-scale 1.3)
(defn button-animation [val to-value loop? answered?]
(anim/anim-sequence
[(anim/anim-delay
(if (and @loop? (not @answered?))
request-message-icon-scale-delay
0))
(anim/spring val {:toValue to-value})]))
(defn request-button-animation-logic (defn request-button-animation-logic
[{:keys [to-value val loop?] :as context}] [{:keys [to-value val loop? answered?] :as context}]
(anim/start (anim/start
(anim/anim-sequence (button-animation val to-value loop? answered?)
[(anim/anim-delay (if @loop? request-message-icon-scale-delay 0)) #(if (and @loop? (not @answered?))
(anim/spring val {:toValue to-value})])
#(when @loop?
(let [new-value (if (= to-value min-scale) max-scale min-scale) (let [new-value (if (= to-value min-scale) max-scale min-scale)
context' (assoc context :to-value new-value)] context' (assoc context :to-value new-value)]
(request-button-animation-logic context'))))) (request-button-animation-logic context'))
(anim/start
(button-animation val min-scale loop? answered?)))))
(defn request-button [msg-id command] (defn request-button [msg-id command]
(let [scale-anim-val (anim/create-value min-scale) (let [scale-anim-val (anim/create-value min-scale)
answered? (subscribe [:is-request-answered? msg-id])
loop? (r/atom true) loop? (r/atom true)
context {:to-value max-scale context {:to-value max-scale
:val scale-anim-val :val scale-anim-val
:loop? loop?}] :answered? answered?
:loop? loop?}]
(r/create-class (r/create-class
{:component-did-mount {:component-did-mount
#(request-button-animation-logic context) #(request-button-animation-logic context)
@ -46,10 +56,10 @@
#(reset! loop? false) #(reset! loop? false)
:reagent-render :reagent-render
(fn [msg-id command] (fn [msg-id command]
[touchable-highlight {:on-press (fn [] [touchable-highlight
(reset! loop? false) {:on-press (when-not @answered?
(set-chat-command msg-id command)) #(set-chat-command msg-id command))
:style st/command-request-image-touchable} :style st/command-request-image-touchable}
[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

@ -71,8 +71,8 @@
[suggestions [:get-suggestions] [suggestions [:get-suggestions]
requests [:get-requests]] requests [:get-requests]]
[scroll-view [scroll-view
(when requests [title "Requests"]) (when (seq requests) [title "Requests"])
(when requests (when (seq requests)
[view [view
[list-view {:dataSource (to-datasource requests) [list-view {:dataSource (to-datasource requests)
:keyboardShouldPersistTaps true :keyboardShouldPersistTaps true

View File

@ -137,7 +137,7 @@
(let [q (to-query schema-name :eq field value)] (let [q (to-query schema-name :eq field value)]
(.filtered (.objects realm (name schema-name)) q))) (.filtered (.objects realm (name schema-name)) q)))
(defn get-by-fieds [schema-name fields] (defn get-by-fields [schema-name fields]
(let [queries (map (fn [[k v]] (let [queries (map (fn [[k v]]
(to-query schema-name :eq k v)) (to-query schema-name :eq k v))
fields)] fields)]