From a782cf07262cfee70f150e29b6f98a68be76b4ce Mon Sep 17 00:00:00 2001 From: virvar Date: Fri, 20 May 2016 12:41:55 +0300 Subject: [PATCH] Last message date --- project.clj | 3 +- src/syng_im/chat/handlers.cljs | 16 +++-- .../chats_list/views/chat_list_item.cljs | 5 +- src/syng_im/chats_list/views/inner_item.cljs | 68 ++++++++++--------- src/syng_im/utils/datetime.cljs | 24 +++++++ 5 files changed, 73 insertions(+), 43 deletions(-) create mode 100644 src/syng_im/utils/datetime.cljs diff --git a/project.clj b/project.clj index 8e27322b77..e1079eb235 100644 --- a/project.clj +++ b/project.clj @@ -11,7 +11,8 @@ ^{:voom {:repo "https://github.com/status-im/status-lib.git" :branch "feature-discover"}} [syng-im/protocol "0.1.1-20160506_171115-ge2c95c1"] - [natal-shell "0.1.6"]] + [natal-shell "0.1.6"] + [com.andrewmcveigh/cljs-time "0.4.0"]] :plugins [[lein-cljsbuild "1.1.1"] [lein-figwheel "0.5.0-2"]] :clean-targets ["target/" "index.ios.js" "index.android.js"] diff --git a/src/syng_im/chat/handlers.cljs b/src/syng_im/chat/handlers.cljs index 7cf04bd35d..f950109b2c 100644 --- a/src/syng_im/chat/handlers.cljs +++ b/src/syng_im/chat/handlers.cljs @@ -14,6 +14,7 @@ [syng-im.models.chats :as c] [syng-im.handlers.server :as server] [syng-im.utils.phone-number :refer [format-phone-number]] + [syng-im.utils.datetime :as time] [syng-im.utils.handlers :as u])) (register-handler :set-show-actions @@ -114,13 +115,14 @@ {:keys [command]} (suggestions/check-suggestion db (str text " ")) message (check-author-direction db current-chat-id - {:msg-id (random/id) - :chat-id current-chat-id - :content text - :to current-chat-id - :from identity - :content-type text-content-type - :outgoing true})] + {:msg-id (random/id) + :chat-id current-chat-id + :content text + :to current-chat-id + :from identity + :content-type text-content-type + :outgoing true + :timestamp (time/now-ms)})] (if command (commands/set-chat-command db command) (assoc db :new-message (when-not (str/blank? text) message))))) diff --git a/src/syng_im/chats_list/views/chat_list_item.cljs b/src/syng_im/chats_list/views/chat_list_item.cljs index c11e2fcd3f..83a12accfe 100644 --- a/src/syng_im/chats_list/views/chat_list_item.cljs +++ b/src/syng_im/chats_list/views/chat_list_item.cljs @@ -11,10 +11,7 @@ (defn chat-list-item [{:keys [chat-id] :as chat}] [touchable-highlight {:on-press #(dispatch [:show-chat chat-id :push])} - ;; TODO add [photo-path delivery-status new-messages-count online] values to chat-obj [view [chat-list-item-inner-view (merge chat ;; TODO stub data - {:delivery-status :seen - :new-messages-count 3 - :timestamp "13:54" + {:new-messages-count 3 :online true})]]]) diff --git a/src/syng_im/chats_list/views/inner_item.cljs b/src/syng_im/chats_list/views/inner_item.cljs index 8f2b3b1554..09984101b0 100644 --- a/src/syng_im/chats_list/views/inner_item.cljs +++ b/src/syng_im/chats_list/views/inner_item.cljs @@ -2,37 +2,43 @@ (:require-macros [syng-im.utils.views :refer [defview]]) (:require [syng-im.components.react :refer [view image icon text]] [syng-im.components.chat-icon.screen :refer [chat-icon-view-chat-list]] - [syng-im.chats-list.styles :as st])) + [syng-im.chats-list.styles :as st] + [syng-im.utils.datetime :as time])) (defn chat-list-item-inner-view - [{:keys [chat-id name color photo-path delivery-status timestamp new-messages-count + [{:keys [chat-id name color photo-path new-messages-count online group-chat contacts] :as chat}] - [view st/chat-container - [view st/chat-icon-container - [chat-icon-view-chat-list chat-id group-chat name color online]] - [view st/item-container - [view st/name-view - [text {:style st/name-text} name] - (when group-chat - [icon :group st/group-icon]) - (when group-chat - [text {:style st/memebers-text} - (if (< 1 (count contacts)) - (str (count contacts) " members") - "1 member")])] - [text {:style st/last-message-text - :numberOfLines 2} - (when-let [last-message (first (:messages chat))] - (:content last-message))]] - [view - [view st/status-container - (when delivery-status - [image {:source (if (= (keyword delivery-status) :seen) - {:uri :icon_ok_small} - ;; todo change icon - {:uri :icon_ok_small}) - :style st/status-image}]) - [text {:style st/datetime-text} timestamp]] - (when (pos? new-messages-count) - [view st/new-messages-container - [text {:style st/new-messages-text} new-messages-count]])]]) + (let [last-message (first (:messages chat))] + [view st/chat-container + [view st/chat-icon-container + [chat-icon-view-chat-list chat-id group-chat name color online]] + [view st/item-container + [view st/name-view + [text {:style st/name-text} name] + (when group-chat + [icon :group st/group-icon]) + (when group-chat + [text {:style st/memebers-text} + (if (< 1 (count contacts)) + (str (count contacts) " members") + "1 member")])] + [text {:style st/last-message-text + :numberOfLines 2} + (when last-message + (:content last-message))]] + [view + (when last-message + [view st/status-container + ;; TODO currently there is not :delivery-status in last-message + (when (:delivery-status last-message) + [image {:source (if (= (keyword (:delivery-status last-message)) :seen) + {:uri :icon_ok_small} + ;; todo change icon + {:uri :icon_ok_small}) + :style st/status-image}]) + (when (:timestamp last-message) + [text {:style st/datetime-text} + (time/to-short-str (:timestamp last-message))])]) + (when (pos? new-messages-count) + [view st/new-messages-container + [text {:style st/new-messages-text} new-messages-count]])]])) diff --git a/src/syng_im/utils/datetime.cljs b/src/syng_im/utils/datetime.cljs new file mode 100644 index 0000000000..7ba3686b4b --- /dev/null +++ b/src/syng_im/utils/datetime.cljs @@ -0,0 +1,24 @@ +(ns syng-im.utils.datetime + (:require [cljs-time.core :as t :refer [date-time now plus days hours before?]] + [cljs-time.coerce :refer [from-long to-long]] + [cljs-time.format :as format :refer [formatters + formatter + unparse]])) + +(def time-zone-offset (hours (- (/ (.getTimezoneOffset (js/Date.)) 60)))) + +(defn to-short-str [ms] + (let [date (from-long ms) + local (plus date time-zone-offset) + today-date (t/today) + today (date-time (t/year today-date) + (t/month today-date) + (t/day today-date)) + yesterday (plus today (days -1))] + (cond + (before? local yesterday) (unparse (formatter "dd MMM") local) + (before? local today) "Yesterday" + :else (unparse (formatters :hour-minute) local)))) + +(defn now-ms [] + (to-long (now)))