From 780b12049e997190832a06a01245b9ed5f4d1f18 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Sat, 14 May 2016 12:39:01 +0300 Subject: [PATCH] defview suggestion --- src/syng_im/discovery/screen.cljs | 43 +++++++++---------- src/syng_im/discovery/views/popular.cljs | 18 ++++---- src/syng_im/discovery/views/popular_list.cljs | 30 ++++++------- .../discovery/views/popular_list_item.cljs | 1 - src/syng_im/discovery/views/recent.cljs | 15 +++---- src/syng_im/utils/views.clj | 27 ++++++++++++ 6 files changed, 79 insertions(+), 55 deletions(-) create mode 100644 src/syng_im/utils/views.clj diff --git a/src/syng_im/discovery/screen.cljs b/src/syng_im/discovery/screen.cljs index 302a8aa11c..3f3a2fdf8b 100644 --- a/src/syng_im/discovery/screen.cljs +++ b/src/syng_im/discovery/screen.cljs @@ -1,4 +1,5 @@ (ns syng-im.discovery.screen + (:require-macros [syng-im.utils.views :refer [defview]]) (:require [re-frame.core :refer [dispatch subscribe]] [syng-im.components.react :refer [view @@ -29,25 +30,23 @@ (defn toogle-search [current-value] (dispatch [:set ::show-search (not current-value)])) -(defn discovery [] - [] - (let [show-search (subscribe [:get ::show-search])] - (fn [] - [view st/discovery-container - [toolbar - {:style st/discovery-toolbar - :nav-action {:image {:source {:uri :icon_hamburger} - :style st/hamburger-icon} - :handler #(dispatch [:create-fake-discovery!])} - :title "Add Participants" - :content [title-content @show-search] - :action {:image {:source {:uri :icon_search} - :style st/search-icon} - :handler #(toogle-search @show-search)}}] - [scroll-view {:style {}} - [view st/section-spacing - [text {:style st/discovery-subtitle} "Popular tags"]] - [popular] - [view st/section-spacing - [text {:style st/discovery-subtitle} "Recent"]] - [discovery-recent]]]))) +(defview discovery [] + [show-search [:get ::show-search]] + [view st/discovery-container + [toolbar + {:style st/discovery-toolbar + :nav-action {:image {:source {:uri :icon_hamburger} + :style st/hamburger-icon} + :handler #(dispatch [:create-fake-discovery!])} + :title "Add Participants" + :content [title-content show-search] + :action {:image {:source {:uri :icon_search} + :style st/search-icon} + :handler #(toogle-search show-search)}}] + [scroll-view {:style {}} + [view st/section-spacing + [text {:style st/discovery-subtitle} "Popular tags"]] + [popular] + [view st/section-spacing + [text {:style st/discovery-subtitle} "Recent"]] + [discovery-recent]]]) diff --git a/src/syng_im/discovery/views/popular.cljs b/src/syng_im/discovery/views/popular.cljs index 0df56adde7..8d95e8d094 100644 --- a/src/syng_im/discovery/views/popular.cljs +++ b/src/syng_im/discovery/views/popular.cljs @@ -1,4 +1,5 @@ (ns syng-im.discovery.views.popular + (:require-macros [syng-im.utils.views :refer [defview]]) (:require [re-frame.core :refer [subscribe]] [syng-im.utils.logging :as log] @@ -11,12 +12,11 @@ (defn page-width [] (.-width (.get (.. js/React -Dimensions) "window"))) -(defn popular [] - (let [popular-tags (subscribe [:get-popular-tags 3])] - (log/debug "Got popular tags: " @popular-tags) - (if (pos? (count @popular-tags)) - [carousel {:pageStyle st/carousel-page-style - :sneak 20} - (for [{:keys [name count]} @popular-tags] - [discovery-popular-list name count])] - [text "None"]))) +(defview popular [] + [popular-tags [:get-popular-tags 3]] + (if (pos? (count popular-tags)) + [carousel {:pageStyle st/carousel-page-style + :sneak 20} + (for [{:keys [name count]} popular-tags] + [discovery-popular-list name count])] + [text "None"])) diff --git a/src/syng_im/discovery/views/popular_list.cljs b/src/syng_im/discovery/views/popular_list.cljs index c56ac14206..78426570f6 100644 --- a/src/syng_im/discovery/views/popular_list.cljs +++ b/src/syng_im/discovery/views/popular_list.cljs @@ -1,4 +1,5 @@ (ns syng-im.discovery.views.popular-list + (:require-macros [syng-im.utils.views :refer [defview]]) (:require [re-frame.core :refer [subscribe dispatch]] [syng-im.components.react :refer [view @@ -17,18 +18,17 @@ (list-item [view {:style st/row-separator :key row-id}])) -(defn discovery-popular-list [tag count] - (let [discoveries (subscribe [:get-discoveries-by-tag tag 3])] - (fn [tag count] - [view st/popular-list-container - [view st/row - [view st/tag-name-container - [touchable-highlight {:onPress #(dispatch [:show-discovery-tag tag])} - [text {:style st/tag-name} (str " #" (name tag))]]] - [view st/tag-count-container - [text {:style st/tag-count} count]]] - [list-view {:dataSource (to-datasource @discoveries) - :enableEmptySections true - :renderRow render-row - :renderSeparator render-separator - :style st/popular-list}]]))) +(defview discovery-popular-list [tag count] + [discoveries [:get-discoveries-by-tag tag 3]] + [view st/popular-list-container + [view st/row + [view st/tag-name-container + [touchable-highlight {:onPress #(dispatch [:show-discovery-tag tag])} + [text {:style st/tag-name} (str " #" (name tag))]]] + [view st/tag-count-container + [text {:style st/tag-count} count]]] + [list-view {:dataSource (to-datasource discoveries) + :enableEmptySections true + :renderRow render-row + :renderSeparator render-separator + :style st/popular-list}]]) diff --git a/src/syng_im/discovery/views/popular_list_item.cljs b/src/syng_im/discovery/views/popular_list_item.cljs index bb7eb18d91..6812cf4d1c 100644 --- a/src/syng_im/discovery/views/popular_list_item.cljs +++ b/src/syng_im/discovery/views/popular_list_item.cljs @@ -1,5 +1,4 @@ (ns syng-im.discovery.views.popular-list-item - ;syng-im.discovery.views.popular-list-item (:require [syng-im.components.react :refer [view text image]] [syng-im.discovery.styles :as st] [reagent.core :as r])) diff --git a/src/syng_im/discovery/views/recent.cljs b/src/syng_im/discovery/views/recent.cljs index 6eb1dc4038..e690890b2b 100644 --- a/src/syng_im/discovery/views/recent.cljs +++ b/src/syng_im/discovery/views/recent.cljs @@ -1,4 +1,5 @@ (ns syng-im.discovery.views.recent + (:require-macros [syng-im.utils.views :refer [defview]]) (:require [re-frame.core :refer [subscribe]] [syng-im.components.react :refer [view list-view list-item]] @@ -14,11 +15,9 @@ (list-item [view {:style st/row-separator :key row-id}])) -(defn discovery-recent [] - (let [discoveries (subscribe [:get :discoveries])] - (fn [] - ;; todo fetch more on :onEndReached - [list-view {:dataSource (to-datasource @discoveries) - :renderRow render-row - :renderSeparator render-separator - :style st/recent-list}]))) +(defview discovery-recent [] + [discoveries [:get :discoveries]] + [list-view {:dataSource (to-datasource discoveries) + :renderRow render-row + :renderSeparator render-separator + :style st/recent-list}]) diff --git a/src/syng_im/utils/views.clj b/src/syng_im/utils/views.clj new file mode 100644 index 0000000000..3eb1487e76 --- /dev/null +++ b/src/syng_im/utils/views.clj @@ -0,0 +1,27 @@ +(ns syng-im.utils.views) + +(defn prepare-subs [subs] + (let [pairs (map (fn [[form sub]] + {:form form + :sub sub + :sym (gensym)}) + (partition 2 subs))] + [(mapcat (fn [{:keys [sym sub]}] + [sym `(re-frame.core/subscribe ~sub)]) + pairs) + (mapcat (fn [{:keys [sym form]}] + [form `(deref ~sym)]) + pairs)])) + +(defmacro defview + [n params & rest] + (let [[subs body] (if (= 1 (count rest)) + [nil (first rest)] + rest) + [subs-bindings vars-bindings] (prepare-subs subs)] + `(defn ~n ~params + (let [~@subs-bindings] + (fn ~params + (let [~@vars-bindings] + ~body)))))) +