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'; class AllRequestsProvider extends ChangeNotifier { bool isAllLoading = false; bool isOpenLoading = false; bool isInProgressLoading = false; bool isCloseLoading = false; bool isOverdueLoading = false; bool isHighPriorityLoading = false; int stateCode; AllRequestsAndCount allRequestsAndCount; AllRequestsAndCount openRequests; AllRequestsAndCount inProgressRequests; AllRequestsAndCount closeRequests; AllRequestsAndCount overdueRequests; AllRequestsAndCount highPriorityRequests; void getRequests() { getHighPriorityRequests(); getOverdueRequests(); getOpenRequests(); getInProgressRequests(); getCloseRequests(); } Future getAllRequests() async { if (isAllLoading == true) return -2; isAllLoading = true; if (allRequestsAndCount == null) notifyListeners(); Response response; try { Map body = { "typeTransaction": [1, 2, 3, 4], "statusTransaction": [1, 2, 3], "priority": [0, 1], "displayData": [] }; response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body); stateCode = response.statusCode; if (response.statusCode >= 200 && response.statusCode < 300) { allRequestsAndCount = AllRequestsAndCount.fromJson(json.decode(response.body)["data"]); notifyListeners(); } isAllLoading = false; notifyListeners(); return response.statusCode; } catch (error) { isAllLoading = false; stateCode = -1; notifyListeners(); return -1; } } Future getHighPriorityRequests() async { if (isHighPriorityLoading == true) return -2; isHighPriorityLoading = true; if (highPriorityRequests == null) notifyListeners(); Response response; try { Map body = { "typeTransaction": [1, 2, 3, 4], "statusTransaction": [1, 2, 3], "priority": [0], "displayData": [] }; response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body); stateCode = response.statusCode; if (response.statusCode >= 200 && response.statusCode < 300) { highPriorityRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"]); print("highPriorityRequests:${highPriorityRequests.total}"); notifyListeners(); } isHighPriorityLoading = false; notifyListeners(); return response.statusCode; } catch (error) { isHighPriorityLoading = false; stateCode = -1; notifyListeners(); return -1; } } Future getOverdueRequests() async { if (isOverdueLoading == true) return -2; isOverdueLoading = true; if (overdueRequests == null) notifyListeners(); Response response; try { Map body = { "typeTransaction": [1, 2, 3, 4], "statusTransaction": [1, 2, 3], "priority": [], "displayData": [1] }; response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body); stateCode = response.statusCode; if (response.statusCode >= 200 && response.statusCode < 300) { overdueRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"]); notifyListeners(); } isOverdueLoading = false; notifyListeners(); return response.statusCode; } catch (error) { isOverdueLoading = false; stateCode = -1; notifyListeners(); return -1; } } Future getOpenRequests() async { if (isOpenLoading == true) return -2; isOpenLoading = true; if (openRequests == null) notifyListeners(); Response response; try { Map body = { "typeTransaction": [1, 2, 3, 4], "statusTransaction": [1], "priority": [], "displayData": [] }; response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body); stateCode = response.statusCode; if (response.statusCode >= 200 && response.statusCode < 300) { openRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"]); notifyListeners(); } isOpenLoading = false; notifyListeners(); return response.statusCode; } catch (error) { isOpenLoading = false; stateCode = -1; notifyListeners(); return -1; } } Future getInProgressRequests() async { if (isInProgressLoading == true) return -2; isInProgressLoading = true; if (inProgressRequests == null) notifyListeners(); Response response; try { Map body = { "typeTransaction": [1, 2, 3, 4], "statusTransaction": [2], "priority": [], "displayData": [] }; response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body); stateCode = response.statusCode; if (response.statusCode >= 200 && response.statusCode < 300) { inProgressRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"]); notifyListeners(); } isInProgressLoading = false; notifyListeners(); return response.statusCode; } catch (error) { isInProgressLoading = false; stateCode = -1; notifyListeners(); return -1; } } 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": [3], "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; } } }