create subscriptions for wallet data bindings

This commit is contained in:
Eric Dvorsak 2017-08-23 02:01:19 +02:00 committed by Julien Eluard
parent e169949e0a
commit 37a67b3ccf
6 changed files with 147 additions and 88 deletions

View File

@ -7,6 +7,7 @@
status-im.ui.screens.discover.subs
status-im.ui.screens.contacts.subs
status-im.ui.screens.group.subs
status-im.ui.screens.wallet.subs
status-im.transactions.subs
status-im.bots.subs))

View File

@ -5,3 +5,15 @@
;; TODO(oskarth): spec for balance as BigNumber
;; TODO(oskarth): Spec for prices as as: {:from ETH, :to USD, :price 290.11, :last-day 304.17}
(def dummy-transaction-data
[{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "0,4908" :symbol "ETH"} :state :unsigned}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :unsigned}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :unsigned}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "0,4908" :symbol "ETH"} :state :pending}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :pending}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :sent}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :postponed}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "0,4908" :symbol "ETH"} :state :pending}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :pending}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :sent}])

View File

@ -1,7 +1,9 @@
(ns status-im.ui.screens.wallet.events
(:require [re-frame.core :as re-frame :refer [dispatch reg-fx]]
[status-im.utils.handlers :as handlers]
[status-im.utils.prices :as prices]))
[status-im.utils.prices :as prices]
[status-im.ui.screens.wallet.db :as wallet.db]
[status-im.components.status :as status]))
(defn get-balance [{:keys [web3 account-id on-success on-error]}]
(if (and web3 account-id)
@ -52,12 +54,13 @@
:account-id current-account-id
:success-event :update-balance
:error-event :update-balance-fail}
:dispatch [:load-prices]}))
:dispatch [:load-prices]
:db (assoc-in db [:wallet :transactions] wallet.db/dummy-transaction-data)}))
(handlers/register-handler-db
:update-balance
(fn [db [_ balance]]
(assoc db :wallet {:balance balance})))
(assoc-in db [:wallet :balance] balance)))
(handlers/register-handler-db
:update-prices

View File

@ -59,37 +59,32 @@
[action-buttons m])]
[list/item-icon :icons/forward]])
(def dummy-transaction-data
[{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "0,4909" :symbol "ETH"} :state :unsigned}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :unsigned}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :unsigned}])
(def dummy-transaction-data-sorted
[{:title "Postponed"
:key :postponed
:data [{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "0,4909" :symbol "ETH"} :state :pending}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :pending}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :sent}]}
{:title "Pending"
:key :pending
:data [{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "0,4909" :symbol "ETH"} :state :pending}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :pending}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :sent}]}])
;; TODO(yenda) hook with re-frame
(defn empty-text [s]
[rn/text {:style st/empty-text} s])
(defview history-list []
[list/section-list {:sections dummy-transaction-data-sorted
(letsubs [pending-transactions [:wallet/pending-transactions]
postponed-transactions [:wallet/postponed-transactions]
sent-transactions [:wallet/sent-transactions]]
[list/section-list {:sections [{:title "Postponed"
:key :postponed
:data postponed-transactions}
{:title "Pending"
:key :pending
:data pending-transactions}
{:title "Sent"
:key :sent
:data sent-transactions}]
:render-fn render-transaction
:empty-component (empty-text (i18n/label :t/transactions-history-empty))}])
:empty-component (empty-text (i18n/label :t/transactions-history-empty))}]))
(defview unsigned-list []
[list/flat-list {:data dummy-transaction-data
(letsubs [transactions [:wallet/unsigned-transactions]]
[list/flat-list {:data transactions
:render-fn render-transaction
:empty-component (empty-text (i18n/label :t/transactions-unsigned-empty))}])
:empty-component (empty-text (i18n/label :t/transactions-unsigned-empty))}]))
(def tab-list
[{:view-id :wallet-transactions-unsigned

View File

@ -88,31 +88,12 @@
[list/flat-list {:data assets
:render-fn render-asset-fn}]]))
(defn eth-balance [{:keys [balance]}]
(when balance
(money/wei->ether balance)))
(defn portfolio-value [{:keys [balance]} {:keys [price]}]
(when (and balance price)
(-> (money/wei->ether balance)
(money/eth->usd price)
(money/with-precision 2)
str)))
(defn portfolio-change [{:keys [price last-day]}]
(when (and price last-day)
(-> (money/percent-change price last-day)
(money/with-precision 2)
(str "%"))))
(defview wallet []
(letsubs [wallet [:get :wallet]
prices [:get :prices]]
(let [eth (or (eth-balance wallet) "...")
usd (or (portfolio-value wallet prices) "...")
change (or (portfolio-change prices) "-%")]
(letsubs [eth-balance [:eth-balance]
portfolio-value [:portfolio-value]
portfolio-change [:portfolio-change]]
[rn/view {:style st/wallet-container}
[toolbar-view]
[rn/scroll-view
[main-section usd change]
[asset-section eth]]])))
[main-section portfolio-value portfolio-change]
[asset-section eth-balance]]]))

View File

@ -0,0 +1,67 @@
(ns status-im.ui.screens.wallet.subs
(:require [re-frame.core :refer [reg-sub subscribe]]
[status-im.utils.money :as money]))
(reg-sub :balance
(fn [db]
(get-in db [:wallet :balance])))
(reg-sub :price
(fn [db]
(get-in db [:prices :price])))
(reg-sub :last-day
(fn [db]
(get-in db [:prices :last-day])))
(reg-sub :eth-balance
:<- [:balance]
(fn [balance]
(if balance
(money/wei->ether balance)
"...")))
(reg-sub :portfolio-value
:<- [:balance]
:<- [:price]
(fn [[balance price]]
(if (and balance price)
(-> (money/wei->ether balance)
(money/eth->usd price)
(money/with-precision 2)
str)
"...")))
(reg-sub :portfolio-change
:<- [:price]
:<- [:last-day]
(fn [[price last-day]]
(if (and price last-day)
(-> (money/percent-change price last-day)
(money/with-precision 2)
(str "%"))
"-%")))
(reg-sub :wallet/transactions
(fn [db]
(get-in db [:wallet :transactions])))
(reg-sub :wallet/unsigned-transactions
:<- [:wallet/transactions]
(fn [transactions]
(filter #(= (:state %) :unsigned) transactions)))
(reg-sub :wallet/pending-transactions
:<- [:wallet/transactions]
(fn [transactions]
(filter #(= (:state %) :pending) transactions)))
(reg-sub :wallet/postponed-transactions
:<- [:wallet/transactions]
(fn [transactions]
(filter #(= (:state %) :postponed) transactions)))
(reg-sub :wallet/sent-transactions
:<- [:wallet/transactions]
(fn [transactions]
(filter #(= (:state %) :sent) transactions)))