From 4fd5bd6f00bff537d16f1609fc30884720f6708b Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Fri, 13 May 2016 16:52:42 +0300 Subject: [PATCH] tag screen Former-commit-id: 83e4c584cb043302a8a830f979f177333f9d44fa --- src/syng_im/android/core.cljs | 2 +- src/syng_im/db.cljs | 2 - src/syng_im/discovery/discovery_tag.cljs | 57 ------------------- src/syng_im/discovery/handlers.cljs | 24 ++++---- src/syng_im/discovery/screen.cljs | 18 ++---- src/syng_im/discovery/subs.cljs | 22 +++---- src/syng_im/discovery/tag.cljs | 42 ++++++++++++++ src/syng_im/discovery/views/popular.cljs | 2 +- .../popular_list.cljs} | 24 +++----- .../popular_list_item.cljs} | 5 +- .../recent.cljs} | 8 +-- src/syng_im/models/commands.cljs | 7 +-- src/syng_im/models/discoveries.cljs | 9 --- 13 files changed, 88 insertions(+), 134 deletions(-) delete mode 100644 src/syng_im/discovery/discovery_tag.cljs create mode 100644 src/syng_im/discovery/tag.cljs rename src/syng_im/discovery/{discovery_popular_list.cljs => views/popular_list.cljs} (60%) rename src/syng_im/discovery/{discovery_popular_list_item.cljs => views/popular_list_item.cljs} (82%) rename src/syng_im/discovery/{discovery_recent.cljs => views/recent.cljs} (78%) diff --git a/src/syng_im/android/core.cljs b/src/syng_im/android/core.cljs index cec2fd2772..0117816228 100644 --- a/src/syng_im/android/core.cljs +++ b/src/syng_im/android/core.cljs @@ -9,7 +9,7 @@ [syng-im.components.react :refer [navigator app-registry]] [syng-im.contacts.screen :refer [contact-list]] [syng-im.discovery.screen :refer [discovery]] - [syng-im.discovery.discovery-tag :refer [discovery-tag]] + [syng-im.discovery.tag :refer [discovery-tag]] [syng-im.chat.screen :refer [chat]] [syng-im.chats-list.screen :refer [chats-list]] [syng-im.new-group.screen :refer [new-group]] diff --git a/src/syng_im/db.cljs b/src/syng_im/db.cljs index f907e5a618..6e079af948 100644 --- a/src/syng_im/db.cljs +++ b/src/syng_im/db.cljs @@ -39,6 +39,4 @@ [:chats chat-id :command-requests]) (defn chat-command-request-path [chat-id msg-id] [:chats chat-id :command-requests msg-id]) -(def updated-discoveries-signal-path [:discovery-updated-signal]) -(def current-tag-path [:current-tag]) (def updated-current-tag-signal-path [:current-tag-updated-signal]) diff --git a/src/syng_im/discovery/discovery_tag.cljs b/src/syng_im/discovery/discovery_tag.cljs deleted file mode 100644 index 5123eab794..0000000000 --- a/src/syng_im/discovery/discovery_tag.cljs +++ /dev/null @@ -1,57 +0,0 @@ -(ns syng-im.discovery.discovery-tag - (:require - [re-frame.core :refer [subscribe]] - [syng-im.utils.logging :as log] - [syng-im.utils.listview :refer [to-realm-datasource - to-datasource]] - [syng-im.navigation :refer [nav-pop]] - [syng-im.components.react :refer [android? - view - text]] - [syng-im.components.realm :refer [list-view]] - [syng-im.components.toolbar :refer [toolbar]] - [reagent.core :as r] - [syng-im.discovery.discovery-popular-list-item :refer [discovery-popular-list-item]] - [syng-im.discovery.styles :as st])) - -(defn render-row [row section-id row-id] - (log/debug "discovery-tag-row: " row section-id row-id) - (if row - (let [elem (discovery-popular-list-item row)] - elem) - (r/as-element [text "null"]) - )) - -(defn render-separator [sectionID, rowID, adjacentRowHighlighted] - (let [elem (r/as-element [view {:style st/row-separator - :key rowID}])] - elem)) - -(defn title-content [tag] - [view {:style st/tag-title-container} - [text {:style st/tag-title} - (str " #" tag)]]) - -(defn discovery-tag [{:keys [tag navigator]}] - (let [tag (subscribe [:get-current-tag]) - discoveries (subscribe [:get-discoveries-by-tag @tag 0])] - (log/debug "Got discoveries: " @discoveries) - (fn [] - (let [items @discoveries - datasource (to-realm-datasource items)] - [view {:style st/discovery-tag-container} - [toolbar {:navigator navigator - :nav-action {:image {:source {:uri "icon_back"} - :style st/icon-back} - :handler (fn [] (nav-pop navigator))} - :title "Add Participants" - :content (title-content @tag) - :action {:image {:source {:uri "icon_search"} - :style st/icon-search} - :handler (fn [] - ())}}] - - [list-view {:dataSource datasource - :renderRow render-row - :renderSeparator render-separator - :style st/recent-list}]])))) diff --git a/src/syng_im/discovery/handlers.cljs b/src/syng_im/discovery/handlers.cljs index c1cdf901c4..0b00f15cd9 100644 --- a/src/syng_im/discovery/handlers.cljs +++ b/src/syng_im/discovery/handlers.cljs @@ -2,17 +2,18 @@ (:require [re-frame.core :refer [register-handler after dispatch enrich log-ex debug]] [syng-im.protocol.api :as api] - [syng-im.models.discoveries :refer [save-discoveries - set-current-tag]] + [syng-im.models.discoveries :refer [save-discoveries]] [syng-im.navigation.handlers :as nav] [syng-im.models.discoveries :as discoveries] [syng-im.utils.handlers :as u])) (defmethod nav/preload-data! :discovery - [{:keys [] :as db} _] - (-> db - (assoc :tags (discoveries/all-tags)) - (assoc :discoveries (discoveries/discovery-list)))) + [{:keys [discoveries] :as db} _] + (if-not (seq discoveries) + (-> db + (assoc :tags (discoveries/all-tags)) + (assoc :discoveries (discoveries/discovery-list))) + db)) (register-handler :discovery-response-received (u/side-effect! @@ -29,15 +30,14 @@ (dispatch [:add-discovery discovery]))))) (register-handler :broadcast-status - (fn [{:keys [name] :as db} [_ status hashtags]] - (api/broadcast-discover-status name status hashtags) - db)) + (u/side-effect! + (fn [{:keys [name]} [_ status hashtags]] + (api/broadcast-discover-status name status hashtags)))) (register-handler :show-discovery-tag (fn [db [_ tag]] - (let [db (set-current-tag db tag)] - (dispatch [:navigate-to :discovery-tag]) - db))) + (dispatch [:navigate-to :discovery-tag]) + (assoc db :current-tag tag))) ;; todo remove this (register-handler :create-fake-discovery! diff --git a/src/syng_im/discovery/screen.cljs b/src/syng_im/discovery/screen.cljs index eb1838fcd7..751125324a 100644 --- a/src/syng_im/discovery/screen.cljs +++ b/src/syng_im/discovery/screen.cljs @@ -1,27 +1,21 @@ (ns syng-im.discovery.screen - (:require - [syng-im.utils.logging :as log] [re-frame.core :refer [dispatch subscribe]] - [syng-im.models.discoveries :refer [save-discoveries]] - [syng-im.components.react :refer [android? - view + [syng-im.components.react :refer [view scroll-view text text-input]] - [reagent.core :as r] [syng-im.components.toolbar :refer [toolbar]] [syng-im.discovery.views.popular :refer [popular]] - [syng-im.discovery.discovery-recent :refer [discovery-recent]] - [syng-im.discovery.styles :as st] - [syng-im.persistence.realm :as realm])) + [syng-im.discovery.views.recent :refer [discovery-recent]] + [syng-im.discovery.styles :as st])) (defn get-hashtags [status] (let [hashtags (map #(subs % 1) (re-seq #"#[^ !?,;:.]+" status))] (or hashtags []))) -(defn title-content [showSearch] - (if showSearch +(defn title-content [show-search] + (if show-search [text-input {:style st/discovery-search-input :autoFocus true :placeholder "Type your search tags here" @@ -48,7 +42,7 @@ :height 12}} :handler #(dispatch [:create-fake-discovery!])} :title "Add Participants" - :content (title-content @show-search) + :content [title-content @show-search] :action {:image {:source {:uri :icon_search} :style {:width 17 :height 17}} diff --git a/src/syng_im/discovery/subs.cljs b/src/syng_im/discovery/subs.cljs index 09c98628a6..053094706b 100644 --- a/src/syng_im/discovery/subs.cljs +++ b/src/syng_im/discovery/subs.cljs @@ -1,26 +1,20 @@ (ns syng-im.discovery.subs (:require-macros [reagent.ratom :refer [reaction]]) - (:require [re-frame.core :refer [register-sub]] - [syng-im.models.discoveries :refer [current-tag - current-tag-updated?]])) + (:require [re-frame.core :refer [register-sub]])) (register-sub :get-discoveries-by-tag (fn [db [_ tag limit]] - (let [discoveries (reaction (:discoveries @db))] + (let [discoveries (reaction (:discoveries @db)) + tag' (or tag (:current-tag @db)) + filter-tag (filter #(some #{tag'} (map :name (:tags %)))) + xform (if limit + (comp filter-tag (take limit)) + filter-tag)] (->> @discoveries - (into [] (comp (filter #(some #{tag} (map :name (:tags %)))) - (take limit))) + (into [] xform) (reaction))))) (register-sub :get-popular-tags (fn [db [_ limit]] (reaction (take limit (:tags @db))))) -(register-sub :get-current-tag - (fn [db _] - (let [current-tag-updated (-> (current-tag-updated? @db) - (reaction))] - (reaction - (let [_ @current-tag-updated] - (current-tag @db)))))) - diff --git a/src/syng_im/discovery/tag.cljs b/src/syng_im/discovery/tag.cljs new file mode 100644 index 0000000000..ab04eb55c9 --- /dev/null +++ b/src/syng_im/discovery/tag.cljs @@ -0,0 +1,42 @@ +(ns syng-im.discovery.tag + (:require + [re-frame.core :refer [subscribe dispatch]] + [syng-im.utils.logging :as log] + [syng-im.utils.listview :refer [to-datasource2]] + [syng-im.components.react :refer [view text list-view list-item]] + [syng-im.components.toolbar :refer [toolbar]] + [syng-im.discovery.views.popular-list-item :refer [popular-list-item]] + [syng-im.discovery.styles :as st])) + +(defn render-row [row _ _] + (list-item [popular-list-item row])) + +(defn render-separator [_ row-id _] + (list-item [view {:style st/row-separator + :key row-id}])) + +(defn title-content [tag] + [view st/tag-title-container + [text {:style st/tag-title} (str " #" tag)]]) + +(defn discovery-tag [] + (let [tag (subscribe [:get :current-tag]) + discoveries (subscribe [:get-discoveries-by-tag])] + (log/debug "Got discoveries: " @discoveries) + (fn [] + (let [items @discoveries + datasource (to-datasource2 items)] + [view st/discovery-tag-container + [toolbar {:nav-action {:image {:source {:uri :icon_back} + :style st/icon-back} + :handler #(dispatch [:navigate-back])} + :title "Add Participants" + :content (title-content @tag) + :action {:image {:source {:uri :icon_search} + :style st/icon-search} + :handler (fn [])}}] + + [list-view {:dataSource datasource + :renderRow render-row + :renderSeparator render-separator + :style st/recent-list}]])))) diff --git a/src/syng_im/discovery/views/popular.cljs b/src/syng_im/discovery/views/popular.cljs index 91bd8f8b7a..0df56adde7 100644 --- a/src/syng_im/discovery/views/popular.cljs +++ b/src/syng_im/discovery/views/popular.cljs @@ -6,7 +6,7 @@ text]] [syng-im.components.carousel.carousel :refer [carousel]] [syng-im.discovery.styles :as st] - [syng-im.discovery.discovery-popular-list :refer [discovery-popular-list]])) + [syng-im.discovery.views.popular-list :refer [discovery-popular-list]])) (defn page-width [] (.-width (.get (.. js/React -Dimensions) "window"))) diff --git a/src/syng_im/discovery/discovery_popular_list.cljs b/src/syng_im/discovery/views/popular_list.cljs similarity index 60% rename from src/syng_im/discovery/discovery_popular_list.cljs rename to src/syng_im/discovery/views/popular_list.cljs index 9eebed2463..4a82a4573d 100644 --- a/src/syng_im/discovery/discovery_popular_list.cljs +++ b/src/syng_im/discovery/views/popular_list.cljs @@ -1,27 +1,21 @@ -(ns syng-im.discovery.discovery-popular-list +(ns syng-im.discovery.views.popular-list (:require - [re-frame.core :refer [subscribe dispatch dispatch-sync]] - [syng-im.utils.logging :as log] - [syng-im.components.react :refer [android? - view + [re-frame.core :refer [subscribe dispatch]] + [syng-im.components.react :refer [view list-view list-item touchable-highlight - text - image]] - [reagent.core :as r] + text]] [syng-im.discovery.styles :as st] - [syng-im.utils.listview :refer [to-realm-datasource to-datasource2]] - [syng-im.discovery.discovery-popular-list-item :refer [discovery-popular-list-item] ]) - ) - + [syng-im.utils.listview :refer [to-datasource2]] + [syng-im.discovery.views.popular-list-item :refer [popular-list-item]])) (defn render-row [row _ _] - (list-item [discovery-popular-list-item row])) + (list-item [popular-list-item row])) -(defn render-separator [sectionID rowID adjacentRowHighlighted] +(defn render-separator [_ row-id _] (list-item [view {:style st/row-separator - :key rowID}])) + :key row-id}])) (defn discovery-popular-list [tag count] (let [discoveries (subscribe [:get-discoveries-by-tag tag 3])] diff --git a/src/syng_im/discovery/discovery_popular_list_item.cljs b/src/syng_im/discovery/views/popular_list_item.cljs similarity index 82% rename from src/syng_im/discovery/discovery_popular_list_item.cljs rename to src/syng_im/discovery/views/popular_list_item.cljs index 65df82164a..bb7eb18d91 100644 --- a/src/syng_im/discovery/discovery_popular_list_item.cljs +++ b/src/syng_im/discovery/views/popular_list_item.cljs @@ -1,9 +1,10 @@ -(ns syng-im.discovery.discovery-popular-list-item +(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])) -(defn discovery-popular-list-item +(defn popular-list-item [{:keys [name status]}] [view st/popular-list-item [view st/popular-list-item-name-container diff --git a/src/syng_im/discovery/discovery_recent.cljs b/src/syng_im/discovery/views/recent.cljs similarity index 78% rename from src/syng_im/discovery/discovery_recent.cljs rename to src/syng_im/discovery/views/recent.cljs index d9128b2fef..598bcd66f8 100644 --- a/src/syng_im/discovery/discovery_recent.cljs +++ b/src/syng_im/discovery/views/recent.cljs @@ -1,14 +1,14 @@ -(ns syng-im.discovery.discovery-recent +(ns syng-im.discovery.views.recent (:require [re-frame.core :refer [subscribe]] [syng-im.components.react :refer [view list-view list-item]] [syng-im.utils.listview :refer [to-realm-datasource to-datasource2]] [syng-im.discovery.styles :as st] - [syng-im.discovery.discovery-popular-list-item - :refer [discovery-popular-list-item]])) + [syng-im.discovery.views.popular-list-item + :refer [popular-list-item]])) (defn render-row [row _ _] - (list-item [discovery-popular-list-item row])) + (list-item [popular-list-item row])) (defn render-separator [_ row-id _] (list-item [view {:style st/row-separator diff --git a/src/syng_im/models/commands.cljs b/src/syng_im/models/commands.cljs index 177dfaacfd..15d7ffc8fa 100644 --- a/src/syng_im/models/commands.cljs +++ b/src/syng_im/models/commands.cljs @@ -1,12 +1,9 @@ (ns syng-im.models.commands (:require [clojure.string :refer [join split]] [clojure.walk :refer [stringify-keys keywordize-keys]] - [cljs.core.async :as async :refer [chan put! !]] - [re-frame.core :refer [subscribe dispatch dispatch-sync]] + [re-frame.core :refer [subscribe dispatch]] [syng-im.db :as db] - [syng-im.components.styles :refer [color-blue - color-dark-mint]] - [syng-im.utils.utils :refer [log toast]])) + [syng-im.components.styles :refer [color-blue color-dark-mint]])) ;; todo delete (def commands [{:command :money diff --git a/src/syng_im/models/discoveries.cljs b/src/syng_im/models/discoveries.cljs index f374f6ba03..6e8ea4cf0d 100644 --- a/src/syng_im/models/discoveries.cljs +++ b/src/syng_im/models/discoveries.cljs @@ -5,15 +5,6 @@ [syng-im.persistence.realm :as r] [syng-im.db :as db])) -(defn current-tag-updated? [db] - (get-in db db/updated-current-tag-signal-path)) - -(defn current-tag [db] - (get-in db db/current-tag-path)) - -(defn set-current-tag [db tag] - (assoc-in db db/current-tag-path tag)) - (defn get-tag [tag] (log/debug "Getting tag: " tag) (-> (r/get-by-field :tag :name tag)