gecko/lib/providers/wallets_profiles.dart

170 lines
4.8 KiB
Dart
Raw Normal View History

2021-12-20 21:33:03 +01:00
import 'dart:io';
2022-06-17 01:13:14 +02:00
import 'package:easy_localization/easy_localization.dart';
2021-01-23 16:38:03 +01:00
import 'package:flutter/material.dart';
2022-12-09 19:55:40 +01:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:gecko/globals.dart';
2022-08-14 20:02:02 +02:00
import 'package:gecko/models/g1_wallets_list.dart';
import 'package:gecko/screens/wallet_view.dart';
import 'package:jdenticon_dart/jdenticon_dart.dart';
2021-01-23 16:38:03 +01:00
import 'package:permission_handler/permission_handler.dart';
// import 'package:qrscan/qrscan.dart' as scanner;
import 'package:barcode_scan2/barcode_scan2.dart';
2021-01-23 16:38:03 +01:00
2022-12-09 19:55:40 +01:00
class WalletsProfilesProvider extends ChangeNotifier {
2022-05-30 15:32:00 +02:00
WalletsProfilesProvider(this.address);
2021-11-30 01:25:48 +01:00
String address = '';
2021-02-16 03:55:01 +01:00
String pubkeyShort = '';
bool isHistoryScreen = false;
String historySwitchButtun = "Voir l'historique";
2021-12-23 12:36:09 +01:00
String? rawSvg;
2021-04-03 00:07:03 +02:00
TextEditingController payAmount = TextEditingController();
TextEditingController payComment = TextEditingController();
num? _balance;
2022-05-30 14:48:12 +02:00
Future<String> scan(context) async {
2021-12-20 21:33:03 +01:00
if (Platform.isAndroid || Platform.isIOS) {
await Permission.camera.request();
}
ScanResult? barcode;
2021-01-23 16:38:03 +01:00
try {
barcode = await BarcodeScanner.scan();
} catch (e) {
2021-04-02 12:05:37 +02:00
log.e(e);
2021-01-23 16:38:03 +01:00
return 'false';
}
if (isAddress(barcode.rawContent)) {
2021-11-30 01:25:48 +01:00
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
2022-12-05 04:03:53 +01:00
return WalletViewScreen(
address: barcode!.rawContent,
username: '',
);
2021-11-30 01:25:48 +01:00
}),
);
2021-01-23 16:38:03 +01:00
} else {
return 'false';
}
return barcode.rawContent;
2021-01-23 16:38:03 +01:00
}
2022-05-19 13:44:22 +02:00
// Future<String> pay(BuildContext context, {int? derivation}) async {
// MyWalletsProvider _myWalletProvider =
// Provider.of<MyWalletsProvider>(context, listen: false);
// int? currentChest = configBox.get('currentChest');
// String result;
2021-12-03 10:25:36 +01:00
2022-05-19 13:44:22 +02:00
// derivation ??=
// _myWalletProvider.getDefaultWallet(currentChest)!.derivation!;
// result = await Gva(node: endPointGVA).pay(
// recipient: pubkey!,
// amount: double.parse(payAmount.text),
// mnemonic: _myWalletProvider.mnemonic,
// comment: payComment.text,
// derivation: derivation,
// lang: appLang);
2022-05-19 13:44:22 +02:00
// return result;
// }
2021-03-29 22:12:38 +02:00
2022-05-30 15:32:00 +02:00
bool isAddress(address) {
2021-11-14 19:21:20 +01:00
final RegExp regExp = RegExp(
2021-01-23 16:38:03 +01:00
r'^[a-zA-Z0-9]+$',
caseSensitive: false,
multiLine: false,
);
2022-05-30 15:32:00 +02:00
if (regExp.hasMatch(address) == true &&
2022-05-30 20:30:06 +02:00
address.length > 45 &&
address.length < 52) {
2022-05-30 15:32:00 +02:00
log.d("C'est une adresse !");
2021-01-23 16:38:03 +01:00
2022-05-30 15:32:00 +02:00
this.address = address;
2021-11-30 01:25:48 +01:00
return true;
} else {
return false;
2021-01-23 16:38:03 +01:00
}
}
// poka: Do99s6wQR2JLfhirPdpAERSjNbmjjECzGxHNJMiNKT3P
// Pi: D2meevcAHFTS2gQMvmRW5Hzi25jDdikk4nC4u1FkwRaU // For debug
// Boris: JE6mkuzSpT3ePciCPRTpuMT9fqPUVVLJz2618d33p7tn
// Matograine portefeuille: 9p5nHsES6xujFR7pw2yGy4PLKKHgWsMvsDHaHF64Uj25.
// Lion simone: 78jhpprYkMNF6i5kQPXfkAVBpd2aqcpieNsXTSW4c21f
2021-01-30 15:24:08 +01:00
void resetdHistory() {
notifyListeners();
}
String generateIdenticon(String pubkey) {
return Jdenticon.toSvg(pubkey);
}
2021-11-30 10:28:13 +01:00
2021-12-01 08:14:07 +01:00
// Future<num> getBalance(String _pubkey) async {
// final url = Uri.parse(
// '$endPointGVA?query={%20balance(script:%20%22$_pubkey%22)%20{%20amount%20base%20}%20}');
// final response = await http.get(url);
// final result = json.decode(response.body);
// if (result['data']['balance'] == null) {
// balance = 0.0;
// } else {
// balance = removeDecimalZero(result['data']['balance']['amount'] / 100);
// }
// return balance;
// }
Future<num?> getBalance(String? pubkey) async {
while (_balance == null) {
2021-12-01 08:14:07 +01:00
await Future.delayed(const Duration(milliseconds: 50));
2021-11-30 10:28:13 +01:00
}
return _balance;
2021-11-30 10:28:13 +01:00
}
2022-05-25 20:40:55 +02:00
2022-08-14 20:02:02 +02:00
bool isContact(String address) {
return contactsBox.containsKey(address);
}
2022-08-14 22:16:46 +02:00
Future addContact(G1WalletsList profile) async {
// log.d(profile.username);
if (isContact(profile.address)) {
await contactsBox.delete(profile.address);
2022-08-14 20:02:02 +02:00
} else {
await contactsBox.put(profile.address, profile);
2022-08-14 20:02:02 +02:00
}
notifyListeners();
}
2022-05-25 20:40:55 +02:00
void reload() {
notifyListeners();
}
2021-01-23 16:38:03 +01:00
}
2022-05-21 06:47:26 +02:00
snackCopyKey(context) {
2022-06-17 01:13:14 +02:00
final snackBar = SnackBar(
padding: const EdgeInsets.all(20),
content: Text("thisAddressHasBeenCopiedToClipboard".tr(),
style: const TextStyle(fontSize: 16)),
2022-06-17 20:18:54 +02:00
duration: const Duration(seconds: 2));
2022-05-21 06:47:26 +02:00
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
snackCopySeed(context) {
final snackBar = SnackBar(
padding: const EdgeInsets.all(20),
content: Text("thisMnemonicHasBeenCopiedToClipboard".tr(),
style: const TextStyle(fontSize: 17)),
duration: const Duration(seconds: 4));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
2022-12-09 19:55:40 +01:00
final walletsProfilesProvider =
ChangeNotifierProvider<WalletsProfilesProvider>((ref) {
return WalletsProfilesProvider('');
});