-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathsend_amount_page.dart
More file actions
238 lines (224 loc) · 8.27 KB
/
Copy pathsend_amount_page.dart
File metadata and controls
238 lines (224 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import 'package:bb_mobile/core/settings/domain/settings_entity.dart';
import 'package:bb_mobile/core/themes/app_theme.dart';
import 'package:bb_mobile/core/utils/build_context_x.dart';
import 'package:bb_mobile/core/widgets/buttons/button.dart';
import 'package:bb_mobile/core/widgets/loading/fading_linear_progress.dart';
import 'package:bb_mobile/core/widgets/price_input/balance_row.dart';
import 'package:bb_mobile/core/widgets/price_input/price_input.dart';
import 'package:bb_mobile/core/widgets/scrollable_column.dart';
import 'package:bb_mobile/features/ark/presentation/cubit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class SendAmountPage extends StatefulWidget {
const SendAmountPage({
super.key,
this.prefilledAmount,
this.prefilledCurrencyCode,
});
final String? prefilledAmount;
final String? prefilledCurrencyCode;
@override
State<SendAmountPage> createState() => _SendAmountPageState();
}
class _SendAmountPageState extends State<SendAmountPage> {
//final _formKey = GlobalKey<FormState>();
final _controller = TextEditingController();
final _focusNode = FocusNode();
late String _currencyCode;
late final List<String> _availableCurrencies;
late String _equivalentAmount;
late bool _isLoading;
late BitcoinUnit _preferredBitcoinUnit;
String? _error;
int? _maxSpendableSat;
@override
void initState() {
super.initState();
if (widget.prefilledAmount != null) {
_controller.text = widget.prefilledAmount!;
}
_isLoading = context.read<ArkCubit>().state.isLoading;
_currencyCode = context.read<ArkCubit>().state.currencyCode!;
_availableCurrencies = context.read<ArkCubit>().state.inputCurrencyCodes;
_equivalentAmount =
'0 ${context.read<ArkCubit>().state.equivalentCurrencyCode}';
_maxSpendableSat = context.read<ArkCubit>().state.arkBalance?.total;
_preferredBitcoinUnit = context.read<ArkCubit>().state.preferredBitcoinUnit;
if (widget.prefilledCurrencyCode != null) {
context.read<ArkCubit>().onSendCurrencyCodeChanged(
widget.prefilledCurrencyCode!,
);
}
context.read<ArkCubit>().stream.listen((state) {
if (state.isLoading != _isLoading) {
setState(() {
_isLoading = state.isLoading;
});
}
// Make sure the currency and so exchange rate is updated in the Cubit
// first before updating the local currency code. This is to avoid
// having a mismatch between the currency and exchange rate if something
// goes wrong in the Cubit or just because of race conditions.
if (state.currencyCode != _currencyCode) {
setState(() {
_currencyCode = state.currencyCode!;
_equivalentAmount = _calculateEquivalentAmount();
});
}
if (state.error != null) {
setState(() {
_error = state.error!.message;
});
} else {
setState(() {
_error = null;
});
}
});
_controller.addListener(() {
// Calculate equivalent amount when input changes
setState(() {
_equivalentAmount = _calculateEquivalentAmount();
});
});
}
@override
void dispose() {
_controller.dispose();
_focusNode.dispose();
super.dispose();
}
void _onCurrencyCodeChanged(String? newCode) {
if (newCode != null && newCode != _currencyCode) {
context.read<ArkCubit>().onSendCurrencyCodeChanged(newCode);
// Clear the amount input when changing currency
_controller.text = '';
}
}
void _submit() {
// TODO: use a text form field in price input and validate
//if (_formKey.currentState!.validate()) {
// Unfocus to close keyboard before popping (optional, just looks nicer)
FocusScope.of(context).unfocus();
context.read<ArkCubit>().updateAmount(
amount: _controller.text,
currencyCode: _currencyCode,
);
//}
}
String _calculateEquivalentAmount() {
final inputAmount = _controller.text;
final exchangeRate = context.read<ArkCubit>().state.exchangeRate;
final bitcoinUnit = context.read<ArkCubit>().state.preferredBitcoinUnit;
final equivalentCurrencyCode = context
.read<ArkCubit>()
.state
.equivalentCurrencyCode;
String equivalentValue = '0';
if (_currencyCode == BitcoinUnit.sats.code) {
final amountSat = int.tryParse(inputAmount) ?? 0;
equivalentValue = (amountSat / 1e8 * exchangeRate).toStringAsFixed(2);
} else if (_currencyCode == BitcoinUnit.btc.code) {
final amountBtc = double.tryParse(inputAmount) ?? 0;
equivalentValue = (amountBtc * exchangeRate).toStringAsFixed(2);
} else {
final amountFiat = double.tryParse(inputAmount) ?? 0;
equivalentValue = bitcoinUnit == BitcoinUnit.sats
? (amountFiat * 1e8 / exchangeRate).toStringAsFixed(0)
: (amountFiat / exchangeRate).toStringAsFixed(8);
}
return '$equivalentValue $equivalentCurrencyCode';
}
String _calculateMaxAmountValue() {
if (_maxSpendableSat != null) {
if (_preferredBitcoinUnit == BitcoinUnit.btc) {
return (_maxSpendableSat! / 1e8).toStringAsFixed(8);
} else {
return '$_maxSpendableSat';
}
}
return '0';
}
@override
Widget build(BuildContext context) {
return GestureDetector(
// tap outside input to close keyboard
behavior: .opaque,
onTap: () => FocusScope.of(context).unfocus(),
child: Scaffold(
appBar: AppBar(
title: Text(
context.loc.arkSendAmountTitle,
style: context.font.headlineMedium,
),
bottom: PreferredSize(
preferredSize: const Size.fromHeight(3),
child: _isLoading
? FadingLinearProgress(
height: 3,
trigger: _isLoading,
backgroundColor: context.appColors.surface,
foregroundColor: context.appColors.primary,
)
: const SizedBox(height: 3),
),
),
body: SafeArea(
child: Form(
child: ScrollableColumn(
mainAxisAlignment: .spaceBetween,
children: [
PriceInput(
currency: _currencyCode,
amountEquivalent: _equivalentAmount,
availableCurrencies: _availableCurrencies,
onCurrencyChanged: _onCurrencyCodeChanged,
onNoteChanged: null,
amountController: _controller,
focusNode: _focusNode,
error: _error,
),
Column(
mainAxisAlignment: .spaceBetween,
children: [
Divider(
height: 1,
color: context.appColors.secondaryFixedDim,
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16),
child: BalanceRow(
balance: _preferredBitcoinUnit == BitcoinUnit.btc
? (_maxSpendableSat != null
? (_maxSpendableSat! / 1e8).toStringAsFixed(8)
: '0.00000000')
: (_maxSpendableSat?.toString() ?? '0'),
currencyCode: _preferredBitcoinUnit.code,
onMaxPressed: () async {
await context
.read<ArkCubit>()
.onSendCurrencyCodeChanged(
_preferredBitcoinUnit.code,
);
_controller.text = _calculateMaxAmountValue();
},
walletLabel: context.loc.arkInstantPayments,
),
),
],
),
BBButton.big(
label: context.loc.arkContinueButton,
onPressed: _submit,
disabled: _controller.text.isEmpty || _isLoading,
bgColor: context.appColors.secondary,
textColor: context.appColors.onSecondary,
),
],
),
),
),
),
);
}
}