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/device_transfer_provider.dart

172 lines
4.9 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/device/device_transfer.dart';
import '../../../models/device/device_transfer_info.dart';
import '../../../models/user.dart';
import '../../api_routes/urls.dart';
3 years ago
3 years ago
class DeviceTransferProvider extends ChangeNotifier {
3 years ago
// number of items call in each request
final pageItemNumber = 50;
//reset provider data
3 years ago
void reset() {
3 years ago
items = 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<DeviceTransfer>? items;
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> getRequests({
required String host,
required User user,
3 years ago
}) async {
3 years ago
if (isLoading == true) {
3 years ago
return -2;
3 years ago
}
3 years ago
isLoading = true;
// isLoading = false;
// stateCode = 200;
// items = [];
// items.addAll(List.generate(20, (index) => DeviceTransfer(
// title: "ddddd",
// id: "5",
// device: Device(id: "1",brand: "brand",model: "model"),
// destinationClient: Hospital(name: "hospital name",id: "1"),
// destinationDepartment: Department(id: "5",name: "destination Department"),
// senderDepartment: Department(id: "5",name: "sender Department"),
// userId: "5"
// )));
// notifyListeners();
// return 200;
Response response;
3 years ago
try {
3 years ago
response = await get(
3 years ago
Uri.parse("$host${URLs.getDeviceTransfer}?uid=${user.id}"
"&token=${user.token}&page=${(items?.length ?? 0) ~/ pageItemNumber}"),
headers: {"Content-Type": "application/json; charset=utf-8"});
3 years ago
stateCode = response.statusCode;
3 years ago
if (stateCode != null && stateCode! >= 200 && stateCode! < 300) {
3 years ago
// client's request was successfully received
3 years ago
List listJson =
json.decode(utf8.decode(response.bodyBytes).replaceAll("\\", ""));
List<DeviceTransfer> itemsPage = listJson
.map((request) => DeviceTransfer.fromJson(request))
.toList();
3 years ago
items ??= [];
3 years ago
items?.addAll(itemsPage);
if (itemsPage.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;
}
}
3 years ago
Future<int> createRequest({
required String host,
required User? user,
3 years ago
required DeviceTransfer model,
3 years ago
}) async {
3 years ago
Map<String, dynamic> body = {
"uid": user?.id.toString(),
"token": user?.token ?? "",
3 years ago
"serial_id": model.device?.id ?? "",
"destination_client": model.receiver?.client?.id ?? "",
"destination_department": model.receiver?.department?.id ?? "",
3 years ago
};
Response response;
3 years ago
try {
3 years ago
response = await post(
3 years ago
Uri.parse(host + URLs.requestDeviceTransfer),
body: body,
3 years ago
);
stateCode = response.statusCode;
3 years ago
if (response.statusCode >= 200 && response.statusCode < 300) {
final items = this.items;
if (items != null) {
3 years ago
items.insert(
0,
DeviceTransfer.fromJson(
3 years ago
json.decode(utf8.decode(response.bodyBytes))[0]));
3 years ago
notifyListeners();
}
}
return response.statusCode;
3 years ago
} catch (error) {
3 years ago
return -1;
}
}
3 years ago
Future<int> updateRequest({
required String host,
required User user,
required bool isSender,
required String requestId,
required DeviceTransfer? oldModel,
3 years ago
required DeviceTransferInfo newModel,
3 years ago
}) async {
3 years ago
Map<String, dynamic> body = {
3 years ago
"uid": user.id.toString(),
"token": user.token ?? "",
"current_user": user.id ?? "",
};
3 years ago
3 years ago
body.addAll(newModel.toJson(isSender));
Response response;
3 years ago
try {
3 years ago
response = await post(
Uri.parse("$host${URLs.updateDeviceTransfer}/$requestId"),
body: body,
);
stateCode = response.statusCode;
3 years ago
if (response.statusCode >= 200 && response.statusCode < 300) {
3 years ago
reset();
// oldModel.fromDeviceTransfer(
// DeviceTransfer.fromJson(
// json.decode(utf8.decode(response.bodyBytes))[0]
// )
// );
notifyListeners();
}
return response.statusCode;
3 years ago
} catch (error) {
3 years ago
return -1;
}
}
3 years ago
}