freeze button fix

This commit is contained in:
julian 2023-03-15 09:48:39 -06:00
parent a87cf5807c
commit 4be2bd6fcd
5 changed files with 134 additions and 59 deletions

View file

@ -6,20 +6,19 @@ import 'package:flutter_svg/svg.dart';
import 'package:isar/isar.dart';
import 'package:stackwallet/db/main_db.dart';
import 'package:stackwallet/models/isar/models/blockchain_data/utxo.dart';
import 'package:stackwallet/pages_desktop_specific/coin_control/freeze_button.dart';
import 'package:stackwallet/pages_desktop_specific/coin_control/utxo_row.dart';
import 'package:stackwallet/providers/global/wallets_provider.dart';
import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/format.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart';
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
import 'package:stackwallet/widgets/custom_buttons/dropdown_button.dart';
import 'package:stackwallet/widgets/desktop/desktop_app_bar.dart';
import 'package:stackwallet/widgets/desktop/desktop_scaffold.dart';
import 'package:stackwallet/widgets/desktop/primary_button.dart';
import 'package:stackwallet/widgets/desktop/secondary_button.dart';
import 'package:stackwallet/widgets/icon_widgets/x_icon.dart';
import 'package:stackwallet/widgets/stack_text_field.dart';
@ -75,54 +74,10 @@ class _DesktopCoinControlViewState
final Set<UtxoRowData> _selectedUTXOs = {};
String _searchString = "";
String _freezeLabelCache = "Freeze";
CCFilter _filter = CCFilter.all;
CCSortDescriptor _sort = CCSortDescriptor.age;
String _freezeLabel(Set<UtxoRowData> dataSet) {
if (dataSet.isEmpty) return _freezeLabelCache;
bool hasUnblocked = false;
for (final data in dataSet) {
if (!data.utxo.isBlocked) {
hasUnblocked = true;
break;
}
}
_freezeLabelCache = hasUnblocked ? "Freeze" : "Unfreeze";
return _freezeLabelCache;
}
Future<void> _onFreezeStateButtonPressed() async {
switch (_freezeLabelCache) {
case "Freeze":
for (final e in _selectedUTXOs) {
e.utxo = e.utxo.copyWith(isBlocked: true);
}
break;
case "Unfreeze":
for (final e in _selectedUTXOs) {
e.utxo = e.utxo.copyWith(isBlocked: false);
}
break;
default:
Logging.instance.log(
"Unknown utxo method name found in $runtimeType",
level: LogLevel.Fatal,
);
return;
}
// final update utxo set in db
await MainDB.instance.putUTXOs(_selectedUTXOs.map((e) => e.utxo).toList());
// change label of freeze/unfreeze button
setState(() {});
}
@override
void initState() {
_searchController = TextEditingController();
@ -343,11 +298,9 @@ class _DesktopCoinControlViewState
}
},
),
secondChild: PrimaryButton(
buttonHeight: ButtonHeight.l,
width: 200,
label: _freezeLabel(_selectedUTXOs),
onPressed: _onFreezeStateButtonPressed,
secondChild: FreezeButton(
key: Key("${_selectedUTXOs.length}"),
selectedUTXOs: _selectedUTXOs,
),
crossFadeState: _selectedUTXOs.isEmpty
? CrossFadeState.showFirst
@ -405,7 +358,7 @@ class _DesktopCoinControlViewState
.where()
.idEqualTo(ids[index])
.findFirstSync()!;
final data = UtxoRowData(utxo, false);
final data = UtxoRowData(utxo.id, false);
data.selected = _selectedUTXOs.contains(data);
return UtxoRow(

View file

@ -0,0 +1,117 @@
import 'package:async/async.dart';
import 'package:flutter/material.dart';
import 'package:isar/isar.dart';
import 'package:stackwallet/db/main_db.dart';
import 'package:stackwallet/models/isar/models/blockchain_data/utxo.dart';
import 'package:stackwallet/pages_desktop_specific/coin_control/utxo_row.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/widgets/desktop/primary_button.dart';
class FreezeButton extends StatefulWidget {
const FreezeButton({
Key? key,
required this.selectedUTXOs,
}) : super(key: key);
final Set<UtxoRowData> selectedUTXOs;
@override
State<FreezeButton> createState() => _FreezeButtonState();
}
class _FreezeButtonState extends State<FreezeButton> {
String _freezeLabelCache = "Freeze";
String _freezeLabel(Set<UtxoRowData> dataSet) {
if (dataSet.isEmpty) return _freezeLabelCache;
bool hasUnblocked = false;
for (final data in dataSet) {
if (!MainDB.instance.isar.utxos
.where()
.idEqualTo(data.utxoId)
.findFirstSync()!
.isBlocked) {
hasUnblocked = true;
break;
}
}
_freezeLabelCache = hasUnblocked ? "Freeze" : "Unfreeze";
return _freezeLabelCache;
}
Future<void> _onFreezeStateButtonPressed() async {
List<UTXO> utxosToUpdate = [];
switch (_freezeLabelCache) {
case "Freeze":
for (final e in widget.selectedUTXOs) {
final utxo = MainDB.instance.isar.utxos
.where()
.idEqualTo(e.utxoId)
.findFirstSync()!;
if (!utxo.isBlocked) {
utxosToUpdate.add(utxo.copyWith(isBlocked: true));
}
}
break;
case "Unfreeze":
for (final e in widget.selectedUTXOs) {
final utxo = MainDB.instance.isar.utxos
.where()
.idEqualTo(e.utxoId)
.findFirstSync()!;
if (utxo.isBlocked) {
utxosToUpdate.add(utxo.copyWith(isBlocked: false));
}
}
break;
default:
Logging.instance.log(
"Unknown utxo method name found in $runtimeType",
level: LogLevel.Fatal,
);
return;
}
// final update utxo set in db
if (utxosToUpdate.isNotEmpty) {
await MainDB.instance.putUTXOs(utxosToUpdate);
}
}
late Stream<UTXO?> bigStream;
@override
void initState() {
List<Stream<UTXO?>> streams = [];
for (final data in widget.selectedUTXOs) {
final stream = MainDB.instance.watchUTXO(id: data.utxoId);
streams.add(stream);
}
bigStream = StreamGroup.merge(streams);
bigStream.listen((event) {
if (mounted) {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
setState(() {});
});
}
});
super.initState();
}
@override
Widget build(BuildContext context) {
debugPrint("BUILD: $runtimeType");
return PrimaryButton(
buttonHeight: ButtonHeight.l,
width: 200,
label: _freezeLabel(widget.selectedUTXOs),
onPressed: _onFreezeStateButtonPressed,
);
}
}

