import 'dart:convert'; import 'package:flutter/widgets.dart'; import 'package:http/http.dart'; import 'package:test_sa/controllers/api_routes/api_manager.dart'; import 'package:test_sa/controllers/api_routes/urls.dart'; import 'package:test_sa/models/all_requests_and_count_model.dart'; import 'package:test_sa/models/enums/user_types.dart'; import '../../../models/search_all_requests_model.dart'; class AllRequestsProvider extends ChangeNotifier { bool isAllLoading = false; bool isOpenLoading = false; bool isInProgressLoading = false; bool isCompleteLoading = false; bool isOverdueLoading = false; bool isHighPriorityLoading = false; bool isCalendarLoading = false; bool _isFilterRequestLoading = false; int _currentListIndex = 0; int get currentListIndex => _currentListIndex; set currentListIndex(int value) { _currentListIndex = value; notifyListeners(); } int stateCode; AllRequestsAndCount allRequestsAndCount; AllRequestsAndCount _openRequests; AllRequestsAndCount _inProgressRequests; AllRequestsAndCount _completedRequests; AllRequestsAndCount _filterRequest; AllRequestsAndCount overdueRequests; AllRequestsAndCount highPriorityRequests; bool isLoadingList = false; updateIsLoadingList(value) { isLoadingList = value; notifyListeners(); } AllRequestsAndCount calendarRequests; final pageItemNumber = 10; int pageNum = 1; bool nextPage = true; void reset() { allRequestsAndCount?.requestsDetails?.clear(); allRequestsAndCount = null; pageNum = 1; nextPage = true; stateCode = null; } void resetRequestsApiData() { print('reset api data called..'); highPriorityRequests = null; overdueRequests = null; openRequests = null; inProgressRequests = null; completedRequests = null; pageNum = 1; nextPage = true; stateCode = null; } Future getRequests() async { resetRequestsApiData(); await getHighPriorityRequests(pagination: true); await getOverdueRequests(pagination: true); await getOpenRequests(pagination: true); await getInProgressRequests(pagination: true); await getCompletedRequests(pagination: true); } SearchAllRequestsModel searchedModel; Future getAllRequests(BuildContext context, {int typeTransaction, SearchAllRequestsModel search}) async { if (isAllLoading == true) return -2; isAllLoading = true; if (allRequestsAndCount == null) notifyListeners(); Response response; try { if (search != null) { searchedModel = search; } else { searchedModel = null; } final type = typeTransaction == null ? search?.typeTransaction == null || search.typeTransaction.isEmpty ? [1, 2, 3, 4] : search.typeTransaction : [typeTransaction]; List status = (search?.statuses == null || search.statuses.isEmpty) ? ((search?.isArchived ?? false) ? [3] : [1, 2, 4]) : search.statuses; Map body = { "typeTransaction": type, "statusTransaction": status, "priority": [], "displayData": [], "pageNumber": pageNum++, "pageSize": pageItemNumber, if (search?.requestNumber?.value?.isNotEmpty ?? false) "requestNumber": search.requestNumber.value, if (search?.assetName?.value?.isNotEmpty ?? false) "assetName": search.assetName.value, if (search?.assetNo?.value?.isNotEmpty ?? false) "assetNumber": search.assetNo.value, if (search?.sn?.value?.isNotEmpty ?? false) "assetSerialNo": search.sn.value, if (search?.model?.value?.isNotEmpty ?? false) "model": search.model.value, if (search?.manufacture?.value?.isNotEmpty ?? false) "manufacturer": search.manufacture.value, if (search?.startDate != null) "from": search.startDate.toIso8601String(), if (search?.endDate != null) "to": search.endDate.toIso8601String(), }; response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body); stateCode = response.statusCode; if (response.statusCode >= 200 && response.statusCode < 300) { if (allRequestsAndCount == null) { allRequestsAndCount = AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]); } else { allRequestsAndCount.requestsDetails.addAll(AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]).requestsDetails); } if (allRequestsAndCount.requestsDetails.length >= pageItemNumber) { nextPage = true; } else { nextPage = false; } notifyListeners(); } else { allRequestsAndCount = null; } isAllLoading = false; notifyListeners(); return response.statusCode; } catch (error) { isAllLoading = false; stateCode = -1; notifyListeners(); return -1; } } Future getCalendarRequests({@required DateTime from, DateTime to}) async { if (isCalendarLoading == true) return -2; isCalendarLoading = true; notifyListeners(); if (isCalendarLoading == null) notifyListeners(); Response response; try { Map body = { "typeTransaction": [1, 2, 3, 4], "statusTransaction": [1, 2, 3, 4], "priority": [0, 1], "displayData": [], "from": from.toIso8601String(), "to": to?.toIso8601String() ?? from.toIso8601String(), }; response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body); stateCode = response.statusCode; if (response.statusCode >= 200 && response.statusCode < 300) { calendarRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]); notifyListeners(); } isCalendarLoading = false; notifyListeners(); return response.statusCode; } catch (error) { isCalendarLoading = false; stateCode = -1; notifyListeners(); return -1; } } Future getHighPriorityRequests({bool pagination = false}) async { if (isHighPriorityLoading == true) return -2; if (highPriorityRequests == null || pagination) { isHighPriorityLoading = true; notifyListeners(); } Response response; try { Map body = { "typeTransaction": [1], "statusTransaction": [1, 2, 4], "priority": [1], "displayData": [] }; if (pagination) { body["pageNumber"] = 0; body["pageSize"] = 0; } else { body["pageNumber"] = pageNum++; body["pageSize"] = pageItemNumber; } response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body); stateCode = response.statusCode; if (response.statusCode >= 200 && response.statusCode < 300) { if (highPriorityRequests == null) { highPriorityRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]); } else { highPriorityRequests.requestsDetails.addAll(AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]).requestsDetails); } if (highPriorityRequests.requestsDetails.length >= pageItemNumber) { nextPage = true; } else { nextPage = false; } notifyListeners(); } else { allRequestsAndCount = null; } isHighPriorityLoading = false; notifyListeners(); return response.statusCode; } catch (error) { isHighPriorityLoading = false; stateCode = -1; notifyListeners(); return -1; } } Future getOverdueRequests({bool pagination = false}) async { if (isOverdueLoading == true) return -2; if (overdueRequests == null || pagination) { isOverdueLoading = true; notifyListeners(); } Response response; try { Map body = { "typeTransaction": [1, 2, 3, 4], "statusTransaction": [1, 2, 4], "priority": [], "displayData": [1] }; if (pagination) { body["pageNumber"] = 0; body["pageSize"] = 0; } else { body["pageNumber"] = pageNum++; body["pageSize"] = pageItemNumber; } response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body); stateCode = response.statusCode; if (response.statusCode >= 200 && response.statusCode < 300) { if (overdueRequests == null) { overdueRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]); } else { overdueRequests.requestsDetails.addAll(AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]).requestsDetails); } if (overdueRequests.requestsDetails.length >= pageItemNumber) { nextPage = true; } else { nextPage = false; } notifyListeners(); } else { allRequestsAndCount = null; } isOverdueLoading = false; notifyListeners(); return response.statusCode; } catch (error) { isOverdueLoading = false; stateCode = -1; notifyListeners(); return -1; } } Future getOpenRequests({bool pagination = false, bool reset = false}) async { if (isOpenLoading == true) return -2; if (reset) { openRequests = null; pageNum = 1; } if (openRequests == null || pagination) { isOpenLoading = true; notifyListeners(); } Response response; try { bool isEngineer = ApiManager.instance.user.type == UsersTypes.engineer; Map body = { "typeTransaction": [1, 2, 3, 4], "statusTransaction": [isEngineer ? 2 : 1], "priority": [], "displayData": [] }; if (pagination) { body["pageNumber"] = 0; body["pageSize"] = 0; } else { body["pageNumber"] = pageNum++; body["pageSize"] = pageItemNumber; } response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body); stateCode = response.statusCode; if (response.statusCode >= 200 && response.statusCode < 300) { if (openRequests == null) { openRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]); } else { openRequests.requestsDetails.addAll(AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]).requestsDetails); } if (openRequests.requestsDetails.length >= pageItemNumber) { nextPage = true; } else { nextPage = false; } notifyListeners(); } else { allRequestsAndCount = null; } isOpenLoading = false; notifyListeners(); return response.statusCode; } catch (error) { isOpenLoading = false; stateCode = -1; notifyListeners(); return -1; } } Future getInProgressRequests({bool pagination = false, bool reset = false}) async { print('get inprogress request called..'); if (isInProgressLoading == true) return -2; if (reset) { inProgressRequests = null; pageNum = 1; } if (inProgressRequests == null || pagination) { isInProgressLoading = true; notifyListeners(); } Response response; try { Map body = { "typeTransaction": [1, 2, 3, 4], "statusTransaction": [2], "priority": [], "displayData": [] }; if (pagination) { body["pageNumber"] = 0; body["pageSize"] = 0; } else { body["pageNumber"] = pageNum++; body["pageSize"] = pageItemNumber; } response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body); print('response body of inprogress is ${response.body}'); stateCode = response.statusCode; print('state code is $stateCode'); if (response.statusCode >= 200 && response.statusCode < 300) { if (inProgressRequests == null) { print('in in null check...'); inProgressRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]); notifyListeners(); print('data in null check is ${inProgressRequests.requestsDetails.length}'); } else { inProgressRequests.requestsDetails.addAll(AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]).requestsDetails); print('data i have in provider is ${inProgressRequests.requestsDetails.length}'); notifyListeners(); } if (inProgressRequests.requestsDetails.length >= pageItemNumber) { nextPage = true; } else { nextPage = false; } notifyListeners(); } else { allRequestsAndCount = null; } isInProgressLoading = false; notifyListeners(); return response.statusCode; } catch (error) { isInProgressLoading = false; stateCode = -1; notifyListeners(); return -1; } } Future getFilterRequests({bool pagination = false, int status}) async { // if (filterRequest == null || pagination) { // isFilterRequestLoading = true; // notifyListeners(); // } isFilterRequestLoading = true; Response response; try { Map body = { "typeTransaction": [1, 2, 3, 4], "statusTransaction": [status], "priority": [], "displayData": [] }; if (pagination) { body["pageNumber"] = 0; body["pageSize"] = 0; } else { body["pageNumber"] = pageNum++; body["pageSize"] = pageItemNumber; } response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body); print('response body is ${response.body}'); stateCode = response.statusCode; if (response.statusCode >= 200 && response.statusCode < 300) { if (filterRequest == null) { filterRequest = AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]); isFilterRequestLoading = false; notifyListeners(); print('filter list data in provider is ${filterRequest.requestsDetails.length}'); } else { filterRequest.requestsDetails.addAll(AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]).requestsDetails); isFilterRequestLoading = false; notifyListeners(); } if (filterRequest.requestsDetails.length >= pageItemNumber) { nextPage = true; } else { nextPage = false; } notifyListeners(); } else { filterRequest = null; } isFilterRequestLoading = false; notifyListeners(); return response.statusCode; } catch (error) { isFilterRequestLoading = false; stateCode = -1; notifyListeners(); return -1; } } Future getCompletedRequests({bool pagination = false, bool reset = false}) async { if (isCompleteLoading == true) return -2; if (reset) { completedRequests = null; pageNum = 1; } if (completedRequests == null || pagination) { isCompleteLoading = true; notifyListeners(); } Response response; try { Map body = { "typeTransaction": [1, 2, 3, 4], "statusTransaction": [4], "priority": [], "displayData": [] }; if (pagination) { body["pageNumber"] = 0; body["pageSize"] = 0; } else { body["pageNumber"] = pageNum++; body["pageSize"] = pageItemNumber; } response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body); stateCode = response.statusCode; if (response.statusCode >= 200 && response.statusCode < 300) { if (completedRequests == null) { completedRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]); } else { completedRequests.requestsDetails.addAll(AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]).requestsDetails); } print('completed request length is ${completedRequests.requestsDetails.length}'); if (completedRequests.requestsDetails.length >= pageItemNumber) { nextPage = true; } else { nextPage = false; } notifyListeners(); } else { allRequestsAndCount = null; } isCompleteLoading = false; notifyListeners(); return response.statusCode; } catch (error) { isCompleteLoading = false; stateCode = -1; notifyListeners(); return -1; } } AllRequestsAndCount get inProgressRequests => _inProgressRequests; set inProgressRequests(AllRequestsAndCount value) { _inProgressRequests = value; notifyListeners(); } AllRequestsAndCount get completedRequests => _completedRequests; set completedRequests(AllRequestsAndCount value) { _completedRequests = value; notifyListeners(); } AllRequestsAndCount get openRequests => _openRequests; set openRequests(AllRequestsAndCount value) { _openRequests = value; notifyListeners(); } AllRequestsAndCount get filterRequest => _filterRequest; set filterRequest(AllRequestsAndCount value) { _filterRequest = value; notifyListeners(); } bool get isFilterRequestLoading => _isFilterRequestLoading; set isFilterRequestLoading(bool value) { _isFilterRequestLoading = value; notifyListeners(); } // Future getCloseRequests() async { // if (isCloseLoading == true) return -2; // isCloseLoading = true; // if (closeRequests == null) notifyListeners(); // Response response; // try { // Map body = { // "typeTransaction": [1, 2, 3, 4], // "statusTransaction": [4], // "priority": [], // "displayData": [] // }; // response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body); // // stateCode = response.statusCode; // if (response.statusCode >= 200 && response.statusCode < 300) { // closeRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"]); // notifyListeners(); // } // isCloseLoading = false; // notifyListeners(); // return response.statusCode; // } catch (error) { // isCloseLoading = false; // stateCode = -1; // notifyListeners(); // return -1; // } // } }