Chepuhagram/lib/logic/contact_provider.dart

58 lines
1.7 KiB
Dart

import 'package:flutter/material.dart';
import '/data/models/contact_model.dart';
import '/data/repositories/contact_repository.dart';
class ContactProvider extends ChangeNotifier {
final ContactRepository _repository = ContactRepository();
List<Contact> _contacts = [];
List<Contact> _allContacts = [];
bool _isLoading = false;
String? _error;
int? _currentUserId;
List<Contact> get contacts => _contacts;
List<Contact> get allContacts => _allContacts;
bool get isLoading => _isLoading;
String? get error => _error;
void setCurrentUserId(int? id) {
_currentUserId = id;
notifyListeners();
}
Future<void> loadContacts() async {
_isLoading = true;
_error = null;
notifyListeners();
try {
final allContacts = await _repository.fetchContacts();
// Фильтруем: исключаем себя (для основного списка - только чаты)
_contacts = allContacts.where((contact) => contact.id != _currentUserId).toList();
_allContacts = _contacts;
} catch (e) {
_error = e.toString();
} finally {
_isLoading = false;
notifyListeners();
}
}
// Метод для получения всех контактов (исключая себя) для нового чата
Future<void> loadAllContactsForNewChat() async {
_isLoading = true;
_error = null;
notifyListeners();
try {
final allContacts = await _repository.fetchContacts();
// Фильтруем только исключение самого себя
_allContacts = allContacts.where((contact) => contact.id != _currentUserId).toList();
} catch (e) {
_error = e.toString();
} finally {
_isLoading = false;
notifyListeners();
}
}
}