From 38f7ea1ebf36d2578b0750b5c9312db8c0af5274 Mon Sep 17 00:00:00 2001 From: Adrian Tiberius Date: Sat, 30 Apr 2016 22:48:30 +0300 Subject: [PATCH] cleaned logging and changed it to use utils.logging; splitted carousel to multiple functions Former-commit-id: 5aef032c599b73e31d1c7aa46f89f2b228a69477 --- src/syng_im/components/carousel.cljs | 178 ++++++++---------- .../discovery-popular-list-item.cljs | 9 +- .../discovery/discovery-recent.cljs | 10 +- .../components/discovery/discovery.cljs | 4 +- .../discovery/discovery_popular.cljs | 15 +- .../discovery/discovery_popular_list.cljs | 57 +++--- src/syng_im/handlers/discovery.cljs | 7 +- src/syng_im/models/discoveries.cljs | 28 ++- src/syng_im/persistence/realm.cljs | 4 +- src/syng_im/subscribers/discovery.cljs | 10 +- src/syng_im/utils/debug.cljs | 3 - 11 files changed, 142 insertions(+), 183 deletions(-) delete mode 100644 src/syng_im/utils/debug.cljs diff --git a/src/syng_im/components/carousel.cljs b/src/syng_im/components/carousel.cljs index f9ff306f0f..f032b4a1cc 100644 --- a/src/syng_im/components/carousel.cljs +++ b/src/syng_im/components/carousel.cljs @@ -4,7 +4,7 @@ scroll-view touchable-without-feedback text]] - [syng-im.utils.debug :refer [log]])) + [syng-im.utils.logging :as log])) (defn page-width [] @@ -24,21 +24,15 @@ (defn get-page-width [data] (get data :pageWidth (:pageWidth defaults))) - (defn calculate-gap [component props] (let [prop-page-width (get-page-width props) page-width (page-width) sneak (get-sneak props) - gap (quot (- (- page-width (* 2 sneak)) prop-page-width) 2) - _ (log "calculate-gap") - _ (log component) - _ (log props) - _ (log page-width) - _ (log prop-page-width) - _ (log sneak) - _ (log gap)] - (if (> prop-page-width page-width) - (println "Invalid pageWidth")) + gap (quot (- (- page-width (* 2 sneak)) prop-page-width) 2)] + (log/debug "calculate-gap: prop-page-width=" prop-page-width + "; page-width=" page-width"; sneak=" sneak "; gap=" gap) + (when (> prop-page-width page-width) + (log/warn "Invalid pageWidth")) (reagent.core/set-state component {:gap gap}) )) @@ -46,105 +40,99 @@ (.scrollTo (.-scrollView component) (clj->js {:y y :x x}))) +(defn get-current-position [event] + (+ (.-x (.-contentOffset (.-nativeEvent event))) (quot (page-width) 2))) + (defn on-scroll-end [event component] - (let [_ (log "on-scroll-end") - _ (log event) - _ (log component) - props (reagent.core/props component) - _ (log props) + (let [props (reagent.core/props component) state (reagent.core/state component) - _ (log state) prop-page-width (get-page-width props) - _ (log (str "prop-page-width: " prop-page-width)) sneak (get-sneak props) - _ (log (str "sneak: " sneak)) gap (get-gap state) - _ (log (str "gap: " gap)) page-offset (+ prop-page-width gap) - _ (log (str "page-offset: " page-offset)) - current-position (+ (.-x (.-contentOffset (.-nativeEvent event))) (quot (page-width) 2)) - _ (log (str "current-position: " current-position)) + current-position (get-current-position event) 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) (reagent.core/set-state component {:activePage current-page}) - (if (:onPageChange props) + (when (:onPageChange props) ((:onPageChange props) current-page)))) (defn go-to-page [component position] - (let [_ (log "go-to-page") - props (reagent.core/props component) + (let [props (reagent.core/props component) state (reagent.core/state component) props-page-width (get-page-width props) - _ (log (str "props page width: " props-page-width)) gap (get-gap state) - _ (log (str "gap: " gap)) - page-position (* position (+ props-page-width gap)) - _ (log (str "page position: " page-position))] + page-position (* position (+ props-page-width gap))] + (log/debug "go-to-page: props-page-width=" props-page-width "; gap=" gap + "; page-position=" page-position) (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] - (let [_ (log "Creating carousel component: ") - _ (log data) - _ (log children)] - (reagent.core/create-class {:component-did-mount (fn [this] - (let [_ (log this)] - (if (> (.-initialPage (.-props this)) 0) - (go-to-page this (.-initialPage (.-props this)))))) - - :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 - - - ) \ No newline at end of file + (let [component-data {:component-did-mount component-did-mount + :component-will-mount component-will-mount + :component-will-receive-props component-will-receive-props + :display-name "carousel" + :reagent-render reagent-render}] + (log/debug "Creating carousel component: " data children) + (reagent.core/create-class component-data))) diff --git a/src/syng_im/components/discovery/discovery-popular-list-item.cljs b/src/syng_im/components/discovery/discovery-popular-list-item.cljs index de3dcd457b..fd0c55a7b6 100644 --- a/src/syng_im/components/discovery/discovery-popular-list-item.cljs +++ b/src/syng_im/components/discovery/discovery-popular-list-item.cljs @@ -1,6 +1,6 @@ (ns syng-im.components.discovery.discovery-popular-list-item (:require - [syng-im.utils.debug :refer [log]] + [syng-im.utils.logging :as log] [syng-im.components.react :refer [android? view text @@ -10,8 +10,8 @@ ) (defn discovery-popular-list-item [discovery] - (let [_ (log discovery)] - (r/as-element [view {:style {:flexDirection "row" + (log/debug discovery) + (r/as-element [view {:style {:flexDirection "row" :paddingTop 10 :paddingBottom 10}} [view {:style {:flex 0.8 @@ -34,5 +34,4 @@ :height 30} :source res/user-no-photo}] ] - ]) - )) \ No newline at end of file + ])) \ No newline at end of file diff --git a/src/syng_im/components/discovery/discovery-recent.cljs b/src/syng_im/components/discovery/discovery-recent.cljs index ef4c93dfb9..a7b1fb0566 100644 --- a/src/syng_im/components/discovery/discovery-recent.cljs +++ b/src/syng_im/components/discovery/discovery-recent.cljs @@ -20,7 +20,7 @@ (defn render-separator [sectionID, rowID, adjacentRowHighlighted] (let [elem (r/as-element [view {:style {:borderBottomWidth 1 - :borderBottomColor "#eff2f3"} + :borderBottomColor "#eff2f3"} :key rowID}])] elem)) @@ -28,8 +28,8 @@ (let [discoveries (subscribe [:get-discoveries]) datasource (to-realm-datasource @discoveries)] [list-view {:dataSource datasource - :renderRow render-row - :renderSeparator render-separator - :style {:backgroundColor "white" - :paddingLeft 15}}] + :renderRow render-row + :renderSeparator render-separator + :style {:backgroundColor "white" + :paddingLeft 15}}] )) \ No newline at end of file diff --git a/src/syng_im/components/discovery/discovery.cljs b/src/syng_im/components/discovery/discovery.cljs index f455bfc0a6..c4ec3e989d 100644 --- a/src/syng_im/components/discovery/discovery.cljs +++ b/src/syng_im/components/discovery/discovery.cljs @@ -1,7 +1,7 @@ (ns syng-im.components.discovery.discovery (:require - [syng-im.utils.debug :refer [log]] + [syng-im.utils.logging :as log] [re-frame.core :refer [dispatch]] [syng-im.models.discoveries :refer [save-discoveries]] [syng-im.components.react :refer [android? @@ -67,7 +67,7 @@ :placeholder "Type your search tags here" :onChangeText (fn [new-text] (let [old-text (:search @search-input)] - (log (str new-text "-" old-text)))) + (log/debug (str new-text "-" old-text)))) :onSubmitEditing (fn [e] (let [search (aget e "nativeEvent" "text") hashtags (get-hashtags search)] diff --git a/src/syng_im/components/discovery/discovery_popular.cljs b/src/syng_im/components/discovery/discovery_popular.cljs index 418d60603c..1dae49a7f1 100644 --- a/src/syng_im/components/discovery/discovery_popular.cljs +++ b/src/syng_im/components/discovery/discovery_popular.cljs @@ -1,7 +1,7 @@ (ns syng-im.components.discovery.discovery-popular (:require [re-frame.core :refer [subscribe]] - [syng-im.utils.debug :refer [log]] + [syng-im.utils.logging :as log] [syng-im.components.react :refer [android? text]] [syng-im.components.carousel :refer [carousel]] @@ -12,9 +12,8 @@ (.-width (.get (.. js/React -Dimensions) "window"))) (defn discovery-popular [] - (let [popular-tags (subscribe [:get-popular-tags 3]) - _ (log "Got popular tags: ") - _ (log @popular-tags)] + (let [popular-tags (subscribe [:get-popular-tags 3])] + (log/debug "Got popular tags: " @popular-tags) (if (> (count @popular-tags) 0) [carousel {:pageStyle {:borderRadius 1 :shadowColor "black" @@ -25,14 +24,8 @@ :pageWidth (- (page-width) 60) :sneak 20} (for [tag @popular-tags] - (discovery-popular-list (.-name tag))) - ] + (discovery-popular-list (.-name tag)))] [text "None"] ) ) - ) - -(comment - (set! React (js/require "react-native")) - (.get (.Dimensions React) "window") ) \ No newline at end of file diff --git a/src/syng_im/components/discovery/discovery_popular_list.cljs b/src/syng_im/components/discovery/discovery_popular_list.cljs index fdb8199120..5938e3e9af 100644 --- a/src/syng_im/components/discovery/discovery_popular_list.cljs +++ b/src/syng_im/components/discovery/discovery_popular_list.cljs @@ -1,10 +1,7 @@ (ns syng-im.components.discovery.discovery-popular-list - (:require-macros - [natal-shell.data-source :refer [data-source clone-with-rows]] - ) (:require [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? view list-view @@ -29,33 +26,27 @@ elem)) (defn discovery-popular-list [tag] - (let [discoveries (subscribe [:get-discoveries-by-tag tag 3]) - _ (log (str "Got discoveries for tag (" tag "): ") @discoveries) - _ (log @discoveries)] + (let [discoveries (subscribe [:get-discoveries-by-tag tag 3])] + (log/debug "Got discoveries for tag (" tag "): " @discoveries) [view {:style {:flex 1 - :backgroundColor "white" - :paddingLeft 10 - :paddingTop 10}} - [view {:style {:flexDirection "row" - :backgroundColor "white" - :padding 0}} - [view {:style {:flexDirection "column" - :backgroundColor "#e9f7fe" - :borderRadius 5 - :padding 0}} - [text {:style {:color "#6092df" - :paddingRight 5 - :paddingBottom 2 - :alignItems "center" - :justifyContent "center"}} (str " #" (name tag))]]] - [list-view {:dataSource (to-realm-datasource @discoveries) - :renderRow render-row - :renderSeparator render-separator - :style {:backgroundColor "white"}}] - ])) - -(comment - list-view {:dataSource elements - :renderRow (partial render-row list-element) - :style {:backgroundColor "white"}} - ) \ No newline at end of file + :backgroundColor "white" + :paddingLeft 10 + :paddingTop 10}} + [view {:style {:flexDirection "row" + :backgroundColor "white" + :padding 0}} + [view {:style {:flexDirection "column" + :backgroundColor "#e9f7fe" + :borderRadius 5 + :padding 0}} + [text {:style {:color "#6092df" + :paddingRight 5 + :paddingBottom 2 + :alignItems "center" + :justifyContent "center"}} + (str " #" (name tag))]]] + [list-view {:dataSource (to-realm-datasource @discoveries) + :renderRow render-row + :renderSeparator render-separator + :style {:backgroundColor "white"}}] + ])) diff --git a/src/syng_im/handlers/discovery.cljs b/src/syng_im/handlers/discovery.cljs index f1d4661ff5..b1d81d472a 100644 --- a/src/syng_im/handlers/discovery.cljs +++ b/src/syng_im/handlers/discovery.cljs @@ -1,6 +1,6 @@ (ns syng-im.handlers.discovery (: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.models.discoveries :refer [save-discoveries signal-discoveries-updated]])) @@ -27,8 +27,7 @@ (register-handler :broadcast-status (fn [db [action status hashtags]] - (let [_ (log "Status: " status) - _ (log "Hashtags: " hashtags) - name (:name db)] + (let [name (:name db)] + (log/debug "Status: " status ", Hashtags: " hashtags) (api/broadcast-discover-status name status hashtags) db))) \ No newline at end of file diff --git a/src/syng_im/models/discoveries.cljs b/src/syng_im/models/discoveries.cljs index dad0be5f64..91cc9d9030 100644 --- a/src/syng_im/models/discoveries.cljs +++ b/src/syng_im/models/discoveries.cljs @@ -1,7 +1,7 @@ (ns syng-im.models.discoveries (:require [cljs.core.async :as async :refer [chan put! !]] [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 r] [syng-im.resources :as res] @@ -17,9 +17,9 @@ (get-in db db/updated-discoveries-signal-path)) (defn get-tag [tag] - (let [_ (log (str "Getting tag: " tag))] + (log/debug "Getting tag: " tag) (-> (r/get-by-field :tag :name tag) - (r/single-cljs)))) + (r/single-cljs))) (defn decrease-tag-counter [tag] (let [tag (:name tag) @@ -60,13 +60,10 @@ :photo photo :location location :tags tags - :last-updated last-updated} - _ (log "Creating discovery") - _ (log discovery) - _ (log tags)] - (do - (realm/create :discoveries discovery true) - (increase-tags-counter tags)))) + :last-updated last-updated}] + (log/debug "Creating discovery: " discovery tags) + (realm/create :discoveries discovery true) + (increase-tags-counter tags))) (defn- update-discovery [{:keys [name status whisper-id photo location tags last-updated]}] (let [old-tags (get-tags whisper-id) @@ -78,10 +75,9 @@ :location location :tags tags :last-updated last-updated}] - (do (decrease-tags-counter old-tags) (realm/create :discoveries discovery true) - (increase-tags-counter tags)))) + (increase-tags-counter tags))) (defn- discovery-exist? [discoveries discovery] (some #(= (:whisper-id discovery) (:whisper-id %)) discoveries)) @@ -92,9 +88,7 @@ (defn- add-discoveries [discoveries] (realm/write (fn [] - (let [db-discoveries (.slice (discovery-list) 0) - _ (log discoveries) - _ (log (.slice db-discoveries 0))] + (let [db-discoveries (.slice (discovery-list) 0)] (dorun (map (fn [discovery] (if (not (discovery-exist? db-discoveries discovery)) (create-discovery discovery) @@ -106,10 +100,10 @@ (add-discoveries discoveries)) (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/sorted :last-updated :desc) - (r/page 0 limit)))) + (r/page 0 limit))) (defn get-tag-popular [limit] (-> (r/get-all :tag) diff --git a/src/syng_im/persistence/realm.cljs b/src/syng_im/persistence/realm.cljs index 27483c3c86..4af9552dc5 100644 --- a/src/syng_im/persistence/realm.cljs +++ b/src/syng_im/persistence/realm.cljs @@ -1,6 +1,5 @@ (ns syng-im.persistence.realm (:require [cljs.reader :refer [read-string]] - [syng-im.utils.debug :refer [log]] [syng-im.utils.types :refer [to-string]]) (:refer-clojure :exclude [exists?])) @@ -100,9 +99,8 @@ query)) (defn get-by-filter [schema-name filter] - (let [_ (log filter)] (-> (.objects realm (name schema-name)) - (.filtered filter)))) + (.filtered filter))) (defn get-by-field [schema-name field value] (let [q (to-query schema-name :eq field value)] diff --git a/src/syng_im/subscribers/discovery.cljs b/src/syng_im/subscribers/discovery.cljs index 601e65b484..eeca5fe593 100644 --- a/src/syng_im/subscribers/discovery.cljs +++ b/src/syng_im/subscribers/discovery.cljs @@ -2,7 +2,7 @@ (:require-macros [reagent.ratom :refer [reaction]]) (:require [re-frame.core :refer [register-sub]] [syng-im.db :as db] - [syng-im.utils.debug :refer [log]] + [syng-im.utils.logging :as log] [syng-im.models.discoveries :refer [discovery-list get-tag-popular discoveries-by-tag @@ -21,8 +21,8 @@ (register-sub :get-discoveries-by-tag (fn [db [_ tag limit]] (let [discoveries-updated (-> (discoveries-updated? @db) - (reaction)) - _ (log (str "getting discoveries for: " tag))] + (reaction))] + (log/debug "Getting discoveries for: " tag) (reaction (let [_ @discoveries-updated] (discoveries-by-tag tag limit)))))) @@ -30,8 +30,8 @@ (register-sub :get-popular-tags (fn [db [_ limit]] (let [discoveries-updated (-> (discoveries-updated? @db) - (reaction)) - _ (log (str "getting tags limited: " limit))] + (reaction))] + (log/debug "Getting tags limited: " limit) (reaction (let [_ @discoveries-updated] (get-tag-popular limit)))))) \ No newline at end of file diff --git a/src/syng_im/utils/debug.cljs b/src/syng_im/utils/debug.cljs deleted file mode 100644 index aa5f7202ed..0000000000 --- a/src/syng_im/utils/debug.cljs +++ /dev/null @@ -1,3 +0,0 @@ -(ns syng-im.utils.debug) - -(def log (.-log js/console)) \ No newline at end of file