View file

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:isar/isar.dart';
import 'package:stackwallet/db/main_db.dart';
import 'package:stackwallet/models/isar/models/isar_models.dart';
import 'package:stackwallet/pages/coin_control/utxo_details_view.dart';
@ -13,23 +14,23 @@ import 'package:stackwallet/widgets/icon_widgets/utxo_status_icon.dart';
import 'package:stackwallet/widgets/rounded_white_container.dart';
class UtxoRowData {
UtxoRowData(this.utxo, this.selected);
UtxoRowData(this.utxoId, this.selected);
UTXO utxo;
Id utxoId;
bool selected;
@override
String toString() {
return "selected=$selected: $utxo";
return "selected=$selected: $utxoId";
}
@override
bool operator ==(Object other) {
return other is UtxoRowData && other.utxo == utxo;
return other is UtxoRowData && other.utxoId == utxoId;
}
@override
int get hashCode => Object.hashAll([utxo.hashCode]);
int get hashCode => Object.hashAll([utxoId.hashCode]);
}
class UtxoRow extends ConsumerStatefulWidget {
@ -54,7 +55,10 @@ class _UtxoRowState extends ConsumerState<UtxoRow> {
@override
void initState() {
utxo = widget.data.utxo;
utxo = MainDB.instance.isar.utxos
.where()
.idEqualTo(widget.data.utxoId)
.findFirstSync()!;
stream = MainDB.instance.watchUTXO(id: utxo.id);
super.initState();

View file

@ -66,7 +66,7 @@ packages:
source: hosted
version: "1.4.0"
async:
dependency: transitive
dependency: "direct main"
description:
name: async
sha256: bfe67ef28df125b7dddcea62755991f807aa39a2492a23e1550161692950bbe0

View file

@ -141,6 +141,7 @@ dependencies:
dropdown_button2: 1.7.2
string_validator: ^0.3.0
equatable: ^2.0.5
async: ^2.10.0
dev_dependencies:
flutter_test: