mirror of
https://git.meshkee.com/applications/cssd.git
synced 2026-10-10 18:58:54 +03:30
378 lines
10 KiB
Dart
Executable File
378 lines
10 KiB
Dart
Executable File
import 'dart:convert';
|
|
import 'dart:typed_data';
|
|
// import 'dart:typed_data';
|
|
import 'package:dio/dio.dart' as di;
|
|
|
|
import 'package:cssd/config/urls.dart';
|
|
import 'package:cssd/config/user.dart';
|
|
import 'package:cssd/models/test_result_models.dart';
|
|
import 'package:file_picker/file_picker.dart';
|
|
// import 'package:cssd/providers/test_result.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:random_string/random_string.dart';
|
|
// import 'package:multi_image_picker/multi_image_picker.dart';
|
|
|
|
class TestResultsProvider with ChangeNotifier {
|
|
List<PlatformFile> catchImages = [];
|
|
List<Uint8List> images = [];
|
|
String testType = '';
|
|
String testCode = '';
|
|
|
|
resetTests() {
|
|
testCode = '';
|
|
testType = '';
|
|
}
|
|
|
|
setImages() async {
|
|
images = [];
|
|
for (var image in catchImages) {
|
|
var result = image.bytes;
|
|
// final batchId = randomString(10);
|
|
// addImage(batchId, result, image.path);
|
|
images.add(result!.buffer
|
|
.asUint8List(result.offsetInBytes, result.lengthInBytes));
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
setCatchImages(List<PlatformFile> images) {
|
|
catchImages = images;
|
|
notifyListeners();
|
|
}
|
|
|
|
removeImage(int index) {
|
|
catchImages.removeAt(index);
|
|
notifyListeners();
|
|
}
|
|
|
|
// Future<String> addImage() async {
|
|
// Map<dynamic, dynamic> imgs = {};
|
|
// var counter = 0;
|
|
// for (var image in images) {
|
|
// imgs[counter.toString()] =
|
|
// di.MultipartFile.fromBytes(image, filename: 'test$counter.jpg');
|
|
// counter += 1;
|
|
// }
|
|
|
|
// return 'True';
|
|
// }
|
|
|
|
String searchBoxValue = '';
|
|
bool filterOn = false;
|
|
List<TestResultObject>? items;
|
|
List<TestResultObject>? originalItems;
|
|
Map<String, dynamic>? result;
|
|
|
|
TestResultFilterObject _filters = TestResultFilterObject(
|
|
id: '',
|
|
labelID: '',
|
|
userName: '',
|
|
hospital: '',
|
|
testType: '',
|
|
fDate: '',
|
|
uDate: '',
|
|
);
|
|
|
|
void resetFilters() {
|
|
filterOn = false;
|
|
_filters = TestResultFilterObject(
|
|
id: '',
|
|
labelID: '',
|
|
userName: '',
|
|
hospital: '',
|
|
testType: '',
|
|
fDate: '',
|
|
uDate: '',
|
|
);
|
|
}
|
|
|
|
int pageNumber = 1;
|
|
void resetPageNumber() => pageNumber = 1;
|
|
bool isloading = false;
|
|
void resetLoading() {
|
|
isloading = false;
|
|
notifyListeners();
|
|
}
|
|
|
|
void setLoading() {
|
|
isloading = true;
|
|
// notifyListeners();
|
|
}
|
|
|
|
TestResultFilterObject get filters {
|
|
return _filters;
|
|
}
|
|
|
|
void search(String value) {
|
|
print(value);
|
|
items = [];
|
|
searchBoxValue = value;
|
|
originalItems!.forEach((result) {
|
|
if (result.name.toString().contains(searchBoxValue)) {
|
|
items!.add(result);
|
|
}
|
|
});
|
|
notifyListeners();
|
|
}
|
|
|
|
void setFilters(id, labelId, username, testType, hospital, fDate, uDate) {
|
|
pageNumber = 1;
|
|
_filters = TestResultFilterObject(
|
|
id: id,
|
|
labelID: labelId,
|
|
userName: username,
|
|
hospital: hospital,
|
|
testType: testType,
|
|
fDate: fDate,
|
|
uDate: uDate);
|
|
if (_filters.id == '' &&
|
|
_filters.labelID == '' &&
|
|
_filters.testType == '' &&
|
|
_filters.hospital == '' &&
|
|
_filters.userName == '' &&
|
|
_filters.fDate == '' &&
|
|
_filters.uDate == '') {
|
|
filterOn = false;
|
|
} else {
|
|
filterOn = true;
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<List<Map<String, String>>> getFilterItems() async {
|
|
var response = await http.get(
|
|
Uri.parse(Url.filterItems),
|
|
headers: {
|
|
'Authorization': User.token,
|
|
},
|
|
);
|
|
List<Map<String, String>> result = [];
|
|
Map<String, String> hospitals = {};
|
|
Map<String, String> testTypes = {};
|
|
|
|
var filterDatas = json.decode(response.body);
|
|
for (var hospital in filterDatas['data']['MedicalCenters']) {
|
|
hospitals.addAll({hospital['name']: hospital['id'].toString()});
|
|
}
|
|
result.add(hospitals);
|
|
for (var testType in filterDatas['data']['TestTypes']) {
|
|
testTypes.addAll({testType['name']: testType['id'].toString()});
|
|
}
|
|
result.add(testTypes);
|
|
|
|
return result;
|
|
}
|
|
|
|
Future<void> getTestResults() async {
|
|
String url = Url.testResults(pageNumber);
|
|
if (filterOn) {
|
|
items = [];
|
|
if (_filters.id != '') {
|
|
url += '&device_id=${_filters.id}';
|
|
}
|
|
if (_filters.labelID != '') {
|
|
url += '&device_label=${_filters.labelID}';
|
|
}
|
|
if (_filters.hospital != '') {
|
|
url += '&medicalCenter_id=${_filters.hospital}';
|
|
}
|
|
if (_filters.testType != '') {
|
|
url += '&testType_id=${_filters.testType}';
|
|
}
|
|
if (_filters.userName != '') {
|
|
url += '&user_name=${_filters.userName}';
|
|
}
|
|
if (_filters.fDate != '') {
|
|
url += '&fromPicker=${_filters.fDate}';
|
|
}
|
|
if (_filters.uDate != '') {
|
|
url += '&upToPicker=${_filters.uDate}';
|
|
}
|
|
print(url);
|
|
}
|
|
//'device_id=3526&testType_id=107&medicalCenter_id=2&user_name=erfan&fromPicker=2020-05-12&upToPicker=2020-05-19&device_label=Instrument%20ID&page=1'
|
|
var response = await http.get(
|
|
Uri.parse(url),
|
|
headers: {
|
|
'Authorization': User.token,
|
|
},
|
|
);
|
|
|
|
var testResultData = json.decode(response.body);
|
|
if (pageNumber == 1) {
|
|
originalItems = [];
|
|
}
|
|
|
|
for (var item in testResultData['data']['data']) {
|
|
originalItems!.add(
|
|
TestResultObject(
|
|
id: item['id'].toString(),
|
|
name: item['test_type'] != null ? item['test_type']['name'] : '',
|
|
deviceId: item['device_id'].toString(),
|
|
result: item['result'] == 'Accept' ? 'Accepted' : 'Not Accepted',
|
|
date: item['created_at'].toString().substring(0, 10),
|
|
hospital: item['user']['medical_center']['name'],
|
|
verified: item['verified'].toString(),
|
|
),
|
|
);
|
|
}
|
|
pageNumber += 1;
|
|
search('');
|
|
notifyListeners();
|
|
}
|
|
|
|
static Future<void> updateTestResult(String id, values) async {
|
|
var response = await http.post(
|
|
Uri.parse(Url.updateTestResult(id)),
|
|
body: json.encode(values),
|
|
headers: {
|
|
'Authorization': User.token,
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json"
|
|
},
|
|
);
|
|
print(response.body);
|
|
}
|
|
|
|
Future<String> addImage(String batchId, dynamic file, String fileName) async {
|
|
var imageListInt = file.cast<int>();
|
|
var dio = new di.Dio();
|
|
dio.options.headers = {
|
|
'Authorization': User.token,
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
'Content-Type': 'application/json',
|
|
};
|
|
di.FormData formData = new di.FormData.fromMap({
|
|
"batch_id": batchId,
|
|
"file": di.MultipartFile.fromBytes(imageListInt, filename: fileName)
|
|
});
|
|
|
|
await dio.post(
|
|
Url.addImage,
|
|
data: formData,
|
|
);
|
|
// print(response.statusCode);
|
|
// print(response.statusMessage);
|
|
// print(response.data);
|
|
|
|
return 'Added';
|
|
}
|
|
|
|
Future<String> createTestResult(values, batchId) async {
|
|
final batchId = randomString(10);
|
|
|
|
for (var image in catchImages) {
|
|
await addImage(batchId, image.bytes, image.path!);
|
|
}
|
|
dynamic vals = values;
|
|
vals['batch_id'] = batchId;
|
|
|
|
// int counter = 0;
|
|
// if (images.length != 0) {
|
|
// for (var image in images) {
|
|
// vals['imgs'][counter.toString()] = base64Encode(
|
|
// image,
|
|
// );
|
|
// counter += 1;
|
|
// }
|
|
// }
|
|
|
|
var dio = new di.Dio();
|
|
dio.options.headers = {
|
|
'Authorization': User.token,
|
|
// 'X-Requested-With': 'XMLHttpRequest'
|
|
};
|
|
var response = await dio.post(
|
|
Url.createTestResult,
|
|
data: vals,
|
|
);
|
|
print(response.data);
|
|
if (response.data['status'] == 'Success') {
|
|
catchImages = [];
|
|
return 'done';
|
|
} else {
|
|
return 'error';
|
|
}
|
|
// var response = await http
|
|
// .post(Url.createTestResult, body: json.encode(values), headers: {
|
|
// 'Authorization': User.token,
|
|
// 'X-Requested-With': 'XMLHttpRequest',
|
|
// "Content-Type": "application/json",
|
|
// "Accept": "application/json"
|
|
// });
|
|
|
|
// print(response.body);
|
|
}
|
|
|
|
Future<Map<String, dynamic>> showTestResult(String id) async {
|
|
var response = await http.get(Uri.parse(Url.getTestResult(id)), headers: {
|
|
'Authorization': User.token,
|
|
});
|
|
|
|
var data = json.decode(response.body)['data'];
|
|
// print(data);
|
|
result = {
|
|
'id': data['id'],
|
|
'auto_cleave_id': data['inputs']['Autoclave ID'] != null
|
|
? data['inputs']['Autoclave ID']
|
|
: '',
|
|
'test_code': data['test_type_id'] != null ? data['test_type_id'] : '',
|
|
'date': data['created_at'] != null ? data['created_at'] : '',
|
|
'accepted': originalItems!
|
|
.firstWhere((element) => element.id == data['id'].toString())
|
|
.result, //az api safheye asli estefade mishavad
|
|
'description':
|
|
data['inputs']['Note'] != null ? data['inputs']['Note'] : '',
|
|
'test_type_name':
|
|
data['test_type']['name'] != null ? data['test_type']['name'] : '',
|
|
'hospital': originalItems!
|
|
.firstWhere((element) => element.id == data['id'].toString())
|
|
.hospital // az api safheye asli estefade mi shavad
|
|
};
|
|
return result!;
|
|
}
|
|
|
|
Future<bool> removeTestResult(String id) async {
|
|
var response = await http.post(Uri.parse(Url.removeTestResult(id)), body: {
|
|
'_method': 'delete'
|
|
}, headers: {
|
|
'Authorization': User.token,
|
|
});
|
|
|
|
if (response.statusCode == 200) {
|
|
items!.removeWhere((item) {
|
|
return item.id.toString() == id;
|
|
});
|
|
}
|
|
|
|
notifyListeners();
|
|
return true;
|
|
}
|
|
|
|
Future<bool> changeVerification(String id, String value) async {
|
|
print(id);
|
|
print(value);
|
|
var response = await http.post(
|
|
Uri.parse(Url.testResultVerification(id, value)),
|
|
headers: {
|
|
'Authorization': User.token,
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json"
|
|
},
|
|
);
|
|
var testResultData = json.decode(response.body);
|
|
// print(response.body);
|
|
try {
|
|
if (testResultData['status'] == 'Success') {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
} catch (e) {}
|
|
return false;
|
|
}
|
|
}
|