Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:cloud_firestore/cloud_firestore.dart';
- import '../../models/app_notification_model.dart';
- import '../constants/firestore_paths.dart';
- class NotificationsFirestoreService {
- final FirebaseFirestore _firestore;
- NotificationsFirestoreService({FirebaseFirestore? firestore})
- : _firestore = firestore ?? FirebaseFirestore.instance;
- Future<List<AppNotificationModel>> getNotifications({int limit = 20}) async {
- final snapshot = await _firestore
- .collection(FirestorePaths.notifications)
- .where('isActive', isEqualTo: true)
- .orderBy('publishedAt', descending: true)
- .limit(limit)
- .get();
- return snapshot.docs
- .map((doc) => AppNotificationModel.fromMap(doc.data(), doc.id))
- .toList();
- }
- }
- import 'package:cloud_firestore/cloud_firestore.dart';
- import '../../models/info_slide_model.dart';
- import '../constants/firestore_paths.dart';
- class InfoFirestoreService {
- final FirebaseFirestore _firestore;
- InfoFirestoreService({FirebaseFirestore? firestore})
- : _firestore = firestore ?? FirebaseFirestore.instance;
- Future<List<InfoSlideModel>> getInfoSlides() async {
- final snapshot = await _firestore
- .collection(FirestorePaths.infoSlides)
- .where('isActive', isEqualTo: true)
- .orderBy('orderIndex')
- .get();
- return snapshot.docs
- .map((doc) => InfoSlideModel.fromMap(doc.data(), doc.id))
- .toList();
- }
- }
- import 'package:flutter/material.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- import '../core/services/notifications_firestore_service.dart';
- import '../core/utils/app_error_messages.dart';
- import '../models/app_notification_model.dart';
- class NotificationsProvider extends ChangeNotifier {
- static const String _readIdsKey = 'read_notification_ids';
- final NotificationsFirestoreService _service;
- NotificationsProvider({NotificationsFirestoreService? service})
- : _service = service ?? NotificationsFirestoreService();
- List<AppNotificationModel> _notifications = [];
- Set<String> _readIds = {};
- bool _isLoading = false;
- String? _errorMessage;
- AppErrorType _errorType = AppErrorType.unknown;
- List<AppNotificationModel> get notifications => _notifications;
- bool get isLoading => _isLoading;
- String? get errorMessage => _errorMessage;
- AppErrorType get errorType => _errorType;
- bool isRead(String id) => _readIds.contains(id);
- bool get hasUnread {
- return _notifications.any((notification) => !_readIds.contains(notification.id));
- }
- Future<void> initialize() async {
- await _loadReadIds();
- await loadNotifications();
- }
- Future<void> _loadReadIds() async {
- final prefs = await SharedPreferences.getInstance();
- final stored = prefs.getStringList(_readIdsKey) ?? [];
- _readIds = stored.toSet();
- notifyListeners();
- }
- Future<void> _saveReadIds() async {
- final prefs = await SharedPreferences.getInstance();
- await prefs.setStringList(_readIdsKey, _readIds.toList());
- }
- Future<void> loadNotifications() async {
- _isLoading = true;
- _errorMessage = null;
- _errorType = AppErrorType.unknown;
- notifyListeners();
- try {
- final result = await _service.getNotifications(limit: 20);
- _notifications = result;
- } catch (e, stackTrace) {
- debugPrint('NOTIFICATIONS LOAD ERROR: $e');
- debugPrintStack(stackTrace: stackTrace);
- final errorInfo = AppErrorMessages.fromError(
- e,
- context: 'notifications',
- );
- _errorMessage = errorInfo.message;
- _errorType = errorInfo.type;
- _notifications = [];
- } finally {
- _isLoading = false;
- notifyListeners();
- }
- }
- Future<void> markAsRead(String id) async {
- if (_readIds.contains(id)) return;
- _readIds.add(id);
- await _saveReadIds();
- notifyListeners();
- }
- String getPreviewText(String text, {int maxLength = 70}) {
- final trimmed = text.trim();
- if (trimmed.length <= maxLength) return trimmed;
- return '${trimmed.substring(0, maxLength)}...';
- }
- String formatNotificationDate(DateTime? date) {
- if (date == null) return '';
- final now = DateTime.now();
- final today = DateTime(now.year, now.month, now.day);
- final target = DateTime(date.year, date.month, date.day);
- final difference = today.difference(target).inDays;
- if (difference == 0) return 'Today';
- if (difference == 1) return 'Yesterday';
- final day = date.day.toString().padLeft(2, '0');
- final month = date.month.toString().padLeft(2, '0');
- final year = date.year.toString();
- return '$day.$month.$year';
- }
- String formatNotificationTime(DateTime? date) {
- if (date == null) return '';
- final hour = date.hour.toString().padLeft(2, '0');
- final minute = date.minute.toString().padLeft(2, '0');
- return '$hour:$minute';
- }
- }
- import 'package:flutter/material.dart';
- import '../core/services/info_firestore_service.dart';
- import '../core/utils/app_error_messages.dart';
- import '../models/info_slide_model.dart';
- class InfoProvider extends ChangeNotifier {
- final InfoFirestoreService _service;
- InfoProvider({InfoFirestoreService? service})
- : _service = service ?? InfoFirestoreService();
- List<InfoSlideModel> _slides = [];
- bool _isLoading = false;
- String? _errorMessage;
- AppErrorType _errorType = AppErrorType.unknown;
- List<InfoSlideModel> get slides => _slides;
- bool get isLoading => _isLoading;
- String? get errorMessage => _errorMessage;
- AppErrorType get errorType => _errorType;
- Future<void> loadSlides() async {
- _isLoading = true;
- _errorMessage = null;
- _errorType = AppErrorType.unknown;
- notifyListeners();
- try {
- final fetchedSlides = await _service.getInfoSlides();
- _slides = fetchedSlides;
- } catch (e, stackTrace) {
- debugPrint('INFO LOAD ERROR: $e');
- debugPrintStack(stackTrace: stackTrace);
- final errorInfo = AppErrorMessages.fromError(
- e,
- context: 'app info',
- );
- _errorMessage = errorInfo.message;
- _errorType = errorInfo.type;
- _slides = [];
- } finally {
- _isLoading = false;
- notifyListeners();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment