gecko/lib/providers/wallets_profiles.dart

255 lines
8.0 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';
import 'package:flutter/services.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/models/widgets_keys.dart';
import 'package:gecko/providers/cesium_plus.dart';
import 'package:gecko/providers/substrate_sdk.dart';
import 'package:gecko/providers/wallet_options.dart';
import 'package:gecko/screens/common_elements.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';
import 'package:provider/provider.dart';
2021-01-23 16:38:03 +01:00
2021-11-30 01:25:48 +01:00
class WalletsProfilesProvider with 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) {
return WalletViewScreen(address: barcode!.rawContent);
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-06-17 20:18:54 +02:00
Widget headerProfileView(
BuildContext context, String address, String? username) {
const double avatarSize = 140;
WalletOptionsProvider walletOptions =
Provider.of<WalletOptionsProvider>(context, listen: false);
CesiumPlusProvider cesiumPlusProvider =
Provider.of<CesiumPlusProvider>(context, listen: false);
// SubstrateSdk _sub = Provider.of<SubstrateSdk>(context, listen: false);
return Stack(children: <Widget>[
Consumer<SubstrateSdk>(builder: (context, sub, _) {
bool isAccountExist = balanceCache[address] != 0;
return Container(
height: 180,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
isAccountExist ? yellowC : Colors.grey[400]!,
isAccountExist ? const Color(0xFFE7811A) : Colors.grey[600]!,
],
),
));
}),
Padding(
padding: const EdgeInsets.only(left: 30, right: 40),
child: Row(children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
height: 10,
color: yellowC, // Colors.grey[400],
),
Row(children: [
GestureDetector(
key: keyCopyAddress,
onTap: () {
Clipboard.setData(ClipboardData(text: address));
snackCopyKey(context);
},
child: Text(
getShortPubkey(address),
style: const TextStyle(
fontSize: 30,
fontWeight: FontWeight.w800,
),
),
),
]),
const SizedBox(height: 25),
balance(context, address, 22),
const SizedBox(height: 10),
walletOptions.idtyStatus(context, address,
isOwner: false, color: Colors.black),
getCerts(context, address, 14),
2022-08-14 22:16:46 +02:00
// if (username == null &&
// g1WalletsBox.get(address)?.username != null)
// SizedBox(
// width: 230,
// child: Text(
// g1WalletsBox.get(address)?.username ?? '',
// style: const TextStyle(
// fontSize: 27,
// color: Color(0xff814C00),
// ),
// ),
// ),
// if (username != null)
// SizedBox(
// width: 230,
// child: Text(
// username,
// style: const TextStyle(
// fontSize: 27,
// color: Color(0xff814C00),
// ),
// ),
// ),
const SizedBox(height: 55),
]),
const Spacer(),
Column(children: <Widget>[
2022-06-17 20:18:54 +02:00
ClipOval(
child: cesiumPlusProvider.defaultAvatar(avatarSize),
2022-06-17 20:18:54 +02:00
),
const SizedBox(height: 25),
]),
]),
),
CommonElements().offlineInfo(context),
]);
}
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);
}