remove request after answering & request icon animation

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

View File

@ -1,7 +1,8 @@
(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.persistence.realm :as realm]))
[status-im.persistence.realm :as realm]
[status-im.utils.handlers :as u]))
(defn store-request!
[{:keys [new-request]}]
@ -25,15 +26,32 @@
(let [chat-id' (or chat-id current-chat-id)
requests (-> :requests
;; todo maybe limit is needed
(realm/get-by-fieds {:chat-id chat-id'
:status "open"})
(realm/get-by-fields {:chat-id chat-id'
:status "open"})
(realm/sorted :added :desc)
(realm/collection->map))
requests' (map #(update % :type keyword) 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
(after store-request!)
add-request)
(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]]
(let [chat-id (subscribe [:get-current-chat-id])]
(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 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
[{:keys [to-value val loop?] :as context}]
[{:keys [to-value val loop? answered?] :as context}]
(anim/start
(anim/anim-sequence
[(anim/anim-delay (if @loop? request-message-icon-scale-delay 0))
(anim/spring val {:toValue to-value})])
#(when @loop?
(button-animation val to-value loop? answered?)
#(if (and @loop? (not @answered?))
(let [new-value (if (= to-value min-scale) max-scale min-scale)
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]
(let [scale-anim-val (anim/create-value min-scale)
answered? (subscribe [:is-request-answered? msg-id])
loop? (r/atom true)
context {:to-value max-scale
:val scale-anim-val
:loop? loop?}]
context {:to-value max-scale
:val scale-anim-val
:answered? answered?
:loop? loop?}]
(r/create-class
{:component-did-mount
#(request-button-animation-logic context)
@ -46,10 +56,10 @@
#(reset! loop? false)
:reagent-render
(fn [msg-id command]
[touchable-highlight {:on-press (fn []
(reset! loop? false)
(set-chat-command msg-id command))
:style st/command-request-image-touchable}
[touchable-highlight
{:on-press (when-not @answered?
#(set-chat-command msg-id command))
:style st/command-request-image-touchable}
[animated-view {:style (st/command-request-image-view command scale-anim-val)}
[image {:source {:uri (:icon command)}
:style st/command-request-image}]]])})))

View File

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

View File

@ -137,7 +137,7 @@
(let [q (to-query schema-name :eq field value)]
(.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]]
(to-query schema-name :eq k v))
fields)]