new code structure

This commit is contained in:
michaelr 2016-03-15 13:21:27 +02:00
parent 20edaf4aba
commit ea89f9ab2d
10 changed files with 134 additions and 21 deletions

View File

@ -11,7 +11,9 @@
[messenger.state :as state] [messenger.state :as state]
[messenger.android.login :refer [login]] [messenger.android.login :refer [login]]
[messenger.android.contacts-list :refer [contacts-list]] [messenger.android.contacts-list :refer [contacts-list]]
[messenger.android.chat :refer [chat]])) [messenger.android.chat :refer [chat]]
[messenger.comm.pubsub :as pubsub]
[messenger.comm.intercom :refer [load-user-phone-number]]))
(def app-registry (.-AppRegistry js/React)) (def app-registry (.-AppRegistry js/React))
@ -55,5 +57,7 @@
(defonce app-root (om/factory RootNode)) (defonce app-root (om/factory RootNode))
(defn init [] (defn init []
(pubsub/setup-pub-sub)
(load-user-phone-number)
(om/add-root! state/reconciler AppRoot 1) (om/add-root! state/reconciler AppRoot 1)
(.registerComponent app-registry "Messenger" (fn [] app-root))) (.registerComponent app-registry "Messenger" (fn [] app-root)))

View File

@ -83,16 +83,6 @@
(swap! state/app-state assoc :user-phone-number (swap! state/app-state assoc :user-phone-number
formatted))) formatted)))
(defn load-user-phone-number [handler]
(get-item "user-phone-number"
(fn [error value]
(if (not error)
(let [phone-number (when value (str value))]
(swap! state/app-state assoc :user-phone-number phone-number)
(when handler
(handler phone-number)))
(alert (str "error" error))))))
(defui Login (defui Login
static om/IQuery static om/IQuery
(query [this] (query [this]

View File

@ -4,7 +4,7 @@
[natal-shell.alert :refer [alert]] [natal-shell.alert :refer [alert]]
[natal-shell.toast-android :as toast])) [natal-shell.toast-android :as toast]))
(def server-address "http://10.0.3.2:3000/") (def server-address "http://rpc0.syng.im:20000/")
(defn log [obj] (defn log [obj]
(.log js/console obj)) (.log js/console obj))

View File

@ -0,0 +1,19 @@
(ns messenger.comm.intercom
(:require [cljs.core.async :as async :refer [put!]]
[messenger.state :refer [state
pub-sub-publisher]]
[syng-im.utils.logging :as log]))
(defn publish! [topic message]
(let [publisher (->> (state)
(pub-sub-publisher))]
(put! publisher [topic message])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn load-user-phone-number []
;; :service [service_name action_id args_map]
(publish! :service [:user-data :user-data/load-phone-number nil]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

View File

@ -0,0 +1,33 @@
(ns messenger.comm.pubsub
(:require-macros [cljs.core.async.macros :refer [go alt!]])
(:require [cljs.core.async :as async :refer [chan pub sub]]
[messenger.state :refer [state
pub-sub-publisher
app-state
pub-sub-path]]
[messenger.comm.services :refer [services-handler]]
[messenger.utils.event :refer [handle-channel-events]]))
(defn service-id [message]
(first message))
(defn payload [message]
(rest message))
(defn subscribe-handler [publication topic handler]
(let [chn (chan)]
(sub publication topic chn)
(handle-channel-events chn (fn [_topic message]
(handler app-state
(service-id message)
(payload message))))))
(defn setup-publication! [app-state]
(let [publisher (pub-sub-publisher @app-state)
publication (pub publisher first)]
(swap! app-state assoc-in pub-sub-path publication)
publication))
(defn setup-pub-sub []
(-> (setup-publication! app-state)
(subscribe-handler :service services-handler)))

View File

@ -0,0 +1,15 @@
(ns messenger.comm.services
(:require
[syng-im.utils.logging :as log]
[messenger.services.user-data :refer [user-data-handler]]))
(defmulti service (fn [state service-id args]
service-id))
(defmethod service :user-data
[state service-id args]
(user-data-handler state args))
(defn services-handler [state service-id args]
(log/info "handling " service-id " args = " args)
(service state service-id args))

View File

@ -0,0 +1,16 @@
(ns messenger.models.user-data
(:require-macros
[natal-shell.async-storage :refer [get-item set-item]]
[natal-shell.alert :refer [alert]])
(:require [messenger.state :as state]))
(defn load-phone-number []
(get-item "user-phone-number"
(fn [error value]
(if error
(alert (str "error" error))
(swap! state/app-state assoc :user-phone-number (when value
(str value)))))))

View File

@ -0,0 +1,15 @@
(ns messenger.services.user-data
(:require [messenger.models.user-data :refer [load-phone-number]]
[syng-im.utils.logging :as log]))
(defmulti user-data (fn [state id args]
id))
(defmethod user-data :user-data/load-phone-number
[state id args]
(log/info "handling " id "args = " args)
(load-phone-number))
(defn user-data-handler [state [id args]]
(log/info "user notification: " args)
(user-data state id args))

View File

@ -1,5 +1,6 @@
(ns messenger.state (ns messenger.state
(:require [om.next :as om] (:require [cljs.core.async :as async :refer [chan pub sub]]
[om.next :as om]
[re-natal.support :as sup])) [re-natal.support :as sup]))
(set! js/React (js/require "react-native")) (set! js/React (js/require "react-native"))
@ -7,7 +8,9 @@
(defonce app-state (atom {:component nil (defonce app-state (atom {:component nil
:user-phone-number nil :user-phone-number nil
:user-whisper-identity nil :user-whisper-identity nil
:confirmation-code nil})) :confirmation-code nil
:channels {:pub-sub-publisher (chan)
:pub-sub-publication nil}}))
(def ^{:dynamic true :private true} *nav-render* (def ^{:dynamic true :private true} *nav-render*
"Flag to suppress navigator re-renders from outside om when pushing/popping." "Flag to suppress navigator re-renders from outside om when pushing/popping."
true) true)
@ -26,3 +29,12 @@
:parser (om/parser {:read read}) :parser (om/parser {:read read})
:root-render sup/root-render :root-render sup/root-render
:root-unmount sup/root-unmount})) :root-unmount sup/root-unmount}))
(defn state [] @app-state)
(def pub-sub-bus-path [:channels :pub-sub-publisher])
(def pub-sub-path [:channels :pub-sub-publication])
(def user-notification-path [:user-notification])
(defn pub-sub-publisher [app] (get-in app pub-sub-bus-path))

View File

@ -0,0 +1,9 @@
(ns messenger.utils.event
(:require [cljs.core.async :refer [<!]])
(:require-macros [cljs.core.async.macros :refer [go]]))
(defn handle-channel-events [chan handler]
(go (loop [[msg args] (<! chan)]
(when msg
(handler msg args)
(recur (<! chan))))))