import 'package:cheminova/constants/constant.dart'; import 'package:cheminova/models/announcement_model.dart'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import '../models/notification_list_response.dart'; import '../services/api_client.dart'; import '../services/api_urls.dart'; class NotificationProvider extends ChangeNotifier { NotificationProvider() { // getNotification(); } final _apiClient = ApiClient(); List notificationList = []; List _announcementList = []; int _totalAnnouncements = 0; String _selectedDate = DateFormat('dd/MM/yyyy').format(DateTime.now()); bool _isLoading = false; bool get isLoading => _isLoading; int get totalAnnouncements => _totalAnnouncements; List get announcementList => _announcementList; String get selectedDate => _selectedDate; void setLoading(bool loading) { _isLoading = loading; notifyListeners(); } void clearLists() { notificationList = []; _announcementList = []; _totalAnnouncements = 0; notifyListeners(); } void setDate(String date) { _selectedDate = date; notifyListeners(); //getNotification(); } // Future getNotification() async { // clearLists(); // setLoading(true); // try { // Response response = await _apiClient // .get('${ApiUrls.notificationUrl}?Date=$_selectedDate'); // setLoading(false); // if (response.statusCode == 200) { // final data = NotificationListResponse.fromJson(response.data); // notificationList = data.notifications ?? []; // notifyListeners(); // } // } catch (e) { // setLoading(false); // } // } Future getNotification(String date) async { if (date.isEmpty) return; // Invalid call ko prevent karein final String url = "${ApiUrls.notificationUrl}?Date=$date"; setLoading(true); try { Response response = await _apiClient.get(url); if (response.statusCode == 200) { final data = NotificationListResponse.fromJson(response.data); notificationList = data.notifications ?? []; } else { notificationList = []; } } catch (e) { notificationList = []; } setLoading(false); notifyListeners(); } Future getAnnouncement(int page, int rowsPerPage) async { clearLists(); setLoading(true); try { Response response = await _apiClient .get('${ApiUrls.announcements}?page=$page&rowsPerPage=$rowsPerPage'); if (response.statusCode == 200) { _announcementList = (response.data['announcements'] as List) .map((e) => AnnouncementModel.fromJson(e)) .toList(); notifyListeners(); _totalAnnouncements = response.data['totalAnnouncements']; } setLoading(false); } catch (e) { String error = "Something went wrong"; if (e is DioException) { error = e.response!.data['message'] ?? "Something went wrong"; } ScaffoldMessenger.of( navigatorKey.currentContext!, ).showSnackBar( SnackBar( content: Text( error, ), ), ); setLoading(false); } } }