cleaned logging and changed it to use utils.logging; splitted carousel to multiple functions

Former-commit-id: 5aef032c59
This commit is contained in:
Adrian Tiberius 2016-04-30 22:48:30 +03:00
parent bce474e857
commit 38f7ea1ebf
11 changed files with 142 additions and 183 deletions

View File

@ -4,7 +4,7 @@
scroll-view scroll-view
touchable-without-feedback touchable-without-feedback
text]] text]]
[syng-im.utils.debug :refer [log]])) [syng-im.utils.logging :as log]))
(defn page-width [] (defn page-width []
@ -24,21 +24,15 @@
(defn get-page-width [data] (defn get-page-width [data]
(get data :pageWidth (:pageWidth defaults))) (get data :pageWidth (:pageWidth defaults)))
(defn calculate-gap [component props] (defn calculate-gap [component props]
(let [prop-page-width (get-page-width props) (let [prop-page-width (get-page-width props)
page-width (page-width) page-width (page-width)
sneak (get-sneak props) sneak (get-sneak props)
gap (quot (- (- page-width (* 2 sneak)) prop-page-width) 2) gap (quot (- (- page-width (* 2 sneak)) prop-page-width) 2)]
_ (log "calculate-gap") (log/debug "calculate-gap: prop-page-width=" prop-page-width
_ (log component) "; page-width=" page-width"; sneak=" sneak "; gap=" gap)
_ (log props) (when (> prop-page-width page-width)
_ (log page-width) (log/warn "Invalid pageWidth"))
_ (log prop-page-width)
_ (log sneak)
_ (log gap)]
(if (> prop-page-width page-width)
(println "Invalid pageWidth"))
(reagent.core/set-state component {:gap gap}) (reagent.core/set-state component {:gap gap})
)) ))
@ -46,105 +40,99 @@
(.scrollTo (.-scrollView component) (clj->js {:y y (.scrollTo (.-scrollView component) (clj->js {:y y
:x x}))) :x x})))
(defn get-current-position [event]
(+ (.-x (.-contentOffset (.-nativeEvent event))) (quot (page-width) 2)))
(defn on-scroll-end [event component] (defn on-scroll-end [event component]
(let [_ (log "on-scroll-end") (let [props (reagent.core/props component)
_ (log event)
_ (log component)
props (reagent.core/props component)
_ (log props)
state (reagent.core/state component) state (reagent.core/state component)
_ (log state)
prop-page-width (get-page-width props) prop-page-width (get-page-width props)
_ (log (str "prop-page-width: " prop-page-width))
sneak (get-sneak props) sneak (get-sneak props)
_ (log (str "sneak: " sneak))
gap (get-gap state) gap (get-gap state)
_ (log (str "gap: " gap))
page-offset (+ prop-page-width gap) page-offset (+ prop-page-width gap)
_ (log (str "page-offset: " page-offset)) current-position (get-current-position event)
current-position (+ (.-x (.-contentOffset (.-nativeEvent event))) (quot (page-width) 2))
_ (log (str "current-position: " current-position))
current-page (quot current-position page-offset) current-page (quot current-position page-offset)
_ (log (str "current-page: " current-page))
] ]
(log/debug "on-scroll-end: prop-page-width=" prop-page-width
"; sneak=" sneak "; gap=" gap "; page-offset=" page-offset
"; current-position=" current-position
"; current-page=" current-page)
(scroll-to component (* current-page page-offset) 0) (scroll-to component (* current-page page-offset) 0)
(reagent.core/set-state component {:activePage current-page}) (reagent.core/set-state component {:activePage current-page})
(if (:onPageChange props) (when (:onPageChange props)
((:onPageChange props) current-page)))) ((:onPageChange props) current-page))))
(defn go-to-page [component position] (defn go-to-page [component position]
(let [_ (log "go-to-page") (let [props (reagent.core/props component)
props (reagent.core/props component)
state (reagent.core/state component) state (reagent.core/state component)
props-page-width (get-page-width props) props-page-width (get-page-width props)
_ (log (str "props page width: " props-page-width))
gap (get-gap state) gap (get-gap state)
_ (log (str "gap: " gap)) page-position (* position (+ props-page-width gap))]
page-position (* position (+ props-page-width gap)) (log/debug "go-to-page: props-page-width=" props-page-width "; gap=" gap
_ (log (str "page position: " page-position))] "; page-position=" page-position)
(scroll-to component page-position 0))) (scroll-to component page-position 0)))
(defn component-will-mount [component new-args]
(let [props (reagent.core/props component)]
(log/debug "component-will-mount: component=" component "; new-args="new-args)
(calculate-gap component props)))
(defn component-did-mount [component]
(let [props (reagent.core/props component)
initial-page (.-initialPage props)]
(log/debug "component-did-mount: initial-page="initial-page)
(when (pos? initial-page)
(go-to-page component initial-page))))
(defn component-will-receive-props [component new-argv]
(log/debug "component-will-receive-props: component=" component
"; new-argv=" new-argv)
(calculate-gap component new-argv))
(defn get-pages [component data children]
(let [props-page-width (get-page-width data)
page-style (get data :pageStyle {})
gap (get-gap data)
margin (quot gap 2)]
(map-indexed (fn [index child]
(let [page-index index
touchable-data {:key index
:onPress (fn [event]
(go-to-page component page-index))}]
(log/debug "page " index " - " child)
[touchable-without-feedback touchable-data
[view {:style [{:width props-page-width
:justifyContent "center"
:marginLeft margin
:marginRight margin}
page-style]}
child]])) children)))
(defn reagent-render [data children]
(let [component (reagent.core/current-component)
sneak (get-sneak data)
gap (get-gap data)
pages (get-pages component data children)]
(log/debug "reagent-render: ")
[view {:style {:flex 1}}
[scroll-view {:contentContainerStyle {:paddingLeft (+ sneak (quot gap 2))
:paddingRight (+ sneak (quot gap 2))}
:automaticallyAdjustContentInsets false
:bounces false
:decelerationRate 0.9
:horizontal true
:onScrollEndDrag (fn [event] (on-scroll-end event component))
:showsHorizontalScrollIndicator false
:ref (fn [c] (set! (.-scrollView component) c))
}
pages]]
))
(defn carousel [data children] (defn carousel [data children]
(let [_ (log "Creating carousel component: ") (let [component-data {:component-did-mount component-did-mount
_ (log data) :component-will-mount component-will-mount
_ (log children)] :component-will-receive-props component-will-receive-props
(reagent.core/create-class {:component-did-mount (fn [this] :display-name "carousel"
(let [_ (log this)] :reagent-render reagent-render}]
(if (> (.-initialPage (.-props this)) 0) (log/debug "Creating carousel component: " data children)
(go-to-page this (.-initialPage (.-props this)))))) (reagent.core/create-class component-data)))
:component-will-mount (fn [this]
(let [_ (log "component-will-mount")
_ (log this)]
(calculate-gap this (reagent.core/props this))))
:component-will-receive-props (fn [this new-argv]
(let [_ (log "component-will-receive-props-mount")
_ (log this)
_ (log new-argv)]
(calculate-gap this new-argv)))
:display-name "carousel1"
:reagent-render (fn [data children]
(let [_ (log "reagent-render")
_ (log data)
_ (log children)
component (reagent.core/current-component)
props-page-width (get-page-width data)
sneak (get-sneak data)
gap (get-gap data)
page-style (get data :pageStyle {})
pages (map-indexed (fn [index child]
(let [page-index index
_ (log (str "page index" index "-" page-index))
_ (log child)]
[touchable-without-feedback {:key index
:onPress (fn [a b c]
(let [_ (log "touchable pressed")
_ (log (.-target a))
_ (log component)
_ (log page-index)]
(go-to-page component page-index)))
}
[view {:style [{:width props-page-width
:justifyContent "center"
:marginLeft (quot gap 2)
:marginRight (quot gap 2)}
page-style]}
child]])) children)]
[view {:style {:flex 1}}
[scroll-view {:contentContainerStyle {:paddingLeft (+ sneak (quot gap 2))
:paddingRight (+ sneak (quot gap 2))}
:automaticallyAdjustContentInsets false
:bounces false
:decelerationRate 0.9
:horizontal true
:onScrollEndDrag (fn [event] (on-scroll-end event component))
:showsHorizontalScrollIndicator false
:ref (fn [c] (set! (.-scrollView component) c))
}
pages]]))})))
(comment
)

