address label save fix

This commit is contained in:
julian 2023-03-06 13:27:04 -06:00
parent 456abf7ee4
commit c7bcabf328
4 changed files with 24 additions and 20 deletions

View file

@ -1,6 +1,5 @@
import 'package:isar/isar.dart'; import 'package:isar/isar.dart';
import 'package:stackwallet/exceptions/main_db/main_db_exception.dart'; import 'package:stackwallet/exceptions/main_db/main_db_exception.dart';
import 'package:stackwallet/exceptions/sw_exception.dart';
import 'package:stackwallet/models/isar/models/isar_models.dart'; import 'package:stackwallet/models/isar/models/isar_models.dart';
import 'package:stackwallet/utilities/stack_file_system.dart'; import 'package:stackwallet/utilities/stack_file_system.dart';
import 'package:tuple/tuple.dart'; import 'package:tuple/tuple.dart';
@ -236,10 +235,6 @@ class MainDB {
Future<int> updateAddressLabel(AddressLabel addressLabel) async { Future<int> updateAddressLabel(AddressLabel addressLabel) async {
try { try {
return await isar.writeTxn(() async { return await isar.writeTxn(() async {
final deleted = await isar.addresses.delete(addressLabel.id);
if (!deleted) {
throw SWException("Failed to delete $addressLabel before updating");
}
return await isar.addressLabels.put(addressLabel); return await isar.addressLabels.put(addressLabel);
}); });
} catch (e) { } catch (e) {

View file

@ -84,7 +84,7 @@ class _AddressCardState extends State<AddressCard> {
onTap: () { onTap: () {
Navigator.of(context).pushNamed( Navigator.of(context).pushNamed(
EditAddressLabelView.routeName, EditAddressLabelView.routeName,
arguments: label!, arguments: label!.id,
); );
}, },
), ),
@ -122,6 +122,7 @@ class _AddressCardState extends State<AddressCard> {
text: widget.address.value, text: widget.address.value,
), ),
); );
if (mounted) {
unawaited( unawaited(
showFloatingFlushBar( showFloatingFlushBar(
type: FlushBarType.info, type: FlushBarType.info,
@ -129,6 +130,7 @@ class _AddressCardState extends State<AddressCard> {
context: context, context: context,
), ),
); );
}
}, },
), ),
), ),

View file

@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:isar/isar.dart';
import 'package:stackwallet/db/main_db.dart'; import 'package:stackwallet/db/main_db.dart';
import 'package:stackwallet/models/isar/models/address_label.dart'; import 'package:stackwallet/models/isar/models/address_label.dart';
import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/constants.dart';
@ -18,12 +19,12 @@ import 'package:stackwallet/widgets/textfield_icon_button.dart';
class EditAddressLabelView extends ConsumerStatefulWidget { class EditAddressLabelView extends ConsumerStatefulWidget {
const EditAddressLabelView({ const EditAddressLabelView({
Key? key, Key? key,
required this.addressLabel, required this.addressLabelId,
}) : super(key: key); }) : super(key: key);
static const String routeName = "/editAddressLabel"; static const String routeName = "/editAddressLabel";
final AddressLabel addressLabel; final int addressLabelId;
@override @override
ConsumerState<EditAddressLabelView> createState() => ConsumerState<EditAddressLabelView> createState() =>
@ -36,11 +37,17 @@ class _EditAddressLabelViewState extends ConsumerState<EditAddressLabelView> {
late final bool isDesktop; late final bool isDesktop;
late AddressLabel addressLabel;
@override @override
void initState() { void initState() {
isDesktop = Util.isDesktop; isDesktop = Util.isDesktop;
_labelFieldController = TextEditingController(); _labelFieldController = TextEditingController();
_labelFieldController.text = widget.addressLabel.value; addressLabel = MainDB.instance.isar.addressLabels
.where()
.idEqualTo(widget.addressLabelId)
.findFirstSync()!;
_labelFieldController.text = addressLabel.value;
super.initState(); super.initState();
} }
@ -195,7 +202,7 @@ class _EditAddressLabelViewState extends ConsumerState<EditAddressLabelView> {
label: "Save", label: "Save",
onPressed: () async { onPressed: () async {
await MainDB.instance.updateAddressLabel( await MainDB.instance.updateAddressLabel(
widget.addressLabel.copyWith( addressLabel.copyWith(
label: _labelFieldController.text, label: _labelFieldController.text,
), ),
); );
@ -209,7 +216,7 @@ class _EditAddressLabelViewState extends ConsumerState<EditAddressLabelView> {
TextButton( TextButton(
onPressed: () async { onPressed: () async {
await MainDB.instance.updateAddressLabel( await MainDB.instance.updateAddressLabel(
widget.addressLabel.copyWith( addressLabel.copyWith(
label: _labelFieldController.text, label: _labelFieldController.text,
), ),
); );

View file

@ -489,11 +489,11 @@ class RouteGenerator {
return _routeError("${settings.name} invalid args: ${args.toString()}"); return _routeError("${settings.name} invalid args: ${args.toString()}");
case EditAddressLabelView.routeName: case EditAddressLabelView.routeName:
if (args is AddressLabel) { if (args is int) {
return getRoute( return getRoute(
shouldUseMaterialRoute: useMaterialPageRoute, shouldUseMaterialRoute: useMaterialPageRoute,
builder: (_) => EditAddressLabelView( builder: (_) => EditAddressLabelView(
addressLabel: args, addressLabelId: args,
), ),
settings: RouteSettings( settings: RouteSettings(
name: settings.name, name: settings.name,