mirror of
https://git.meshkee.com/applications/cssd.git
synced 2026-10-13 05:28:55 +03:30
beta commit
This commit is contained in:
@@ -0,0 +1,346 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:cssd/models/item_model.dart';
|
||||
import 'package:cssd/models/my_item_model.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:random_string/random_string.dart';
|
||||
|
||||
import '../config/urls.dart';
|
||||
import '../config/user.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class ItemProvider extends ChangeNotifier {
|
||||
List<PlatformFile> catchImages = [];
|
||||
List<Uint8List> images = [];
|
||||
String searchBoxValue = '';
|
||||
bool filterOn = false;
|
||||
List<ItemResultObject>? items;
|
||||
|
||||
List<ItemResultObject>? originalItems;
|
||||
|
||||
ItemDetailObject? result;
|
||||
ItemFilterObject? filters = ItemFilterObject(
|
||||
id: '',
|
||||
hospital: '',
|
||||
name: '',
|
||||
udi: '',
|
||||
size: '',
|
||||
specific: '',
|
||||
);
|
||||
|
||||
MyItemFilterObject? myItemsFilters = MyItemFilterObject(
|
||||
id: '',
|
||||
name: '',
|
||||
udi: '',
|
||||
size: '',
|
||||
specific: '',
|
||||
);
|
||||
int pageNumber = 1;
|
||||
void resetPageNumber() => pageNumber = 1;
|
||||
bool isloading = false;
|
||||
void resetLoading() {
|
||||
isloading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void setLoading() {
|
||||
isloading = true;
|
||||
// notifyListeners();
|
||||
}
|
||||
|
||||
Future<List<Map<String, String>>> getHospitalsFilters() async {
|
||||
var response = await http.get(
|
||||
Uri.parse(Url.filterItems),
|
||||
headers: {
|
||||
'Authorization': User.token,
|
||||
},
|
||||
);
|
||||
List<Map<String, String>> result = [];
|
||||
var filterData = json.decode(response.body);
|
||||
for (var hospital in filterData['data']['MedicalCenters']) {
|
||||
result.add({
|
||||
'name': hospital['name'].toString(),
|
||||
'id': hospital['id'].toString()
|
||||
});
|
||||
// print(hospital['name']);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
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(String batchId, dynamic file, String fileName) async {
|
||||
var imageListInt = file.cast<int>();
|
||||
var dio = Dio();
|
||||
dio.options.headers = {
|
||||
'Authorization': User.token,
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
FormData formData = FormData.fromMap(
|
||||
{
|
||||
"batch_id": batchId,
|
||||
'model_type': 'Item',
|
||||
"file": MultipartFile.fromBytes(imageListInt, filename: fileName)
|
||||
},
|
||||
);
|
||||
|
||||
await dio.post(
|
||||
Url.addImage,
|
||||
data: formData,
|
||||
);
|
||||
return 'Added';
|
||||
}
|
||||
|
||||
Future<void> createItemResult(
|
||||
name,
|
||||
size,
|
||||
specific,
|
||||
udi,
|
||||
note,
|
||||
batchId,
|
||||
) async {
|
||||
batchId = randomString(10);
|
||||
|
||||
for (var image in catchImages) {
|
||||
await addImage(batchId, image.bytes, image.path!);
|
||||
}
|
||||
var response = await http.post(Uri.parse(Url.createItemResult),
|
||||
body: json.encode(
|
||||
{
|
||||
'note': note,
|
||||
'size': size,
|
||||
'specific': specific,
|
||||
'name': name,
|
||||
'udi': udi,
|
||||
'file_batch_id': batchId,
|
||||
},
|
||||
),
|
||||
headers: {
|
||||
'Authorization': User.token,
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json"
|
||||
});
|
||||
print(response.body);
|
||||
}
|
||||
|
||||
Future<void> updateItemResult(
|
||||
id,
|
||||
name,
|
||||
size,
|
||||
specific,
|
||||
udi,
|
||||
description,
|
||||
) async {
|
||||
var response = await http.put(Uri.parse(Url.updateItemResult(id)),
|
||||
body: json.encode(
|
||||
{
|
||||
'name': name,
|
||||
'udi': udi,
|
||||
'note': description,
|
||||
'size': size,
|
||||
'specific': specific,
|
||||
},
|
||||
),
|
||||
headers: {
|
||||
'Authorization': User.token,
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json"
|
||||
});
|
||||
|
||||
print(response.body);
|
||||
}
|
||||
|
||||
Future<bool> removeItems(String id) async {
|
||||
var response = await http.post(Uri.parse(Url.removeItems(id)), body: {
|
||||
'_method': 'delete'
|
||||
}, headers: {
|
||||
'Authorization': User.token,
|
||||
});
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
items!.removeWhere((item) {
|
||||
return item.id.toString() == id;
|
||||
});
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
return true;
|
||||
}
|
||||
|
||||
void setFilters(
|
||||
id,
|
||||
name,
|
||||
udi,
|
||||
hospital,
|
||||
size,
|
||||
specific,
|
||||
) {
|
||||
pageNumber = 1;
|
||||
filters = ItemFilterObject(
|
||||
id: id,
|
||||
name: name,
|
||||
udi: udi,
|
||||
hospital: hospital,
|
||||
size: size,
|
||||
specific: specific,
|
||||
);
|
||||
if (filters!.id == '' &&
|
||||
filters!.name == '' &&
|
||||
filters!.hospital == '' &&
|
||||
filters!.size == '' &&
|
||||
filters!.specific != '' &&
|
||||
filters!.udi == '') {
|
||||
filterOn = false;
|
||||
} else {
|
||||
filterOn = true;
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void search(String value) {
|
||||
items = [];
|
||||
searchBoxValue = value;
|
||||
originalItems!.forEach((result) {
|
||||
if (result.id.toString().contains(searchBoxValue)) {
|
||||
items!.add(result);
|
||||
}
|
||||
});
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> getItemResults() async {
|
||||
String url = Url.itemResults(pageNumber);
|
||||
if (filterOn) {
|
||||
if (filters!.id != '') {
|
||||
url += '&item_id=${filters!.id}';
|
||||
}
|
||||
if (filters!.name != '') {
|
||||
url += '&name=${filters!.name}';
|
||||
}
|
||||
if (filters!.hospital != '') {
|
||||
url += '&medical_center_id=${filters!.hospital}';
|
||||
}
|
||||
if (filters!.size != '') {
|
||||
url += '&size=${filters!.size}';
|
||||
}
|
||||
if (filters!.specific != '') {
|
||||
url += '&specific=${filters!.specific}';
|
||||
}
|
||||
if (filters!.udi != '') {
|
||||
url += '&udi=${filters!.udi}';
|
||||
}
|
||||
}
|
||||
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(
|
||||
ItemResultObject(
|
||||
name: item['name'],
|
||||
id: item['id'].toString(),
|
||||
size: item['size'].toString(),
|
||||
userName: item['user'] != null ? item['user']['name'] : '',
|
||||
status: item['status'],
|
||||
),
|
||||
);
|
||||
}
|
||||
pageNumber += 1;
|
||||
|
||||
search('');
|
||||
}
|
||||
|
||||
Future<ItemDetailObject> showItemResult(String id) async {
|
||||
var response = await http.get(Uri.parse(Url.getItemResult(id)), headers: {
|
||||
'Authorization': User.token,
|
||||
});
|
||||
if (response.statusCode != 200) {
|
||||
return ItemDetailObject(
|
||||
id: 'null',
|
||||
description: 'null',
|
||||
name: 'null',
|
||||
userName: 'null',
|
||||
specific: 'null',
|
||||
hospital: 'null',
|
||||
size: 'null',
|
||||
createdAt: '2019-09-18 10:02:03',
|
||||
udi: 'null',
|
||||
status: 0,
|
||||
);
|
||||
}
|
||||
var data = json.decode(response.body)['data'];
|
||||
print(data);
|
||||
result = ItemDetailObject(
|
||||
id: data['id'].toString(),
|
||||
description: data['note'] != null ? data['note'] : '',
|
||||
name: data['name'] != null ? data['name'] : '',
|
||||
hospital:
|
||||
data['user'] != null ? data['user']['medical_center']['name'] : '',
|
||||
userName: data['user'] != null ? data['user']['name'] : '',
|
||||
size: data['size'] != null ? data['size'] : '',
|
||||
specific: data['specific'] != null ? data['specific'] : '',
|
||||
createdAt: data['created_at'] != null ? data['created_at'] : '',
|
||||
udi: data['udi'] != null ? data['udi'] : '',
|
||||
status: data['status'],
|
||||
);
|
||||
return result!;
|
||||
}
|
||||
|
||||
Future<bool> changeStatus(String id) async {
|
||||
print(id);
|
||||
var response = await http.post(
|
||||
Uri.parse(Url.changeItemStatus(id)),
|
||||
headers: {
|
||||
'Authorization': User.token,
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json"
|
||||
},
|
||||
);
|
||||
|
||||
var itemData = json.decode(response.body);
|
||||
|
||||
try {
|
||||
if (itemData['status'] == 'Success') {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user