diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index 7015df6723..68b8a79eac 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -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)))) diff --git a/src/status_im/chat/handlers/requests.cljs b/src/status_im/chat/handlers/requests.cljs index 534a84cf6c..d229b0dc94 100644 --- a/src/status_im/chat/handlers/requests.cljs +++ b/src/status_im/chat/handlers/requests.cljs @@ -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!)) diff --git a/src/status_im/chat/subs.cljs b/src/status_im/chat/subs.cljs index 0a8d999432..ae90e45bc0 100644 --- a/src/status_im/chat/subs.cljs +++ b/src/status_im/chat/subs.cljs @@ -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)))))) diff --git a/src/status_im/chat/views/request_message.cljs b/src/status_im/chat/views/request_message.cljs index 77e5834327..38b9237137 100644 --- a/src/status_im/chat/views/request_message.cljs +++ b/src/status_im/chat/views/request_message.cljs @@ -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}]]])}))) diff --git a/src/status_im/chat/views/suggestions.cljs b/src/status_im/chat/views/suggestions.cljs index 4438fe5865..d946c5fa8d 100644 --- a/src/status_im/chat/views/suggestions.cljs +++ b/src/status_im/chat/views/suggestions.cljs @@ -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 diff --git a/src/status_im/persistence/realm.cljs b/src/status_im/persistence/realm.cljs index 277da95dc3..e0ee29a606 100644 --- a/src/status_im/persistence/realm.cljs +++ b/src/status_im/persistence/realm.cljs @@ -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)]