Skip to content

Commit

Permalink
Simplify adding contacts
Browse files Browse the repository at this point in the history
  • Loading branch information
hhanh00 committed Mar 2, 2023
1 parent 02ff9a9 commit 6d4e20a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 31 deletions.
48 changes: 25 additions & 23 deletions lib/contact.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:material_design_icons_flutter/material_design_icons_flutter.dart
import 'package:warp_api/data_fb_generated.dart';
import 'package:warp_api/warp_api.dart';

import 'home.dart';
import 'main.dart';
import 'generated/l10n.dart';
import 'store.dart';
Expand All @@ -18,13 +19,15 @@ class ContactsTab extends StatefulWidget {
class ContactsState extends State<ContactsTab> {
@override
Widget build(BuildContext context) {
final s = S.of(context);
final theme = Theme.of(context);
return Observer(builder: (context) =>
Padding(padding: EdgeInsets.all(8), child: contacts.contacts.isEmpty
? NoContact()
: Column(children: [
if (!settings.coins[active.coin].contactsSaved) OutlinedButton(onPressed: active.canPay ? _onCommit : null, child: Text(S.of(context).saveToBlockchain), style: OutlinedButton.styleFrom(
if (!settings.coins[active.coin].contactsSaved) OutlinedButton(onPressed: _onCommit, child: Text(s.saveToBlockchain), style: OutlinedButton.styleFrom(
side: BorderSide(
width: 1, color: Theme.of(context).primaryColor))),
width: 1, color: theme.primaryColor))),
Expanded(child: ListView.builder(
itemCount: contacts.contacts.length,
itemBuilder: (BuildContext context, int index) {
Expand All @@ -34,7 +37,7 @@ class ContactsState extends State<ContactsTab> {
key: Key("${c.id}"),
child: ListTile(
title: Text(c.name!,
style: Theme.of(context).textTheme.headlineSmall),
style: theme.textTheme.headlineSmall),
subtitle: Text(c.address!),
trailing: Icon(Icons.chevron_right),
onTap: () { _onContact(c); },
Expand All @@ -56,10 +59,7 @@ class ContactsState extends State<ContactsTab> {
}

_editContact(ContactT c) async {
final contact = await showContactForm(context, c, true);
if (contact != null) {
contacts.add(contact);
}
await addContact(context, c);
}

Future<bool> _onConfirmDelContact(ContactT c) async {
Expand All @@ -73,22 +73,6 @@ class ContactsState extends State<ContactsTab> {
contacts.remove(c);
}

Future<ContactT?> showContactForm(BuildContext context, ContactT c, bool edit) async {
final key = GlobalKey<ContactState>();

final contact = await showDialog<ContactT>(
context: context,
builder: (context) => AlertDialog(
contentPadding: EdgeInsets.all(16),
title: Text(edit ? S.of(context).editContact : S.of(context).addContact),
content: ContactForm(c, key: key),
actions: confirmButtons(context, () {
key.currentState!.onOK();
}),
));
return contact;
}

_onCommit() async {
try {
final txPlan = WarpApi.commitUnsavedContacts(settings.anchorOffset);
Expand Down Expand Up @@ -226,3 +210,21 @@ class AddressState extends State<AddressInput> {
});
}
}

Future<void> addContact(BuildContext context, ContactT? c) async {
final s = S.of(context);
final key = GlobalKey<ContactState>();
final contact = await showDialog<ContactT>(
context: context,
builder: (context) => AlertDialog(
contentPadding: EdgeInsets.all(16),
title: Text(c?.name != null ? s.editContact : s.addContact),
content: ContactForm(c ?? ContactT(), key: key),
actions: confirmButtons(context, () {
key.currentState!.onOK();
}),
));
if (contact != null) {
contacts.add(contact);
}
}
6 changes: 1 addition & 5 deletions lib/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -485,11 +485,7 @@ class HomeInnerState extends State<HomeInnerPage> with SingleTickerProviderState
}

_onAddContact() async {
final contact = await contactKey.currentState
?.showContactForm(context, ContactT(), false);
if (contact != null) {
contacts.add(contact);
}
await addContact(context, ContactT());
}

void _initForegroundTask() {
Expand Down
10 changes: 9 additions & 1 deletion lib/send.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:mobx/mobx.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:warp_api/data_fb_generated.dart' hide Account;
import 'accounts.dart';
import 'contact.dart';
import 'dualmoneyinput.dart';
import 'package:warp_api/types.dart';
import 'package:warp_api/warp_api.dart';
Expand Down Expand Up @@ -190,7 +191,10 @@ class SendState extends State<SendPage> {
)),
IconButton(
icon: new Icon(MdiIcons.qrcodeScan),
onPressed: _onScan)
onPressed: _onScan),
IconButton(
icon: new Icon(MdiIcons.contacts),
onPressed: _onAddContact),
]),
DualMoneyInputWidget(
key: _amountKey,
Expand Down Expand Up @@ -357,6 +361,10 @@ class SendState extends State<SendPage> {
}
}

Future<void> _onAddContact() async {
await addContact(context, ContactT(address: _addressController.text));
}

void _setPaymentURI(String uriOrAddress) {
try {
final paymentURI = decodeAddress(context, uriOrAddress);
Expand Down
1 change: 1 addition & 0 deletions lib/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,7 @@ abstract class _ContactStore with Store {
fetchContacts();
}

@action
markContactsSaved(int coin, bool v) {
settings.coins[coin].contactsSaved = v;
Future.microtask(() async {
Expand Down
9 changes: 8 additions & 1 deletion lib/transaction.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:YWallet/contact.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:warp_api/data_fb_generated.dart';

import 'main.dart';
import 'settings.dart';
Expand Down Expand Up @@ -43,7 +45,8 @@ class TransactionState extends State<TransactionPage> {
subtitle: Text('${tx.timestamp}')),
ListTile(title: Text(S.of(context).amount), subtitle: SelectableText(decimalFormat(tx.value, 8))),
ListTile(
title: Text(S.of(context).address), subtitle: SelectableText('${tx.address}')),
title: Text(S.of(context).address), subtitle: SelectableText('${tx.address}'),
trailing: IconButton(icon: Icon(Icons.contacts), onPressed: _addContact)),
ListTile(
title: Text(S.of(context).contactName), subtitle: SelectableText('${tx.contact ?? "N/A"}')),
ListTile(title: Text(S.of(context).memo), subtitle: SelectableText('${tx.memo}')),
Expand All @@ -70,4 +73,8 @@ class TransactionState extends State<TransactionPage> {
txIndex += 1;
});
}

_addContact() async {
await addContact(context, ContactT(address: tx.address));
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.3.4+388
version: 1.3.4+389

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down

0 comments on commit 6d4e20a

Please sign in to comment.