344 lines
9.5 KiB
Dart
344 lines
9.5 KiB
Dart
// ignore_for_file: avoid_print
|
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
// import 'package:hive_flutter/hive_flutter.dart';
|
|
import 'package:qr_flutter/qr_flutter.dart';
|
|
import 'package:truncate/truncate.dart';
|
|
import 'package:firebase_core/firebase_core.dart';
|
|
import 'firebase_options.dart';
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
|
|
// late Box addressBox;
|
|
late FirebaseFirestore db;
|
|
bool canAdd = false;
|
|
List profiles = [];
|
|
|
|
Future<void> main() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
// await Hive.initFlutter();
|
|
await Firebase.initializeApp(
|
|
options: DefaultFirebaseOptions.currentPlatform,
|
|
);
|
|
db = FirebaseFirestore.instance;
|
|
// await eraseDB();
|
|
await FirebaseAuth.instance.signInAnonymously();
|
|
FirebaseAuth.instance.authStateChanges().listen((User? user) {
|
|
if (user == null) {
|
|
print('User is currently signed out!');
|
|
} else {
|
|
print('User is signed in!');
|
|
}
|
|
});
|
|
|
|
// addressBox = await Hive.openBox("addressBox");
|
|
// await addressBox.clear();
|
|
|
|
runApp(const MyApp());
|
|
}
|
|
|
|
class MyApp extends StatelessWidget {
|
|
const MyApp({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: 'ĞDev annuaire',
|
|
theme: ThemeData(
|
|
primarySwatch: Colors.blue,
|
|
),
|
|
home: const MyHomePage(title: 'ĞDev annuaire'),
|
|
);
|
|
}
|
|
}
|
|
|
|
class MyHomePage extends StatefulWidget {
|
|
const MyHomePage({Key? key, required this.title}) : super(key: key);
|
|
|
|
final String title;
|
|
|
|
@override
|
|
State<MyHomePage> createState() => _MyHomePageState();
|
|
}
|
|
|
|
class _MyHomePageState extends State<MyHomePage> {
|
|
refresh() {
|
|
setState(() {});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// profiles.clear();
|
|
// addressBox.toMap().forEach((key, value) {
|
|
// profiles.add(value);
|
|
// });
|
|
|
|
return Scaffold(
|
|
backgroundColor: Colors.orange[200],
|
|
body: profileTiles(context, refresh),
|
|
);
|
|
}
|
|
}
|
|
|
|
Widget profileTiles(BuildContext context, refresh) {
|
|
final double screenWidth = MediaQuery.of(context).size.width;
|
|
int nbrColumn = 9;
|
|
TextEditingController nameController = TextEditingController();
|
|
TextEditingController addressController = TextEditingController();
|
|
|
|
if (screenWidth <= 600) {
|
|
nbrColumn = 1;
|
|
} else if (screenWidth <= 900) {
|
|
nbrColumn = 2;
|
|
} else if (screenWidth <= 1200) {
|
|
nbrColumn = 3;
|
|
} else if (screenWidth <= 1500) {
|
|
nbrColumn = 4;
|
|
} else if (screenWidth <= 1800) {
|
|
nbrColumn = 5;
|
|
} else if (screenWidth <= 2100) {
|
|
nbrColumn = 6;
|
|
} else if (screenWidth <= 2400) {
|
|
nbrColumn = 7;
|
|
} else if (screenWidth <= 2700) {
|
|
nbrColumn = 8;
|
|
}
|
|
|
|
return CustomScrollView(slivers: <Widget>[
|
|
const SliverToBoxAdapter(child: SizedBox(height: 20)),
|
|
SliverToBoxAdapter(
|
|
child: Row(
|
|
children: [
|
|
const SizedBox(width: 20),
|
|
SizedBox(
|
|
width: 150,
|
|
child: TextField(
|
|
controller: nameController,
|
|
onChanged: (a) {
|
|
canAddProfile(nameController.text, addressController.text);
|
|
// refresh();
|
|
},
|
|
decoration: const InputDecoration(
|
|
hintText: "Nom",
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 20),
|
|
SizedBox(
|
|
width: 470,
|
|
child: TextField(
|
|
controller: addressController,
|
|
onChanged: (a) {
|
|
canAddProfile(nameController.text, addressController.text);
|
|
// refresh();
|
|
},
|
|
decoration: const InputDecoration(
|
|
hintText: "Adresse",
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 20),
|
|
IconButton(
|
|
icon: const Icon(Icons.add_reaction_outlined),
|
|
onPressed: (() async {
|
|
if (canAdd) {
|
|
await addProfile(nameController.text, addressController.text);
|
|
refresh();
|
|
canAdd = false;
|
|
}
|
|
}),
|
|
),
|
|
const Spacer(),
|
|
const Text(
|
|
'Annuaire ĞDev',
|
|
style: TextStyle(fontSize: 20),
|
|
),
|
|
const SizedBox(width: 80),
|
|
],
|
|
)),
|
|
const SliverToBoxAdapter(child: SizedBox(height: 30)),
|
|
FutureBuilder(
|
|
future: getDbData(),
|
|
builder: (context, AsyncSnapshot<List>? snapshot) {
|
|
List result = snapshot?.data ?? [];
|
|
result.sort();
|
|
return SliverGrid.count(
|
|
key: const Key('listWallets'),
|
|
crossAxisCount: nbrColumn,
|
|
childAspectRatio: 1,
|
|
crossAxisSpacing: 0,
|
|
mainAxisSpacing: 0,
|
|
children: <Widget>[
|
|
for (String profile in result)
|
|
Container(
|
|
color: Colors.orange[200],
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(10),
|
|
child: Container(
|
|
color: Colors.orange[100],
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(10),
|
|
child: tile(context, profile.split(':')[0],
|
|
profile.split(':')[1], refresh),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
]);
|
|
},
|
|
),
|
|
]);
|
|
}
|
|
|
|
Widget tile(BuildContext context, String name, String address, refresh) {
|
|
int lastTap = DateTime.now().millisecondsSinceEpoch;
|
|
int consecutiveTaps = 0;
|
|
return Container(
|
|
color: Colors.orange[50],
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
GestureDetector(
|
|
onTap: () {
|
|
int now = DateTime.now().millisecondsSinceEpoch;
|
|
if (now - lastTap < 400) {
|
|
consecutiveTaps++;
|
|
if (consecutiveTaps > 8) {
|
|
print("DELETE PROFILE");
|
|
deleteAddress(address, refresh);
|
|
}
|
|
} else {
|
|
consecutiveTaps = 0;
|
|
}
|
|
lastTap = now;
|
|
},
|
|
child: Text(
|
|
name,
|
|
style:
|
|
const TextStyle(fontWeight: FontWeight.w600, fontSize: 17),
|
|
),
|
|
),
|
|
const SizedBox(height: 3),
|
|
InkWell(
|
|
onTap: () => snackCopyKey(context, address),
|
|
child: Text(
|
|
getShortPubkey(address),
|
|
style:
|
|
const TextStyle(fontWeight: FontWeight.w500, fontSize: 17),
|
|
),
|
|
),
|
|
QrImageWidget(
|
|
// gapless: false,
|
|
data: address,
|
|
version: QrVersions.auto,
|
|
size: 200,
|
|
),
|
|
],
|
|
)),
|
|
);
|
|
}
|
|
|
|
String getShortPubkey(String pubkey) {
|
|
String pubkeyShort = truncate(pubkey, 7,
|
|
omission: String.fromCharCode(0x2026),
|
|
position: TruncatePosition.end) +
|
|
truncate(pubkey, 6, omission: "", position: TruncatePosition.start);
|
|
return pubkeyShort;
|
|
}
|
|
|
|
snackCopyKey(BuildContext context, String address) {
|
|
// _copyToClipboardHack(address);
|
|
Clipboard.setData(ClipboardData(text: address));
|
|
|
|
const snackBar = SnackBar(
|
|
padding: EdgeInsets.all(20),
|
|
content: Text("Cette adresse a été copié dans votre presse-papier.",
|
|
style: TextStyle(fontSize: 16)),
|
|
duration: Duration(seconds: 2));
|
|
ScaffoldMessenger.of(context).showSnackBar(snackBar);
|
|
}
|
|
|
|
Future addProfile(String name, String address) async {
|
|
final newProfile = <String, dynamic>{"name": name, "address": address};
|
|
|
|
db.collection("profiles").add(newProfile).then((DocumentReference doc) =>
|
|
print('DocumentSnapshot added with ID: ${doc.id}'));
|
|
}
|
|
|
|
Future<List> getDbData() async {
|
|
await db.collection("profiles").get().then((event) {
|
|
profiles.clear();
|
|
for (var doc in event.docs) {
|
|
final name = doc.data()['name'];
|
|
final address = doc.data()['address'];
|
|
profiles.add('$name:$address');
|
|
print("${doc.id} => ${doc.data()}");
|
|
}
|
|
});
|
|
|
|
return profiles;
|
|
}
|
|
|
|
Future eraseDB() async {
|
|
var collection = FirebaseFirestore.instance.collection('profiles');
|
|
var snapshots = await collection.get();
|
|
for (var doc in snapshots.docs) {
|
|
await doc.reference.delete();
|
|
}
|
|
}
|
|
|
|
Future deleteAddress(String address, refresh) async {
|
|
await db.collection("profiles").get().then((event) async {
|
|
profiles.clear();
|
|
for (var doc in event.docs) {
|
|
if (address == doc.data()['address']) {
|
|
await FirebaseFirestore.instance
|
|
.runTransaction((Transaction myTransaction) async {
|
|
myTransaction.delete(doc.reference);
|
|
});
|
|
refresh();
|
|
return;
|
|
}
|
|
}
|
|
// await doc.reference.delete();
|
|
});
|
|
}
|
|
|
|
bool isAddress(address) {
|
|
final RegExp regExp = RegExp(
|
|
r'^[a-zA-Z0-9]+$',
|
|
caseSensitive: false,
|
|
multiLine: false,
|
|
);
|
|
|
|
if (regExp.hasMatch(address) == true &&
|
|
address.length > 45 &&
|
|
address.length < 52) {
|
|
// canAdd = true;
|
|
|
|
return true;
|
|
} else {
|
|
// canAdd = false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool canAddProfile(String name, String address) {
|
|
if (name == '' || !isAddress(address)) {
|
|
return false;
|
|
}
|
|
|
|
for (var profile in profiles) {
|
|
final nameStored = profile.split(':')[0];
|
|
final addressStored = profile.split(':')[1];
|
|
if (address == addressStored || name == nameStored) return false;
|
|
}
|
|
|
|
canAdd = true;
|
|
return true;
|
|
}
|