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';
2023-03-22 15:25:21 +00:00
import 'package:stackwallet/db/isar/main_db.dart';
2023-03-06 18:11:13 +00:00
import 'package:stackwallet/models/isar/models/isar_models.dart';
import 'package:stackwallet/pages/coin_control/utxo_card.dart';
import 'package:stackwallet/pages/coin_control/utxo_details_view.dart';
2023-03-07 19:51:06 +00:00
import 'package:stackwallet/providers/global/wallets_provider.dart';
import 'package:stackwallet/services/mixins/coin_control_interface.dart';
import 'package:stackwallet/themes/stack_colors.dart';
2023-04-06 21:24:56 +00:00
import 'package:stackwallet/utilities/amount/amount.dart';
2023-05-29 21:10:55 +00:00
import 'package:stackwallet/utilities/amount/amount_formatter.dart';
2023-03-16 22:12:16 +00:00
import 'package:stackwallet/utilities/assets.dart';
2023-03-06 18:11:13 +00:00
import 'package:stackwallet/utilities/constants.dart';
2023-03-07 19:51:06 +00:00
import 'package:stackwallet/utilities/enums/coin_enum.dart';
2023-03-06 18:11:13 +00:00
import 'package:stackwallet/utilities/text_styles.dart';
2023-03-22 15:25:21 +00:00
import 'package:stackwallet/widgets/animated_widgets/rotate_icon.dart';
2023-03-16 22:12:16 +00:00
import 'package:stackwallet/widgets/app_bar_field.dart';
2023-03-06 18:11:13 +00:00
import 'package:stackwallet/widgets/background.dart';
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
2023-03-16 22:42:46 +00:00
import 'package:stackwallet/widgets/custom_buttons/dropdown_button.dart';
2023-03-07 19:51:06 +00:00
import 'package:stackwallet/widgets/desktop/primary_button.dart';
import 'package:stackwallet/widgets/desktop/secondary_button.dart';
2023-03-16 22:42:46 +00:00
import 'package:stackwallet/widgets/expandable2.dart';
2023-03-07 21:45:22 +00:00
import 'package:stackwallet/widgets/icon_widgets/x_icon.dart';
2023-03-22 15:25:21 +00:00
import 'package:stackwallet/widgets/rounded_container.dart';
2023-03-06 18:11:13 +00:00
import 'package:stackwallet/widgets/rounded_white_container.dart';
import 'package:stackwallet/widgets/toggle.dart';
import 'package:tuple/tuple.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({
Key? key,
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,
2023-03-06 18:11:13 +00:00
}) : super(key: key);
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)
.getManager(widget.walletId)
.wallet as CoinControlInterface;
await coinControlInterface.refreshBalance(notify: true);
}
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-03-07 19:51:06 +00:00
final coin = ref.watch(
pWallets.select(
2023-03-07 19:51:06 +00:00
(value) => value
.getManager(
widget.walletId,
)
.coin,
),
);
2023-03-08 17:34:00 +00:00
final currentChainHeight = ref.watch(
pWallets.select(
2023-03-08 17:34:00 +00:00
(value) => value
.getManager(
widget.walletId,
)
.currentHeight,
),
);
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: "",
coin: coin,
);
} 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 : "",
coin: coin,
);
}
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(
widget.type == CoinControlViewType.use ? _selectedAvailable : null);
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(
widget.type == CoinControlViewType.use
? _selectedAvailable
: null);
},
),
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(
"${utxo.walletId}_${utxo.id}_$isSelected"),
walletId: widget.walletId,
utxo: utxo,
canSelect: widget.type ==
CoinControlViewType.manage ||
(widget.type == CoinControlViewType.use &&
!utxo.isBlocked &&
utxo.isConfirmed(
currentChainHeight,
coin.requiredConfirmations,
)),
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(
"${utxo.walletId}_${utxo.id}_$isSelected"),
walletId: widget.walletId,
utxo: utxo,
canSelect: widget.type ==
CoinControlViewType.manage ||
(widget.type ==
CoinControlViewType.use &&
!_showBlocked &&
utxo.isConfirmed(
currentChainHeight,
coin.requiredConfirmations,
)),
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(
context),
),
const SizedBox(
height: 2,
),
Text(
"${entry.value.length} "
"output${entry.value.length > 1 ? "s" : ""}",
style:
STextStyles.w500_12(
context)
.copyWith(
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(
"${utxo.walletId}_${utxo.id}_$isSelected"),
walletId: widget.walletId,
utxo: utxo,
canSelect: widget.type ==
CoinControlViewType
.manage ||
(widget.type ==
CoinControlViewType
.use &&
!utxo.isBlocked &&
utxo.isConfirmed(
currentChainHeight,
coin.requiredConfirmations,
)),
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) {
await MainDB.instance.putUTXOs(_selectedBlocked
.map(
(e) => e.copyWith(
isBlocked: false,
),
)
.toList());
_selectedBlocked.clear();
} else {
await MainDB.instance.putUTXOs(_selectedAvailable
.map(
(e) => e.copyWith(
isBlocked: true,
),
)
.toList());
_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(
fractionDigits: coin.decimals,
);
2023-03-07 21:45:22 +00:00
return Text(
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)
: STextStyles.w600_14(context).copyWith(
color: selectedSum >=
widget
.requestedTotal!
? Theme.of(context)
.extension<
StackColors>()!
.accentColorGreen
: Theme.of(context)
.extension<
StackColors>()!
.accentColorRed),
);
},
),
],
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),
),
Text(
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
),
),
),
);
}
}