more cleanup in handlers/subs

Former-commit-id: 4752643ed7
This commit is contained in:
Roman Volosovskyi 2016-05-17 13:32:20 +03:00
parent e25eb65a49
commit f7bb988954
9 changed files with 53 additions and 93 deletions

View File

@ -23,15 +23,15 @@
;; todo: it might be better always return false from
;; this listener and handle application's closing
;; in handlers
(let [stack (subscribe [:navigation-stack])]
(let [stack (subscribe [:get :navigation-stack])]
(when (< 1 (count @stack))
(dispatch [:navigate-back])
true)))]
(add-event-listener "hardwareBackPress" new-listener)))
(defn app-root []
(let [signed-up (subscribe [:signed-up])
view-id (subscribe [:view-id])]
(let [signed-up (subscribe [:get :signed-up])
view-id (subscribe [:get :view-id])]
(fn []
(case (if @signed-up @view-id :chat)
:discovery [discovery]

View File

@ -9,10 +9,11 @@
content-type-command]]
[syng-im.utils.random :as random]
[syng-im.components.react :as r]
[syng-im.handlers.sign-up :as sign-up-service]
[syng-im.chat.sign-up :as sign-up-service]
[syng-im.models.chats :as chats]
[syng-im.navigation.handlers :as nav]
[syng-im.models.chats :as c]))
[syng-im.models.chats :as c]
[syng-im.utils.handlers :as u]))
(register-handler :set-show-actions
(fn [db [_ show-actions]]
@ -71,17 +72,18 @@
((enrich update-command) update-text))
(register-handler :send-group-chat-msg
(fn [db [_ chat-id text]]
(let [{msg-id :msg-id
{from :from} :msg} (api/send-group-user-msg {:group-id chat-id
:content text})
msg {:msg-id msg-id
:from from
:to nil
:content text
:content-type text-content-type
:outgoing true}]
(messages/save-message chat-id msg))))
(u/side-effect!
(fn [_ [_ chat-id text]]
(let [{msg-id :msg-id
{from :from} :msg} (api/send-group-user-msg {:group-id chat-id
:content text})
msg {:msg-id msg-id
:from from
:to nil
:content text
:content-type text-content-type
:outgoing true}]
(messages/save-message chat-id msg)))))
(defn console? [s]
(= "console" s))
@ -236,7 +238,6 @@
(fn [db [_ signed-up]]
(sign-up-service/set-signed-up db signed-up)))
(defn load-messages!
([db] (load-messages! db nil))
([db _]
@ -289,15 +290,9 @@
((after store-message!))))
(register-handler :group-received-msg
(fn [db [_ {chat-id :group-id :as msg}]]
(messages/save-message chat-id msg)
db))
(defn load-chat!
[{:keys [chats current-chat-id] :as db}]
(when-not (chats current-chat-id)
(c/create-chat {}))
db)
(u/side-effect!
(fn [_ [_ {chat-id :group-id :as msg}]]
(messages/save-message chat-id msg))))
(defmethod nav/preload-data! :chat
[{:keys [current-chat-id] :as db} [_ _ id]]

View File

@ -1,4 +1,5 @@
(ns syng-im.handlers.sign-up
(ns syng-im.chat.sign-up
;syng-im.handlers.sign-up
(:require [re-frame.core :refer [subscribe dispatch dispatch-sync]]
[syng-im.persistence.simple-kv-store :as kv]
[syng-im.protocol.state.storage :as s]

View File

@ -46,18 +46,15 @@
@username]]
[view st/menu-items-container
[menu-item {:name "Profile"
:handler (fn []
(dispatch [:show-my-profile]))}]
:handler #(dispatch [:navigate-to :my-profile])}]
[menu-item {:name "Settings"
:handler (fn []
;; TODO not implemented
)}]
[menu-item {:name "Discovery"
:handler (fn []
(dispatch [:navigate-to :discovery]))}]
:handler #(dispatch [:navigate-to :discovery])}]
[menu-item {:name "Contacts"
:handler (fn []
(dispatch [:show-contacts navigator]))}]
:handler #(dispatch [:show-contacts navigator])}]
[menu-item {:name "Invite friends"
:handler (fn []
;; TODO not implemented

View File

@ -15,7 +15,6 @@
[syng-im.models.commands :refer [set-commands]]
[syng-im.handlers.server :as server]
[syng-im.chat.suggestions :refer [load-commands]]
[syng-im.handlers.sign-up :as sign-up-service]
[syng-im.models.chats :refer [chat-exists?
create-chat
chat-add-participants

View File

@ -17,28 +17,28 @@
(keywordize-keys (apply hash-map (split s #"[;=]"))))
(defn save-message
[chat-id {:keys [from to msg-id content content-type outgoing
same-author same-direction]
;; todo remove chat-id parameter
[chat-id {:keys [to msg-id content outgoing]
;; outgoing should be explicitely defined in handlers
:or {outgoing false
to nil} :as msg}]
(log/debug "save-message" chat-id msg)
to nil} :as message}]
(when-not (r/exists? :msgs :msg-id msg-id)
(r/write
(fn []
(let [content (if (string? content)
content
(map-to-str content))]
(r/create :msgs {:chat-id chat-id
:msg-id msg-id
:from from
:to to
:content content
:content-type content-type
:outgoing outgoing
:timestamp (timestamp)
:delivery-status nil
:same-author same-author
:same-direction same-direction} true))))))
(let [content' (if (string? content)
content
(map-to-str content))
message' (merge message
{:chat-id chat-id
:content content'
:timestamp (timestamp)
:delivery-status nil})]
(r/create :msgs message' true))))))
(defn command-type? [type]
(contains?
#{c/content-type-command c/content-type-command-request}
type))
(defn get-messages [chat-id]
(->> (-> (r/get-by-field :msgs :chat-id chat-id)
@ -46,8 +46,7 @@
(r/collection->map))
(into '())
(map (fn [{:keys [content-type] :as message}]
(if (#{c/content-type-command c/content-type-command-request}
content-type)
(if (command-type? content-type)
(update message :content str-to-map)
message)))))

View File

@ -1,5 +1,6 @@
(ns syng-im.navigation.handlers
(:require [re-frame.core :refer [register-handler dispatch debug enrich]]))
(:require [re-frame.core :refer [register-handler dispatch debug enrich
after]]))
(defn push-view [db view-id]
(-> db
@ -71,14 +72,10 @@
(push-view :add-participants)
clear-new-participants)))
(register-handler :show-profile
(debug
(fn [db [_ identity]]
(let [db (assoc db :contact-identity identity)]
(dispatch [:navigate-to :profile])
db))))
(defn show-profile
[db [_ identity]]
(-> db
(assoc :contact-identity identity)
(push-view :profile)))
(register-handler :show-my-profile
(fn [db _]
(dispatch [:navigate-to :my-profile])
db))
(register-handler :show-profile show-profile)

View File

@ -1,11 +0,0 @@
(ns syng-im.navigation.subs
(:require-macros [reagent.ratom :refer [reaction]])
(:require [re-frame.core :refer [register-sub]]))
(register-sub :view-id
(fn [db _]
(reaction (@db :view-id))))
(register-sub :navigation-stack
(fn [db _]
(reaction (:navigation-stack @db))))

View File

@ -1,27 +1,10 @@
(ns syng-im.subs
(:require-macros [reagent.ratom :refer [reaction]])
(:require [re-frame.core :refer [register-sub]]
[syng-im.models.chats :refer [chats-list chat-by-id]]
[syng-im.models.contacts :refer [get-contacts
contacts-list-exclude
contacts-list-include
contact-by-identity]]
syng-im.chat.subs
syng-im.navigation.subs
syng-im.discovery.subs
syng-im.contacts.subs))
;; -- Chats list --------------------------------------------------------------
(register-sub :get
(fn [db [_ k]]
(reaction (k @db))))
;; -- User data --------------------------------------------------------------
(register-sub
:signed-up
(fn [db _]
(reaction (:signed-up @db))))
(register-sub :db
(fn [db _] (reaction @db)))