mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-12-01 12:04:21 +01:00
Cw 830 coin control getting cleared (#1825)
* init commit * add select all button * localisation all coins * fix isSending and isFrozen state updates * fix: clean up electrum UTXOs * ui fixes * address the review comments[skip ci] * remove onPopInvoked[skip ci] --------- Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
import 'package:cake_wallet/bitcoin_cash/bitcoin_cash.dart';
|
||||
import 'package:cake_wallet/generated/i18n.dart';
|
||||
import 'package:cake_wallet/routes.dart';
|
||||
import 'package:cake_wallet/src/screens/base_page.dart';
|
||||
import 'package:cake_wallet/src/screens/unspent_coins/widgets/unspent_coins_list_item.dart';
|
||||
import 'package:cake_wallet/src/widgets/alert_with_no_action.dart.dart';
|
||||
import 'package:cake_wallet/src/widgets/standard_checkbox.dart';
|
||||
import 'package:cake_wallet/themes/extensions/cake_text_theme.dart';
|
||||
import 'package:cake_wallet/view_model/unspent_coins/unspent_coins_list_view_model.dart';
|
||||
import 'package:cw_core/wallet_type.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_mobx/flutter_mobx.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
|
||||
class UnspentCoinsListPage extends BasePage {
|
||||
UnspentCoinsListPage({required this.unspentCoinsListViewModel});
|
||||
@@ -15,16 +16,53 @@ class UnspentCoinsListPage extends BasePage {
|
||||
@override
|
||||
String get title => S.current.unspent_coins_title;
|
||||
|
||||
@override
|
||||
Widget leading(BuildContext context) {
|
||||
return MergeSemantics(
|
||||
child: SizedBox(
|
||||
height: 37,
|
||||
width: 37,
|
||||
child: ButtonTheme(
|
||||
minWidth: double.minPositive,
|
||||
child: Semantics(
|
||||
label: S.of(context).seed_alert_back,
|
||||
child: TextButton(
|
||||
style: ButtonStyle(
|
||||
overlayColor: WidgetStateColor.resolveWith((states) => Colors.transparent),
|
||||
),
|
||||
onPressed: () async => await handleOnPopInvoked(context),
|
||||
child: backButton(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final UnspentCoinsListViewModel unspentCoinsListViewModel;
|
||||
|
||||
Future<void> handleOnPopInvoked(BuildContext context) async {
|
||||
final hasChanged = unspentCoinsListViewModel.hasAdjustableFieldChanged;
|
||||
if (unspentCoinsListViewModel.items.isEmpty || !hasChanged) {
|
||||
Navigator.of(context).pop();
|
||||
} else {
|
||||
unspentCoinsListViewModel.setIsDisposing(true);
|
||||
await unspentCoinsListViewModel.dispose();
|
||||
Navigator.of(context).pop();
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget body(BuildContext context) => UnspentCoinsListForm(unspentCoinsListViewModel);
|
||||
Widget body(BuildContext context) =>
|
||||
UnspentCoinsListForm(unspentCoinsListViewModel, handleOnPopInvoked);
|
||||
}
|
||||
|
||||
class UnspentCoinsListForm extends StatefulWidget {
|
||||
UnspentCoinsListForm(this.unspentCoinsListViewModel);
|
||||
UnspentCoinsListForm(this.unspentCoinsListViewModel, this.handleOnPopInvoked);
|
||||
|
||||
final UnspentCoinsListViewModel unspentCoinsListViewModel;
|
||||
final Future<void> Function(BuildContext context) handleOnPopInvoked;
|
||||
|
||||
@override
|
||||
UnspentCoinsListFormState createState() => UnspentCoinsListFormState(unspentCoinsListViewModel);
|
||||
@@ -35,36 +73,126 @@ class UnspentCoinsListFormState extends State<UnspentCoinsListForm> {
|
||||
|
||||
final UnspentCoinsListViewModel unspentCoinsListViewModel;
|
||||
|
||||
late Future<void> _initialization;
|
||||
ReactionDisposer? _disposer;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_initialization = unspentCoinsListViewModel.initialSetup();
|
||||
_setupReactions();
|
||||
}
|
||||
|
||||
void _setupReactions() {
|
||||
_disposer = reaction<bool>(
|
||||
(_) => unspentCoinsListViewModel.isDisposing,
|
||||
(isDisposing) {
|
||||
if (isDisposing) {
|
||||
_showSavingDataAlert();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _showSavingDataAlert() {
|
||||
showDialog<void>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertWithNoAction(
|
||||
alertContent: 'Updating, please wait…',
|
||||
alertBarrierDismissible: false,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_disposer?.call();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: EdgeInsets.fromLTRB(24, 12, 24, 24),
|
||||
child: Observer(
|
||||
builder: (_) => ListView.separated(
|
||||
itemCount: unspentCoinsListViewModel.items.length,
|
||||
separatorBuilder: (_, __) => SizedBox(height: 15),
|
||||
itemBuilder: (_, int index) {
|
||||
return Observer(builder: (_) {
|
||||
final item = unspentCoinsListViewModel.items[index];
|
||||
return PopScope(
|
||||
canPop: false,
|
||||
onPopInvokedWithResult: (bool didPop, Object? result) async {
|
||||
if (didPop) return;
|
||||
if(mounted)
|
||||
await widget.handleOnPopInvoked(context);
|
||||
},
|
||||
child: FutureBuilder<void>(
|
||||
future: _initialization,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
return GestureDetector(
|
||||
onTap: () => Navigator.of(context).pushNamed(Routes.unspentCoinsDetails,
|
||||
arguments: [item, unspentCoinsListViewModel]),
|
||||
child: UnspentCoinsListItem(
|
||||
note: item.note,
|
||||
amount: item.amount,
|
||||
address: item.address,
|
||||
isSending: item.isSending,
|
||||
isFrozen: item.isFrozen,
|
||||
isChange: item.isChange,
|
||||
isSilentPayment: item.isSilentPayment,
|
||||
onCheckBoxTap: item.isFrozen
|
||||
? null
|
||||
: () async {
|
||||
item.isSending = !item.isSending;
|
||||
await unspentCoinsListViewModel.saveUnspentCoinInfo(item);
|
||||
}));
|
||||
});
|
||||
})));
|
||||
if (snapshot.hasError) return Center(child: Text('Failed to load unspent coins'));
|
||||
|
||||
return Container(
|
||||
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 12),
|
||||
child: Observer(
|
||||
builder: (_) => Column(
|
||||
children: [
|
||||
if (unspentCoinsListViewModel.items.isNotEmpty)
|
||||
Row(
|
||||
children: [
|
||||
SizedBox(width: 12),
|
||||
StandardCheckbox(
|
||||
iconColor: Theme.of(context).extension<CakeTextTheme>()!.buttonTextColor,
|
||||
value: unspentCoinsListViewModel.isAllSelected,
|
||||
onChanged: (value) => unspentCoinsListViewModel.toggleSelectAll(value),
|
||||
),
|
||||
SizedBox(width: 12),
|
||||
Text(
|
||||
S.current.all_coins,
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
],
|
||||
),
|
||||
SizedBox(height: 15),
|
||||
Expanded(
|
||||
child: unspentCoinsListViewModel.items.isEmpty
|
||||
? Center(child: Text('No unspent coins available\ntry to reconnect',textAlign: TextAlign.center))
|
||||
: ListView.separated(
|
||||
itemCount: unspentCoinsListViewModel.items.length,
|
||||
separatorBuilder: (_, __) => SizedBox(height: 15),
|
||||
itemBuilder: (_, int index) {
|
||||
final item = unspentCoinsListViewModel.items[index];
|
||||
return Observer(
|
||||
builder: (_) => GestureDetector(
|
||||
onTap: () => Navigator.of(context).pushNamed(
|
||||
Routes.unspentCoinsDetails,
|
||||
arguments: [item, unspentCoinsListViewModel],
|
||||
),
|
||||
child: UnspentCoinsListItem(
|
||||
note: item.note,
|
||||
amount: item.amount,
|
||||
address: item.address,
|
||||
isSending: item.isSending,
|
||||
isFrozen: item.isFrozen,
|
||||
isChange: item.isChange,
|
||||
isSilentPayment: item.isSilentPayment,
|
||||
onCheckBoxTap: item.isFrozen
|
||||
? null
|
||||
: () async {
|
||||
item.isSending = !item.isSending;
|
||||
await unspentCoinsListViewModel
|
||||
.saveUnspentCoinInfo(item);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,18 +3,18 @@ import 'package:cake_wallet/src/widgets/base_alert_dialog.dart';
|
||||
|
||||
class AlertWithNoAction extends BaseAlertDialog {
|
||||
AlertWithNoAction({
|
||||
required this.alertTitle,
|
||||
this.alertTitle,
|
||||
required this.alertContent,
|
||||
this.alertBarrierDismissible = true,
|
||||
Key? key,
|
||||
});
|
||||
|
||||
final String alertTitle;
|
||||
final String? alertTitle;
|
||||
final String alertContent;
|
||||
final bool alertBarrierDismissible;
|
||||
|
||||
@override
|
||||
String get titleText => alertTitle;
|
||||
String? get titleText => alertTitle;
|
||||
|
||||
@override
|
||||
String get contentText => alertContent;
|
||||
@@ -26,5 +26,5 @@ class AlertWithNoAction extends BaseAlertDialog {
|
||||
bool get isBottomDividerExists => false;
|
||||
|
||||
@override
|
||||
Widget actionButtons(BuildContext context) => Container(height: 60);
|
||||
Widget actionButtons(BuildContext context) => Container();
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import 'package:flutter/material.dart';
|
||||
class BaseAlertDialog extends StatelessWidget {
|
||||
String? get headerText => '';
|
||||
|
||||
String get titleText => '';
|
||||
String? get titleText => '';
|
||||
|
||||
String get contentText => '';
|
||||
|
||||
@@ -43,7 +43,7 @@ class BaseAlertDialog extends StatelessWidget {
|
||||
|
||||
Widget title(BuildContext context) {
|
||||
return Text(
|
||||
titleText,
|
||||
titleText!,
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
fontSize: 20,
|
||||
@@ -191,10 +191,11 @@ class BaseAlertDialog extends StatelessWidget {
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
if (headerText?.isNotEmpty ?? false) headerTitle(context),
|
||||
titleText != null ?
|
||||
Padding(
|
||||
padding: EdgeInsets.fromLTRB(24, 20, 24, 0),
|
||||
child: title(context),
|
||||
),
|
||||
) : SizedBox(height: 16),
|
||||
isDividerExists
|
||||
? Padding(
|
||||
padding: EdgeInsets.only(top: 16, bottom: 8),
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import 'package:cw_core/unspent_comparable_mixin.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
|
||||
part 'unspent_coins_item.g.dart';
|
||||
|
||||
class UnspentCoinsItem = UnspentCoinsItemBase with _$UnspentCoinsItem;
|
||||
|
||||
abstract class UnspentCoinsItemBase with Store {
|
||||
abstract class UnspentCoinsItemBase with Store, UnspentComparable {
|
||||
UnspentCoinsItemBase({
|
||||
required this.address,
|
||||
required this.amount,
|
||||
@@ -13,7 +14,7 @@ abstract class UnspentCoinsItemBase with Store {
|
||||
required this.note,
|
||||
required this.isSending,
|
||||
required this.isChange,
|
||||
required this.amountRaw,
|
||||
required this.value,
|
||||
required this.vout,
|
||||
required this.keyImage,
|
||||
required this.isSilentPayment,
|
||||
@@ -41,7 +42,7 @@ abstract class UnspentCoinsItemBase with Store {
|
||||
bool isChange;
|
||||
|
||||
@observable
|
||||
int amountRaw;
|
||||
int value;
|
||||
|
||||
@observable
|
||||
int vout;
|
||||
|
||||
@@ -10,6 +10,7 @@ import 'package:cw_core/wallet_base.dart';
|
||||
import 'package:cw_core/wallet_type.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
|
||||
part 'unspent_coins_list_view_model.g.dart';
|
||||
@@ -22,55 +23,66 @@ abstract class UnspentCoinsListViewModelBase with Store {
|
||||
required Box<UnspentCoinsInfo> unspentCoinsInfo,
|
||||
this.coinTypeToSpendFrom = UnspentCoinType.any,
|
||||
}) : _unspentCoinsInfo = unspentCoinsInfo,
|
||||
_items = ObservableList<UnspentCoinsItem>() {
|
||||
_updateUnspentCoinsInfo();
|
||||
_updateUnspents();
|
||||
}
|
||||
items = ObservableList<UnspentCoinsItem>(),
|
||||
_originalState = {};
|
||||
|
||||
WalletBase wallet;
|
||||
final WalletBase wallet;
|
||||
final Box<UnspentCoinsInfo> _unspentCoinsInfo;
|
||||
final UnspentCoinType coinTypeToSpendFrom;
|
||||
|
||||
@observable
|
||||
ObservableList<UnspentCoinsItem> _items;
|
||||
ObservableList<UnspentCoinsItem> items;
|
||||
|
||||
final Map<String, Map<String, dynamic>> _originalState;
|
||||
|
||||
@observable
|
||||
bool isDisposing = false;
|
||||
|
||||
@computed
|
||||
ObservableList<UnspentCoinsItem> get items => _items;
|
||||
bool get isAllSelected => items.every((element) => element.isFrozen || element.isSending);
|
||||
|
||||
Future<void> saveUnspentCoinInfo(UnspentCoinsItem item) async {
|
||||
try {
|
||||
final info =
|
||||
getUnspentCoinInfo(item.hash, item.address, item.amountRaw, item.vout, item.keyImage);
|
||||
Future<void> initialSetup() async {
|
||||
await _updateUnspents();
|
||||
_storeOriginalState();
|
||||
}
|
||||
|
||||
if (info == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
info.isFrozen = item.isFrozen;
|
||||
info.isSending = item.isSending;
|
||||
info.note = item.note;
|
||||
|
||||
await info.save();
|
||||
await _updateUnspents();
|
||||
await wallet.updateBalance();
|
||||
} catch (e) {
|
||||
print(e.toString());
|
||||
void _storeOriginalState() {
|
||||
_originalState.clear();
|
||||
for (final item in items) {
|
||||
_originalState[item.hash] = {
|
||||
'isFrozen': item.isFrozen,
|
||||
'note': item.note,
|
||||
'isSending': item.isSending,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
UnspentCoinsInfo? getUnspentCoinInfo(
|
||||
String hash, String address, int value, int vout, String? keyImage) {
|
||||
bool _hasAdjustableFieldChanged(UnspentCoinsItem item) {
|
||||
final original = _originalState[item.hash];
|
||||
if (original == null) return false;
|
||||
return original['isFrozen'] != item.isFrozen ||
|
||||
original['note'] != item.note ||
|
||||
original['isSending'] != item.isSending;
|
||||
}
|
||||
|
||||
bool get hasAdjustableFieldChanged => items.any(_hasAdjustableFieldChanged);
|
||||
|
||||
|
||||
Future<void> saveUnspentCoinInfo(UnspentCoinsItem item) async {
|
||||
try {
|
||||
return _unspentCoinsInfo.values.firstWhere((element) =>
|
||||
element.walletId == wallet.id &&
|
||||
element.hash == hash &&
|
||||
element.address == address &&
|
||||
element.value == value &&
|
||||
element.vout == vout &&
|
||||
element.keyImage == keyImage);
|
||||
final existingInfo = _unspentCoinsInfo.values
|
||||
.firstWhereOrNull((element) => element.walletId == wallet.id && element == item);
|
||||
if (existingInfo == null) return;
|
||||
|
||||
existingInfo.isFrozen = item.isFrozen;
|
||||
existingInfo.isSending = item.isSending;
|
||||
existingInfo.note = item.note;
|
||||
|
||||
|
||||
await existingInfo.save();
|
||||
_updateUnspentCoinsInfo();
|
||||
} catch (e) {
|
||||
print("UnspentCoinsInfo not found for coin: $e");
|
||||
return null;
|
||||
print('Error saving coin info: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,37 +127,60 @@ abstract class UnspentCoinsListViewModelBase with Store {
|
||||
|
||||
@action
|
||||
void _updateUnspentCoinsInfo() {
|
||||
_items.clear();
|
||||
items.clear();
|
||||
|
||||
List<UnspentCoinsItem> unspents = [];
|
||||
_getUnspents().forEach((Unspent elem) {
|
||||
try {
|
||||
final info =
|
||||
getUnspentCoinInfo(elem.hash, elem.address, elem.value, elem.vout, elem.keyImage);
|
||||
if (info == null) {
|
||||
return;
|
||||
}
|
||||
final unspents = _getUnspents()
|
||||
.map((elem) {
|
||||
try {
|
||||
final existingItem = _unspentCoinsInfo.values
|
||||
.firstWhereOrNull((item) => item.walletId == wallet.id && item == elem);
|
||||
|
||||
unspents.add(UnspentCoinsItem(
|
||||
address: elem.address,
|
||||
amount: '${formatAmountToString(elem.value)} ${wallet.currency.title}',
|
||||
hash: elem.hash,
|
||||
isFrozen: info.isFrozen,
|
||||
note: info.note,
|
||||
isSending: info.isSending,
|
||||
amountRaw: elem.value,
|
||||
vout: elem.vout,
|
||||
keyImage: elem.keyImage,
|
||||
isChange: elem.isChange,
|
||||
isSilentPayment: info.isSilentPayment ?? false,
|
||||
));
|
||||
} catch (e, s) {
|
||||
print(s);
|
||||
print(e.toString());
|
||||
ExceptionHandler.onError(FlutterErrorDetails(exception: e, stack: s));
|
||||
}
|
||||
});
|
||||
if (existingItem == null) return null;
|
||||
|
||||
_items.addAll(unspents);
|
||||
return UnspentCoinsItem(
|
||||
address: elem.address,
|
||||
amount: '${formatAmountToString(elem.value)} ${wallet.currency.title}',
|
||||
hash: elem.hash,
|
||||
isFrozen: existingItem.isFrozen,
|
||||
note: existingItem.note,
|
||||
isSending: existingItem.isSending,
|
||||
value: elem.value,
|
||||
vout: elem.vout,
|
||||
keyImage: elem.keyImage,
|
||||
isChange: elem.isChange,
|
||||
isSilentPayment: existingItem.isSilentPayment ?? false,
|
||||
);
|
||||
} catch (e, s) {
|
||||
print('Error: $e\nStack: $s');
|
||||
ExceptionHandler.onError(
|
||||
FlutterErrorDetails(exception: e, stack: s),
|
||||
);
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.whereType<UnspentCoinsItem>()
|
||||
.toList();
|
||||
|
||||
unspents.sort((a, b) => b.value.compareTo(a.value));
|
||||
|
||||
items.addAll(unspents);
|
||||
}
|
||||
|
||||
@action
|
||||
void toggleSelectAll(bool value) {
|
||||
for (final item in items) {
|
||||
if (item.isFrozen || item.isSending == value) continue;
|
||||
item.isSending = value;
|
||||
saveUnspentCoinInfo(item);
|
||||
}
|
||||
}
|
||||
|
||||
@action
|
||||
void setIsDisposing(bool value) => isDisposing = value;
|
||||
|
||||
@action
|
||||
Future<void> dispose() async {
|
||||
await _updateUnspents();
|
||||
await wallet.updateBalance();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user