156 lines
4.4 KiB
Dart
156 lines
4.4 KiB
Dart
import 'dart:convert';
|
||
import 'package:http/http.dart' as http;
|
||
import '/core/constants.dart';
|
||
import '/data/models/contact_model.dart';
|
||
import '/domain/services/api_service.dart';
|
||
|
||
class ContactRepository {
|
||
final ApiService _apiService = ApiService();
|
||
|
||
Future<List<Contact>> fetchChatContacts({bool forceRefresh = false}) async {
|
||
final token = await _apiService.getAccessToken();
|
||
|
||
DateTime now = DateTime.now();
|
||
Duration offset = now.timeZoneOffset;
|
||
|
||
final Map<String, String> requestHeaders = {
|
||
'Authorization': 'Bearer $token',
|
||
'Content-Type': 'application/json',
|
||
};
|
||
|
||
if (forceRefresh) {
|
||
requestHeaders['Cache-Control'] = 'no-cache';
|
||
}
|
||
|
||
//await _ensureCacheReady();
|
||
|
||
try {
|
||
final response = await http.get(
|
||
Uri.parse('${AppConstants.baseUrl}/users/chats'),
|
||
headers: requestHeaders,
|
||
);
|
||
|
||
if (response.statusCode == 200) {
|
||
final List<dynamic> data = jsonDecode(utf8.decode(response.bodyBytes));
|
||
print(data);
|
||
return data.map((json) {
|
||
final contact = Contact.fromJson(json);
|
||
print(contact.lastMessageType);
|
||
if (contact.lastMessageTime != null) {
|
||
return contact.copyWith(
|
||
lastMessageTime: contact.lastMessageTime!.add(offset),
|
||
);
|
||
}
|
||
return contact;
|
||
}).toList();
|
||
} else {
|
||
throw Exception('Failed to load contacts');
|
||
}
|
||
} catch (e) {
|
||
print('⚠️ Ошибка сети при загрузке контактов: $e.');
|
||
throw Exception('Нет доступа к сети. Проверте подключение к интернету.');
|
||
}
|
||
}
|
||
|
||
Future<List<Contact>> fetchAllUsers({bool forceRefresh = false}) async {
|
||
final token = await _apiService.getAccessToken();
|
||
|
||
DateTime now = DateTime.now();
|
||
Duration offset = now.timeZoneOffset;
|
||
|
||
if (token == null) {
|
||
throw Exception('No access token');
|
||
}
|
||
|
||
final Map<String, String> requestHeaders = {
|
||
'Authorization': 'Bearer $token',
|
||
'Content-Type': 'application/json',
|
||
};
|
||
|
||
if (forceRefresh) {
|
||
requestHeaders['Cache-Control'] = 'no-cache';
|
||
}
|
||
final response = await http.get(
|
||
Uri.parse('${AppConstants.baseUrl}/users/all'),
|
||
headers: requestHeaders,
|
||
);
|
||
|
||
if (response.statusCode == 200) {
|
||
final List<dynamic> data = jsonDecode(utf8.decode(response.bodyBytes));
|
||
return data.map((json) {
|
||
final contact = Contact.fromJson(json);
|
||
if (contact.lastMessageTime != null) {
|
||
return contact.copyWith(
|
||
lastMessageTime: contact.lastMessageTime!.add(offset),
|
||
);
|
||
}
|
||
return contact;
|
||
}).toList();
|
||
} else {
|
||
throw Exception('Failed to load contacts');
|
||
}
|
||
}
|
||
|
||
Future<Contact> fetchContactById(
|
||
int userId, {
|
||
bool forceRefresh = false,
|
||
}) async {
|
||
final token = await _apiService.getAccessToken();
|
||
|
||
final Map<String, String> requestHeaders = {
|
||
'Authorization': 'Bearer $token',
|
||
'Content-Type': 'application/json',
|
||
};
|
||
|
||
if (forceRefresh) {
|
||
requestHeaders['Cache-Control'] = 'no-cache';
|
||
}
|
||
|
||
final response = await http.get(
|
||
Uri.parse('${AppConstants.baseUrl}/users/$userId'),
|
||
headers: requestHeaders,
|
||
);
|
||
|
||
if (response.statusCode == 200) {
|
||
final data = jsonDecode(utf8.decode(response.bodyBytes));
|
||
return Contact.fromJson(data);
|
||
} else {
|
||
throw Exception('Не удалось загрузить данные контакта');
|
||
}
|
||
}
|
||
|
||
Future<List<Map<String, dynamic>>> getLastMessagesForContact(
|
||
int contactId, {
|
||
int limit = 2,
|
||
bool forceRefresh = false,
|
||
}) async {
|
||
final token = await _apiService.getAccessToken();
|
||
if (token == null) {
|
||
throw Exception('No access token');
|
||
}
|
||
|
||
final Map<String, String> requestHeaders = {
|
||
'Authorization': 'Bearer $token',
|
||
'Content-Type': 'application/json',
|
||
};
|
||
|
||
if (forceRefresh) {
|
||
requestHeaders['Cache-Control'] = 'no-cache';
|
||
}
|
||
|
||
final response = await http.get(
|
||
Uri.parse(
|
||
'${AppConstants.baseUrl}/messages/last?contact_id=$contactId&limit=$limit',
|
||
),
|
||
headers: requestHeaders,
|
||
);
|
||
|
||
if (response.statusCode == 200) {
|
||
final List<dynamic> data = jsonDecode(utf8.decode(response.bodyBytes));
|
||
return data.map((item) => item as Map<String, dynamic>).toList();
|
||
} else {
|
||
throw Exception('Failed to load last messages');
|
||
}
|
||
}
|
||
}
|