2024-10-26 20:53:30 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_contacts/flutter_contacts.dart';
|
2024-12-12 16:47:49 +00:00
|
|
|
import '../../widgets/contact_service.dart';
|
2024-10-26 20:53:30 +00:00
|
|
|
|
|
|
|
class ContactState extends StatefulWidget {
|
|
|
|
final Widget child;
|
|
|
|
|
|
|
|
const ContactState({super.key, required this.child});
|
|
|
|
|
|
|
|
static _ContactStateState of(BuildContext context) {
|
2024-12-13 09:00:20 +00:00
|
|
|
return context
|
|
|
|
.dependOnInheritedWidgetOfExactType<_InheritedContactState>()!
|
|
|
|
.data;
|
2024-10-26 20:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
_ContactStateState createState() => _ContactStateState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ContactStateState extends State<ContactState> {
|
|
|
|
final ContactService _contactService = ContactService();
|
|
|
|
List<Contact> _contacts = [];
|
|
|
|
bool _loading = true;
|
|
|
|
double _scrollOffset = 0.0;
|
2024-11-29 08:27:29 +00:00
|
|
|
Contact? _selfContact = Contact();
|
2024-10-26 20:53:30 +00:00
|
|
|
|
|
|
|
List<Contact> get contacts => _contacts;
|
|
|
|
bool get loading => _loading;
|
|
|
|
double get scrollOffset => _scrollOffset;
|
2024-11-29 08:27:29 +00:00
|
|
|
Contact? get selfContact => _selfContact;
|
2024-10-26 20:53:30 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-12-13 09:00:20 +00:00
|
|
|
fetchContacts();
|
2024-11-29 08:27:29 +00:00
|
|
|
|
|
|
|
// Add listener for contact changes
|
|
|
|
FlutterContacts.addListener(_onContactChange);
|
|
|
|
}
|
|
|
|
|
2024-12-13 09:00:20 +00:00
|
|
|
void _onContactChange() => fetchContacts();
|
2024-11-29 08:27:29 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
// Remove listener
|
|
|
|
FlutterContacts.removeListener(_onContactChange);
|
|
|
|
super.dispose();
|
2024-10-26 20:53:30 +00:00
|
|
|
}
|
|
|
|
|
2024-12-13 09:00:20 +00:00
|
|
|
Future<void> fetchContacts({bool onlyStarred = false}) async {
|
|
|
|
List<Contact> contacts = onlyStarred
|
|
|
|
? await _contactService.fetchFavoriteContacts()
|
|
|
|
: await _contactService.fetchContacts();
|
2024-11-29 08:27:29 +00:00
|
|
|
|
2024-12-13 09:00:20 +00:00
|
|
|
debugPrint(
|
|
|
|
"Fetched ${contacts.length} ${onlyStarred ? 'favorite' : ''} contacts");
|
2024-11-29 08:27:29 +00:00
|
|
|
|
|
|
|
// Find selfContact before filtering
|
|
|
|
_selfContact = contacts.firstWhere(
|
|
|
|
(contact) => contact.displayName.toLowerCase() == "user",
|
|
|
|
orElse: () => Contact(),
|
|
|
|
);
|
2024-12-13 09:00:20 +00:00
|
|
|
|
2024-11-29 08:27:29 +00:00
|
|
|
if (_selfContact!.phones.isEmpty) {
|
|
|
|
debugPrint("Self contact has no phone numbers");
|
|
|
|
_selfContact = null;
|
|
|
|
}
|
|
|
|
|
2024-10-26 20:53:30 +00:00
|
|
|
contacts = contacts.where((contact) => contact.phones.isNotEmpty).toList();
|
|
|
|
contacts.sort((a, b) => a.displayName.compareTo(b.displayName));
|
2024-12-13 09:00:20 +00:00
|
|
|
|
2024-10-26 20:53:30 +00:00
|
|
|
setState(() {
|
|
|
|
_contacts = contacts;
|
|
|
|
_loading = false;
|
2024-11-29 08:27:29 +00:00
|
|
|
_selfContact = _selfContact;
|
2024-10-26 20:53:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> addNewContact(Contact contact) async {
|
|
|
|
await _contactService.addNewContact(contact);
|
2024-12-13 09:00:20 +00:00
|
|
|
await fetchContacts();
|
2024-10-26 20:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|