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.
80 lines
2.3 KiB
Dart
80 lines
2.3 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
import 'package:http/http.dart';
|
|
import 'package:test_sa/api/gas_refill_api_client.dart';
|
|
|
|
import '../../../models/gas_refill/gas_refill_model.dart';
|
|
import '../../../models/user.dart';
|
|
import '../../api_routes/urls.dart';
|
|
import '../../http_status_manger/http_status_manger.dart';
|
|
import '../../localization/localization.dart';
|
|
import '../loading_notifier.dart';
|
|
|
|
class GasRefillProvider extends LoadingNotifier {
|
|
// number of items call in each request
|
|
final pageItemNumber = 50;
|
|
|
|
//reset provider data
|
|
void reset() {
|
|
items.clear();
|
|
nextPage = true;
|
|
stateCode = null;
|
|
}
|
|
|
|
// state code of current request to defied error message
|
|
// like 400 customer request failed
|
|
// 500 service not available
|
|
int? stateCode;
|
|
|
|
// true if there is next page in product list and false if not
|
|
bool nextPage = true;
|
|
|
|
// list of user requests
|
|
List<GasRefillModel> items = [];
|
|
|
|
Future getRequests() async {
|
|
waitApiRequest(() async {
|
|
items.clear();
|
|
items.addAll(await GasRefillApiClient().getRequestPages(items: items, pageItemNumber: pageItemNumber));
|
|
notifyListeners();
|
|
if (items.length == pageItemNumber) {
|
|
nextPage = true;
|
|
} else {
|
|
nextPage = false;
|
|
}
|
|
}, onSuccess: () {
|
|
stateCode = 200;
|
|
notifyListeners();
|
|
});
|
|
}
|
|
|
|
Future createModel({required GasRefillModel model, required BuildContext context}) async {
|
|
final subtitle = AppLocalization.of(context)?.subtitle;
|
|
|
|
waitApiRequest(() async {
|
|
items.insert(0, await GasRefillApiClient().createModel(model: model));
|
|
notifyListeners();
|
|
}, onError: (error) {
|
|
String errorMessage = HttpStatusManger.getStatusMessage(status: error.error?.errorCode, subtitle: subtitle);
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
content: Text(errorMessage),
|
|
));
|
|
}, onSuccess: () {
|
|
Fluttertoast.showToast(
|
|
msg: subtitle?.requestCompleteSuccessfully ?? "",
|
|
);
|
|
Navigator.of(context).pop();
|
|
});
|
|
}
|
|
|
|
Future updateModel({
|
|
required GasRefillModel? oldModel,
|
|
required GasRefillModel newModel,
|
|
}) async {
|
|
await GasRefillApiClient().updateModel(oldModel: oldModel, newModel: newModel);
|
|
}
|
|
}
|