From 02fb65516be5e408653403f673328fd832ac7174 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Thu, 19 May 2016 19:29:17 +0300 Subject: [PATCH 1/6] disable 'create button' until new chat will be opened Former-commit-id: 6e52fca420a9ac5ef129914742578c639fa85e58 --- src/syng_im/db.cljs | 41 +++++++++++++++-------------- src/syng_im/new_group/handlers.cljs | 19 ++++++++++++- src/syng_im/new_group/screen.cljs | 6 +++-- 3 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/syng_im/db.cljs b/src/syng_im/db.cljs index 92c9c0d645..fa814e08a2 100644 --- a/src/syng_im/db.cljs +++ b/src/syng_im/db.cljs @@ -7,27 +7,28 @@ (def default-view :chat-list) ;; initial state of app-db -(def app-db {:identity-password "replace-me-with-user-entered-password" - :identity "me" - :contacts [] - :contacts-ids #{} - :selected-contacts #{} - :current-chat-id "console" - :chat {:command nil - :last-message nil} - :chats {} - :chats-updated-signal 0 - :show-actions false - :new-participants #{} - :signed-up true - :view-id default-view - :navigation-stack (list default-view) +(def app-db {:identity-password "replace-me-with-user-entered-password" + :identity "me" + :contacts [] + :contacts-ids #{} + :selected-contacts #{} + :current-chat-id "console" + :chat {:command nil + :last-message nil} + :chats {} + :chats-updated-signal 0 + :show-actions false + :new-participants #{} + :signed-up true + :view-id default-view + :navigation-stack (list default-view) ;; TODO fix hardcoded values - :username "My Name" - :phone-number "3147984309" - :email "myemail@gmail.com" - :status "Hi, this is my status" - :current-tag nil}) + :username "My Name" + :phone-number "3147984309" + :email "myemail@gmail.com" + :status "Hi, this is my status" + :current-tag nil + :disable-group-creation false}) (def protocol-initialized-path [:protocol-initialized]) (defn chat-input-text-path [chat-id] diff --git a/src/syng_im/new_group/handlers.cljs b/src/syng_im/new_group/handlers.cljs index bbd07a1f60..3823eb1c2e 100644 --- a/src/syng_im/new_group/handlers.cljs +++ b/src/syng_im/new_group/handlers.cljs @@ -58,12 +58,29 @@ [{:keys [new-group-id]} _] (dispatch [:show-chat new-group-id :replace])) +(defn enable-creat-buttion + [db _] + (assoc db :disable-group-creation false)) + (register-handler :create-new-group (-> start-group-chat! ((enrich prepare-chat)) ((enrich add-chat)) ((after create-chat!)) - ((after show-chat!)))) + ((after show-chat!)) + ((enrich enable-creat-buttion)))) + +(defn disable-creat-button + [db _] + (assoc db :disable-group-creation true)) + +(defn dispatch-create-group + [_ [_ group-name]] + (dispatch [:create-new-group group-name])) + +(register-handler :init-group-creation + (after dispatch-create-group) + disable-creat-button) ; todo rewrite (register-handler :group-chat-invite-received diff --git a/src/syng_im/new_group/screen.cljs b/src/syng_im/new_group/screen.cljs index 85a344022b..c551dcca06 100644 --- a/src/syng_im/new_group/screen.cljs +++ b/src/syng_im/new_group/screen.cljs @@ -18,12 +18,14 @@ (defview new-group-toolbar [] - [group-name [:get ::group-name]] + [group-name [:get ::group-name] + creation-disabled? [:get :disable-group-creation]] [toolbar {:title "New group chat" :action {:image {:source res/v ;; {:uri "icon_search"} :style st/toolbar-icon} - :handler #(dispatch [:create-new-group group-name])}}]) + :handler (when-not creation-disabled? + #(dispatch [:init-group-creation group-name]))}}]) (defview group-name-input [] [group-name [:get ::group-name]] From 227f22eb4867ae3a475f57f23bcf4a2433c51971 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Fri, 20 May 2016 19:12:04 +0300 Subject: [PATCH 2/6] chat name/load messages on [:navigate-to :chat id]/ other Former-commit-id: ae1918a2aed109400cda0e376fec6d37cdb5d79f --- .../main/res/drawable-hdpi/icon_ok_purple.png | Bin 0 -> 644 bytes .../main/res/drawable-mdpi/icon_ok_purple.png | Bin 0 -> 504 bytes .../res/drawable-xhdpi/icon_ok_purple.png | Bin 0 -> 931 bytes .../res/drawable-xxhdpi/icon_ok_purple.png | Bin 0 -> 1520 bytes .../res/drawable-xxxhdpi/icon_ok_purple.png | Bin 0 -> 2055 bytes src/syng_im/android/core.cljs | 4 +- src/syng_im/chat/handlers.cljs | 14 ++- .../chats_list/views/chat_list_item.cljs | 2 +- src/syng_im/components/react.cljs | 8 +- src/syng_im/group_settings/handlers.cljs | 95 ++++++++---------- src/syng_im/group_settings/screen.cljs | 93 ++++++++++------- .../group_settings/styles/chat_name_edit.cljs | 19 ---- .../group_settings/styles/group_settings.cljs | 46 ++++----- src/syng_im/group_settings/subs.cljs | 11 +- .../group_settings/views/chat_name_edit.cljs | 31 ------ src/syng_im/group_settings/views/member.cljs | 2 +- src/syng_im/handlers.cljs | 8 +- src/syng_im/navigation/handlers.cljs | 8 +- src/syng_im/new_group/handlers.cljs | 2 +- src/syng_im/new_group/subs.cljs | 8 +- src/syng_im/participants/subs.cljs | 3 + .../views/{create.cljs => add.cljs} | 19 ++-- src/syng_im/participants/views/remove.cljs | 17 ++-- src/syng_im/profile/screen.cljs | 2 +- src/syng_im/subs.cljs | 4 + src/syng_im/utils/subs.cljs | 10 ++ 26 files changed, 190 insertions(+), 216 deletions(-) create mode 100644 android/app/src/main/res/drawable-hdpi/icon_ok_purple.png create mode 100644 android/app/src/main/res/drawable-mdpi/icon_ok_purple.png create mode 100644 android/app/src/main/res/drawable-xhdpi/icon_ok_purple.png create mode 100644 android/app/src/main/res/drawable-xxhdpi/icon_ok_purple.png create mode 100644 android/app/src/main/res/drawable-xxxhdpi/icon_ok_purple.png delete mode 100644 src/syng_im/group_settings/styles/chat_name_edit.cljs delete mode 100644 src/syng_im/group_settings/views/chat_name_edit.cljs create mode 100644 src/syng_im/participants/subs.cljs rename src/syng_im/participants/views/{create.cljs => add.cljs} (66%) create mode 100644 src/syng_im/utils/subs.cljs diff --git a/android/app/src/main/res/drawable-hdpi/icon_ok_purple.png b/android/app/src/main/res/drawable-hdpi/icon_ok_purple.png new file mode 100644 index 0000000000000000000000000000000000000000..b28a5ca6d021ccc79a439897407957341a607a51 GIT binary patch literal 644 zcmV-~0(Px%K1oDDR7efAl+RBSQ4q)9ESQude*he+mtC&}ymBNVrWY!vP$VQY2niv^AU8R& zYm71e02(Z{TOe4YjVF&ilW^_Z3)RHGqCwc_cecE`4aGuj-(jb3_RaURGxIt^ba~|+ z8Bi0H5{Y9_|V>0JA?Z96jt zxsKctN~E+HdUOzpn#p+lGKkLvJwu@3Zpf8V_Gd%~{>w5b?|A7j2)Ax861A=bu^tr) zs;63!IVD5_0pC%A7{2WED%b^R2X;=MLFwG-RdW{B*S==fIIVd10}#JeYOMOh`&25a zrsWMP;K)oPNPN5wh~MIG$~FPx$vPnciR45gtlF2UvaTLej`?XUl5<77u#J(il)pBwY2SO|tHzLGR;ou~+8br9b znIh4PKR{*9E+Up0y9lv6S)0!E`HiGh(?nWx`Tgd-@B8Gv@9zQp<8w`%#)&q$M@L|! zoVDrm_-D`iIuVs4W5W@Fmd7s=RCrwnpfcXdsR2zE7H&tbsmnwSU)?%{cX47i87o?> z*;iq8v^b|E!Hdrg1f>XgBrsOlWX-(}%cB*?VX~^KZXzf_#6ygN%GTce8~;RKqe&7O z{ee)Y4)J=m4IRSs2hq%I354i1%sDNmUv4T}i*~s&6vQ=qoT7R)iF;Cv2#R1fTN(y4 zZiBUqJW$V{un09;a@yp&K!G@UlKTGUl?bR15ck0lWohooybIL$@2qVof1VpRMgT_g z(W+>dYxU&H%vl`Af0000|00001b5ch_0Itp) z=>Px&T}ebiR7efQmQ73(K@`W|n?(gKM$dZTptM{(crh9cYBWUS7eT^D7Zi*!8e8fozW3` z^`O6HRe%!4*gl)X^EAvXDbuv7f-hMtoZQa^4!dCxSX-dI{B=Qr5>e{_fecf=l*|TY zra-{UdWWw|HVU?6X2d#3AWs2Au@iAQLr=i_H;nZcFu7gGnD3>`SGSt;byQ$x8pKCh}Bppm+tdnx-iqjbpzJsRi)~}XN zDxIzaoeQ8uoqKxHKq<0q2!tvIl-*Lw^e&Umda$C6)<5HZ5dt>R@&ER5*VoO`#td7{ zUNF>Ezg!qOeny0(Ad{Qky4}DT+VY{E*UvLTYKF>6Ocj^;PEw>sltzCrEi_iW5-@U(1tX*& zMtdiVr+)o?DFAmOgA;nY|CQ=nwQ~60k5V)8dDlt<{0B+LLJ72M>kR+^002ovPDHLk FV1m&BuO$Eg literal 0 HcmV?d00001 diff --git a/android/app/src/main/res/drawable-xxhdpi/icon_ok_purple.png b/android/app/src/main/res/drawable-xxhdpi/icon_ok_purple.png new file mode 100644 index 0000000000000000000000000000000000000000..b2942242644f2d7dd118965e582153e77c3e271f GIT binary patch literal 1520 zcmVPx)s!2paRA>d=nR`r|RTRMQ?TfE4hA_8GlWy`@W^4RsF<~Y;0|Ud54KSnQAwyye zCL=^P%`p0(Mj*s3KF|=2N|_=I<~GJgbci}1VVR2>>9@qej3nwtr3?mrzW_ zURs($i{C%@-gC}(?)lwwUtdee|KREDWJ+HQg|A?>wc8qo-ms5|U6)#zS|0nV1~WC7 zkGdE+$QUayTgA@?2AE2Mqf%wHm8N>}O-5Wb^0+A_-MVx`aYhE6Ta{p~^+Rt1P_-ON zssqq*-F8Fif<-48L}Lk4j;0Cwni(Mnc=18j6f@VY?VdTwGmI8FLbWxB?=r%kn0_BXrYhwD%j>XF{Gw7Rvr1ps z>IH>1Q3-}n#~Q>BA-)PZh*zDGpV=BkR%!Ltf4$`Mji>`dDA@fzhTX5+U%)7%lwKAz z@!7^|-8k2Vr-+0cFSGVxb1R%vW&Hl>Pbw0ZUG~yuE8ixHrXoVwhQ&4rwuBcjWa?6U zj5fQlcJl$$6NJLE#yXpCtg%m8&cKQPnEO3S|Iny4J4z0vjRxT-pb0|R zhsAF2pT{$XHT5-7CgtsaapN@4CRn)}tg~S_!Ok4#pwoxtFM{$a73hxgTEpLR`BLjR zy)DzTC%}I$_gzW`k`fd1c30gy!?jt0Lg5}s;yCXMg+AbFlg1pZ-_&$1aKLyG?~Gk{+&lgwRY#!De#%i^9d)_ z7n?R}^D`=w?R=ZiG@e~?f1TkqLKUy`+(3ZaY{b!bs_{xZ&qk6Xl+8KSvltYNXy0-{ zVH_SXY*f_gUsh0Z1nYhu#g>+RGjLArpe z7z)el9ykQku30khEnyRQblf^|8rP4n4xy~piC$QjnZ7!y4ZeHV$Epi+YqmHf*>aSg zH70isQ;4=Tii`h<0;8@?(HG$6gYTM}{)r=VVO6X0+^i`nGag8Z~L)ljq&P#6Oj43+K=z+rPc zY}ZBD_V<8;ugnK2$|#c{bseFA9d8;WyLhpx6Z&%nzk=fI0zC z#VZMgaW2?Ae1pfoWP}vG5k{cTR~N8NNf%7jeJc%xaRmfJBU~4{jj@HW_`kw-Fb_`A zP3eXb9GGD2!)+537ADjP)Px+!AV3xRA>e5n+t4HRTzNJIqfzO5S6DO;s#^wCK@z~M$tWaDu~Jywr(hD3^7D` ztX+&I2IJZdh7l!^L}l&DOu_JyhdkD4j7FnIjcYqLM$8mJ0m0$At-a^?pT%45+TPyV z(T#QEHrYMrKmU3B-~G??-XV1Dibw!QOdSM?+cz|f7JR}RNl7Et=C7XQ4K|qs3kFl% z@lN9yzh9`Oh^_Q?w(fIOwhRrnf6xN5Ry2+Ew)vclvFTk<7|UtzY~ADh=;RP7q(KR2 z``gXqkiWx;7|W2t09bh~XWDj4Aq~1Q$WeZBd^>HcWen-1aHL^?a%l9h#K-2ZO6?d_ z9%S3(y4fqVLAR2J!$C2G=ALcyeF7eZ2MwSai)RAsKu)k`LK?nGKPyljN zHcaefv<^HXEgTT3j6*?~tt>XCZ{hL*KL%A>8gHSr(+S;XsuV=HFoy%b7Y(KQtz!Cs z_Mn=LO_K!HS*IQjdO-?@LmIFiWHq0-6|r{iIVqwy$lo!Aq51jZ(u@y-bp|9L`v%u! z!H;0JV#&br+b+vOLjwkoz3h03fcf4E-5P3UcL~f^0>-4+VAR>W zr-a4^#4Pr4dE*p6^1(VsB^-hcYT$5OaSzB=*)+A2cGiI{lej^VF77$7G%hfd=xZYB zNz>xGi`mOvseXZPAJ{~xQQ^X1s2^j1W6E%ZOP7h1qI?oN=O@!y!bo zw9u&ADazE|4?IU14m|lzuzAUqz~Ri|#)GUCuG^8oj=@wuR_0*`#9eSO zd9ui)-xm<}Fc24js?F}{0%ONuwi=^oC&v7k^a+D0eZQiv3Wa{(#kN;??ts@awQAwO z_TlkZ!QuS+wFlYE4R!HZ%IhqpH@AX|C;EI(#We2E?2wdv#y(HIsBU0b>9F7WN6Fk9WE zR32;}yo8{Ig(dpKO1eEs59wmnmiiolqPzyWCF130? z!##dXkE($Kx!MpTkMt%S-m(CoDs%lJ$`GtLS+{r7TnQk}Lc+Pd(pR!v9?Q#aX}FjA zg(FazVwZ+Z=f4Dz1x2MgnB98!>H=gl*XP6Apq=pK66LCc1zL8?>9$=C2b0y2%c~@> zwz%#SsBi?%kVh)%ayYQJS^#e!>XdY2k=_M}A~Fu(Xq5^Cow@DJZimy!6m^x#64R?H z-1k$8J_9ekUX;n*VZgiu>zsK-CHl|hbYd+J4ANC)u659v%GFc=nRX1BlD;%Q9}8;q z<#ejcJr4+!9)w-_2sxdA9OG7uH1ms0X!4;x|%w{la_d!-P@o` z1oErNC)Kf<8y=*Ba7Zm2g7DnFhQr(2%|Lh`y&hrmmedg0Fk^)_i+d|T3V5NcZcu{V z?0N_>|3LsfJSZt73d@XtkOt2&nDk$TB=&)z8z5dEyd!#*U{tB5Zh*4_AZ=EoE9zLw z-46?h9Z&@a$~jJx0SixgH7T}H4TD+b7PS)W37^|-QvmeQ=!{B zOD+#A0xxQ?W_E!w?OQpWzL1A_kXUJCPxxV=Z&N8A-=l=E+tI2bS*_ zpsVLzZ8+Q>Sr6igd=R!;+*J@LC1s6X%GuS^!I(TDD_3I_YlRC zw;1gwTzmLxS}G402G0XEv<3b=2i-F%oQ_lm`e$Jgm}@Yl{Unw5hp=iuys><<3f~d$ z=lil)HJ-!KYDmtqb-JHpRc0*9i8uxYSCsWkay!zDS_orGjF$WOFZegpf%BYVvZ(r*72oP@|KZQAQ1%r#}J>_ zTL*FFFW3?O0~3-3A1^hC`TP65#~vgaw(RAUGl@i#1E^1kvaolIPq9Rt8BcJy9ajv9 z7qZl-JB87ZZ0K&k@_6Wlf7cR}S-eJnJT$F;ORl(!NzLOQQu>kq3)sa4zW9RK>NlL2 zlx0{wwMnWQFX5PYkZ5c{>9l4-NDiN+{+G2RlEln-g~M%#DIi`TA7iu_<-iL02Q5m- le7Ynx^4P>PUAy9!z<+~=w3IT#vVQ;o002ovPDHLkV1kJJ-A@1j literal 0 HcmV?d00001 diff --git a/src/syng_im/android/core.cljs b/src/syng_im/android/core.cljs index a7c6315b1e..3ab9a35ad5 100644 --- a/src/syng_im/android/core.cljs +++ b/src/syng_im/android/core.cljs @@ -12,10 +12,9 @@ [syng-im.chat.screen :refer [chat]] [syng-im.chats-list.screen :refer [chats-list]] [syng-im.new-group.screen :refer [new-group]] - [syng-im.participants.views.create :refer [new-participants]] + [syng-im.participants.views.add :refer [new-participants]] [syng-im.participants.views.remove :refer [remove-participants]] [syng-im.group-settings.screen :refer [group-settings]] - [syng-im.group-settings.views.chat-name-edit :refer [chat-name-edit]] [syng-im.profile.screen :refer [profile my-profile]] [syng-im.utils.utils :refer [toast]] [syng-im.utils.encryption])) @@ -43,7 +42,6 @@ :chat-list [chats-list] :new-group [new-group] :group-settings [group-settings] - :chat-name-edit [chat-name-edit] :contact-list [contact-list] :chat [chat] :profile [profile] diff --git a/src/syng_im/chat/handlers.cljs b/src/syng_im/chat/handlers.cljs index 67acc9a6fa..73f2a2ece6 100644 --- a/src/syng_im/chat/handlers.cljs +++ b/src/syng_im/chat/handlers.cljs @@ -187,7 +187,8 @@ ((enrich add-commands)) ((enrich clear-input)) ((enrich clear-staged-commands)) - ((after send-message!)) + ;; todo uncomment once + ;((after send-message!)) ((after save-message-to-realm!)) ((after save-commands-to-realm!)) ((after handle-commands)))) @@ -283,10 +284,13 @@ (defmethod nav/preload-data! :chat [{:keys [current-chat-id] :as db} [_ _ id]] - (-> db - (assoc :current-chat-id (or id current-chat-id)) - load-messages! - init-chat)) + (let [messages (get-in db [:chats current-chat-id :messages])] + (if (seq messages) + (-> db + (assoc :current-chat-id (or id current-chat-id)) + load-messages! + init-chat) + db))) (defn prepare-chat [{:keys [contacts] :as db} [_ contcat-id]] 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 3673701ccc..ddc6c87b9b 100644 --- a/src/syng_im/chats_list/views/chat_list_item.cljs +++ b/src/syng_im/chats_list/views/chat_list_item.cljs @@ -10,7 +10,7 @@ (defn chat-list-item [{:keys [chat-id] :as chat}] [touchable-highlight - {:on-press #(dispatch [:show-chat chat-id :push])} + {:on-press #(dispatch [:navigate-to :chat chat-id])} ;; TODO add [photo-path delivery-status new-messages-count online] values to chat-obj [view [chat-list-item-inner-view (merge chat {:photo-path nil diff --git a/src/syng_im/components/react.cljs b/src/syng_im/components/react.cljs index 58c99aa6e4..e3f1289e00 100644 --- a/src/syng_im/components/react.cljs +++ b/src/syng_im/components/react.cljs @@ -35,9 +35,11 @@ (def picker-item (r/adapt-react-class (.-Item (.-Picker js/React)))) -(defn icon [n style] - [image {:source {:uri (keyword (str "icon_" (name n)))} - :style style}]) +(defn icon + ([n] (icon n {})) + ([n style] + [image {:source {:uri (keyword (str "icon_" (name n)))} + :style style}])) (def platform (.. js/React -Platform -OS)) diff --git a/src/syng_im/group_settings/handlers.cljs b/src/syng_im/group_settings/handlers.cljs index 0871624cb1..58dbda4067 100644 --- a/src/syng_im/group_settings/handlers.cljs +++ b/src/syng_im/group_settings/handlers.cljs @@ -1,72 +1,59 @@ (ns syng-im.group-settings.handlers - (:require [re-frame.core :refer [register-handler debug dispatch]] + (:require [re-frame.core :refer [register-handler debug dispatch after]] [syng-im.persistence.realm :as r] - [syng-im.models.messages :refer [clear-history]])) + [syng-im.models.messages :refer [clear-history]] + [clojure.string :as s])) -(defn set-chat-name [db] - (let [chat-id (:current-chat-id db) - name (:new-chat-name db)] - (r/write (fn [] - (-> (r/get-by-field :chats :chat-id chat-id) - (r/single) - (aset "name" name)))) - (assoc-in db [:chats chat-id :name] name))) +(defn save-chat-property! + [db-name property-name] + (fn [{:keys [current-chat-id] :as db} _] + (let [property (db-name db)] + (r/write (fn [] + (-> (r/get-by-field :chats :chat-id current-chat-id) + (r/single) + (aset (name property-name) property))))))) -(defn set-chat-color [db] - (let [chat-id (:current-chat-id db) - color (:new-chat-color db)] - (r/write (fn [] - (-> (r/get-by-field :chats :chat-id chat-id) - (r/single) - (aset "color" color)))) - (assoc-in db [:chats chat-id :color] color))) +(defn update-chat-property + [db-name property-name] + (fn [{:keys [current-chat-id] :as db} _] + (let [property (db-name db)] + (assoc-in db [:chats current-chat-id property-name] property)))) (defn delete-chat [chat-id] (r/write - (fn [] - (-> (r/get-by-field :chats :chat-id chat-id) - (r/single) - (r/delete)))) + (fn [] + (-> (r/get-by-field :chats :chat-id chat-id) + (r/single) + (r/delete)))) ;; TODO temp. Update chat in db atom (dispatch [:initialize-chats])) +(defn prepare-chat-settings + [{:keys [current-chat-id] :as db} _] + (let [{:keys [name color]} (-> db + (get-in [:chats current-chat-id]) + (select-keys [:name :color]))] + (-> db + (assoc :new-chat-name name + :new-chat-color color + :group-settings {})))) + (register-handler :show-group-settings - (fn [db _] - (let [chat-id (:current-chat-id db) - chat-name (get-in db [:chats chat-id :name]) - chat-color (get-in db [:chats chat-id :color]) - db (assoc db - :new-chat-name chat-name - :new-chat-color chat-color - :group-settings-show-color-picker false - :group-settings-selected-member nil)] - (dispatch [:navigate-to :group-settings]) - db))) + (after (fn [_ _] (dispatch [:navigate-to :group-settings]))) + prepare-chat-settings) (register-handler :set-chat-name - (fn [db [action]] - (set-chat-name db))) + (after (save-chat-property! :new-chat-name :name)) + (update-chat-property :new-chat-name :name)) (register-handler :set-chat-color - (fn [db [action]] - (set-chat-color db))) - -(register-handler :set-new-chat-name - (fn [db [action name]] - (assoc db :new-chat-name name))) - -(register-handler :set-new-chat-color - (fn [db [action color]] - (assoc db :new-chat-color color))) - -(register-handler :select-group-chat-member - (fn [db [action identity]] - (assoc db :group-settings-selected-member identity))) - -(register-handler :set-group-settings-show-color-picker - (fn [db [action show?]] - (assoc db :group-settings-show-color-picker show?))) + (after (save-chat-property! :new-chat-color :color)) + (update-chat-property :new-chat-color :color)) (register-handler :clear-history - (fn [db [action]] + (fn [db _] (clear-history (:current-chat-id db)))) + +(register-handler :group-settings + (fn [db [_ k v]] + (assoc-in db [:group-settings k] v))) diff --git a/src/syng_im/group_settings/screen.cljs b/src/syng_im/group_settings/screen.cljs index 7e06fd98af..c74c74c020 100644 --- a/src/syng_im/group_settings/screen.cljs +++ b/src/syng_im/group_settings/screen.cljs @@ -13,13 +13,14 @@ touchable-highlight]] [syng-im.components.toolbar :refer [toolbar]] [syng-im.group-settings.styles.group-settings :as st] - [syng-im.group-settings.views.member :refer [member-view]])) + [syng-im.group-settings.views.member :refer [member-view]] + [clojure.string :as s])) (defn remove-member [{:keys [whisper-identity]}] (dispatch [:chat-remove-member whisper-identity])) (defn close-member-menu [] - (dispatch [:select-group-chat-member nil])) + (dispatch [:set :group-settings-selected-member nil])) (defview member-menu [] [member [:group-settings-selected-member]] @@ -58,15 +59,15 @@ subtitle])]]]) (defn close-chat-color-picker [] - (dispatch [:set-group-settings-show-color-picker false])) + (dispatch [:group-settings :show-color-picker false])) (defn set-chat-color [] (close-chat-color-picker) (dispatch [:set-chat-color])) (defview chat-color-picker [] - [show-color-picker [:get :group-settings-show-color-picker] - new-color [:get :new-chat-color]] + [show-color-picker [:group-settings :show-color-picker] + new-color [:get :new-chat-color]] [modal {:animated false :transparent false :onRequestClose close-chat-color-picker} @@ -74,11 +75,11 @@ :on-press close-chat-color-picker} [view st/modal-color-picker-inner-container [picker {:selectedValue new-color - :onValueChange #(dispatch [:set-new-chat-color %])} - [picker-item {:label "Blue" :value "#7099e6"}] - [picker-item {:label "Purple" :value "#a187d5"}] - [picker-item {:label "Green" :value "green"}] - [picker-item {:label "Red" :value "red"}]] + :onValueChange #(dispatch [:set :new-chat-color %])} + [picker-item {:label "Blue" :value "#7099e6"}] + [picker-item {:label "Purple" :value "#a187d5"}] + [picker-item {:label "Green" :value "green"}] + [picker-item {:label "Red" :value "red"}]] [touchable-highlight {:on-press set-chat-color} [text {:style st/modal-color-picker-save-btn-text} "Save"]]]]]) @@ -88,7 +89,7 @@ [view {:style (st/chat-color-icon chat-color)}]) (defn show-chat-color-picker [] - (dispatch [:set-group-settings-show-color-picker true])) + (dispatch [:group-settings :show-color-picker true])) (defn settings-view [] ;; TODO implement settings handlers @@ -105,22 +106,22 @@ {:icon :muted :icon-style {:width 18 :height 21}})) - {:icon :close-gray - :icon-style {:width 12 - :height 12} - :title "Clear history" - :handler #(dispatch [:clear-history])} - {:icon :bin - :icon-style {:width 12 - :height 18} - :title "Delete and leave" - :handler #(dispatch [:leave-group-chat])}]] + {:icon :close-gray + :icon-style {:width 12 + :height 12} + :title "Clear history" + :handler #(dispatch [:clear-history])} + {:icon :bin + :icon-style {:width 12 + :height 18} + :title "Delete and leave" + :handler #(dispatch [:leave-group-chat])}]] [view st/settings-container (for [setting settings] ^{:key setting} [setting-view setting])])) (defview chat-icon [] - [name [:chat :name] + [name [:chat :name] color [:chat :color]] [view (st/chat-icon color) [text {:style st/chat-icon-text} (first name)]]) @@ -129,24 +130,44 @@ [toolbar {:title "Chat settings" :custom-action [chat-icon]}]) +(defn focus [] + (dispatch [:set ::name-input-focused true])) + +(defn blur [] + (dispatch [:set ::name-input-focused false])) + +(defn save [] + (dispatch [:set-chat-name])) + +(defview chat-name [] + [name [:chat :name] + new-name [:get :new-chat-name] + focused? [:get ::name-input-focused]] + [view + [text {:style st/chat-name-text} "Chat name"] + [view (st/chat-name-value-container focused?) + [text-input {:style st/chat-name-value + :ref #(when (and % focused?) (.focus %)) + :on-change-text #(dispatch [:set :new-chat-name %]) + :on-focus focus + :on-blur blur} + name] + (if (or focused? (not= name new-name)) + [touchable-highlight {:style st/chat-name-btn-edit-container + :on-press save} + [view [icon :ok-purple st/add-members-icon]]] + [touchable-highlight {:style st/chat-name-btn-edit-container + :on-press focus} + [text {:style st/chat-name-btn-edit-text} "Edit"]])]]) + (defview group-settings [] - [chat-name [:chat :name] - selected-member [:group-settings-selected-member] - show-color-picker [:get :group-settings-show-color-picker]] + [selected-member [:group-settings-selected-member] + show-color-picker [:group-settings :show-color-picker]] [view st/group-settings [new-group-toolbar] [scroll-view st/body - [text {:style st/chat-name-text} - "Chat name"] - [view st/chat-name-value-container - [text {:style st/chat-name-value} - chat-name] - [touchable-highlight {:style st/chat-name-btn-edit-container - :on-press show-chat-name-edit} - [text {:style st/chat-name-btn-edit-text} - "Edit"]]] - [text {:style st/members-text} - "Members"] + [chat-name] + [text {:style st/members-text} "Members"] [touchable-highlight {:on-press #(dispatch [:show-add-participants])} [view st/add-members-container [icon :add-gray st/add-members-icon] diff --git a/src/syng_im/group_settings/styles/chat_name_edit.cljs b/src/syng_im/group_settings/styles/chat_name_edit.cljs deleted file mode 100644 index 0a5afb59d3..0000000000 --- a/src/syng_im/group_settings/styles/chat_name_edit.cljs +++ /dev/null @@ -1,19 +0,0 @@ -(ns syng-im.group-settings.styles.chat-name-edit - (:require [syng-im.components.styles :refer [font - color-white - text1-color]])) - -(def save-action-icon - {:width 18 - :height 14}) - -(def chat-name-container - {:flex 1 - :flexDirection :column - :backgroundColor color-white}) - -(def chat-name-input - {:marginLeft 12 - :fontSize 14 - :fontFamily font - :color text1-color}) diff --git a/src/syng_im/group_settings/styles/group_settings.cljs b/src/syng_im/group_settings/styles/group_settings.cljs index 092ab7387e..118caf7121 100644 --- a/src/syng_im/group_settings/styles/group_settings.cljs +++ b/src/syng_im/group_settings/styles/group_settings.cljs @@ -24,17 +24,17 @@ :backgroundColor color-white}) (def modal-member-name - {:color text2-color - :fontFamily font - :fontSize 14 - :lineHeight 20}) + {:color text2-color + :fontFamily font + :fontSize 14 + :lineHeight 20}) (def modal-remove-text - {:margin 10 - :color text1-color - :fontFamily font - :fontSize 14 - :lineHeight 20}) + {:margin 10 + :color text1-color + :fontFamily font + :fontSize 14 + :lineHeight 20}) (def modal-color-picker-inner-container {:borderRadius 10 @@ -42,12 +42,12 @@ :backgroundColor color-white}) (def modal-color-picker-save-btn-text - {:margin 10 - :alignSelf :center - :color text1-color - :fontFamily font - :fontSize 14 - :lineHeight 20}) + {:margin 10 + :alignSelf :center + :color text1-color + :fontFamily font + :fontSize 14 + :lineHeight 20}) (def chat-members-container {:marginBottom 10}) @@ -85,14 +85,14 @@ :fontSize 14 :lineHeight 20}) -(def chat-name-value-container +(defn chat-name-value-container [focused?] {:flexDirection :row :marginLeft 16 :height 56 :alignItems :center :justifyContent :center - :borderBottomWidth 1 - :borderBottomColor separator-color}) + :borderBottomWidth 2 + :borderBottomColor (if focused? color-purple separator-color)}) (def chat-name-value {:flex 1 @@ -105,11 +105,11 @@ :justifyContent :center}) (def chat-name-btn-edit-text - {:marginTop -1 - :color text2-color - :fontFamily font - :fontSize 16 - :lineHeight 20}) + {:marginTop -1 + :color text2-color + :fontFamily font + :fontSize 16 + :lineHeight 20}) (def members-text {:marginTop 24 diff --git a/src/syng_im/group_settings/subs.cljs b/src/syng_im/group_settings/subs.cljs index 8ef774b5f6..844a7088af 100644 --- a/src/syng_im/group_settings/subs.cljs +++ b/src/syng_im/group_settings/subs.cljs @@ -1,14 +1,13 @@ (ns syng-im.group-settings.subs (:require-macros [reagent.ratom :refer [reaction]]) - (:require [re-frame.core :refer [register-sub]] - [syng-im.models.contacts :refer [contact-by-identity]])) + (:require [re-frame.core :refer [register-sub]])) (register-sub :group-settings-selected-member (fn [db [_]] (reaction (let [identity (get @db :group-settings-selected-member)] - (contact-by-identity identity))))) + (get-in @db [:contacts identity]))))) -(register-sub :group-settings-show-color-picker - (fn [db [_]] - (reaction (get @db :group-settings-show-color-picker)))) +(register-sub :group-settings + (fn [db [_ k]] + (reaction (get-in @db [:group-settings k])))) diff --git a/src/syng_im/group_settings/views/chat_name_edit.cljs b/src/syng_im/group_settings/views/chat_name_edit.cljs deleted file mode 100644 index 7ba34e6dd7..0000000000 --- a/src/syng_im/group_settings/views/chat_name_edit.cljs +++ /dev/null @@ -1,31 +0,0 @@ -(ns syng-im.group-settings.views.chat-name-edit - (:require-macros [syng-im.utils.views :refer [defview]]) - (:require [reagent.core :as r] - [re-frame.core :refer [subscribe dispatch dispatch-sync]] - [syng-im.components.react :refer [view text-input]] - [syng-im.components.toolbar :refer [toolbar]] - [syng-im.group-settings.styles.chat-name-edit :as st] - [syng-im.components.styles :refer [toolbar-background2 - text2-color]])) - -(defn save-group-chat-name [] - (dispatch [:set-chat-name]) - (dispatch [:navigate-back])) - -(defn chat-name-edit-toolbar [chat-name] - [toolbar {:background-color toolbar-background2 - :title "Edit chat name" - ;; TODO change to dark 'ok' icon - :action {:image {:source {:uri :icon_ok} - :style st/save-action-icon} - :handler save-group-chat-name}}]) - -(defview chat-name-edit [] - [new-chat-name [:get :new-chat-name]] - [view st/chat-name-container - [chat-name-edit-toolbar] - [text-input {:style st/chat-name-input - :autoFocus true - :placeholderTextColor text2-color - :onChangeText #(dispatch [:set-new-chat-name %])} - new-chat-name]]) diff --git a/src/syng_im/group_settings/views/member.cljs b/src/syng_im/group_settings/views/member.cljs index 6584724755..593a8c7eab 100644 --- a/src/syng_im/group_settings/views/member.cljs +++ b/src/syng_im/group_settings/views/member.cljs @@ -38,6 +38,6 @@ [text {:style st/role-text} role])] [touchable-highlight - {:on-press #(dispatch [:select-group-chat-member whisper-identity])} + {:on-press #(dispatch [:set :group-settings-selected-member whisper-identity])} [view st/more-btn [icon :more-vertical st/more-btn-icon]]]]) diff --git a/src/syng_im/handlers.cljs b/src/syng_im/handlers.cljs index 5ca72c1d61..3123cf44b6 100644 --- a/src/syng_im/handlers.cljs +++ b/src/syng_im/handlers.cljs @@ -63,6 +63,11 @@ (fn [db [_ k v]] (assoc db k v)))) +(register-handler :set-in + (debug + (fn [db [_ path v]] + (assoc-in db path v)))) + (register-handler :initialize-db (fn [_ _] (assoc app-db @@ -279,12 +284,13 @@ (dispatch [:initialize-chats]) db)) + (register-handler :chat-remove-member (fn [db [action]] (let [chat-id (:current-chat-id db) identity (:group-settings-selected-member db) db (chat-remove-member db)] - (dispatch [:select-group-chat-member nil]) + (dispatch [:set :group-settings-selected-member nil]) ;; TODO fix and uncomment (api/group-remove-participant chat-id identity) (removed-participant-msg chat-id identity) diff --git a/src/syng_im/navigation/handlers.cljs b/src/syng_im/navigation/handlers.cljs index 36dd8a768d..6166d13ec6 100644 --- a/src/syng_im/navigation/handlers.cljs +++ b/src/syng_im/navigation/handlers.cljs @@ -27,6 +27,7 @@ (push-view db view-id))) (register-handler :navigation-replace + (enrich preload-data!) (fn [db [_ view-id]] (replace-view db view-id))) @@ -46,13 +47,6 @@ (push-view :new-group) (assoc :new-group #{}))))) -(register-handler :show-chat - (fn [db [_ chat-id nav-type]] - (let [update-view-id-fn (if (= :replace nav-type) replace-view push-view)] - (-> db - (update-view-id-fn :chat) - (assoc :current-chat-id chat-id))))) - (register-handler :show-contacts (fn [db _] (push-view db :contact-list))) diff --git a/src/syng_im/new_group/handlers.cljs b/src/syng_im/new_group/handlers.cljs index 3823eb1c2e..949466b268 100644 --- a/src/syng_im/new_group/handlers.cljs +++ b/src/syng_im/new_group/handlers.cljs @@ -56,7 +56,7 @@ (defn show-chat! [{:keys [new-group-id]} _] - (dispatch [:show-chat new-group-id :replace])) + (dispatch [:navigation-replace :chat new-group-id])) (defn enable-creat-buttion [db _] diff --git a/src/syng_im/new_group/subs.cljs b/src/syng_im/new_group/subs.cljs index 62fc941747..fd33058b1e 100644 --- a/src/syng_im/new_group/subs.cljs +++ b/src/syng_im/new_group/subs.cljs @@ -1,9 +1,7 @@ (ns syng-im.new-group.subs (:require-macros [reagent.ratom :refer [reaction]]) - (:require [re-frame.core :refer [register-sub]])) + (:require [re-frame.core :refer [register-sub]] + [syng-im.utils.subs :as u])) (register-sub :is-contact-selected? - (fn [db [_ id]] - (-> (:selected-contacts @db) - (contains? id) - (reaction)))) + (u/contains-sub :selected-contacts)) diff --git a/src/syng_im/participants/subs.cljs b/src/syng_im/participants/subs.cljs new file mode 100644 index 0000000000..810a9ff3f6 --- /dev/null +++ b/src/syng_im/participants/subs.cljs @@ -0,0 +1,3 @@ +(ns syng-im.participants.subs) + +() diff --git a/src/syng_im/participants/views/create.cljs b/src/syng_im/participants/views/add.cljs similarity index 66% rename from src/syng_im/participants/views/create.cljs rename to src/syng_im/participants/views/add.cljs index 20a84dfbfa..826579fcd1 100644 --- a/src/syng_im/participants/views/create.cljs +++ b/src/syng_im/participants/views/add.cljs @@ -1,4 +1,5 @@ -(ns syng-im.participants.views.create +(ns syng-im.participants.views.add + (:require-macros [syng-im.utils.views :refer [defview]]) (:require [re-frame.core :refer [subscribe dispatch]] [syng-im.resources :as res] [syng-im.components.react :refer [view list-view list-item]] @@ -19,12 +20,10 @@ [row _ _] (list-item [participant-contact row])) -(defn new-participants [] - (let [contacts (subscribe [:all-new-contacts])] - (fn [] - (let [contacts-ds (to-datasource @contacts)] - [view st/participants-container - [new-participants-toolbar] - [list-view {:dataSource contacts-ds - :renderRow new-participants-row - :style st/participants-list}]])))) +(defview new-participants [] + [contacts [:all-new-contacts]] + [view st/participants-container + [new-participants-toolbar] + [list-view {:dataSource (to-datasource contacts) + :renderRow new-participants-row + :style st/participants-list}]]) diff --git a/src/syng_im/participants/views/remove.cljs b/src/syng_im/participants/views/remove.cljs index 40ff3528f7..32cbd1882e 100644 --- a/src/syng_im/participants/views/remove.cljs +++ b/src/syng_im/participants/views/remove.cljs @@ -1,4 +1,5 @@ (ns syng-im.participants.views.remove + (:require-macros [syng-im.utils.views :refer [defview]]) (:require [re-frame.core :refer [subscribe dispatch]] [syng-im.resources :as res] [syng-im.components.react :refer [view text-input text image @@ -22,12 +23,10 @@ [row _ _] (r/as-element [participant-contact row])) -(defn remove-participants [] - (let [contacts (subscribe [:current-chat-contacts])] - (fn [] - (let [contacts-ds (to-datasource @contacts)] - [view st/participants-container - [remove-participants-toolbar] - [list-view {:dataSource contacts-ds - :renderRow remove-participants-row - :style st/participants-list}]])))) +(defview remove-participants [] + [contacts [:current-chat-contacts]] + [view st/participants-container + [remove-participants-toolbar] + [list-view {:dataSource (to-datasource contacts) + :renderRow remove-participants-row + :style st/participants-list}]]) diff --git a/src/syng_im/profile/screen.cljs b/src/syng_im/profile/screen.cljs index 9d075c4999..00f36886ed 100644 --- a/src/syng_im/profile/screen.cljs +++ b/src/syng_im/profile/screen.cljs @@ -32,7 +32,7 @@ (defn message-user [identity] (when identity - (dispatch [:show-chat identity :push]))) + (dispatch [:navigate-to :chat identity]))) (defview profile [] [{:keys [name whisper-identity phone-number]} [:contact]] diff --git a/src/syng_im/subs.cljs b/src/syng_im/subs.cljs index 055e3e8760..6964ed715a 100644 --- a/src/syng_im/subs.cljs +++ b/src/syng_im/subs.cljs @@ -10,3 +10,7 @@ (register-sub :get (fn [db [_ k]] (reaction (k @db)))) + +(register-sub :get-in + (fn [db [_ path]] + (reaction (get-in @db path)))) diff --git a/src/syng_im/utils/subs.cljs b/src/syng_im/utils/subs.cljs new file mode 100644 index 0000000000..1ea1b7d0ee --- /dev/null +++ b/src/syng_im/utils/subs.cljs @@ -0,0 +1,10 @@ +(ns syng-im.utils.subs + (:require-macros [reagent.ratom :refer [reaction]])) + +(defn contains-sub + "Creates subscrition that cheks if collection (map or set) contains element" + [collection] + (fn [db [_ element]] + (-> (collection @db) + (contains? element) + (reaction)))) From 97b4ba8653652a616219f7a810ef464fa47d397e Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Fri, 20 May 2016 19:58:07 +0300 Subject: [PATCH 3/6] add/remove participants Former-commit-id: cd45d3420e74a49312468eb0c7822487492c257f --- src/syng_im/chat/screen.cljs | 8 +++---- src/syng_im/group_settings/handlers.cljs | 3 +-- src/syng_im/group_settings/screen.cljs | 2 +- src/syng_im/handlers.cljs | 5 ++-- src/syng_im/navigation/handlers.cljs | 15 ------------ src/syng_im/new_group/views/contact.cljs | 1 - src/syng_im/participants/handlers.cljs | 23 ++++++++++++++++++ src/syng_im/participants/subs.cljs | 8 +++++-- src/syng_im/participants/views/contact.cljs | 26 ++++++++++++--------- src/syng_im/subs.cljs | 3 ++- 10 files changed, 55 insertions(+), 39 deletions(-) create mode 100644 src/syng_im/participants/handlers.cljs diff --git a/src/syng_im/chat/screen.cljs b/src/syng_im/chat/screen.cljs index dd355b4223..1541c7b5f7 100644 --- a/src/syng_im/chat/screen.cljs +++ b/src/syng_im/chat/screen.cljs @@ -67,8 +67,8 @@ (defn on-action-selected [position] (case position - 0 (dispatch [:show-add-participants]) - 1 (dispatch [:show-remove-participants]) + 0 (dispatch [:navigate-to :add-participants]) + 1 (dispatch [:navigate-to :remove-participants]) 2 (dispatch [:leave-group-chat]))) (defn overlay [{:keys [on-click-outside]} items] @@ -119,13 +119,13 @@ :icon :menu_group :icon-style {:width 25 :height 19} - :handler #(dispatch [:show-add-participants])} + :handler #(dispatch [:navigate-to :add-participants])} {:title "Remove Contact from chat" :subtitle "Alex, John" :icon :search_gray_copy :icon-style {:width 17 :height 17} - :handler #(dispatch [:show-remove-participants])} + :handler #(dispatch [:navigate-to :remove-participants])} {:title "Leave Chat" :icon :muted :icon-style {:width 18 diff --git a/src/syng_im/group_settings/handlers.cljs b/src/syng_im/group_settings/handlers.cljs index 58dbda4067..71247222ec 100644 --- a/src/syng_im/group_settings/handlers.cljs +++ b/src/syng_im/group_settings/handlers.cljs @@ -1,8 +1,7 @@ (ns syng-im.group-settings.handlers (:require [re-frame.core :refer [register-handler debug dispatch after]] [syng-im.persistence.realm :as r] - [syng-im.models.messages :refer [clear-history]] - [clojure.string :as s])) + [syng-im.models.messages :refer [clear-history]])) (defn save-chat-property! [db-name property-name] diff --git a/src/syng_im/group_settings/screen.cljs b/src/syng_im/group_settings/screen.cljs index c74c74c020..3debc05c29 100644 --- a/src/syng_im/group_settings/screen.cljs +++ b/src/syng_im/group_settings/screen.cljs @@ -168,7 +168,7 @@ [scroll-view st/body [chat-name] [text {:style st/members-text} "Members"] - [touchable-highlight {:on-press #(dispatch [:show-add-participants])} + [touchable-highlight {:on-press #(dispatch [:navigate-to :add-participants])} [view st/add-members-container [icon :add-gray st/add-members-icon] [text {:style st/add-members-text} diff --git a/src/syng_im/handlers.cljs b/src/syng_im/handlers.cljs index 3123cf44b6..327e336144 100644 --- a/src/syng_im/handlers.cljs +++ b/src/syng_im/handlers.cljs @@ -38,9 +38,10 @@ syng-im.chat.handlers [syng-im.group-settings.handlers :refer [delete-chat]] syng-im.navigation.handlers - syng-im.discovery.handlers syng-im.contacts.handlers - syng-im.new-group.handlers)) + syng-im.discovery.handlers + syng-im.new-group.handlers + syng-im.participants.handlers)) ;; -- Middleware ------------------------------------------------------------ ;; diff --git a/src/syng_im/navigation/handlers.cljs b/src/syng_im/navigation/handlers.cljs index 6166d13ec6..f385485298 100644 --- a/src/syng_im/navigation/handlers.cljs +++ b/src/syng_im/navigation/handlers.cljs @@ -51,21 +51,6 @@ (fn [db _] (push-view db :contact-list))) -(defn clear-new-participants [db] - (assoc db :new-participants #{})) - -(register-handler :show-remove-participants - (fn [db _] - (-> db - (push-view :remove-participants) - clear-new-participants))) - -(register-handler :show-add-participants - (fn [db _] - (-> db - (push-view :add-participants) - clear-new-participants))) - (defn show-profile [db [_ identity]] (-> db diff --git a/src/syng_im/new_group/views/contact.cljs b/src/syng_im/new_group/views/contact.cljs index 6701e9fafc..e861bf02ba 100644 --- a/src/syng_im/new_group/views/contact.cljs +++ b/src/syng_im/new_group/views/contact.cljs @@ -8,7 +8,6 @@ (defn on-toggle [whisper-identity] (fn [checked?] - (println checked?) (let [action (if checked? :select-contact :deselect-contact)] (dispatch [action whisper-identity])))) diff --git a/src/syng_im/participants/handlers.cljs b/src/syng_im/participants/handlers.cljs new file mode 100644 index 0000000000..a2b71cf64d --- /dev/null +++ b/src/syng_im/participants/handlers.cljs @@ -0,0 +1,23 @@ +(ns syng-im.participants.handlers + (:require [syng-im.navigation.handlers :as nav] + [re-frame.core :refer [register-handler debug]])) + +(defmethod nav/preload-data! :add-participants + [db _] + (assoc db :new-participants #{})) + +(defmethod nav/preload-data! :remove-participants + [db _] + (assoc db :new-participants #{})) + +(defn deselect-participant + [db [_ id]] + (update db :new-participants disj id)) + +(register-handler :deselect-participant deselect-participant) + +(defn select-participant + [db [_ id]] + (update db :new-participants conj id)) + +(register-handler :select-participant (debug select-participant)) diff --git a/src/syng_im/participants/subs.cljs b/src/syng_im/participants/subs.cljs index 810a9ff3f6..c6e53a8af1 100644 --- a/src/syng_im/participants/subs.cljs +++ b/src/syng_im/participants/subs.cljs @@ -1,3 +1,7 @@ -(ns syng-im.participants.subs) +(ns syng-im.participants.subs + (:require-macros [reagent.ratom :refer [reaction]]) + (:require [re-frame.core :refer [register-sub]] + [syng-im.utils.subs :as u])) -() +(register-sub :is-participant-selected? + (u/contains-sub :new-participants)) diff --git a/src/syng_im/participants/views/contact.cljs b/src/syng_im/participants/views/contact.cljs index f193f24deb..4adbf51ba3 100644 --- a/src/syng_im/participants/views/contact.cljs +++ b/src/syng_im/participants/views/contact.cljs @@ -1,4 +1,5 @@ (ns syng-im.participants.views.contact + (:require-macros [syng-im.utils.views :refer [defview]]) (:require [re-frame.core :refer [subscribe dispatch dispatch-sync]] [syng-im.components.react :refer [view]] [syng-im.contacts.views.contact-inner :refer [contact-inner-view]] @@ -6,14 +7,17 @@ [reagent.core :as r] [syng-im.participants.styles :as st])) -(defn participant-contact [{:keys [whisper-identity] :as contact}] - ;; todo must be moved to handlers - (let [checked (r/atom false)] - (fn [{:keys [whisper-identity] :as contact}] - [view st/participant-container - [item-checkbox {:onToggle (fn [checked?] - (reset! checked checked?) - (dispatch [:select-new-participant whisper-identity checked?])) - :checked @checked - :size 30}] - [contact-inner-view contact]]))) +;; todo duplication +(defn on-toggle [whisper-identity] + (fn [checked?] + (let [action (if checked? :select-participant :deselect-participant)] + (dispatch [action whisper-identity])))) + +(defview participant-contact + [{:keys [whisper-identity] :as contact}] + [checked [:is-participant-selected? whisper-identity]] + [view st/participant-container + [item-checkbox {:onToggle (on-toggle whisper-identity) + :checked checked + :size 30}] + [contact-inner-view contact]]) diff --git a/src/syng_im/subs.cljs b/src/syng_im/subs.cljs index 6964ed715a..b2cf4af3bc 100644 --- a/src/syng_im/subs.cljs +++ b/src/syng_im/subs.cljs @@ -5,7 +5,8 @@ syng-im.group-settings.subs syng-im.discovery.subs syng-im.contacts.subs - syng-im.new-group.subs)) + syng-im.new-group.subs + syng-im.participants.subs)) (register-sub :get (fn [db [_ k]] From c086162f6810d8820307ed58d88ebbfda1205e1c Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Sat, 21 May 2016 15:23:24 +0300 Subject: [PATCH 4/6] leave group/ clear history/ add-remove participants Former-commit-id: 01d1ccdab893664cfdf0005b877771c53b107aa7 --- src/syng_im/chat/handlers.cljs | 45 +++- src/syng_im/db.cljs | 2 +- src/syng_im/group_settings/handlers.cljs | 112 ++++++++-- src/syng_im/group_settings/screen.cljs | 39 ++-- src/syng_im/group_settings/subs.cljs | 4 +- src/syng_im/group_settings/views/member.cljs | 2 +- src/syng_im/handlers.cljs | 217 +------------------ src/syng_im/models/messages.cljs | 7 - src/syng_im/navigation.cljs | 17 -- src/syng_im/navigation/handlers.cljs | 5 +- src/syng_im/participants/handlers.cljs | 8 +- src/syng_im/participants/subs.cljs | 2 +- src/syng_im/participants/views/add.cljs | 9 +- src/syng_im/participants/views/remove.cljs | 9 +- src/syng_im/protocol/handlers.cljs | 115 ++++++++++ 15 files changed, 302 insertions(+), 291 deletions(-) delete mode 100644 src/syng_im/navigation.cljs create mode 100644 src/syng_im/protocol/handlers.cljs diff --git a/src/syng_im/chat/handlers.cljs b/src/syng_im/chat/handlers.cljs index 73f2a2ece6..e6371a55d5 100644 --- a/src/syng_im/chat/handlers.cljs +++ b/src/syng_im/chat/handlers.cljs @@ -11,7 +11,8 @@ [syng-im.chat.sign-up :as sign-up-service] [syng-im.models.chats :as chats] [syng-im.navigation.handlers :as nav] - [syng-im.utils.handlers :as u])) + [syng-im.utils.handlers :as u] + [syng-im.persistence.realm :as r])) (register-handler :set-show-actions (fn [db [_ show-actions]] @@ -325,3 +326,45 @@ (register-handler :switch-command-suggestions (fn [db [_]] (suggestions/switch-command-suggestions db))) + +(defn remove-chat + [{:keys [current-chat-id] :as db} _] + (update db :chats dissoc current-chat-id)) + +(defn notify-about-leaving! + [{:keys [current-chat-id]} _] + (api/leave-group-chat current-chat-id)) + +; todo do we really need this message? +(defn leaving-message! + [{:keys [current-chat-id]} _] + (messages/save-message + current-chat-id + {:from "system" + :msg-id (random/id) + :content "You left this chat" + :content-type text-content-type})) + +(defn delete-messages! + [{:keys [current-chat-id]} _] + (r/write + (fn [] + (r/delete (r/get-by-field :msgs :chat-id current-chat-id))))) + +(defn delete-chat! + [{:keys [current-chat-id]} _] + (r/write + (fn [] + (-> (r/get-by-field :chats :chat-id current-chat-id) + (r/single) + (r/delete))))) + +(register-handler :leave-group-chat + ;; todo oreder of operations tbd + (after (fn [_ _] (dispatch [:navigation-replace :chat-list]))) + (-> remove-chat + ;; todo uncomment + ;((after notify-about-leaving!)) + ;((after leaving-message!)) + ((after delete-messages!)) + ((after delete-chat!)))) diff --git a/src/syng_im/db.cljs b/src/syng_im/db.cljs index fa814e08a2..edab7d93b6 100644 --- a/src/syng_im/db.cljs +++ b/src/syng_im/db.cljs @@ -18,7 +18,7 @@ :chats {} :chats-updated-signal 0 :show-actions false - :new-participants #{} + :selected-participants #{} :signed-up true :view-id default-view :navigation-stack (list default-view) diff --git a/src/syng_im/group_settings/handlers.cljs b/src/syng_im/group_settings/handlers.cljs index 71247222ec..d2d64d62d6 100644 --- a/src/syng_im/group_settings/handlers.cljs +++ b/src/syng_im/group_settings/handlers.cljs @@ -1,7 +1,20 @@ (ns syng-im.group-settings.handlers - (:require [re-frame.core :refer [register-handler debug dispatch after]] + (:require [re-frame.core :refer [register-handler debug dispatch after + enrich]] [syng-im.persistence.realm :as r] - [syng-im.models.messages :refer [clear-history]])) + [syng-im.chat.handlers :refer [delete-messages!]] + [syng-im.protocol.api :as api] + [syng-im.utils.random :as random] + [syng-im.models.contacts :as contacts] + [syng-im.models.messages :as messages] + [syng-im.models.chats :as chats] + [syng-im.constants :refer [text-content-type]] + [syng-im.utils.handlers :as u] + [syng-im.navigation.handlers :as nav])) + +(defmethod nav/preload-data! :group-settings + [db _] + (assoc db :selected-participants #{})) (defn save-chat-property! [db-name property-name] @@ -18,15 +31,6 @@ (let [property (db-name db)] (assoc-in db [:chats current-chat-id property-name] property)))) -(defn delete-chat [chat-id] - (r/write - (fn [] - (-> (r/get-by-field :chats :chat-id chat-id) - (r/single) - (r/delete)))) - ;; TODO temp. Update chat in db atom - (dispatch [:initialize-chats])) - (defn prepare-chat-settings [{:keys [current-chat-id] :as db} _] (let [{:keys [name color]} (-> db @@ -49,10 +53,92 @@ (after (save-chat-property! :new-chat-color :color)) (update-chat-property :new-chat-color :color)) +(defn clear-messages + [{:keys [current-chat-id] :as db} _] + (assoc-in db [:chats current-chat-id :messages] '())) + (register-handler :clear-history - (fn [db _] - (clear-history (:current-chat-id db)))) + (after delete-messages!) + clear-messages) (register-handler :group-settings (fn [db [_ k v]] (assoc-in db [:group-settings k] v))) + +(defn remove-identities [collection identities] + (remove #(identities (:identity %)) collection)) + +(defn remove-members + [{:keys [current-chat-id selected-participants] :as db} _] + (update-in db [:chats current-chat-id :contacts] + remove-identities selected-participants)) + +(defn remove-members-from-realm! + [{:keys [current-chat-id selected-participants] :as db} _] + (let [chat (get-in db [:chats current-chat-id])] + (r/write + (fn [] + (r/create + :chats + (update chat :contacts remove-identities selected-participants) + true))))) + +(defn notify-about-removing! + [{:keys [current-chat-id selected-participants]} _] + (doseq [participant selected-participants] + (api/group-remove-participant current-chat-id participant))) + +(defn system-message [msg-id content] + {:from "system" + :msg-id msg-id + :content content + :content-type text-content-type}) + +(defn removed-participant-msg [chat-id identity] + (let [contact-name (:name (contacts/contact-by-identity identity))] + (->> (str "You've removed " (or contact-name identity)) + (system-message (random/id)) + (messages/save-message chat-id)))) + +(defn create-removing-messages! + [{:keys [current-chat-id selected-participants]} _] + (doseq [participant selected-participants] + (removed-participant-msg current-chat-id participant))) + +(defn deselect-members [db _] + (assoc db :selected-participants #{})) + +(register-handler :remove-participants + ;; todo check if user have rights to add/remove participants + ;; todo order of operations tbd + (-> remove-members + ;; todo shouldn't this be done only after receiving of the "ack message" + ;; about the api call that removes participants from the group? + ((after remove-members-from-realm!)) + ;; todo uncomment + ;((after notify-about-removing!)) + ((after create-removing-messages!)) + ((enrich deselect-members)) + debug)) + +(defn add-memebers + [{:keys [current-chat-id selected-participants] :as db} _] + (let [new-identities (map #(hash-map :identity %) selected-participants)] + (update db [:chats current-chat-id :contacts] concat new-identities))) + +(defn add-members-to-realm! + [{:keys [current-chat-id selected-participants]} _] + (chats/chat-add-participants current-chat-id selected-participants)) + +(defn notify-about-new-members! + [{:keys [current-chat-id selected-participants]} _] + (doseq [identity selected-participants] + (api/group-add-participant current-chat-id identity))) + +(register-handler :add-new-participants + ;; todo order of operations tbd + (-> add-memebers + ((after add-members-to-realm!)) + ;; todo uncomment + ;((after notify-about-new-members!)) + ((enrich deselect-members)))) diff --git a/src/syng_im/group_settings/screen.cljs b/src/syng_im/group_settings/screen.cljs index 3debc05c29..d89022c7b4 100644 --- a/src/syng_im/group_settings/screen.cljs +++ b/src/syng_im/group_settings/screen.cljs @@ -16,25 +16,25 @@ [syng-im.group-settings.views.member :refer [member-view]] [clojure.string :as s])) -(defn remove-member [{:keys [whisper-identity]}] - (dispatch [:chat-remove-member whisper-identity])) +(defn remove-member [] + (dispatch [:remove-participants])) (defn close-member-menu [] - (dispatch [:set :group-settings-selected-member nil])) + (dispatch [:set :selected-participants #{}])) (defview member-menu [] - [member [:group-settings-selected-member]] - [modal {:animated false - :transparent false - :onRequestClose close-member-menu} - [touchable-highlight {:style st/modal-container - :on-press close-member-menu} - [view st/modal-inner-container - [text {:style st/modal-member-name} - (:name member)] - [touchable-highlight {:on-press #(remove-member member)} - [text {:style st/modal-remove-text} - "Remove"]]]]]) + [{:keys [name] :as participant} [:selected-participant]] + (when participant + [modal {:animated false + :transparent false + :onRequestClose close-member-menu} + [touchable-highlight {:style st/modal-container + :on-press close-member-menu} + [view st/modal-inner-container + [text {:style st/modal-member-name} name] + [touchable-highlight {:on-press remove-member} + [text {:style st/modal-remove-text} + "Remove"]]]]])) (defview chat-members [] [members [:current-chat-contacts]] @@ -42,9 +42,6 @@ (for [member members] ^{:key member} [member-view member])]) -(defn show-chat-name-edit [] - (dispatch [:navigate-to :chat-name-edit])) - (defn setting-view [{:keys [icon-style custom-icon handler title subtitle] icon-name :icon}] [touchable-highlight {:on-press handler} @@ -161,8 +158,7 @@ [text {:style st/chat-name-btn-edit-text} "Edit"]])]]) (defview group-settings [] - [selected-member [:group-settings-selected-member] - show-color-picker [:group-settings :show-color-picker]] + [show-color-picker [:group-settings :show-color-picker]] [view st/group-settings [new-group-toolbar] [scroll-view st/body @@ -179,5 +175,4 @@ [settings-view]] (when show-color-picker [chat-color-picker]) - (when selected-member - [member-menu])]) + [member-menu]]) diff --git a/src/syng_im/group_settings/subs.cljs b/src/syng_im/group_settings/subs.cljs index 844a7088af..816c5b0954 100644 --- a/src/syng_im/group_settings/subs.cljs +++ b/src/syng_im/group_settings/subs.cljs @@ -2,10 +2,10 @@ (:require-macros [reagent.ratom :refer [reaction]]) (:require [re-frame.core :refer [register-sub]])) -(register-sub :group-settings-selected-member +(register-sub :selected-participant (fn [db [_]] (reaction - (let [identity (get @db :group-settings-selected-member)] + (let [identity (first (get @db :selected-participants))] (get-in @db [:contacts identity]))))) (register-sub :group-settings diff --git a/src/syng_im/group_settings/views/member.cljs b/src/syng_im/group_settings/views/member.cljs index 593a8c7eab..79f0bc6e4c 100644 --- a/src/syng_im/group_settings/views/member.cljs +++ b/src/syng_im/group_settings/views/member.cljs @@ -38,6 +38,6 @@ [text {:style st/role-text} role])] [touchable-highlight - {:on-press #(dispatch [:set :group-settings-selected-member whisper-identity])} + {:on-press #(dispatch [:set :selected-participants #{whisper-identity}])} [view st/more-btn [icon :more-vertical st/more-btn-icon]]]]) diff --git a/src/syng_im/handlers.cljs b/src/syng_im/handlers.cljs index 327e336144..da547db56c 100644 --- a/src/syng_im/handlers.cljs +++ b/src/syng_im/handlers.cljs @@ -1,47 +1,23 @@ (ns syng-im.handlers (:require - [re-frame.core :refer [register-handler after dispatch debug enrich]] + [re-frame.core :refer [register-handler after dispatch debug]] [schema.core :as s :include-macros true] - [syng-im.persistence.realm :as r] [syng-im.db :refer [app-db schema]] [syng-im.persistence.simple-kv-store :as kv] [syng-im.protocol.state.storage :as storage] - [syng-im.db :as db :refer [app-db schema]] - [syng-im.protocol.api :refer [init-protocol]] - [syng-im.protocol.protocol-handler :refer [make-handler]] - [syng-im.models.protocol :refer [update-identity - set-initialized]] - [syng-im.models.contacts :as contacts] - [syng-im.models.messages :refer [save-message - update-message! - clear-history]] [syng-im.models.commands :refer [set-commands]] - [syng-im.handlers.server :as server] [syng-im.chat.suggestions :refer [load-commands]] - [syng-im.models.chats :refer [chat-exists? - create-chat - chat-add-participants - chat-remove-participants - set-chat-active - re-join-group-chat - chat-by-id2]] [syng-im.utils.logging :as log] - [syng-im.protocol.api :as api] - [syng-im.constants :refer [text-content-type - content-type-command]] - [syng-im.navigation :refer [nav-push - nav-replace - nav-pop]] [syng-im.utils.crypt :refer [gen-random-bytes]] - [syng-im.utils.random :as random] [syng-im.utils.handlers :as u] syng-im.chat.handlers - [syng-im.group-settings.handlers :refer [delete-chat]] + syng-im.group-settings.handlers syng-im.navigation.handlers syng-im.contacts.handlers syng-im.discovery.handlers syng-im.new-group.handlers - syng-im.participants.handlers)) + syng-im.participants.handlers + syng-im.protocol.handlers)) ;; -- Middleware ------------------------------------------------------------ ;; @@ -106,193 +82,8 @@ (log/debug action commands) (set-commands db commands))) -;; -- Protocol -------------------------------------------------------------- - -(register-handler :initialize-protocol - (u/side-effect! - (fn [db [_]] - (init-protocol (make-handler db))))) - -(register-handler :protocol-initialized - (fn [db [_ identity]] - (-> db - (update-identity identity) - (set-initialized true)))) - -(defn system-message [msg-id content] - {:from "system" - :msg-id msg-id - :content content - :content-type text-content-type}) - -(defn joined-chat-msg [chat-id from msg-id] - (let [contact-name (:name (contacts/contact-by-identity from))] - (save-message chat-id {:from "system" - :msg-id (str msg-id "_" from) - :content (str (or contact-name from) " received chat invitation") - :content-type text-content-type}))) - -(defn participant-invited-to-group-msg [chat-id identity from msg-id] - (let [inviter-name (:name (contacts/contact-by-identity from)) - invitee-name (if (= identity (api/my-identity)) - "You" - (:name (contacts/contact-by-identity identity)))] - (save-message chat-id {:from "system" - :msg-id msg-id - :content (str (or inviter-name from) " invited " (or invitee-name identity)) - :content-type text-content-type}))) - -(defn participant-removed-from-group-msg [chat-id identity from msg-id] - (let [remover-name (:name (contacts/contact-by-identity from)) - removed-name (:name (contacts/contact-by-identity identity))] - (->> (str (or remover-name from) " removed " (or removed-name identity)) - (system-message msg-id) - (save-message chat-id)))) - -(defn you-removed-from-group-msg [chat-id from msg-id] - (let [remover-name (:name (contacts/contact-by-identity from))] - (->> (str (or remover-name from) " removed you from group chat") - (system-message msg-id) - (save-message chat-id)))) - -(defn participant-left-group-msg [chat-id from msg-id] - (let [left-name (:name (contacts/contact-by-identity from))] - (->> (str (or left-name from) " left") - (system-message msg-id) - (save-message chat-id)))) - -(defn removed-participant-msg [chat-id identity] - (let [contact-name (:name (contacts/contact-by-identity identity))] - (->> (str "You've removed " (or contact-name identity)) - (system-message (random/id)) - (save-message chat-id)))) - -(defn left-chat-msg [chat-id] - (save-message chat-id {:from "system" - :msg-id (random/id) - :content "You left this chat" - :content-type text-content-type})) - -(register-handler :group-chat-invite-acked - (u/side-effect! - (fn [_ [action from group-id ack-msg-id]] - (log/debug action from group-id ack-msg-id) - (joined-chat-msg group-id from ack-msg-id)))) - -(register-handler :participant-removed-from-group - (u/side-effect! - (fn [_ [action from group-id identity msg-id]] - (log/debug action msg-id from group-id identity) - (chat-remove-participants group-id [identity]) - (participant-removed-from-group-msg group-id identity from msg-id)))) - -(register-handler :you-removed-from-group - (u/side-effect! - (fn [_ [action from group-id msg-id]] - (log/debug action msg-id from group-id) - (you-removed-from-group-msg group-id from msg-id) - (set-chat-active group-id false)))) - -(register-handler :participant-left-group - (u/side-effect! - (fn [_ [action from group-id msg-id]] - (log/debug action msg-id from group-id) - (when-not (= (api/my-identity) from) - (participant-left-group-msg group-id from msg-id))))) - -(register-handler :participant-invited-to-group - (u/side-effect! - (fn [_ [action from group-id identity msg-id]] - (log/debug action msg-id from group-id identity) - (participant-invited-to-group-msg group-id identity from msg-id)))) - -(register-handler :acked-msg - (u/side-effect! - (fn [_ [action from msg-id]] - (log/debug action from msg-id) - (update-message! {:msg-id msg-id - :delivery-status :delivered})))) - -(register-handler :msg-delivery-failed - (u/side-effect! - (fn [_ [action msg-id]] - (log/debug action msg-id) - (update-message! {:msg-id msg-id - :delivery-status :failed})))) - -(register-handler :leave-group-chat - (u/side-effect! - (fn [db [action]] - (log/debug action) - (let [chat-id (:current-chat-id db)] - (api/leave-group-chat chat-id) - (set-chat-active chat-id false) - (left-chat-msg chat-id) - (delete-chat chat-id) - (dispatch [:navigate-back]))))) - ;; -- User data -------------------------------------------------------------- (register-handler :load-user-phone-number (fn [db [_]] ;; todo fetch phone number from db (assoc db :user-phone-number "123"))) - -;; -- Chats -------------------------------------------------------------- -(defn update-new-participants-selection [db identity add?] - (update db :new-participants (fn [new-participants] - (if add? - (conj new-participants identity) - (disj new-participants identity))))) - -(register-handler :select-new-participant - (fn [db [action identity add?]] - (log/debug action identity add?) - (update-new-participants-selection db identity add?))) - -(register-handler :remove-selected-participants - (fn [db [action]] - (log/debug action) - (let [identities (vec (:new-participants db)) - chat-id (:current-chat-id db)] - (chat-remove-participants chat-id identities) - (dispatch [:navigate-back]) - (doseq [ident identities] - (api/group-remove-participant chat-id ident) - (removed-participant-msg chat-id ident))))) - -(register-handler :add-new-participants - (fn [db [action navigator]] - (log/debug action) - (let [identities (vec (:new-participants db)) - chat-id (:current-chat-id db)] - (chat-add-participants chat-id identities) - (dispatch [:navigate-back]) - (doseq [ident identities] - (api/group-add-participant chat-id ident)) - db))) - -(defn chat-remove-member [db] - (let [chat (get-in db [:chats (:current-chat-id db)]) - identity (:group-settings-selected-member db)] - (r/write - (fn [] - (r/create :chats - (update chat :contacts - (fn [members] - (filter #(not= (:identity %) identity) members))) - true))) - ;; TODO temp. Update chat in db atom - (dispatch [:initialize-chats]) - db)) - - -(register-handler :chat-remove-member - (fn [db [action]] - (let [chat-id (:current-chat-id db) - identity (:group-settings-selected-member db) - db (chat-remove-member db)] - (dispatch [:set :group-settings-selected-member nil]) - ;; TODO fix and uncomment - (api/group-remove-participant chat-id identity) - (removed-participant-msg chat-id identity) - db))) diff --git a/src/syng_im/models/messages.cljs b/src/syng_im/models/messages.cljs index 2706cad94a..eeca56ce29 100644 --- a/src/syng_im/models/messages.cljs +++ b/src/syng_im/models/messages.cljs @@ -62,10 +62,3 @@ (fn [] (when (r/exists? :msgs :msg-id msg-id) (r/create :msgs msg true))))) - -(defn clear-history [chat-id] - (r/write - (fn [] - (r/delete (r/get-by-field :msgs :chat-id chat-id)))) - ;; TODO temp. Update chat in db atom - (dispatch [:initialize-chats])) diff --git a/src/syng_im/navigation.cljs b/src/syng_im/navigation.cljs deleted file mode 100644 index 9c95aa81b2..0000000000 --- a/src/syng_im/navigation.cljs +++ /dev/null @@ -1,17 +0,0 @@ -(ns syng-im.navigation) - -(def ^{:dynamic true :private true} *nav-render* - "Flag to suppress navigator re-renders from outside om when pushing/popping." - true) - -(defn nav-pop [nav] - (binding [*nav-render* true] - (.pop nav))) - -(defn nav-push [nav route] - (binding [*nav-render* true] - (.push nav (clj->js route)))) - -(defn nav-replace [nav route] - (binding [*nav-render* true] - (.replace nav (clj->js route)))) diff --git a/src/syng_im/navigation/handlers.cljs b/src/syng_im/navigation/handlers.cljs index f385485298..17f742254d 100644 --- a/src/syng_im/navigation/handlers.cljs +++ b/src/syng_im/navigation/handlers.cljs @@ -18,7 +18,9 @@ (update :navigation-stack replace-top-element view-id) (assoc :view-id view-id))) -(defmulti preload-data! (fn [_ [_ view-id]] view-id)) +(defmulti preload-data! + (fn [db [_ view-id]] (or view-id (:view-id db)))) + (defmethod preload-data! :default [db _] db) (register-handler :navigate-to @@ -32,6 +34,7 @@ (replace-view db view-id))) (register-handler :navigate-back + (enrich preload-data!) (fn [{:keys [navigation-stack] :as db} _] (if (>= 1 (count navigation-stack)) db diff --git a/src/syng_im/participants/handlers.cljs b/src/syng_im/participants/handlers.cljs index a2b71cf64d..06eb073190 100644 --- a/src/syng_im/participants/handlers.cljs +++ b/src/syng_im/participants/handlers.cljs @@ -4,20 +4,20 @@ (defmethod nav/preload-data! :add-participants [db _] - (assoc db :new-participants #{})) + (assoc db :selected-participants #{})) (defmethod nav/preload-data! :remove-participants [db _] - (assoc db :new-participants #{})) + (assoc db :selected-participants #{})) (defn deselect-participant [db [_ id]] - (update db :new-participants disj id)) + (update db :selected-participants disj id)) (register-handler :deselect-participant deselect-participant) (defn select-participant [db [_ id]] - (update db :new-participants conj id)) + (update db :selected-participants conj id)) (register-handler :select-participant (debug select-participant)) diff --git a/src/syng_im/participants/subs.cljs b/src/syng_im/participants/subs.cljs index c6e53a8af1..3d4a60bfa3 100644 --- a/src/syng_im/participants/subs.cljs +++ b/src/syng_im/participants/subs.cljs @@ -4,4 +4,4 @@ [syng-im.utils.subs :as u])) (register-sub :is-participant-selected? - (u/contains-sub :new-participants)) + (u/contains-sub :selected-participants)) diff --git a/src/syng_im/participants/views/add.cljs b/src/syng_im/participants/views/add.cljs index 826579fcd1..e336cbc4d2 100644 --- a/src/syng_im/participants/views/add.cljs +++ b/src/syng_im/participants/views/add.cljs @@ -11,10 +11,11 @@ (defn new-participants-toolbar [] [toolbar - {:title "Add Participants" - :action {:image {:source res/v ;; {:uri "icon_search"} - :style st/new-participant-image} - :handler #(dispatch [:add-new-participants])}}]) + {:title "Add Participants" + :action {:image {:source res/v ;; {:uri "icon_search"} + :style st/new-participant-image} + :handler #(do (dispatch [:add-new-participants]) + (dispatch [:navigate-back]))}}]) (defn new-participants-row [row _ _] diff --git a/src/syng_im/participants/views/remove.cljs b/src/syng_im/participants/views/remove.cljs index 32cbd1882e..d2ebd11cfd 100644 --- a/src/syng_im/participants/views/remove.cljs +++ b/src/syng_im/participants/views/remove.cljs @@ -14,10 +14,11 @@ (defn remove-participants-toolbar [] [toolbar - {:title "Remove Participants" - :action {:handler #(dispatch [:remove-selected-participants]) - :image {:source res/trash-icon ;; {:uri "icon_search"} - :style st/remove-participants-image}}}]) + {:title "Remove Participants" + :action {:handler #(do (dispatch [:remove-participants]) + (dispatch [:navigate-back])) + :image {:source res/trash-icon ;; {:uri "icon_search"} + :style st/remove-participants-image}}}]) (defn remove-participants-row [row _ _] diff --git a/src/syng_im/protocol/handlers.cljs b/src/syng_im/protocol/handlers.cljs new file mode 100644 index 0000000000..10bb1a4d2b --- /dev/null +++ b/src/syng_im/protocol/handlers.cljs @@ -0,0 +1,115 @@ +; todo everything inside this namespace must be revievew in common with future +; changes in protocol lib +(ns syng-im.protocol.handlers + (:require [syng-im.utils.handlers :as u] + [syng-im.utils.logging :as log] + [syng-im.protocol.api :as api] + [re-frame.core :refer [register-handler dispatch debug]] + [syng-im.models.contacts :as contacts] + [syng-im.protocol.api :refer [init-protocol]] + [syng-im.protocol.protocol-handler :refer [make-handler]] + [syng-im.models.protocol :refer [update-identity + set-initialized]] + [syng-im.constants :refer [text-content-type]] + [syng-im.models.messages :as messages] + [syng-im.models.chats :as chats])) + +(register-handler :initialize-protocol + (u/side-effect! + (fn [db [_]] + (init-protocol (make-handler db))))) + +(register-handler :protocol-initialized + (fn [db [_ identity]] + (-> db + (update-identity identity) + (set-initialized true)))) + +(defn system-message [msg-id content] + {:from "system" + :msg-id msg-id + :content content + :content-type text-content-type}) + +(defn joined-chat-msg [chat-id from msg-id] + (let [contact-name (:name (contacts/contact-by-identity from))] + (messages/save-message chat-id {:from "system" + :msg-id (str msg-id "_" from) + :content (str (or contact-name from) " received chat invitation") + :content-type text-content-type}))) + +(defn participant-invited-to-group-msg [chat-id identity from msg-id] + (let [inviter-name (:name (contacts/contact-by-identity from)) + invitee-name (if (= identity (api/my-identity)) + "You" + (:name (contacts/contact-by-identity identity)))] + (messages/save-message chat-id {:from "system" + :msg-id msg-id + :content (str (or inviter-name from) " invited " (or invitee-name identity)) + :content-type text-content-type}))) + +(defn participant-removed-from-group-msg [chat-id identity from msg-id] + (let [remover-name (:name (contacts/contact-by-identity from)) + removed-name (:name (contacts/contact-by-identity identity))] + (->> (str (or remover-name from) " removed " (or removed-name identity)) + (system-message msg-id) + (messages/save-message chat-id)))) + +(defn you-removed-from-group-msg [chat-id from msg-id] + (let [remover-name (:name (contacts/contact-by-identity from))] + (->> (str (or remover-name from) " removed you from group chat") + (system-message msg-id) + (messages/save-message chat-id)))) + +(defn participant-left-group-msg [chat-id from msg-id] + (let [left-name (:name (contacts/contact-by-identity from))] + (->> (str (or left-name from) " left") + (system-message msg-id) + (messages/save-message chat-id)))) + +(register-handler :group-chat-invite-acked + (u/side-effect! + (fn [_ [action from group-id ack-msg-id]] + (log/debug action from group-id ack-msg-id) + (joined-chat-msg group-id from ack-msg-id)))) + +(register-handler :participant-removed-from-group + (u/side-effect! + (fn [_ [action from group-id identity msg-id]] + (log/debug action msg-id from group-id identity) + (chats/chat-remove-participants group-id [identity]) + (participant-removed-from-group-msg group-id identity from msg-id)))) + +(register-handler :you-removed-from-group + (u/side-effect! + (fn [_ [action from group-id msg-id]] + (log/debug action msg-id from group-id) + (you-removed-from-group-msg group-id from msg-id) + (chats/set-chat-active group-id false)))) + +(register-handler :participant-left-group + (u/side-effect! + (fn [_ [action from group-id msg-id]] + (log/debug action msg-id from group-id) + (when-not (= (api/my-identity) from) + (participant-left-group-msg group-id from msg-id))))) + +(register-handler :participant-invited-to-group + (u/side-effect! + (fn [_ [action from group-id identity msg-id]] + (log/debug action msg-id from group-id identity) + (participant-invited-to-group-msg group-id identity from msg-id)))) + +(register-handler :acked-msg + (u/side-effect! + (fn [_ [action from msg-id]] + (log/debug action from msg-id) + (messages/update-message! {:msg-id msg-id + :delivery-status :delivered})))) + +(register-handler :msg-delivery-failed + (u/side-effect! + (fn [_ [action msg-id]] + (log/debug action msg-id) + (messages/update-message! {:msg-id msg-id + :delivery-status :failed})))) From ec1439943d755229339fba004e2c0695b32f7070 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Sat, 21 May 2016 16:32:04 +0300 Subject: [PATCH 5/6] fix loading of chat's messages Former-commit-id: 8bda73920150ec96d177635479a718db122157ca --- src/status_im/chat/handlers.cljs | 12 +++++++----- src/status_im/group_settings/subs.cljs | 10 ++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index e7e7ed1621..29ccd437a1 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -285,13 +285,15 @@ (defmethod nav/preload-data! :chat [{:keys [current-chat-id] :as db} [_ _ id]] - (let [messages (get-in db [:chats current-chat-id :messages])] + (let [chat-id (or id current-chat-id) + messages (get-in db [:chats chat-id :messages]) + db' (assoc db :current-chat-id chat-id)] + (println "wuuut..." chat-id messages) (if (seq messages) - (-> db - (assoc :current-chat-id (or id current-chat-id)) + db' + (-> db' load-messages! - init-chat) - db))) + init-chat)))) (defn prepare-chat [{:keys [contacts] :as db} [_ contcat-id]] diff --git a/src/status_im/group_settings/subs.cljs b/src/status_im/group_settings/subs.cljs index ee9ad34c8a..f2b6ac1db3 100644 --- a/src/status_im/group_settings/subs.cljs +++ b/src/status_im/group_settings/subs.cljs @@ -3,12 +3,10 @@ (:require [re-frame.core :refer [register-sub]])) (register-sub :selected-participant - (fn [db [_]] - (let [participants (:selected-participants @db)] - (reaction - (when (seq participants) - (let [identity (first participants)] - (get-in @db [:contacts identity]))))))) + (fn [db _] + (reaction + (let [identity (first (:selected-participants @db))] + (get-in @db [:contacts identity]))))) (register-sub :group-settings (fn [db [_ k]] From c6de5075fe4239f3563d7797505c4c2ee53060cf Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Sat, 21 May 2016 16:34:06 +0300 Subject: [PATCH 6/6] remove println Former-commit-id: 86f80dc52560402abe0956abae83ee4092f35dc7 --- src/status_im/chat/handlers.cljs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index 29ccd437a1..3564ea8e69 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -288,7 +288,6 @@ (let [chat-id (or id current-chat-id) messages (get-in db [:chats chat-id :messages]) db' (assoc db :current-chat-id chat-id)] - (println "wuuut..." chat-id messages) (if (seq messages) db' (-> db'