187 lines
4.7 KiB
Dart
187 lines
4.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'dart:convert';
|
|
|
|
class IndexPage extends StatefulWidget {
|
|
IndexPage(
|
|
{Key? key,
|
|
required this.uuid,
|
|
required this.wallet,
|
|
required this.setWallet})
|
|
: super(key: key);
|
|
|
|
final String uuid;
|
|
String wallet;
|
|
final Function setWallet;
|
|
|
|
@override
|
|
_IndexPageState createState() => _IndexPageState();
|
|
}
|
|
|
|
class _IndexPageState extends State<IndexPage> {
|
|
late String wallet;
|
|
List<String> walletNames = [];
|
|
late double walletBalance;
|
|
bool balanceLoaded = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
wallet = widget.wallet;
|
|
fetchWalletNames();
|
|
fetchWalletBalance();
|
|
}
|
|
|
|
Future<void> fetchWalletNames() async {
|
|
final response = await http.get(
|
|
Uri.parse('https://api.firewallet.au/account?uuid=${widget.uuid}'));
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(response.body);
|
|
final wallets = data['wallets'] as List;
|
|
setState(() {
|
|
walletNames =
|
|
wallets.map((wallet) => wallet['name'] as String).toList();
|
|
});
|
|
} else {
|
|
// Handle error
|
|
print('Failed to load wallet names');
|
|
}
|
|
}
|
|
|
|
Future<void> addNewWallet(String name, String xpub) async {
|
|
final response = await http.post(
|
|
Uri.parse(
|
|
'https://api.firewallet.au/wallet?uuid=${widget.uuid}&name=$name&xpub=$xpub'),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
// Refresh the wallet list after adding a new wallet
|
|
fetchWalletNames();
|
|
} else {
|
|
// Handle error
|
|
print('Failed to add new wallet');
|
|
}
|
|
}
|
|
|
|
void showNewWalletDialog() {
|
|
String name = '';
|
|
String xpub = '';
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) {
|
|
return AlertDialog(
|
|
title: Text('Add New Wallet'),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: <Widget>[
|
|
TextField(
|
|
decoration: InputDecoration(labelText: 'Name'),
|
|
onChanged: (value) {
|
|
name = value;
|
|
},
|
|
),
|
|
TextField(
|
|
decoration: InputDecoration(labelText: 'XPUB'),
|
|
onChanged: (value) {
|
|
xpub = value;
|
|
},
|
|
),
|
|
],
|
|
),
|
|
actions: <Widget>[
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
addNewWallet(name, xpub);
|
|
},
|
|
child: Text('Add'),
|
|
),
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
},
|
|
child: Text('Cancel'),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
void setWallet(String s) {
|
|
setState(() {
|
|
wallet = s;
|
|
balanceLoaded = false;
|
|
});
|
|
widget.setWallet(s);
|
|
}
|
|
|
|
Future<void> fetchWalletBalance() async {
|
|
if (wallet.isEmpty) {
|
|
walletBalance = 0;
|
|
return;
|
|
}
|
|
|
|
if (balanceLoaded) {
|
|
return;
|
|
}
|
|
|
|
final response = await http.get(Uri.parse(
|
|
'https://api.firewallet.au/wallet/balance?uuid=${widget.uuid}&name=$wallet'));
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(response.body);
|
|
print(data);
|
|
|
|
setState(() {
|
|
balanceLoaded = true;
|
|
walletBalance = data['available'];
|
|
});
|
|
} else {
|
|
// Handle error
|
|
print('Failed to load wallet balance');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: FutureBuilder<void>(
|
|
future: fetchWalletBalance(),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
return Text('$wallet - Loading...');
|
|
} else if (snapshot.hasError) {
|
|
return Text('$wallet - Error');
|
|
} else {
|
|
return Text('$wallet - ${walletBalance.toStringAsFixed(2)} HNS');
|
|
}
|
|
},
|
|
),
|
|
),
|
|
body: Center(
|
|
child: LayoutBuilder(
|
|
builder: (BuildContext context, BoxConstraints constraints) {
|
|
return Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
...walletNames.map((name) {
|
|
return TextButton(
|
|
onPressed: () => setWallet(name),
|
|
child: Text(name),
|
|
);
|
|
}).toList(),
|
|
SizedBox(height: 20),
|
|
ElevatedButton(
|
|
onPressed: showNewWalletDialog,
|
|
child: Text('New Wallet'),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|