Merge pull request #2 from syng-im/new-structure

new code structure

Former-commit-id: f1c3bc7b0f
This commit is contained in:
michaelr524 2016-03-15 15:43:19 +02:00
commit c1e97574c1
10 changed files with 134 additions and 24 deletions

View File

@ -11,7 +11,9 @@
[messenger.state :as state]
[messenger.android.login :refer [login]]
[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))
@ -55,5 +57,7 @@
(defonce app-root (om/factory RootNode))
(defn init []
(pubsub/setup-pub-sub)
(load-user-phone-number)
(om/add-root! state/reconciler AppRoot 1)
(.registerComponent app-registry "Messenger" (fn [] app-root)))

View File

@ -83,23 +83,10 @@
(swap! state/app-state assoc :user-phone-number
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
static om/IQuery
(query [this]
'[:user-phone-number])
Object
(componentDidMount [this]
(load-user-phone-number nil))
(render [this]
(let [{:keys [user-phone-number]} (om/props this)
{:keys [nav]} (om/get-computed this)]

View File

@ -4,7 +4,7 @@
[natal-shell.alert :refer [alert]]
[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]
(.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,13 +1,16 @@
(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]))
(set! js/React (js/require "react-native"))
(defonce app-state (atom {:component nil
:user-phone-number nil
(defonce app-state (atom {:component nil
:user-phone-number 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*
"Flag to suppress navigator re-renders from outside om when pushing/popping."
true)
@ -21,8 +24,17 @@
{:value :not-found})))
(defonce reconciler
(om/reconciler
{:state app-state
:parser (om/parser {:read read})
:root-render sup/root-render
:root-unmount sup/root-unmount}))
(om/reconciler
{:state app-state
:parser (om/parser {:read read})
:root-render sup/root-render
: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))))))