dzocesrce

Untitled

Apr 7th, 2026
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. import 'package:cloud_firestore/cloud_firestore.dart';
  2.  
  3. import '../../models/app_notification_model.dart';
  4. import '../constants/firestore_paths.dart';
  5.  
  6. class NotificationsFirestoreService {
  7. final FirebaseFirestore _firestore;
  8.  
  9. NotificationsFirestoreService({FirebaseFirestore? firestore})
  10. : _firestore = firestore ?? FirebaseFirestore.instance;
  11.  
  12. Future<List<AppNotificationModel>> getNotifications({int limit = 20}) async {
  13. final snapshot = await _firestore
  14. .collection(FirestorePaths.notifications)
  15. .where('isActive', isEqualTo: true)
  16. .orderBy('publishedAt', descending: true)
  17. .limit(limit)
  18. .get();
  19.  
  20. return snapshot.docs
  21. .map((doc) => AppNotificationModel.fromMap(doc.data(), doc.id))
  22. .toList();
  23. }
  24. }
  25.  
  26.  
  27. import 'package:cloud_firestore/cloud_firestore.dart';
  28.  
  29. import '../../models/info_slide_model.dart';
  30. import '../constants/firestore_paths.dart';
  31.  
  32. class InfoFirestoreService {
  33. final FirebaseFirestore _firestore;
  34.  
  35. InfoFirestoreService({FirebaseFirestore? firestore})
  36. : _firestore = firestore ?? FirebaseFirestore.instance;
  37.  
  38. Future<List<InfoSlideModel>> getInfoSlides() async {
  39. final snapshot = await _firestore
  40. .collection(FirestorePaths.infoSlides)
  41. .where('isActive', isEqualTo: true)
  42. .orderBy('orderIndex')
  43. .get();
  44.  
  45. return snapshot.docs
  46. .map((doc) => InfoSlideModel.fromMap(doc.data(), doc.id))
  47. .toList();
  48. }
  49. }
  50.  
  51.  
  52.  
  53. import 'package:flutter/material.dart';
  54. import 'package:shared_preferences/shared_preferences.dart';
  55.  
  56. import '../core/services/notifications_firestore_service.dart';
  57. import '../core/utils/app_error_messages.dart';
  58. import '../models/app_notification_model.dart';
  59.  
  60. class NotificationsProvider extends ChangeNotifier {
  61. static const String _readIdsKey = 'read_notification_ids';
  62.  
  63. final NotificationsFirestoreService _service;
  64.  
  65. NotificationsProvider({NotificationsFirestoreService? service})
  66. : _service = service ?? NotificationsFirestoreService();
  67.  
  68. List<AppNotificationModel> _notifications = [];
  69. Set<String> _readIds = {};
  70. bool _isLoading = false;
  71. String? _errorMessage;
  72. AppErrorType _errorType = AppErrorType.unknown;
  73.  
  74. List<AppNotificationModel> get notifications => _notifications;
  75. bool get isLoading => _isLoading;
  76. String? get errorMessage => _errorMessage;
  77. AppErrorType get errorType => _errorType;
  78.  
  79. bool isRead(String id) => _readIds.contains(id);
  80.  
  81. bool get hasUnread {
  82. return _notifications.any((notification) => !_readIds.contains(notification.id));
  83. }
  84.  
  85. Future<void> initialize() async {
  86. await _loadReadIds();
  87. await loadNotifications();
  88. }
  89.  
  90. Future<void> _loadReadIds() async {
  91. final prefs = await SharedPreferences.getInstance();
  92. final stored = prefs.getStringList(_readIdsKey) ?? [];
  93. _readIds = stored.toSet();
  94. notifyListeners();
  95. }
  96.  
  97. Future<void> _saveReadIds() async {
  98. final prefs = await SharedPreferences.getInstance();
  99. await prefs.setStringList(_readIdsKey, _readIds.toList());
  100. }
  101.  
  102. Future<void> loadNotifications() async {
  103. _isLoading = true;
  104. _errorMessage = null;
  105. _errorType = AppErrorType.unknown;
  106. notifyListeners();
  107.  
  108. try {
  109. final result = await _service.getNotifications(limit: 20);
  110. _notifications = result;
  111. } catch (e, stackTrace) {
  112. debugPrint('NOTIFICATIONS LOAD ERROR: $e');
  113. debugPrintStack(stackTrace: stackTrace);
  114.  
  115. final errorInfo = AppErrorMessages.fromError(
  116. e,
  117. context: 'notifications',
  118. );
  119.  
  120. _errorMessage = errorInfo.message;
  121. _errorType = errorInfo.type;
  122. _notifications = [];
  123. } finally {
  124. _isLoading = false;
  125. notifyListeners();
  126. }
  127. }
  128.  
  129. Future<void> markAsRead(String id) async {
  130. if (_readIds.contains(id)) return;
  131.  
  132. _readIds.add(id);
  133. await _saveReadIds();
  134. notifyListeners();
  135. }
  136.  
  137. String getPreviewText(String text, {int maxLength = 70}) {
  138. final trimmed = text.trim();
  139. if (trimmed.length <= maxLength) return trimmed;
  140. return '${trimmed.substring(0, maxLength)}...';
  141. }
  142.  
  143. String formatNotificationDate(DateTime? date) {
  144. if (date == null) return '';
  145.  
  146. final now = DateTime.now();
  147. final today = DateTime(now.year, now.month, now.day);
  148. final target = DateTime(date.year, date.month, date.day);
  149. final difference = today.difference(target).inDays;
  150.  
  151. if (difference == 0) return 'Today';
  152. if (difference == 1) return 'Yesterday';
  153.  
  154. final day = date.day.toString().padLeft(2, '0');
  155. final month = date.month.toString().padLeft(2, '0');
  156. final year = date.year.toString();
  157.  
  158. return '$day.$month.$year';
  159. }
  160.  
  161. String formatNotificationTime(DateTime? date) {
  162. if (date == null) return '';
  163.  
  164. final hour = date.hour.toString().padLeft(2, '0');
  165. final minute = date.minute.toString().padLeft(2, '0');
  166.  
  167. return '$hour:$minute';
  168. }
  169. }
  170.  
  171.  
  172.  
  173.  
  174. import 'package:flutter/material.dart';
  175.  
  176. import '../core/services/info_firestore_service.dart';
  177. import '../core/utils/app_error_messages.dart';
  178. import '../models/info_slide_model.dart';
  179.  
  180. class InfoProvider extends ChangeNotifier {
  181. final InfoFirestoreService _service;
  182.  
  183. InfoProvider({InfoFirestoreService? service})
  184. : _service = service ?? InfoFirestoreService();
  185.  
  186. List<InfoSlideModel> _slides = [];
  187. bool _isLoading = false;
  188. String? _errorMessage;
  189. AppErrorType _errorType = AppErrorType.unknown;
  190.  
  191. List<InfoSlideModel> get slides => _slides;
  192. bool get isLoading => _isLoading;
  193. String? get errorMessage => _errorMessage;
  194. AppErrorType get errorType => _errorType;
  195.  
  196. Future<void> loadSlides() async {
  197. _isLoading = true;
  198. _errorMessage = null;
  199. _errorType = AppErrorType.unknown;
  200. notifyListeners();
  201.  
  202. try {
  203. final fetchedSlides = await _service.getInfoSlides();
  204. _slides = fetchedSlides;
  205. } catch (e, stackTrace) {
  206. debugPrint('INFO LOAD ERROR: $e');
  207. debugPrintStack(stackTrace: stackTrace);
  208.  
  209. final errorInfo = AppErrorMessages.fromError(
  210. e,
  211. context: 'app info',
  212. );
  213.  
  214. _errorMessage = errorInfo.message;
  215. _errorType = errorInfo.type;
  216. _slides = [];
  217. } finally {
  218. _isLoading = false;
  219. notifyListeners();
  220. }
  221. }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment