defview suggestion

This commit is contained in:
Roman Volosovskyi 2016-05-14 12:39:01 +03:00
parent 85e05a451d
commit 780b12049e
6 changed files with 79 additions and 55 deletions

View File

@ -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]]])

View File

@ -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"]))

View File

@ -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}]])

View File

@ -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]))

View File

@ -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}])

View File

@ -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))))))