Skip to content

Commit

Permalink
Added seen and viewed to the trade messages table. Open trades will n…
Browse files Browse the repository at this point in the history
…ow be highlighted when they have a new unseen message.
  • Loading branch information
macourtney committed Jul 26, 2011
1 parent 710f724 commit b482a10
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 9 deletions.
19 changes: 15 additions & 4 deletions src/darkexchange/controller/main/home/open_trade_panel.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
[javax.swing.table TableCellRenderer]))

(def requires-action-color (Color/YELLOW))
(def unseen-message-color (Color/CYAN))

(defn find-open-trade-table [main-frame]
(seesaw-core/select main-frame ["#open-trade-table"]))
Expand Down Expand Up @@ -88,13 +89,23 @@
(trade-model/add-delete-trade-listener (fn [trade] (trade-delete-listener main-frame trade)))
main-frame)

(defn set-requires-action-background [render-component table row]
(let [row-map (seesaw-table/value-at table row)
trade (:original-trade row-map)]
(when (trade-model/requires-action? trade)
(.setBackground render-component requires-action-color)
true)))

(defn set-unseen-message-background [render-component table row]
(let [row-map (seesaw-table/value-at table row)]
(when (:unseen-message? row-map)
(.setBackground render-component unseen-message-color))))

(defn set-background [render-component table is-selected row]
(if is-selected
(.setBackground render-component (.getSelectionBackground table))
(let [row-map (seesaw-table/value-at table row)
trade (:original-trade row-map)]
(when (trade-model/requires-action? trade)
(.setBackground render-component requires-action-color)))))
(when-not (set-requires-action-background render-component table row)
(set-unseen-message-background render-component table row))))

(defn attach-trade-table-cell-renderer [main-frame]
(let [open-trade-table (find-open-trade-table main-frame)
Expand Down
3 changes: 2 additions & 1 deletion src/darkexchange/controller/trade/view.clj
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
(when-let [trade-messages (trade-model/table-trade-messages trade)]
(seesaw-core/config! (find-trade-messages-table parent-component)
:model [:columns view-view/trade-messages-table-columns
:rows trade-messages]))
:rows trade-messages])
(future (trade-message-model/mark-as-seen trade-messages)))
parent-component)

(defn load-data [parent-component trade]
Expand Down
9 changes: 9 additions & 0 deletions src/darkexchange/database/h2.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[clojure.contrib.sql :as sql]
[clojure.tools.loading-utils :as conjure-loading-utils]
[clojure.tools.string-utils :as conjure-string-utils]
[clojure.string :as clojure-str]
[darkexchange.database.protocol :as flavor-protocol])
(:import [darkexchange.database.protocol Flavor]
[java.text SimpleDateFormat]
Expand Down Expand Up @@ -141,6 +142,14 @@ any keyword into a string, and replaces dashes with underscores."}
(when (some #(= (.toUpperCase (table-name table)) %) (map :table_name (flavor-protocol/execute-query flavor ["SHOW TABLES"])))
(sql/with-connection (flavor-protocol/db-map flavor)
(sql/drop-table (table-name table))))))

(add-column [flavor table spec]
(flavor-protocol/execute-commands flavor
[(str "ALTER TABLE " (table-name table) " ADD IF NOT EXISTS " (clojure-str/join " " spec))]))

(drop-column [flavor table column]
(flavor-protocol/execute-commands flavor
[(str "ALTER TABLE " (table-name table) " DROP COLUMN IF EXISTS " (column-name column))]))

(describe-table [flavor table]
(do
Expand Down
14 changes: 14 additions & 0 deletions src/darkexchange/database/migrations/010_add_viewed_and_seen.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(ns darkexchange.database.migrations.010-add-viewed-and-seen
(:use darkexchange.database.util))

(defn
#^{ :doc "Adds the viewed and seen columns to the trades table." }
up []
(add-column :trade-messages (integer :seen))
(add-column :trade-messages (integer :viewed)))

(defn
#^{ :doc "Removes the viewed and seen columns from the trades table." }
down []
(drop-column :trade-messages :seen)
(drop-column :trade-messages :viewed))
2 changes: 2 additions & 0 deletions src/darkexchange/database/protocol.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
(create-table [flavor table specs]
"Creates a new table with the given name and with columns based on the given specs.")
(drop-table [flavor table] "Deletes the table with the given name.")
(add-column [flavor table spec] "Adds a column described by spec to the given table.")
(drop-column [flavor table column] "Removes the given column from the given table.")
(describe-table [flavor table] "Shows the columns of the given table.")
(delete [flavor table where] "Deletes rows from the table with the given name.")
(integer [flavor column] [flavor column mods]
Expand Down
4 changes: 4 additions & 0 deletions src/darkexchange/database/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ database flavor function with the current db spec and any arguments"}

(def-db-fn :drop-table)

(def-db-fn :add-column)

(def-db-fn :drop-column)

(defn insert-into [table & records]
(flavor-protocol/insert-into @conjure-flavor table records))

Expand Down
9 changes: 7 additions & 2 deletions src/darkexchange/model/trade.clj
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@
(defn next-step-text [trade]
(waiting-for-key-to-text (next-step-key trade)))

(defn has-unseen-message? [trade]
(trade-message/has-unseen-message? (:id trade)))

(defn convert-to-table-trade [trade]
(let [offer (find-offer trade)]
{ :id (:id trade)
Expand All @@ -182,7 +185,8 @@
:im-receiving-amount (offer/wants-amount-str offer)
:im-receiving-by (offer/wants-payment-type-str offer)
:waiting-for (next-step-text trade)
:original-trade trade }))
:original-trade trade
:unseen-message? (if (has-unseen-message? trade) true false) }))

(defn table-open-trades []
(map convert-to-table-trade (open-trades)))
Expand Down Expand Up @@ -295,4 +299,5 @@
([user] (find-records ["(((updated IS NULL OR updated = 0) AND closed = 1) OR (closed IS NULL OR closed = 0)) AND user_id = ?" (:id user)])))

(defn trade-partner-text [trade]
(identity-model/identity-text (:identity trade)))
(identity-model/identity-text (:identity trade)))

15 changes: 13 additions & 2 deletions src/darkexchange/model/trade_message.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
(update { :id message-id :foreign_message_id foreign-message-id }))

(defn as-table-trade-message [trade-message]
{ :id (:id trade-message) :from (:name (find-identity trade-message)) :date-received (:created_at trade-message) })
{ :id (:id trade-message) :from (:name (find-identity trade-message)) :date-received (:created_at trade-message)
:seen (:seen trade-message) })

(defn update-or-create-message [trade-id message from-identity]
(if-let [foreign-message-id (:foreign_message_id message)]
Expand All @@ -46,4 +47,14 @@
(create-new-message (:body message) { :id trade-id } (:id message) from-identity))))

(defn find-matching-messages [messages]
(map #(find-record { :foreign_message_id (:id %1)}) messages))
(map #(find-record { :foreign_message_id (:id %1)}) messages))

(defn seen? [trade-message]
(as-boolean (:seen trade-message)))

(defn has-unseen-message? [trade-id]
(find-record ["trade_id = ? AND (seen IS NULL OR seen = 0) LIMIT 1" trade-id]))

(defn mark-as-seen [trade-messages]
(doseq [trade-message trade-messages]
(when-not (seen? trade-message) (update { :id (:id trade-message) :seen 1 }))))

0 comments on commit b482a10

Please sign in to comment.