G-EIP-700-TLS-7-1-eip-steph.../dialer/lib/features/contacts/contact_state.dart

123 lines
3.2 KiB
Dart
Raw Normal View History

2024-10-26 20:53:30 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_contacts/flutter_contacts.dart';
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) {
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();
2024-12-15 02:25:44 +00:00
List<Contact> _allContacts = [];
List<Contact> _favoriteContacts = [];
2024-10-26 20:53:30 +00:00
bool _loading = true;
double _scrollOffset = 0.0;
Contact? _selfContact = Contact();
2024-10-26 20:53:30 +00:00
2024-12-15 02:25:44 +00:00
// Getters for all contacts and favorites
List<Contact> get contacts => _allContacts;
List<Contact> get favoriteContacts => _favoriteContacts;
2024-10-26 20:53:30 +00:00
bool get loading => _loading;
double get scrollOffset => _scrollOffset;
Contact? get selfContact => _selfContact;
2024-10-26 20:53:30 +00:00
@override
void initState() {
super.initState();
2024-12-15 02:25:44 +00:00
fetchContacts(); // Fetch all contacts by default
FlutterContacts.addListener(_onContactChange);
}
void _onContactChange() => fetchContacts();
@override
void dispose() {
FlutterContacts.removeListener(_onContactChange);
super.dispose();
2024-10-26 20:53:30 +00:00
}
2024-12-15 02:25:44 +00:00
// Fetch all contacts
Future<void> fetchContacts() async {
setState(() => _loading = true);
try {
List<Contact> contacts = await _contactService.fetchContacts();
_processContacts(contacts);
} finally {
setState(() => _loading = false);
}
}
2024-12-15 02:25:44 +00:00
// Fetch only favorite contacts
Future<void> fetchFavoriteContacts() async {
setState(() => _loading = true);
try {
List<Contact> contacts = await _contactService.fetchFavoriteContacts();
setState(() => _favoriteContacts = contacts);
} finally {
setState(() => _loading = false);
}
}
2024-12-15 02:25:44 +00:00
void _processContacts(List<Contact> contacts) {
_selfContact = contacts.firstWhere(
(contact) => contact.displayName.toLowerCase() == "user",
orElse: () => Contact(),
);
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-10-26 20:53:30 +00:00
setState(() {
2024-12-15 02:25:44 +00:00
_allContacts = contacts;
_favoriteContacts =
contacts.where((contact) => contact.isStarred).toList();
_selfContact = _selfContact;
2024-10-26 20:53:30 +00:00
});
}
Future<void> addNewContact(Contact contact) async {
await _contactService.addNewContact(contact);
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,
);
}
}
2024-12-15 02:25:44 +00:00
2024-10-26 20:53:30 +00:00
class _InheritedContactState extends InheritedWidget {
final _ContactStateState data;
const _InheritedContactState({required this.data, required super.child});
@override
bool updateShouldNotify(_InheritedContactState oldWidget) => true;
}