You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.7 KiB
Dart
97 lines
2.7 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:http/http.dart';
|
|
import 'package:test_sa/controllers/api_routes/api_manager.dart';
|
|
import 'package:test_sa/controllers/api_routes/urls.dart';
|
|
import 'package:test_sa/modules/alerts/alert_model.dart';
|
|
|
|
class AlertProvider extends ChangeNotifier {
|
|
final pageItemNumber = 12;
|
|
|
|
void reset() {
|
|
items = null;
|
|
nextPage = true;
|
|
stateCode = null;
|
|
}
|
|
|
|
int? stateCode;
|
|
|
|
bool nextPage = true;
|
|
|
|
List<AlertModel>? items;
|
|
|
|
bool isLoading = false;
|
|
|
|
AlertModel? alertModel;
|
|
|
|
Future<int> getAlerts() async {
|
|
if (isLoading == true) return -2;
|
|
isLoading = true;
|
|
if (items?.isEmpty ?? true) notifyListeners();
|
|
late Response response;
|
|
|
|
Map<String, dynamic> body = {};
|
|
body["pageNumber"] = (items?.length ?? 0) ~/ pageItemNumber + 1;
|
|
body["pageSize"] = pageItemNumber;
|
|
|
|
try {
|
|
response = await ApiManager.instance.post(URLs.getAlerts, body: body);
|
|
stateCode = response.statusCode;
|
|
if (stateCode! >= 200 && stateCode! < 300) {
|
|
final decoded = json.decode(response.body);
|
|
final List requestsListJson = (decoded is Map && decoded.containsKey('data')) ? decoded['data'] : decoded;
|
|
List<AlertModel> itemsPage = requestsListJson.map((request) => AlertModel.fromJson(request)).toList();
|
|
items ??= [];
|
|
items!.addAll(itemsPage);
|
|
nextPage = itemsPage.length == pageItemNumber;
|
|
}
|
|
isLoading = false;
|
|
notifyListeners();
|
|
return response.statusCode;
|
|
} catch (error) {
|
|
isLoading = false;
|
|
stateCode = -1;
|
|
notifyListeners();
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
Future<AlertModel?> getAlertById(num id, {bool refresh = false}) async {
|
|
if (refresh) {
|
|
return alertModel;
|
|
}
|
|
try {
|
|
alertModel = null;
|
|
Response response = await ApiManager.instance.get("${URLs.getAlertById}/$id");
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
final decoded = json.decode(response.body);
|
|
final Map<String, dynamic> dataNode = (decoded is Map && decoded.containsKey('data')) ? decoded['data'] : decoded;
|
|
alertModel = AlertModel.fromJson(dataNode);
|
|
notifyListeners();
|
|
return alertModel;
|
|
}
|
|
return null;
|
|
} catch (error) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<bool> resolveAlert(num id) async {
|
|
try {
|
|
Response response = await ApiManager.instance.post(URLs.resolveAlert, body: {"id": id});
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
if (items != null) {
|
|
reset();
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
|