View File

@ -1,6 +1,6 @@
(ns syng-im.components.discovery.discovery-popular-list-item (ns syng-im.components.discovery.discovery-popular-list-item
(:require (:require
[syng-im.utils.debug :refer [log]] [syng-im.utils.logging :as log]
[syng-im.components.react :refer [android? [syng-im.components.react :refer [android?
view view
text text
@ -10,8 +10,8 @@
) )
(defn discovery-popular-list-item [discovery] (defn discovery-popular-list-item [discovery]
(let [_ (log discovery)] (log/debug discovery)
(r/as-element [view {:style {:flexDirection "row" (r/as-element [view {:style {:flexDirection "row"
:paddingTop 10 :paddingTop 10
:paddingBottom 10}} :paddingBottom 10}}
[view {:style {:flex 0.8 [view {:style {:flex 0.8
@ -34,5 +34,4 @@
:height 30} :height 30}
:source res/user-no-photo}] :source res/user-no-photo}]
] ]
]) ]))
))

View File

@ -20,7 +20,7 @@
(defn render-separator [sectionID, rowID, adjacentRowHighlighted] (defn render-separator [sectionID, rowID, adjacentRowHighlighted]
(let [elem (r/as-element [view {:style {:borderBottomWidth 1 (let [elem (r/as-element [view {:style {:borderBottomWidth 1
:borderBottomColor "#eff2f3"} :borderBottomColor "#eff2f3"}
:key rowID}])] :key rowID}])]
elem)) elem))
@ -28,8 +28,8 @@
(let [discoveries (subscribe [:get-discoveries]) (let [discoveries (subscribe [:get-discoveries])
datasource (to-realm-datasource @discoveries)] datasource (to-realm-datasource @discoveries)]
[list-view {:dataSource datasource [list-view {:dataSource datasource
:renderRow render-row :renderRow render-row
:renderSeparator render-separator :renderSeparator render-separator
:style {:backgroundColor "white" :style {:backgroundColor "white"
:paddingLeft 15}}] :paddingLeft 15}}]
)) ))

View File

@ -1,7 +1,7 @@
(ns syng-im.components.discovery.discovery (ns syng-im.components.discovery.discovery
(:require (:require
[syng-im.utils.debug :refer [log]] [syng-im.utils.logging :as log]
[re-frame.core :refer [dispatch]] [re-frame.core :refer [dispatch]]
[syng-im.models.discoveries :refer [save-discoveries]] [syng-im.models.discoveries :refer [save-discoveries]]
[syng-im.components.react :refer [android? [syng-im.components.react :refer [android?
@ -67,7 +67,7 @@
:placeholder "Type your search tags here" :placeholder "Type your search tags here"
:onChangeText (fn [new-text] :onChangeText (fn [new-text]
(let [old-text (:search @search-input)] (let [old-text (:search @search-input)]
(log (str new-text "-" old-text)))) (log/debug (str new-text "-" old-text))))
:onSubmitEditing (fn [e] :onSubmitEditing (fn [e]
(let [search (aget e "nativeEvent" "text") (let [search (aget e "nativeEvent" "text")
hashtags (get-hashtags search)] hashtags (get-hashtags search)]

View File

@ -1,7 +1,7 @@
(ns syng-im.components.discovery.discovery-popular (ns syng-im.components.discovery.discovery-popular
(:require (:require
[re-frame.core :refer [subscribe]] [re-frame.core :refer [subscribe]]
[syng-im.utils.debug :refer [log]] [syng-im.utils.logging :as log]
[syng-im.components.react :refer [android? [syng-im.components.react :refer [android?
text]] text]]
[syng-im.components.carousel :refer [carousel]] [syng-im.components.carousel :refer [carousel]]
@ -12,9 +12,8 @@
(.-width (.get (.. js/React -Dimensions) "window"))) (.-width (.get (.. js/React -Dimensions) "window")))
(defn discovery-popular [] (defn discovery-popular []
(let [popular-tags (subscribe [:get-popular-tags 3]) (let [popular-tags (subscribe [:get-popular-tags 3])]
_ (log "Got popular tags: ") (log/debug "Got popular tags: " @popular-tags)
_ (log @popular-tags)]
(if (> (count @popular-tags) 0) (if (> (count @popular-tags) 0)
[carousel {:pageStyle {:borderRadius 1 [carousel {:pageStyle {:borderRadius 1
:shadowColor "black" :shadowColor "black"
@ -25,14 +24,8 @@
:pageWidth (- (page-width) 60) :pageWidth (- (page-width) 60)
:sneak 20} :sneak 20}
(for [tag @popular-tags] (for [tag @popular-tags]
(discovery-popular-list (.-name tag))) (discovery-popular-list (.-name tag)))]
]
[text "None"] [text "None"]
) )
) )
) )
(comment
(set! React (js/require "react-native"))
(.get (.Dimensions React) "window")
)

View File

@ -1,10 +1,7 @@
(ns syng-im.components.discovery.discovery-popular-list (ns syng-im.components.discovery.discovery-popular-list
(:require-macros
[natal-shell.data-source :refer [data-source clone-with-rows]]
)
(:require (:require
[re-frame.core :refer [subscribe dispatch dispatch-sync]] [re-frame.core :refer [subscribe dispatch dispatch-sync]]
[syng-im.utils.debug :refer [log]] [syng-im.utils.logging :as log]
[syng-im.components.react :refer [android? [syng-im.components.react :refer [android?
view view
list-view list-view
@ -29,33 +26,27 @@
elem)) elem))
(defn discovery-popular-list [tag] (defn discovery-popular-list [tag]
(let [discoveries (subscribe [:get-discoveries-by-tag tag 3]) (let [discoveries (subscribe [:get-discoveries-by-tag tag 3])]
_ (log (str "Got discoveries for tag (" tag "): ") @discoveries) (log/debug "Got discoveries for tag (" tag "): " @discoveries)
_ (log @discoveries)]
[view {:style {:flex 1 [view {:style {:flex 1
:backgroundColor "white" :backgroundColor "white"
:paddingLeft 10 :paddingLeft 10
:paddingTop 10}} :paddingTop 10}}
[view {:style {:flexDirection "row" [view {:style {:flexDirection "row"
:backgroundColor "white" :backgroundColor "white"
:padding 0}} :padding 0}}
[view {:style {:flexDirection "column" [view {:style {:flexDirection "column"
:backgroundColor "#e9f7fe" :backgroundColor "#e9f7fe"
:borderRadius 5 :borderRadius 5
:padding 0}} :padding 0}}
[text {:style {:color "#6092df" [text {:style {:color "#6092df"
:paddingRight 5 :paddingRight 5
:paddingBottom 2 :paddingBottom 2
:alignItems "center" :alignItems "center"
:justifyContent "center"}} (str " #" (name tag))]]] :justifyContent "center"}}
[list-view {:dataSource (to-realm-datasource @discoveries) (str " #" (name tag))]]]
:renderRow render-row [list-view {:dataSource (to-realm-datasource @discoveries)
:renderSeparator render-separator :renderRow render-row
:style {:backgroundColor "white"}}] :renderSeparator render-separator
])) :style {:backgroundColor "white"}}]
]))
(comment
list-view {:dataSource elements
:renderRow (partial render-row list-element)
:style {:backgroundColor "white"}}
)

View File

@ -1,6 +1,6 @@
(ns syng-im.handlers.discovery (ns syng-im.handlers.discovery
(:require [re-frame.core :refer [register-handler after dispatch]] (:require [re-frame.core :refer [register-handler after dispatch]]
[syng-im.utils.debug :refer [log]] [syng-im.utils.logging :as log]
[syng-im.protocol.api :as api] [syng-im.protocol.api :as api]
[syng-im.models.discoveries :refer [save-discoveries [syng-im.models.discoveries :refer [save-discoveries
signal-discoveries-updated]])) signal-discoveries-updated]]))
@ -27,8 +27,7 @@
(register-handler :broadcast-status (register-handler :broadcast-status
(fn [db [action status hashtags]] (fn [db [action status hashtags]]
(let [_ (log "Status: " status) (let [name (:name db)]
_ (log "Hashtags: " hashtags) (log/debug "Status: " status ", Hashtags: " hashtags)
name (:name db)]
(api/broadcast-discover-status name status hashtags) (api/broadcast-discover-status name status hashtags)
db))) db)))

View File

@ -1,7 +1,7 @@
(ns syng-im.models.discoveries (ns syng-im.models.discoveries
(:require [cljs.core.async :as async :refer [chan put! <! >!]] (:require [cljs.core.async :as async :refer [chan put! <! >!]]
[re-frame.core :refer [subscribe dispatch dispatch-sync]] [re-frame.core :refer [subscribe dispatch dispatch-sync]]
[syng-im.utils.debug :refer [log]] [syng-im.utils.logging :as log]
[syng-im.persistence.realm :as realm] [syng-im.persistence.realm :as realm]
[syng-im.persistence.realm :as r] [syng-im.persistence.realm :as r]
[syng-im.resources :as res] [syng-im.resources :as res]
@ -17,9 +17,9 @@
(get-in db db/updated-discoveries-signal-path)) (get-in db db/updated-discoveries-signal-path))
(defn get-tag [tag] (defn get-tag [tag]
(let [_ (log (str "Getting tag: " tag))] (log/debug "Getting tag: " tag)
(-> (r/get-by-field :tag :name tag) (-> (r/get-by-field :tag :name tag)
(r/single-cljs)))) (r/single-cljs)))
(defn decrease-tag-counter [tag] (defn decrease-tag-counter [tag]
(let [tag (:name tag) (let [tag (:name tag)
@ -60,13 +60,10 @@
:photo photo :photo photo
:location location :location location
:tags tags :tags tags
:last-updated last-updated} :last-updated last-updated}]
_ (log "Creating discovery") (log/debug "Creating discovery: " discovery tags)
_ (log discovery) (realm/create :discoveries discovery true)
_ (log tags)] (increase-tags-counter tags)))
(do
(realm/create :discoveries discovery true)
(increase-tags-counter tags))))
(defn- update-discovery [{:keys [name status whisper-id photo location tags last-updated]}] (defn- update-discovery [{:keys [name status whisper-id photo location tags last-updated]}]
(let [old-tags (get-tags whisper-id) (let [old-tags (get-tags whisper-id)
@ -78,10 +75,9 @@
:location location :location location
:tags tags :tags tags
:last-updated last-updated}] :last-updated last-updated}]
(do
(decrease-tags-counter old-tags) (decrease-tags-counter old-tags)
(realm/create :discoveries discovery true) (realm/create :discoveries discovery true)
(increase-tags-counter tags)))) (increase-tags-counter tags)))
(defn- discovery-exist? [discoveries discovery] (defn- discovery-exist? [discoveries discovery]
(some #(= (:whisper-id discovery) (:whisper-id %)) discoveries)) (some #(= (:whisper-id discovery) (:whisper-id %)) discoveries))
@ -92,9 +88,7 @@
(defn- add-discoveries [discoveries] (defn- add-discoveries [discoveries]
(realm/write (fn [] (realm/write (fn []
(let [db-discoveries (.slice (discovery-list) 0) (let [db-discoveries (.slice (discovery-list) 0)]
_ (log discoveries)
_ (log (.slice db-discoveries 0))]
(dorun (map (fn [discovery] (dorun (map (fn [discovery]
(if (not (discovery-exist? db-discoveries discovery)) (if (not (discovery-exist? db-discoveries discovery))
(create-discovery discovery) (create-discovery discovery)
@ -106,10 +100,10 @@
(add-discoveries discoveries)) (add-discoveries discoveries))
(defn discoveries-by-tag [tag limit] (defn discoveries-by-tag [tag limit]
(let [_ (log (str "discoveries by tag: " tag))] (log/debug "Discoveries by tag: " tag)
(-> (r/get-by-filter :discoveries (str "tags.name = '" tag "'")) (-> (r/get-by-filter :discoveries (str "tags.name = '" tag "'"))
(r/sorted :last-updated :desc) (r/sorted :last-updated :desc)
(r/page 0 limit)))) (r/page 0 limit)))
(defn get-tag-popular [limit] (defn get-tag-popular [limit]
(-> (r/get-all :tag) (-> (r/get-all :tag)

View File

@ -1,6 +1,5 @@
(ns syng-im.persistence.realm (ns syng-im.persistence.realm
(:require [cljs.reader :refer [read-string]] (:require [cljs.reader :refer [read-string]]
[syng-im.utils.debug :refer [log]]
[syng-im.utils.types :refer [to-string]]) [syng-im.utils.types :refer [to-string]])
(:refer-clojure :exclude [exists?])) (:refer-clojure :exclude [exists?]))
@ -100,9 +99,8 @@
query)) query))
(defn get-by-filter [schema-name filter] (defn get-by-filter [schema-name filter]
(let [_ (log filter)]
(-> (.objects realm (name schema-name)) (-> (.objects realm (name schema-name))
(.filtered filter)))) (.filtered filter)))
(defn get-by-field [schema-name field value] (defn get-by-field [schema-name field value]
(let [q (to-query schema-name :eq field value)] (let [q (to-query schema-name :eq field value)]

View File

@ -2,7 +2,7 @@
(:require-macros [reagent.ratom :refer [reaction]]) (:require-macros [reagent.ratom :refer [reaction]])
(:require [re-frame.core :refer [register-sub]] (:require [re-frame.core :refer [register-sub]]
[syng-im.db :as db] [syng-im.db :as db]
[syng-im.utils.debug :refer [log]] [syng-im.utils.logging :as log]
[syng-im.models.discoveries :refer [discovery-list [syng-im.models.discoveries :refer [discovery-list
get-tag-popular get-tag-popular
discoveries-by-tag discoveries-by-tag
@ -21,8 +21,8 @@
(register-sub :get-discoveries-by-tag (register-sub :get-discoveries-by-tag
(fn [db [_ tag limit]] (fn [db [_ tag limit]]
(let [discoveries-updated (-> (discoveries-updated? @db) (let [discoveries-updated (-> (discoveries-updated? @db)
(reaction)) (reaction))]
_ (log (str "getting discoveries for: " tag))] (log/debug "Getting discoveries for: " tag)
(reaction (reaction
(let [_ @discoveries-updated] (let [_ @discoveries-updated]
(discoveries-by-tag tag limit)))))) (discoveries-by-tag tag limit))))))
@ -30,8 +30,8 @@
(register-sub :get-popular-tags (register-sub :get-popular-tags
(fn [db [_ limit]] (fn [db [_ limit]]
(let [discoveries-updated (-> (discoveries-updated? @db) (let [discoveries-updated (-> (discoveries-updated? @db)
(reaction)) (reaction))]
_ (log (str "getting tags limited: " limit))] (log/debug "Getting tags limited: " limit)
(reaction (reaction
(let [_ @discoveries-updated] (let [_ @discoveries-updated]
(get-tag-popular limit)))))) (get-tag-popular limit))))))

View File

@ -1,3 +0,0 @@
(ns syng-im.utils.debug)
(def log (.-log js/console))