WIP: toogle favorite fix
set favorites -> works unset favorites -> unstable
This commit is contained in:
parent
7ff9418e06
commit
110020bb4b
@ -116,6 +116,38 @@ class _ContactStateState extends State<ContactState> {
|
|||||||
await fetchContacts();
|
await fetchContacts();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add this new method to update a single contact in state without reloading all contacts
|
||||||
|
Future<void> updateContactInState(Contact updatedContact) async {
|
||||||
|
setState(() {
|
||||||
|
// Find and update in the all contacts list
|
||||||
|
final allIndex = _allContacts.indexWhere((c) => c.id == updatedContact.id);
|
||||||
|
if (allIndex != -1) {
|
||||||
|
_allContacts[allIndex] = updatedContact;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the favorites list based on the star status
|
||||||
|
if (updatedContact.isStarred) {
|
||||||
|
// Add to favorites if not already there
|
||||||
|
if (!_favoriteContacts.any((c) => c.id == updatedContact.id)) {
|
||||||
|
_favoriteContacts.add(updatedContact);
|
||||||
|
} else {
|
||||||
|
// If already in favorites, update it
|
||||||
|
final favIndex = _favoriteContacts.indexWhere((c) => c.id == updatedContact.id);
|
||||||
|
if (favIndex != -1) {
|
||||||
|
_favoriteContacts[favIndex] = updatedContact;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Remove from favorites if it's there
|
||||||
|
_favoriteContacts.removeWhere((c) => c.id == updatedContact.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Re-sort both lists to maintain alphabetical order
|
||||||
|
_allContacts.sort((a, b) => a.displayName.compareTo(b.displayName));
|
||||||
|
_favoriteContacts.sort((a, b) => a.displayName.compareTo(b.displayName));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void setScrollOffset(double offset) {
|
void setScrollOffset(double offset) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_scrollOffset = offset;
|
_scrollOffset = offset;
|
||||||
|
@ -52,20 +52,42 @@ class _AlphabetScrollPageState extends State<AlphabetScrollPage> {
|
|||||||
|
|
||||||
void _toggleFavorite(Contact contact) async {
|
void _toggleFavorite(Contact contact) async {
|
||||||
try {
|
try {
|
||||||
if (await FlutterContacts.requestPermission()) {
|
// Check permission only once
|
||||||
Contact? fullContact = await FlutterContacts.getContact(contact.id,
|
if (!await FlutterContacts.requestPermission()) {
|
||||||
withProperties: true,
|
print("Could not get contact permission");
|
||||||
withAccounts: true,
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
withPhoto: true,
|
SnackBar(content: Text('Contact permission not granted')),
|
||||||
withThumbnail: true);
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Contact? fullContact = await FlutterContacts.getContact(contact.id,
|
||||||
|
withProperties: true,
|
||||||
|
withAccounts: true,
|
||||||
|
withPhoto: true,
|
||||||
|
withThumbnail: true);
|
||||||
|
|
||||||
if (fullContact != null) {
|
if (fullContact != null) {
|
||||||
fullContact.isStarred = !fullContact.isStarred;
|
// Toggle the favorite status
|
||||||
await FlutterContacts.updateContact(fullContact);
|
fullContact.isStarred = !fullContact.isStarred;
|
||||||
}
|
// Update in database
|
||||||
await _refreshContacts();
|
await FlutterContacts.updateContact(fullContact);
|
||||||
} else {
|
|
||||||
print("Could not fetch contact details");
|
// Update the UI immediately - we need to update the ContactState
|
||||||
|
final contactState = ContactState.of(context);
|
||||||
|
await contactState.updateContactInState(fullContact);
|
||||||
|
|
||||||
|
// Show feedback to the user
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(
|
||||||
|
fullContact.isStarred
|
||||||
|
? '${fullContact.displayName} added to favorites'
|
||||||
|
: '${fullContact.displayName} removed from favorites'
|
||||||
|
),
|
||||||
|
duration: Duration(seconds: 1),
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print("Error updating favorite status: $e");
|
print("Error updating favorite status: $e");
|
||||||
|
@ -25,7 +25,7 @@ class _MyHomePageState extends State<MyHomePage>
|
|||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
// Set the TabController length to 4
|
// Set the TabController length to 4
|
||||||
_tabController = TabController(length: 4, vsync: this, initialIndex: 1);
|
_tabController = TabController(length: 4, vsync: this, initialIndex: 2);
|
||||||
_tabController.addListener(_handleTabIndex);
|
_tabController.addListener(_handleTabIndex);
|
||||||
_fetchContacts();
|
_fetchContacts();
|
||||||
}
|
}
|
||||||
@ -68,23 +68,42 @@ class _MyHomePageState extends State<MyHomePage>
|
|||||||
|
|
||||||
void _toggleFavorite(Contact contact) async {
|
void _toggleFavorite(Contact contact) async {
|
||||||
try {
|
try {
|
||||||
if (await FlutterContacts.requestPermission()) {
|
// Check permission only once at the beginning
|
||||||
Contact? fullContact = await FlutterContacts.getContact(contact.id,
|
if (!await FlutterContacts.requestPermission()) {
|
||||||
withProperties: true,
|
print("Could not get contact permission");
|
||||||
withAccounts: true,
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
withPhoto: true,
|
const SnackBar(content: Text('Contact permission not granted')),
|
||||||
withThumbnail: true);
|
);
|
||||||
|
return;
|
||||||
if (fullContact != null) {
|
}
|
||||||
fullContact.isStarred = !fullContact.isStarred;
|
|
||||||
await FlutterContacts.updateContact(fullContact);
|
// Get the full contact with all properties
|
||||||
setState(() {
|
Contact? fullContact = await FlutterContacts.getContact(contact.id,
|
||||||
// Updating the contact list after toggling the favorite
|
withProperties: true,
|
||||||
_fetchContacts();
|
withAccounts: true,
|
||||||
});
|
withPhoto: true,
|
||||||
}
|
withThumbnail: true);
|
||||||
} else {
|
|
||||||
print("Could not fetch contact details");
|
if (fullContact != null) {
|
||||||
|
// Toggle the starred status
|
||||||
|
fullContact.isStarred = !fullContact.isStarred;
|
||||||
|
// Update in the database
|
||||||
|
await FlutterContacts.updateContact(fullContact);
|
||||||
|
|
||||||
|
// Update UI immediately with the new state
|
||||||
|
setState(() {
|
||||||
|
// Find and update the contact in our local list
|
||||||
|
final index = _allContacts.indexWhere((c) => c.id == contact.id);
|
||||||
|
if (index != -1) {
|
||||||
|
_allContacts[index] = fullContact;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update contact suggestions if needed
|
||||||
|
final suggestionIndex = _contactSuggestions.indexWhere((c) => c.id == contact.id);
|
||||||
|
if (suggestionIndex != -1) {
|
||||||
|
_contactSuggestions[suggestionIndex] = fullContact;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print("Error updating favorite status: $e");
|
print("Error updating favorite status: $e");
|
||||||
|
Loading…
Reference in New Issue
Block a user