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:flutter_svg/svg.dart';
2021-06-26 07:30:12 -07:00
import 'package:warp/main.dart';
import 'package:warp/store.dart';
2021-08-15 09:18:09 -07:00
import 'generated/l10n.dart';
2021-06-26 07:30:12 -07:00
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-08-15 09:18:09 -07:00
appBar: AppBar(title: Text(S.of(context).accounts), actions: [
2021-07-30 02:45:43 -07:00
PopupMenuButton<String>(
itemBuilder: (context) => [
PopupMenuItem(
child: Text(S.of(context).settings), value: "Settings"),
PopupMenuItem(
child: Text(S.of(context).about), value: "About"),
2021-07-30 02:45:43 -07:00
],
onSelected: _onMenu)
]),
2021-06-26 07:30:12 -07:00
body: Observer(
builder: (context) => Stack(children: [
accountManager.accounts.isEmpty
? Center(child: NoAccount())
2021-06-26 07:30:12 -07:00
: 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>(
2021-08-13 20:44:53 -07:00
context: context,
2021-06-26 07:30:12 -07:00
barrierDismissible: false,
builder: (context) => AlertDialog(
2021-08-15 09:18:09 -07:00
title: Text(S.of(context).seed),
content: Text(S.of(context).confirmDeleteAccount),
2021-08-13 20:44:53 -07:00
actions: confirmButtons(context, () {
Navigator.of(context).pop(true);
2021-08-15 09:18:09 -07:00
}, okLabel: S.of(context).delete, cancelValue: false)),
2021-06-26 07:30:12 -07:00
);
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(
2021-08-15 09:18:09 -07:00
title: Text(S.of(context).changeAccountName),
2021-08-13 20:44:53 -07:00
content: TextField(controller: _accountNameController),
actions: confirmButtons(context, _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
}
class NoAccount extends StatelessWidget {
@override
Widget build(BuildContext context) {
final Widget wallet = SvgPicture.asset(
'assets/wallet.svg',
color: Theme.of(context).primaryColor,
semanticsLabel: 'Wallet'
);
return Column(mainAxisAlignment: MainAxisAlignment.center, children: [
SizedBox(child: wallet, height: 150, width: 150),
Padding(padding: EdgeInsets.symmetric(vertical: 16)),
Text(S.of(context).noAccount,
style: Theme.of(context).textTheme.headline5),
Padding(padding: EdgeInsets.symmetric(vertical: 8)),
Text(S.of(context).createANewAccount,
style: Theme.of(context).textTheme.bodyText1),
]);
}
}