Florian Griffon
fca1eea1c9
All checks were successful
/ mirror (push) Successful in 4s
Feat: contact-modal et refonte du contact-state pour la page de favori et possibilité de update les contacts. Aussi mis la composition page avec le service de contact, on évite de fetch dans la page directement Reviewed-on: #10 Co-authored-by: Florian Griffon <florian.griffon@epitech.eu> Co-committed-by: Florian Griffon <florian.griffon@epitech.eu>
107 lines
2.7 KiB
Dart
107 lines
2.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_contacts/flutter_contacts.dart';
|
|
import '../../widgets/contact_service.dart';
|
|
|
|
class ContactState extends StatefulWidget {
|
|
final Widget child;
|
|
|
|
const ContactState({super.key, required this.child});
|
|
|
|
static _ContactStateState of(BuildContext context) {
|
|
return context
|
|
.dependOnInheritedWidgetOfExactType<_InheritedContactState>()!
|
|
.data;
|
|
}
|
|
|
|
@override
|
|
_ContactStateState createState() => _ContactStateState();
|
|
}
|
|
|
|
class _ContactStateState extends State<ContactState> {
|
|
final ContactService _contactService = ContactService();
|
|
List<Contact> _contacts = [];
|
|
bool _loading = true;
|
|
double _scrollOffset = 0.0;
|
|
Contact? _selfContact = Contact();
|
|
|
|
List<Contact> get contacts => _contacts;
|
|
bool get loading => _loading;
|
|
double get scrollOffset => _scrollOffset;
|
|
Contact? get selfContact => _selfContact;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
fetchContacts();
|
|
|
|
// Add listener for contact changes
|
|
FlutterContacts.addListener(_onContactChange);
|
|
}
|
|
|
|
void _onContactChange() => fetchContacts();
|
|
|
|
@override
|
|
void dispose() {
|
|
// Remove listener
|
|
FlutterContacts.removeListener(_onContactChange);
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> fetchContacts({bool onlyStarred = false}) async {
|
|
List<Contact> contacts = onlyStarred
|
|
? await _contactService.fetchFavoriteContacts()
|
|
: await _contactService.fetchContacts();
|
|
|
|
debugPrint(
|
|
"Fetched ${contacts.length} ${onlyStarred ? 'favorite' : ''} contacts");
|
|
|
|
// Find selfContact before filtering
|
|
_selfContact = contacts.firstWhere(
|
|
(contact) => contact.displayName.toLowerCase() == "user",
|
|
orElse: () => Contact(),
|
|
);
|
|
|
|
if (_selfContact!.phones.isEmpty) {
|
|
debugPrint("Self contact has no phone numbers");
|
|
_selfContact = null;
|
|
}
|
|
|
|
contacts = contacts.where((contact) => contact.phones.isNotEmpty).toList();
|
|
contacts.sort((a, b) => a.displayName.compareTo(b.displayName));
|
|
|
|
setState(() {
|
|
_contacts = contacts;
|
|
_loading = false;
|
|
_selfContact = _selfContact;
|
|
});
|
|
}
|
|
|
|
Future<void> addNewContact(Contact contact) async {
|
|
await _contactService.addNewContact(contact);
|
|
await fetchContacts();
|
|
}
|
|
|
|
void setScrollOffset(double offset) {
|
|
setState(() {
|
|
_scrollOffset = offset;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return _InheritedContactState(
|
|
data: this,
|
|
child: widget.child,
|
|
);
|
|
}
|
|
}
|
|
|
|
class _InheritedContactState extends InheritedWidget {
|
|
final _ContactStateState data;
|
|
|
|
const _InheritedContactState({required this.data, required super.child});
|
|
|
|
@override
|
|
bool updateShouldNotify(_InheritedContactState oldWidget) => true;
|
|
}
|