@@ -19,6 +19,9 @@ import { CustomMatDialogService } from '../../../../services/custom-mat-dialog.s
19
19
import { WalletService } from '../../../../services/wallet/wallet.service' ;
20
20
import { BaseCoin } from '../../../../coins/basecoin' ;
21
21
import { CoinService } from '../../../../services/coin.service' ;
22
+ import { DoubleButtonActive } from '../../../layout/double-button/double-button.component' ;
23
+ import { PriceService } from '../../../../services/price.service' ;
24
+ import { SendFormComponent } from '../send-form/send-form.component' ;
22
25
23
26
@Component ( {
24
27
selector : 'app-send-form-advanced' ,
@@ -44,10 +47,15 @@ export class SendFormAdvancedComponent implements OnInit, OnDestroy {
44
47
autoShareValue = '0.5' ;
45
48
previewTx : boolean ;
46
49
currentCoin : BaseCoin ;
50
+ doubleButtonActive = DoubleButtonActive ;
51
+ selectedCurrency = DoubleButtonActive . LeftButton ;
52
+ values : number [ ] ;
53
+ price : number ;
47
54
48
55
private subscriptions : Subscription ;
49
56
private getOutputsSubscriptions : ISubscription ;
50
57
private unlockSubscription : ISubscription ;
58
+ private destinationSubscriptions : ISubscription [ ] = [ ] ;
51
59
52
60
constructor (
53
61
public walletService : WalletService ,
@@ -58,6 +66,7 @@ export class SendFormAdvancedComponent implements OnInit, OnDestroy {
58
66
private navbarService : NavBarService ,
59
67
private blockchainService : BlockchainService ,
60
68
private coinService : CoinService ,
69
+ private priceService : PriceService ,
61
70
) { }
62
71
63
72
ngOnInit ( ) {
@@ -121,6 +130,11 @@ export class SendFormAdvancedComponent implements OnInit, OnDestroy {
121
130
} )
122
131
) ;
123
132
133
+ this . subscriptions . add ( this . priceService . price . subscribe ( price => {
134
+ this . price = price ;
135
+ this . updateValues ( ) ;
136
+ } ) ) ;
137
+
124
138
if ( this . formData ) {
125
139
this . fillForm ( ) ;
126
140
}
@@ -131,6 +145,7 @@ export class SendFormAdvancedComponent implements OnInit, OnDestroy {
131
145
this . subscriptions . unsubscribe ( ) ;
132
146
this . navbarService . hideSwitch ( ) ;
133
147
this . snackbar . dismiss ( ) ;
148
+ this . destinationSubscriptions . forEach ( s => s . unsubscribe ( ) ) ;
134
149
}
135
150
136
151
preview ( ) {
@@ -143,6 +158,53 @@ export class SendFormAdvancedComponent implements OnInit, OnDestroy {
143
158
this . unlockAndSend ( ) ;
144
159
}
145
160
161
+ changeActiveCurrency ( value ) {
162
+ this . selectedCurrency = value ;
163
+ this . updateValues ( ) ;
164
+ ( this . form . get ( 'destinations' ) as FormArray ) . updateValueAndValidity ( ) ;
165
+ }
166
+
167
+ private updateValues ( ) {
168
+ if ( ! this . price ) {
169
+ this . values = null ;
170
+
171
+ return ;
172
+ }
173
+
174
+ this . values = [ ] ;
175
+
176
+ this . destControls . forEach ( ( dest , i ) => {
177
+ const value = dest . get ( 'coins' ) . value !== undefined ? dest . get ( 'coins' ) . value . replace ( ' ' , '=' ) : '' ;
178
+
179
+ if ( isNaN ( value ) || value . trim ( ) === '' || parseFloat ( value ) <= 0 || value * 1 === 0 ) {
180
+ this . values [ i ] = - 1 ;
181
+
182
+ return ;
183
+ }
184
+
185
+ const parts = value . split ( '.' ) ;
186
+ if ( this . selectedCurrency === DoubleButtonActive . LeftButton ) {
187
+ if ( parts . length === 2 && parts [ 1 ] . length > this . blockchainService . currentMaxDecimals ) {
188
+ this . values [ i ] = - 1 ;
189
+
190
+ return ;
191
+ }
192
+ } else {
193
+ if ( parts . length === 2 && parts [ 1 ] . length > SendFormComponent . MaxUsdDecimal ) {
194
+ this . values [ i ] = - 1 ;
195
+
196
+ return ;
197
+ }
198
+ }
199
+
200
+ if ( this . selectedCurrency === DoubleButtonActive . LeftButton ) {
201
+ this . values [ i ] = new BigNumber ( value ) . multipliedBy ( this . price ) . decimalPlaces ( 2 ) . toNumber ( ) ;
202
+ } else {
203
+ this . values [ i ] = new BigNumber ( value ) . dividedBy ( this . price ) . decimalPlaces ( this . blockchainService . currentMaxDecimals ) . toNumber ( ) ;
204
+ }
205
+ } ) ;
206
+ }
207
+
146
208
unlockAndSend ( ) {
147
209
if ( ! this . form . valid || this . button . isLoading ( ) ) {
148
210
return ;
@@ -191,11 +253,16 @@ export class SendFormAdvancedComponent implements OnInit, OnDestroy {
191
253
addDestination ( ) {
192
254
const destinations = this . form . get ( 'destinations' ) as FormArray ;
193
255
destinations . push ( this . createDestinationFormGroup ( ) ) ;
256
+ this . updateValues ( ) ;
194
257
}
195
258
196
259
removeDestination ( index ) {
197
260
const destinations = this . form . get ( 'destinations' ) as FormArray ;
198
261
destinations . removeAt ( index ) ;
262
+
263
+ this . destinationSubscriptions [ index ] . unsubscribe ( ) ;
264
+ this . destinationSubscriptions . splice ( index , 1 ) ;
265
+ this . updateValues ( ) ;
199
266
}
200
267
201
268
setShareValue ( event ) {
@@ -241,9 +308,10 @@ export class SendFormAdvancedComponent implements OnInit, OnDestroy {
241
308
}
242
309
243
310
this . destControls . forEach ( ( destControl , i ) => {
244
- [ 'address' , 'coins' , ' hours'] . forEach ( name => {
311
+ [ 'address' , 'hours' ] . forEach ( name => {
245
312
destControl . get ( name ) . setValue ( this . formData . form . destinations [ i ] [ name ] ) ;
246
313
} ) ;
314
+ destControl . get ( 'coins' ) . setValue ( this . formData . form . destinations [ i ] . originalAmount ) ;
247
315
} ) ;
248
316
249
317
if ( this . formData . form . hoursSelection . type === HoursSelectionTypes . Auto ) {
@@ -263,6 +331,8 @@ export class SendFormAdvancedComponent implements OnInit, OnDestroy {
263
331
264
332
this . form . get ( 'outputs' ) . setValue ( this . formData . form . outputs ) ;
265
333
}
334
+
335
+ this . selectedCurrency = this . formData . form . currency ;
266
336
}
267
337
268
338
addressCompare ( a , b ) {
@@ -305,8 +375,14 @@ export class SendFormAdvancedComponent implements OnInit, OnDestroy {
305
375
if ( name === 'coins' ) {
306
376
const parts = value . split ( '.' ) ;
307
377
308
- if ( parts . length === 2 && parts [ 1 ] . length > this . blockchainService . currentMaxDecimals ) {
309
- return true ;
378
+ if ( this . selectedCurrency === DoubleButtonActive . LeftButton ) {
379
+ if ( parts . length === 2 && parts [ 1 ] . length > this . blockchainService . currentMaxDecimals ) {
380
+ return true ;
381
+ }
382
+ } else {
383
+ if ( parts . length === 2 && parts [ 1 ] . length > SendFormComponent . MaxUsdDecimal ) {
384
+ return true ;
385
+ }
310
386
}
311
387
} else if ( name === 'hours' ) {
312
388
if ( Number ( value ) < 1 || parseInt ( value , 10 ) !== parseFloat ( value ) ) {
@@ -325,7 +401,12 @@ export class SendFormAdvancedComponent implements OnInit, OnDestroy {
325
401
this . updateAvailableBalance ( ) ;
326
402
327
403
let destinationsCoins = new BigNumber ( 0 ) ;
328
- this . destControls . map ( control => destinationsCoins = destinationsCoins . plus ( control . value . coins ) ) ;
404
+ if ( this . selectedCurrency === DoubleButtonActive . LeftButton ) {
405
+ this . destControls . map ( control => destinationsCoins = destinationsCoins . plus ( control . value . coins ) ) ;
406
+ } else {
407
+ this . updateValues ( ) ;
408
+ this . values . map ( value => destinationsCoins = destinationsCoins . plus ( value ) ) ;
409
+ }
329
410
let destinationsHours = new BigNumber ( 0 ) ;
330
411
if ( ! this . autoHours ) {
331
412
this . destControls . map ( control => destinationsHours = destinationsHours . plus ( control . value . hours ) ) ;
@@ -339,11 +420,17 @@ export class SendFormAdvancedComponent implements OnInit, OnDestroy {
339
420
}
340
421
341
422
private createDestinationFormGroup ( ) {
342
- return this . formBuilder . group ( {
423
+ const group = this . formBuilder . group ( {
343
424
address : '' ,
344
425
coins : '' ,
345
426
hours : '' ,
346
427
} ) ;
428
+
429
+ this . destinationSubscriptions . push ( group . get ( 'coins' ) . valueChanges . subscribe ( value => {
430
+ this . updateValues ( ) ;
431
+ } ) ) ;
432
+
433
+ return group ;
347
434
}
348
435
349
436
private createTransaction ( ) {
@@ -386,6 +473,7 @@ export class SendFormAdvancedComponent implements OnInit, OnDestroy {
386
473
autoOptions : this . autoOptions ,
387
474
allUnspentOutputs : this . loadingUnspentOutputs ? null : this . allUnspentOutputs ,
388
475
outputs : this . form . get ( 'outputs' ) . value ,
476
+ currency : this . selectedCurrency ,
389
477
} ,
390
478
amount : amount ,
391
479
to : this . destinations . map ( d => d . address ) ,
@@ -427,10 +515,11 @@ export class SendFormAdvancedComponent implements OnInit, OnDestroy {
427
515
}
428
516
429
517
private get destinations ( ) : Destination [ ] {
430
- return this . destControls . map ( destControl => {
518
+ return this . destControls . map ( ( destControl , i ) => {
431
519
const destination = {
432
520
address : destControl . get ( 'address' ) . value ,
433
- coins : new BigNumber ( destControl . get ( 'coins' ) . value ) ,
521
+ coins : this . selectedCurrency === DoubleButtonActive . LeftButton ? new BigNumber ( destControl . get ( 'coins' ) . value ) : new BigNumber ( this . values [ i ] . toString ( ) ) ,
522
+ originalAmount : destControl . get ( 'coins' ) . value ,
434
523
} ;
435
524
436
525
if ( ! this . autoHours ) {
0 commit comments