56 lines
1.6 KiB
Dart
56 lines
1.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../models/notification_model.dart';
|
|
import '../services/service_locator.dart';
|
|
import '../services/api/api_client.dart';
|
|
|
|
enum NotificationLoadStatus { idle, loading, success, error }
|
|
|
|
class NotificationProvider extends ChangeNotifier {
|
|
List<NotificationModel> _items = [];
|
|
NotificationLoadStatus _status = NotificationLoadStatus.idle;
|
|
String? _error;
|
|
|
|
List<NotificationModel> get items => List.unmodifiable(_items);
|
|
NotificationLoadStatus get status => _status;
|
|
String? get error => _error;
|
|
bool get isLoading => _status == NotificationLoadStatus.loading;
|
|
|
|
Future<void> loadNotifications() async {
|
|
_status = NotificationLoadStatus.loading;
|
|
_error = null;
|
|
notifyListeners();
|
|
|
|
try {
|
|
_items = await ServiceLocator().notifications.getNotifications();
|
|
_status = NotificationLoadStatus.success;
|
|
} on ApiException catch (e) {
|
|
_error = e.message;
|
|
_status = NotificationLoadStatus.error;
|
|
} catch (e) {
|
|
_error = 'خطا در دریافت اعلانها';
|
|
_status = NotificationLoadStatus.error;
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<bool> sendPublic(String title, String description) async {
|
|
_error = null;
|
|
try {
|
|
await ServiceLocator().notifications.sendPublicNotification(
|
|
title,
|
|
description,
|
|
);
|
|
await loadNotifications();
|
|
return true;
|
|
} on ApiException catch (e) {
|
|
_error = e.message;
|
|
notifyListeners();
|
|
return false;
|
|
} catch (_) {
|
|
_error = 'خطا در ارسال اعلان';
|
|
notifyListeners();
|
|
return false;
|
|
}
|
|
}
|
|
}
|