stack_wallet/lib/pages/coin_control/coin_control_view.dart

781 lines
36 KiB
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
import 'dart:async';
2023-03-06 18:11:13 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
2023-03-16 22:12:16 +00:00
import 'package:flutter_svg/flutter_svg.dart';
2023-03-06 18:11:13 +00:00
import 'package:isar/isar.dart';
2024-05-27 23:56:22 +00:00
import 'package:tuple/tuple.dart';
import '../../db/isar/main_db.dart';
import '../../models/isar/models/isar_models.dart';
import '../../providers/global/wallets_provider.dart';
import '../../themes/stack_colors.dart';
import '../../utilities/amount/amount.dart';
import '../../utilities/amount/amount_formatter.dart';
import '../../utilities/assets.dart';
import '../../utilities/constants.dart';
import '../../utilities/text_styles.dart';
import '../../wallets/isar/providers/wallet_info_provider.dart';
import '../../wallets/wallet/wallet_mixin_interfaces/coin_control_interface.dart';
import '../../widgets/animated_widgets/rotate_icon.dart';
import '../../widgets/app_bar_field.dart';
import '../../widgets/background.dart';
import '../../widgets/custom_buttons/app_bar_icon_button.dart';
import '../../widgets/custom_buttons/dropdown_button.dart';
import '../../widgets/desktop/primary_button.dart';
import '../../widgets/desktop/secondary_button.dart';
import '../../widgets/expandable2.dart';
import '../../widgets/icon_widgets/x_icon.dart';
import '../../widgets/rounded_container.dart';
import '../../widgets/rounded_white_container.dart';
import '../../widgets/toggle.dart';
2024-05-27 23:56:22 +00:00
import 'utxo_card.dart';
import 'utxo_details_view.dart';
2023-03-06 18:11:13 +00:00
2023-03-07 19:51:06 +00:00
enum CoinControlViewType {
manage,
use;
}
2023-03-06 18:11:13 +00:00
class CoinControlView extends ConsumerStatefulWidget {
const CoinControlView({
2024-05-15 21:20:45 +00:00
super.key,
2023-03-06 18:11:13 +00:00
required this.walletId,
2023-03-07 19:51:06 +00:00
required this.type,
this.requestedTotal,
2023-03-07 21:45:22 +00:00
this.selectedUTXOs,
2024-05-15 21:20:45 +00:00
});
2023-03-06 18:11:13 +00:00
static const routeName = "/coinControl";
final String walletId;
2023-03-07 19:51:06 +00:00
final CoinControlViewType type;
final Amount? requestedTotal;
2023-03-07 21:45:22 +00:00
final Set<UTXO>? selectedUTXOs;
2023-03-06 18:11:13 +00:00
@override
ConsumerState<CoinControlView> createState() => _CoinControlViewState();
}
class _CoinControlViewState extends ConsumerState<CoinControlView> {
2023-03-16 22:12:16 +00:00
final searchController = TextEditingController();
final searchFocus = FocusNode();
bool _isSearching = false;
2023-03-07 19:51:06 +00:00
bool _showBlocked = false;
2023-03-16 22:12:16 +00:00
CCSortDescriptor _sort = CCSortDescriptor.age;
2023-03-16 22:42:46 +00:00
Map<String, List<Id>>? _map;
List<Id>? _list;
2023-03-07 19:51:06 +00:00
final Set<UTXO> _selectedAvailable = {};
final Set<UTXO> _selectedBlocked = {};
2023-03-06 18:11:13 +00:00
Future<void> _refreshBalance() async {
final coinControlInterface =
ref.read(pWallets).getWallet(widget.walletId) as CoinControlInterface;
await coinControlInterface.updateBalance();
}
2023-03-07 21:45:22 +00:00
@override
void initState() {
if (widget.selectedUTXOs != null) {
_selectedAvailable.addAll(widget.selectedUTXOs!);
}
2023-03-16 22:12:16 +00:00
searchController.addListener(() {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
setState(() {});
});
});
2023-03-07 21:45:22 +00:00
super.initState();
}
2023-03-16 22:12:16 +00:00
@override
void dispose() {
searchController.dispose();
searchFocus.dispose();
super.dispose();
}
2023-03-06 18:11:13 +00:00
@override
Widget build(BuildContext context) {
debugPrint("BUILD: $runtimeType");
2023-11-08 22:09:28 +00:00
final minConfirms = ref
.watch(pWallets)
.getWallet(widget.walletId)
.cryptoCurrency
.minConfirms;
final coin = ref.watch(pWalletCoin(widget.walletId));
final currentHeight = ref.watch(pWalletChainHeight(widget.walletId));
2023-03-08 17:34:00 +00:00
2023-03-16 22:42:46 +00:00
if (_sort == CCSortDescriptor.address && !_isSearching) {
_list = null;
_map = MainDB.instance.queryUTXOsGroupedByAddressSync(
walletId: widget.walletId,
filter: CCFilter.all,
sort: _sort,
searchTerm: "",
2024-05-15 21:20:45 +00:00
cryptoCurrency: coin,
2023-03-16 22:42:46 +00:00
);
} else {
_map = null;
_list = MainDB.instance.queryUTXOsSync(
walletId: widget.walletId,
filter: _isSearching
? CCFilter.all
: _showBlocked
? CCFilter.frozen
: CCFilter.available,
sort: _sort,
searchTerm: _isSearching ? searchController.text : "",
2024-05-15 21:20:45 +00:00
cryptoCurrency: coin,
2023-03-16 22:42:46 +00:00
);
}
2023-03-06 18:11:13 +00:00
2023-03-07 21:45:22 +00:00
return WillPopScope(
onWillPop: () async {
unawaited(_refreshBalance());
2023-03-07 21:45:22 +00:00
Navigator.of(context).pop(
2024-05-27 23:56:22 +00:00
widget.type == CoinControlViewType.use ? _selectedAvailable : null,
);
2023-03-07 21:45:22 +00:00
return false;
},
child: Background(
child: Scaffold(
backgroundColor:
Theme.of(context).extension<StackColors>()!.background,
appBar: AppBar(
2023-03-16 22:12:16 +00:00
automaticallyImplyLeading: false,
leading: _isSearching
? null
: widget.type == CoinControlViewType.use &&
_selectedAvailable.isNotEmpty
? AppBarIconButton(
icon: XIcon(
width: 24,
height: 24,
color: Theme.of(context)
.extension<StackColors>()!
.topNavIconPrimary,
),
onPressed: () {
setState(() {
_selectedAvailable.clear();
});
},
)
: AppBarBackButton(
onPressed: () {
unawaited(_refreshBalance());
Navigator.of(context).pop(
2024-05-27 23:56:22 +00:00
widget.type == CoinControlViewType.use
? _selectedAvailable
: null,
);
2023-03-16 22:12:16 +00:00
},
),
title: _isSearching
? AppBarSearchField(
controller: searchController,
focusNode: searchFocus,
2023-03-07 21:45:22 +00:00
)
2023-03-16 22:12:16 +00:00
: Text(
"Coin control",
style: STextStyles.navBarTitle(context),
2023-03-07 19:51:06 +00:00
),
2023-03-07 21:45:22 +00:00
titleSpacing: 0,
2023-03-16 22:12:16 +00:00
actions: _isSearching
? [
AspectRatio(
aspectRatio: 1,
child: AppBarIconButton(
size: 36,
icon: SvgPicture.asset(
Assets.svg.x,
width: 20,
height: 20,
color: Theme.of(context)
.extension<StackColors>()!
.topNavIconPrimary,
),
onPressed: () {
// show search
setState(() {
_isSearching = false;
});
},
),
),
]
: [
AspectRatio(
aspectRatio: 1,
child: AppBarIconButton(
size: 36,
icon: SvgPicture.asset(
Assets.svg.search,
width: 20,
height: 20,
color: Theme.of(context)
.extension<StackColors>()!
.topNavIconPrimary,
),
onPressed: () {
// show search
setState(() {
_isSearching = true;
});
},
),
),
AspectRatio(
aspectRatio: 1,
2023-03-16 22:42:46 +00:00
child: JDropdownIconButton(
mobileAppBar: true,
groupValue: _sort,
items: CCSortDescriptor.values.toSet(),
onSelectionChanged: (CCSortDescriptor? newValue) {
if (newValue != null && newValue != _sort) {
setState(() {
_sort = newValue;
});
}
2023-03-16 22:12:16 +00:00
},
2023-03-16 22:42:46 +00:00
displayPrefix: "Sort by",
2023-03-16 22:12:16 +00:00
),
),
],
2023-03-07 21:45:22 +00:00
),
body: SafeArea(
child: Column(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16,
),
child: Column(
children: [
const SizedBox(
height: 10,
2023-03-07 19:51:06 +00:00
),
2023-03-16 22:12:16 +00:00
if (!_isSearching)
RoundedWhiteContainer(
child: Text(
"This option allows you to control, freeze, and utilize "
"outputs at your discretion. Tap the output circle to "
"select.",
style: STextStyles.w500_14(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.textSubtitle1,
),
2023-03-07 19:51:06 +00:00
),
),
2023-03-16 22:12:16 +00:00
if (!_isSearching)
const SizedBox(
height: 10,
),
2023-03-16 22:42:46 +00:00
if (!(_isSearching || _map != null))
2023-03-16 22:12:16 +00:00
SizedBox(
height: 48,
child: Toggle(
key: UniqueKey(),
onColor: Theme.of(context)
.extension<StackColors>()!
.popupBG,
onText: "Available outputs",
offColor: Theme.of(context)
.extension<StackColors>()!
.textFieldDefaultBG,
offText: "Frozen outputs",
isOn: _showBlocked,
onValueChanged: (value) {
setState(() {
_showBlocked = value;
});
},
decoration: BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
2023-03-07 21:45:22 +00:00
),
),
2023-03-07 19:51:06 +00:00
),
2023-03-16 22:12:16 +00:00
if (!_isSearching)
const SizedBox(
height: 10,
),
if (_isSearching)
Expanded(
child: ListView.separated(
2023-03-16 22:42:46 +00:00
itemCount: _list!.length,
2023-03-16 22:12:16 +00:00
separatorBuilder: (context, _) => const SizedBox(
height: 10,
),
itemBuilder: (context, index) {
final utxo = MainDB.instance.isar.utxos
.where()
2023-03-16 22:42:46 +00:00
.idEqualTo(_list![index])
2023-03-16 22:12:16 +00:00
.findFirstSync()!;
final isSelected =
_selectedBlocked.contains(utxo) ||
_selectedAvailable.contains(utxo);
return UtxoCard(
key: Key(
2024-05-27 23:56:22 +00:00
"${utxo.walletId}_${utxo.id}_$isSelected",
),
2023-03-16 22:12:16 +00:00
walletId: widget.walletId,
utxo: utxo,
canSelect: widget.type ==
CoinControlViewType.manage ||
(widget.type == CoinControlViewType.use &&
!utxo.isBlocked &&
utxo.isConfirmed(
currentHeight,
2023-11-08 22:09:28 +00:00
minConfirms,
2023-03-16 22:12:16 +00:00
)),
initialSelectedState: isSelected,
onSelectedChanged: (value) {
if (value) {
utxo.isBlocked
? _selectedBlocked.add(utxo)
: _selectedAvailable.add(utxo);
} else {
utxo.isBlocked
? _selectedBlocked.remove(utxo)
: _selectedAvailable.remove(utxo);
}
setState(() {});
},
onPressed: () async {
final result =
await Navigator.of(context).pushNamed(
UtxoDetailsView.routeName,
arguments: Tuple2(
utxo.id,
widget.walletId,
),
);
if (mounted && result == "refresh") {
setState(() {});
}
},
);
},
2023-03-07 21:45:22 +00:00
),
2023-03-16 22:12:16 +00:00
),
if (!_isSearching)
2023-03-16 22:42:46 +00:00
_list != null
? Expanded(
child: ListView.separated(
itemCount: _list!.length,
separatorBuilder: (context, _) =>
const SizedBox(
height: 10,
),
itemBuilder: (context, index) {
final utxo = MainDB.instance.isar.utxos
.where()
.idEqualTo(_list![index])
.findFirstSync()!;
2023-03-07 19:51:06 +00:00
2023-03-16 22:42:46 +00:00
final isSelected = _showBlocked
? _selectedBlocked.contains(utxo)
: _selectedAvailable.contains(utxo);
2023-03-07 21:45:22 +00:00
2023-03-16 22:42:46 +00:00
return UtxoCard(
key: Key(
2024-05-27 23:56:22 +00:00
"${utxo.walletId}_${utxo.id}_$isSelected",
),
2023-03-16 22:42:46 +00:00
walletId: widget.walletId,
utxo: utxo,
canSelect: widget.type ==
CoinControlViewType.manage ||
(widget.type ==
CoinControlViewType.use &&
!_showBlocked &&
utxo.isConfirmed(
currentHeight,
2023-11-08 22:09:28 +00:00
minConfirms,
2023-03-16 22:42:46 +00:00
)),
initialSelectedState: isSelected,
onSelectedChanged: (value) {
if (value) {
_showBlocked
? _selectedBlocked.add(utxo)
: _selectedAvailable.add(utxo);
} else {
_showBlocked
? _selectedBlocked.remove(utxo)
: _selectedAvailable
.remove(utxo);
}
setState(() {});
},
onPressed: () async {
final result =
await Navigator.of(context)
.pushNamed(
UtxoDetailsView.routeName,
arguments: Tuple2(
utxo.id,
widget.walletId,
),
);
if (mounted && result == "refresh") {
setState(() {});
}
},
);
},
),
)
: Expanded(
child: ListView.separated(
itemCount: _map!.entries.length,
separatorBuilder: (context, _) =>
const SizedBox(
height: 10,
),
itemBuilder: (context, index) {
final entry =
_map!.entries.elementAt(index);
final _controller =
RotateIconController();
return Expandable2(
border: Theme.of(context)
.extension<StackColors>()!
.backgroundAppBar,
background: Theme.of(context)
.extension<StackColors>()!
.popupBG,
animationDurationMultiplier:
0.2 * entry.value.length,
onExpandWillChange: (state) {
if (state ==
Expandable2State.expanded) {
_controller.forward?.call();
} else {
_controller.reverse?.call();
}
},
header: RoundedContainer(
padding: const EdgeInsets.all(14),
color: Colors.transparent,
child: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Text(
entry.key,
style:
STextStyles.w600_14(
2024-05-27 23:56:22 +00:00
context,
),
2023-03-16 22:42:46 +00:00
),
const SizedBox(
height: 2,
),
Text(
"${entry.value.length} "
"output${entry.value.length > 1 ? "s" : ""}",
style:
STextStyles.w500_12(
2024-05-27 23:56:22 +00:00
context,
).copyWith(
2023-03-16 22:42:46 +00:00
color: Theme.of(context)
.extension<
StackColors>()!
.textSubtitle1,
),
),
],
),
),
RotateIcon(
animationDurationMultiplier:
0.2 * entry.value.length,
icon: SvgPicture.asset(
Assets.svg.chevronDown,
width: 14,
color: Theme.of(context)
.extension<StackColors>()!
.textSubtitle1,
),
curve: Curves.easeInOut,
controller: _controller,
),
],
),
),
children: entry.value.map(
(id) {
final utxo = MainDB
.instance.isar.utxos
.where()
.idEqualTo(id)
.findFirstSync()!;
final isSelected = _selectedBlocked
.contains(utxo) ||
_selectedAvailable
.contains(utxo);
return UtxoCard(
key: Key(
2024-05-27 23:56:22 +00:00
"${utxo.walletId}_${utxo.id}_$isSelected",
),
2023-03-16 22:42:46 +00:00
walletId: widget.walletId,
utxo: utxo,
canSelect: widget.type ==
CoinControlViewType
.manage ||
(widget.type ==
CoinControlViewType
.use &&
!utxo.isBlocked &&
utxo.isConfirmed(
currentHeight,
2023-11-08 22:09:28 +00:00
minConfirms,
2023-03-16 22:42:46 +00:00
)),
initialSelectedState: isSelected,
onSelectedChanged: (value) {
if (value) {
utxo.isBlocked
? _selectedBlocked
.add(utxo)
: _selectedAvailable
.add(utxo);
} else {
utxo.isBlocked
? _selectedBlocked
.remove(utxo)
: _selectedAvailable
.remove(utxo);
}
setState(() {});
},
onPressed: () async {
final result =
await Navigator.of(context)
.pushNamed(
UtxoDetailsView.routeName,
arguments: Tuple2(
utxo.id,
widget.walletId,
),
);
if (mounted &&
result == "refresh") {
setState(() {});
}
},
);
},
).toList(),
);
},
),
),
2023-03-07 21:45:22 +00:00
],
),
2023-03-07 18:43:39 +00:00
),
2023-03-06 18:11:13 +00:00
),
2023-03-07 21:45:22 +00:00
if (((_showBlocked && _selectedBlocked.isNotEmpty) ||
(!_showBlocked && _selectedAvailable.isNotEmpty)) &&
widget.type == CoinControlViewType.manage)
Container(
decoration: BoxDecoration(
color: Theme.of(context)
2023-03-07 19:51:06 +00:00
.extension<StackColors>()!
2023-03-07 21:45:22 +00:00
.backgroundAppBar,
boxShadow: [
Theme.of(context)
.extension<StackColors>()!
.standardBoxShadow,
],
),
child: Padding(
padding: const EdgeInsets.all(16),
child: SecondaryButton(
label: _showBlocked ? "Unfreeze" : "Freeze",
onPressed: () async {
if (_showBlocked) {
2024-05-27 23:56:22 +00:00
await MainDB.instance.putUTXOs(
_selectedBlocked
.map(
(e) => e.copyWith(
isBlocked: false,
),
)
.toList(),
);
2023-03-07 21:45:22 +00:00
_selectedBlocked.clear();
} else {
2024-05-27 23:56:22 +00:00
await MainDB.instance.putUTXOs(
_selectedAvailable
.map(
(e) => e.copyWith(
isBlocked: true,
),
)
.toList(),
);
2023-03-07 21:45:22 +00:00
_selectedAvailable.clear();
}
setState(() {});
},
),
2023-03-06 18:11:13 +00:00
),
),
2023-03-07 21:45:22 +00:00
if (!_showBlocked && widget.type == CoinControlViewType.use)
Container(
decoration: BoxDecoration(
color: Theme.of(context)
2023-03-07 19:51:06 +00:00
.extension<StackColors>()!
2023-03-07 21:45:22 +00:00
.backgroundAppBar,
boxShadow: [
Theme.of(context)
.extension<StackColors>()!
.standardBoxShadow,
],
),
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
RoundedWhiteContainer(
padding: const EdgeInsets.all(0),
child: Column(
children: [
2023-03-07 19:51:06 +00:00
Padding(
2023-03-07 21:45:22 +00:00
padding: const EdgeInsets.all(12),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Text(
"Selected amount",
style: STextStyles.w600_14(context),
),
Builder(
builder: (context) {
final int selectedSumInt =
2023-03-07 21:45:22 +00:00
_selectedAvailable.isEmpty
? 0
: _selectedAvailable
.map((e) => e.value)
.reduce(
(value, element) =>
value += element,
);
final selectedSum =
selectedSumInt.toAmountAsRaw(
2024-05-15 21:20:45 +00:00
fractionDigits: coin.fractionDigits,
);
return SelectableText(
2023-05-29 21:10:55 +00:00
ref
.watch(pAmountFormatter(coin))
.format(selectedSum),
2023-03-07 21:45:22 +00:00
style: widget.requestedTotal == null
? STextStyles.w600_14(context)
2024-05-27 23:56:22 +00:00
: STextStyles.w600_14(context)
.copyWith(
2023-03-07 21:45:22 +00:00
color: selectedSum >=
widget
.requestedTotal!
? Theme.of(context)
.extension<
StackColors>()!
.accentColorGreen
: Theme.of(context)
.extension<
StackColors>()!
2024-05-27 23:56:22 +00:00
.accentColorRed,
),
2023-03-07 21:45:22 +00:00
);
},
),
],
2023-03-07 19:51:06 +00:00
),
2023-03-07 21:45:22 +00:00
),
if (widget.requestedTotal != null)
Container(
2023-03-07 19:51:06 +00:00
width: double.infinity,
2023-03-07 21:45:22 +00:00
height: 1.5,
2023-03-07 19:51:06 +00:00
color: Theme.of(context)
.extension<StackColors>()!
.backgroundAppBar,
),
2023-03-07 21:45:22 +00:00
if (widget.requestedTotal != null)
Padding(
padding: const EdgeInsets.all(12),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Text(
"Amount to send",
style: STextStyles.w600_14(context),
),
SelectableText(
2023-05-29 21:10:55 +00:00
ref
.watch(pAmountFormatter(coin))
.format(widget.requestedTotal!),
2023-03-07 21:45:22 +00:00
style: STextStyles.w600_14(context),
),
],
2023-03-07 19:51:06 +00:00
),
2023-03-07 21:45:22 +00:00
),
],
),
),
2023-03-07 21:45:22 +00:00
const SizedBox(
height: 12,
),
PrimaryButton(
label: "Use coins",
enabled: _selectedAvailable.isNotEmpty,
onPressed: () async {
2023-03-16 22:12:16 +00:00
if (searchFocus.hasFocus) {
searchFocus.unfocus();
}
2023-03-07 21:45:22 +00:00
Navigator.of(context).pop(
_selectedAvailable,
);
},
),
],
),
2023-03-07 19:51:06 +00:00
),
),
2023-03-07 21:45:22 +00:00
],
),
2023-03-06 18:11:13 +00:00
),
),
),
);
}
}