1
- module App.Widgets.Main (mainWidget , pasteWidget ) where
1
+ module App.Widgets.Main (mainWidget ) where
2
2
3
3
import qualified App.Misc as Misc
4
4
import App.Types
@@ -12,6 +12,8 @@ import qualified Functora.Miso.Widgets.Field as Field
12
12
import qualified Functora.Miso.Widgets.FieldPairs as FieldPairs
13
13
import qualified Functora.Miso.Widgets.Grid as Grid
14
14
import qualified Functora.Miso.Widgets.Header as Header
15
+ import qualified Functora.Money as Money
16
+ import Lens.Micro ((^..) )
15
17
import qualified Material.Button as Button
16
18
import qualified Material.LayoutGrid as LayoutGrid
17
19
import qualified Material.Theme as Theme
@@ -106,8 +108,19 @@ screenWidget st@Model {modelState = St {stScreen = Donate}} =
106
108
]
107
109
]
108
110
screenWidget st@ Model {modelState = St {stScreen = Main }} =
109
- Asset. assetsViewer st
110
- <> [ Grid. mediumCell
111
+ ( if null assets
112
+ then mempty
113
+ else buttons
114
+ )
115
+ <> Asset. assetsViewer st
116
+ <> totalViewer st
117
+ <> buttons
118
+ where
119
+ assets :: [View Action ]
120
+ assets = Asset. assetsViewer st
121
+ buttons :: [View Action ]
122
+ buttons =
123
+ [ Grid. mediumCell
111
124
[ Button. raised
112
125
( Button. config
113
126
& Button. setIcon (Just " add_box" )
@@ -120,7 +133,7 @@ screenWidget st@Model {modelState = St {stScreen = Main}} =
120
133
)
121
134
" Add item"
122
135
],
123
- Grid. mediumCell
136
+ Grid. mediumCell
124
137
[ Button. raised
125
138
( Button. config
126
139
& Button. setIcon (Just " send" )
@@ -133,30 +146,92 @@ screenWidget st@Model {modelState = St {stScreen = Main}} =
133
146
)
134
147
" Order via Telegram"
135
148
]
136
- ]
149
+ ]
150
+
151
+ totalViewer :: Model -> [View Action ]
152
+ totalViewer st =
153
+ if base == 0
154
+ then mempty
155
+ else
156
+ Header. headerViewer " Total" mempty
157
+ <> FieldPairs. fieldPairsViewer
158
+ FieldPairs. Args
159
+ { FieldPairs. argsModel = st,
160
+ FieldPairs. argsOptic =
161
+ constTraversal
162
+ [ newFieldPairId (" Subtotal " <> baseCur)
163
+ . DynamicFieldText
164
+ $ inspectRatioDef base,
165
+ newFieldPairId (" Subtotal " <> quoteCur)
166
+ . DynamicFieldText
167
+ $ inspectRatioDef quote,
168
+ FieldPair (newTextFieldId " Fee %" )
169
+ $ uniqueToIdentity fee
170
+ & # fieldOpts
171
+ . # fieldOptsQrState
172
+ .~ Nothing ,
173
+ newFieldPairId (" Total " <> quoteCur)
174
+ . DynamicFieldText
175
+ . inspectRatioDef
176
+ . foldField quote
177
+ $ fee
178
+ ],
179
+ FieldPairs. argsAction = PushUpdate . Instant ,
180
+ FieldPairs. argsEmitter = Misc. pushActionQueue st . Instant
181
+ }
182
+ where
183
+ fee = st ^. # modelState . # stMerchantFeePercent
184
+ rate = st ^. # modelState . # stExchangeRate . # fieldOutput
185
+ base =
186
+ foldl
187
+ ( \ acc fps ->
188
+ if any
189
+ ((== FieldTypeNumber ) . (^. # fieldPairValue . # fieldType))
190
+ fps
191
+ then acc + foldl foldFieldPair 1 fps
192
+ else acc
193
+ )
194
+ 0
195
+ ( st
196
+ ^.. # modelState
197
+ . # stAssets
198
+ . each
199
+ . # assetFieldPairs
200
+ )
201
+ quote =
202
+ rate * base
203
+ baseCur =
204
+ st
205
+ ^. # modelState
206
+ . # stAssetCurrency
207
+ . # currencyOutput
208
+ . # currencyInfoCode
209
+ . to Money. inspectCurrencyCode
210
+ . to toUpper
211
+ quoteCur =
212
+ st
213
+ ^. # modelState
214
+ . # stMerchantCurrency
215
+ . # currencyOutput
216
+ . # currencyInfoCode
217
+ . to Money. inspectCurrencyCode
218
+ . to toUpper
219
+
220
+ foldField :: Rational -> Field DynamicField f -> Rational
221
+ foldField acc Field {fieldType = typ, fieldOutput = out} =
222
+ case out of
223
+ DynamicFieldNumber x
224
+ | typ == FieldTypeNumber ->
225
+ acc * x
226
+ DynamicFieldNumber x
227
+ | typ == FieldTypePercent ->
228
+ acc * (1 + (x / 100 ))
229
+ _ ->
230
+ acc
137
231
138
- pasteWidget ::
139
- Unicode ->
140
- ((Maybe Unicode -> JSM () ) -> JSM () ) ->
141
- ATraversal' Model (Field Unicode Unique ) ->
142
- Maybe (Field. OptsWidget Model Action )
143
- pasteWidget icon selector optic =
144
- Just
145
- . Field. ActionWidget icon mempty
146
- . PushUpdate
147
- . Instant
148
- $ \ prev -> do
149
- selector $ \ case
150
- Nothing ->
151
- Jsm. popupText @ Unicode " Failure!"
152
- Just res -> do
153
- Misc. pushActionQueue prev
154
- . Instant
155
- $ pure
156
- . (& cloneTraversal optic . # fieldOutput .~ res)
157
- . (& cloneTraversal optic . # fieldInput . # uniqueValue .~ res)
158
- Jsm. popupText @ Unicode " Success!"
159
- pure prev
232
+ foldFieldPair :: Rational -> FieldPair DynamicField f -> Rational
233
+ foldFieldPair acc =
234
+ foldField acc . fieldPairValue
160
235
161
236
tosWidget :: View Action
162
237
tosWidget =
0 commit comments