format code

merge-requests/16/head
nextwo 3 years ago
parent 55245403d3
commit 6ad1f909bf

@ -8,31 +8,23 @@ import '../models/device/device_transfer.dart';
import '../models/device/device_transfer_info.dart';
class DeviceTransferApiClient {
static final DeviceTransferApiClient _instance = DeviceTransferApiClient._internal();
DeviceTransferApiClient._internal();
factory DeviceTransferApiClient() => _instance;
Future<List<DeviceTransfer>> getRequests({
required List items,
required int pageItemNumber
}) async {
final response = await ApiClient().getJsonForResponse(
"${URLs.host1}${URLs.getDeviceTransfer}",
queryParameters: {
Future<List<DeviceTransfer>> getRequests({required List items, required int pageItemNumber}) async {
final response = await ApiClient().getJsonForResponse("${URLs.host1}${URLs.getDeviceTransfer}", queryParameters: {
"uid": "${UserApiClient().user?.id}",
"token": "${UserApiClient().user?.token}",
"page": "${(items.length) ~/ pageItemNumber}",
},
headers: {"Content-Type": "application/json; charset=utf-8"}
);
}, headers: {
"Content-Type": "application/json; charset=utf-8"
});
List listJson = json.decode(utf8.decode(response.bodyBytes).replaceAll("\\", ""));
return listJson.map((request) => DeviceTransfer.fromJson(request)).toList();
}
Future<DeviceTransfer> createRequest({
@ -46,21 +38,17 @@ class DeviceTransferApiClient{
"destination_department": model.receiver?.department?.id ?? "",
};
final response = await ApiClient().postJsonForResponse(
"${URLs.host1}${URLs.requestDeviceTransfer}", body, isFormData: true);
final response = await ApiClient().postJsonForResponse("${URLs.host1}${URLs.requestDeviceTransfer}", body, isFormData: true);
return DeviceTransfer.fromJson(json.decode(utf8.decode(response.bodyBytes))[0]);
}
Future<DeviceTransfer> updateRequest({
required bool isSender,
required String requestId,
required DeviceTransfer? oldModel,
required DeviceTransferInfo newModel,
}) async {
Map<String, dynamic> body = {
"uid": UserApiClient().user?.id.toString(),
"token": UserApiClient().user?.token,
@ -69,13 +57,8 @@ class DeviceTransferApiClient{
body.addAll(newModel.toJson(isSender));
final response = await ApiClient().postJsonForResponse(
"${URLs.host1}${URLs.updateDeviceTransfer}/$requestId", body);
return DeviceTransfer.fromJson(
json.decode(utf8.decode(response.bodyBytes))[0]
);
final response = await ApiClient().postJsonForResponse("${URLs.host1}${URLs.updateDeviceTransfer}/$requestId", body);
return DeviceTransfer.fromJson(json.decode(utf8.decode(response.bodyBytes))[0]);
}
}

@ -7,39 +7,31 @@ import '../models/gas_refill/gas_refill_model.dart';
import 'api_client.dart';
class GasRefillApiClient {
static final GasRefillApiClient _instance = GasRefillApiClient._internal();
GasRefillApiClient._internal();
factory GasRefillApiClient() => _instance;
Future<List<GasRefillModel>> getRequestPages({
required List items,
required int pageItemNumber
}) async {
final response = await ApiClient().getJsonForResponse(
"${URLs.host1}${URLs.getGasRefill}",//body
headers: {"Content-Type": "application/json; charset=utf-8"},
Future<List<GasRefillModel>> getRequestPages({required List items, required int pageItemNumber}) async {
final response = await ApiClient().getJsonForResponse("${URLs.host1}${URLs.getGasRefill}", //body
headers: {
"Content-Type": "application/json; charset=utf-8"
},
queryParameters: {
"uid": "${UserApiClient().user?.id}",
"token": "${UserApiClient().user?.token}",
"page": "${(items.length) ~/ pageItemNumber}",
}
);
});
// client's request was successfully received
List requestsListJson = json.decode(utf8.decode(response.bodyBytes));
return requestsListJson.map((request) => GasRefillModel.fromJson(request)).toList();
}
Future<GasRefillModel> createModel({
required GasRefillModel model,
}) async {
Map<String, dynamic> body = {
"uid": UserApiClient().user?.id.toString(),
"token": UserApiClient().user?.token ?? "",
@ -61,7 +53,6 @@ class GasRefillApiClient{
);
return GasRefillModel.fromJson(json.decode(utf8.decode(response.bodyBytes))[0]);
}
Future updateModel({
@ -84,11 +75,8 @@ class GasRefillApiClient{
})
.toList());
final reponse = await ApiClient().postJsonForResponse(
"${URLs.host1}${URLs.updateGasRefill}/${newModel.id}",
body);
final reponse = await ApiClient().postJsonForResponse("${URLs.host1}${URLs.updateGasRefill}/${newModel.id}", body);
oldModel?.fromGasRefillModel(newModel);
}
}

@ -1,5 +1,3 @@
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:test_sa/api/device_transfer_api_client.dart';
@ -34,7 +32,6 @@ class DeviceTransferProvider extends LoadingNotifier {
List<DeviceTransfer> items = [];
Future getRequests() async {
waitApiRequest(() async {
items.clear();
items.addAll(await DeviceTransferApiClient().getRequests(items: items, pageItemNumber: pageItemNumber));
@ -44,35 +41,28 @@ class DeviceTransferProvider extends LoadingNotifier {
} else {
nextPage = false;
}
},
onSuccess: (){
}, onSuccess: () {
stateCode = 200;
notifyListeners();
});
}
Future createRequest({
required DeviceTransfer model,
required BuildContext context
}) async {
Future createRequest({required DeviceTransfer model, required BuildContext context}) async {
final subtitle = AppLocalization.of(context)?.subtitle;
waitApiRequest(() async {
items.insert(0, await DeviceTransferApiClient().createRequest(model: model));
notifyListeners();
},
onSuccess: (){
}, onSuccess: () {
Fluttertoast.showToast(
msg: subtitle?.requestCompleteSuccessfully ?? "",
);
Navigator.of(context).pop();
},
onError: (error){
}, onError: (error) {
String errorMessage = HttpStatusManger.getStatusMessage(status: error.error?.errorCode, subtitle: subtitle);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(errorMessage),
));
}
);
});
}
Future updateRequest({
@ -83,7 +73,6 @@ class DeviceTransferProvider extends LoadingNotifier {
required DeviceTransfer? oldModel,
required DeviceTransferInfo newModel,
}) async {
reset();
oldModel = await DeviceTransferApiClient().updateRequest(isSender: isSender, requestId: requestId, oldModel: oldModel, newModel: newModel);
notifyListeners();

@ -45,48 +45,35 @@ class GasRefillProvider extends LoadingNotifier {
} else {
nextPage = false;
}
},
onSuccess: (){
}, onSuccess: () {
stateCode = 200;
notifyListeners();
}
);
});
}
Future createModel({
required GasRefillModel model,
required BuildContext context
}) async {
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){
}, onError: (error) {
String errorMessage = HttpStatusManger.getStatusMessage(status: error.error?.errorCode, subtitle: subtitle);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(errorMessage),
));
},
onSuccess: (){
}, 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);
}
}

@ -165,9 +165,9 @@ class _RequestDeviceTransferState extends State<RequestDeviceTransfer> {
return;
}
await _deviceTransferProvider?.createRequest(
model: _formModel, context: context,
model: _formModel,
context: context,
);
},
),

@ -46,7 +46,6 @@ class _RequestGasRefillState extends State<RequestGasRefill> {
if (mounted) super.setState(() {});
}
bool _addNewModel() {
_validate = true;
if (!(_formKey.currentState?.validate() ?? false)) {
@ -211,11 +210,7 @@ class _RequestGasRefillState extends State<RequestGasRefill> {
if (!_addNewModel()) return;
}
await _gasRefillProvider?.createModel(
model: _formModel,
context: context
);
await _gasRefillProvider?.createModel(model: _formModel, context: context);
},
),
const SizedBox(

Loading…
Cancel
Save