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.
cloudsolutions-atoms/lib/controllers/providers/api/notifications_provider.dart

119 lines
3.7 KiB
Dart

3 years ago
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:http/http.dart';
3 years ago
3 years ago
import '../../../models/app_notification.dart';
import '../../../models/user.dart';
import '../../api_routes/urls.dart';
3 years ago
3 years ago
class NotificationsProvider extends ChangeNotifier {
3 years ago
// number of items call in each request
final pageItemNumber = 20;
//reset provider data
3 years ago
void reset() {
3 years ago
notifications = null;
nextPage = true;
stateCode = null;
}
// state code of current request to defied error message
// like 400 customer request failed
// 500 service not available
3 years ago
int? stateCode;
3 years ago
// true if there is next page in product list and false if not
3 years ago
bool? nextPage = true;
3 years ago
// list of user requests
3 years ago
List<AppNotification>? notifications;
3 years ago
// when requests in-process _loading = true
// done _loading = true
// failed _loading = false
3 years ago
bool? isLoading;
3 years ago
/// return -2 if request in progress
/// return -1 if error happen when sending request
/// return state code if request complete may be 200, 404 or 403
/// for more details check http state manager
/// lib\controllers\http_status_manger\http_status_manger.dart
3 years ago
Future<int> getNotifications({
required String host,
required User user,
required String hospitalId,
3 years ago
}) async {
3 years ago
if (isLoading == true) {
3 years ago
return -2;
3 years ago
}
3 years ago
isLoading = true;
notifyListeners();
Response response;
3 years ago
// userId = 397.toString(); // testing id to view data
try {
3 years ago
response = await get(
3 years ago
Uri.parse(
"${host + URLs.getNotifications}?uid=${user.id}&token=${user.token}&page=${(notifications?.length ?? 0) ~/ pageItemNumber}",
),
headers: {"Content-Type": "application/json; charset=utf-8"});
3 years ago
stateCode = response.statusCode;
3 years ago
if (response.statusCode >= 200 && response.statusCode < 300) {
3 years ago
// client's request was successfully received
List requestsListJson = json.decode(utf8.decode(response.bodyBytes));
3 years ago
List<AppNotification> serviceRequestsPage = requestsListJson
.map((request) => AppNotification.fromJson(request))
.toList();
notifications ??= [];
notifications?.addAll(serviceRequestsPage);
if (serviceRequestsPage.length == pageItemNumber) {
3 years ago
nextPage = true;
3 years ago
} else {
3 years ago
nextPage = false;
}
}
isLoading = false;
notifyListeners();
return response.statusCode;
3 years ago
} catch (error) {
3 years ago
isLoading = false;
stateCode = -1;
notifyListeners();
return -1;
}
}
/// return -2 if request in progress
/// return -1 if error happen when sending request
/// return state code if request complete may be 200, 404 or 403
/// for more details check http state manager
/// lib\controllers\http_status_manger\http_status_manger.dart
3 years ago
Future<List<AppNotification>?> getRecentNotifications({
required String host,
required User user,
3 years ago
}) async {
Response response;
//userId = 397.toString(); // testing id to view data
3 years ago
try {
3 years ago
response = await get(
3 years ago
Uri.parse(
"$host${URLs.getNotifications}?uid=${user.id}&token=${user.token}"),
headers: {"Content-Type": "application/json; charset=utf-8"});
3 years ago
stateCode = response.statusCode;
3 years ago
if (response.statusCode >= 200 && response.statusCode < 300) {
3 years ago
// client's request was successfully received
List requestsListJson = json.decode(utf8.decode(response.bodyBytes));
3 years ago
List<AppNotification> recentNotifications = requestsListJson
.map((request) => AppNotification.fromJson(request))
.toList();
return recentNotifications;
3 years ago
}
return null;
3 years ago
} catch (error) {
3 years ago
return null;
}
}
3 years ago
}