zwallet/lib/account_manager.dart

158 lines
5.2 KiB
Dart
Raw Normal View History

2021-06-26 07:30:12 -07:00
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:warp/main.dart';
import 'package:warp/store.dart';
2021-07-07 21:22:54 -07:00
import 'about.dart';
2021-06-26 07:30:12 -07:00
class AccountManagerPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => AccountManagerState();
}
class AccountManagerState extends State<AccountManagerPage> {
2021-07-19 01:17:23 -07:00
var _accountNameController = TextEditingController();
2021-06-26 07:30:12 -07:00
@override
initState() {
super.initState();
2021-07-07 21:22:54 -07:00
Future.microtask(accountManager.refresh);
showAboutOnce(this.context);
2021-06-26 07:30:12 -07:00
}
@override
Widget build(BuildContext context) {
return Scaffold(
2021-07-30 02:45:43 -07:00
appBar: AppBar(title: Text('Accounts'), actions: [
PopupMenuButton<String>(
itemBuilder: (context) => [
PopupMenuItem(child: Text('Settings'), value: "Settings"),
PopupMenuItem(child: Text("About"), value: "About"),
],
onSelected: _onMenu)
]),
2021-06-26 07:30:12 -07:00
body: Observer(
builder: (context) => Stack(children: [
accountManager.accounts.isEmpty
? Center(
child: Text("No account",
style: Theme.of(context).textTheme.headline5))
: ListView(
children: accountManager.accounts
.asMap()
.entries
.map((a) => Card(
child: Dismissible(
key: Key(a.value.name),
child: ListTile(
title: Text(a.value.name,
style: Theme.of(context)
.textTheme
.headline5),
trailing:
Text("${a.value.balance / ZECUNIT}"),
onTap: () {
_selectAccount(a.value);
},
2021-07-19 01:17:23 -07:00
onLongPress: () {
_editAccount(a.value);
},
2021-06-26 07:30:12 -07:00
),
confirmDismiss: _onAccountDelete,
onDismissed: (direction) =>
_onDismissed(a.key, a.value.id),
)))
.toList()),
])),
floatingActionButton: FloatingActionButton(
onPressed: _onRestore, child: Icon(Icons.add)));
}
Future<bool> _onAccountDelete(DismissDirection _direction) async {
2021-07-07 08:40:05 -07:00
if (accountManager.accounts.length == 1) return false;
2021-06-26 07:30:12 -07:00
final confirm = showDialog<bool>(
context: this.context,
barrierDismissible: false,
builder: (context) => AlertDialog(
title: Text('Seed'),
2021-07-30 02:45:43 -07:00
content: Text(
'Are you SURE you want to DELETE this account? You MUST have a BACKUP to recover it. This operation is NOT reversible.'),
2021-06-26 07:30:12 -07:00
actions: [
TextButton(
child: Text('Cancel'),
onPressed: () {
Navigator.of(this.context).pop(false);
},
),
TextButton(
2021-07-07 08:40:05 -07:00
child: Text('DELETE'),
2021-06-26 07:30:12 -07:00
onPressed: () {
Navigator.of(this.context).pop(true);
},
),
]),
);
return confirm;
}
void _onDismissed(int index, int account) async {
await accountManager.delete(account);
accountManager.refresh();
}
2021-07-09 06:33:39 -07:00
_selectAccount(Account account) async {
await accountManager.setActiveAccount(account);
2021-06-26 07:30:12 -07:00
final navigator = Navigator.of(context);
if (navigator.canPop())
navigator.pop();
else
navigator.pushReplacementNamed('/account');
}
2021-07-19 01:17:23 -07:00
_editAccount(Account account) async {
_accountNameController.text = account.name;
await showDialog(
2021-07-30 02:45:43 -07:00
context: context,
builder: (context) => AlertDialog(
title: Text('Change Account Name'),
content: TextField(controller: _accountNameController),
actions: [
ElevatedButton(
child: Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
}),
ElevatedButton(
child: Text('OK'),
onPressed: () {
_changeAccountName();
},
),
]));
2021-07-19 01:17:23 -07:00
}
_changeAccountName() {
accountManager.changeAccountName(_accountNameController.text);
Navigator.of(context).pop();
}
2021-06-26 07:30:12 -07:00
_onRestore() {
Navigator.of(context).pushNamed('/restore');
}
2021-07-30 02:45:43 -07:00
_onMenu(String choice) {
switch (choice) {
case "Settings":
_settings();
break;
case "About":
showAbout(this.context);
break;
}
}
_settings() {
Navigator.of(this.context).pushNamed('/settings');
}
2021-06-26 07:30:12 -07:00
}