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.
		
		
		
		
		
			
		
			
				
	
	
		
			113 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			Dart
		
	
			
		
		
	
	
			113 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			Dart
		
	
| import 'dart:convert';
 | |
| 
 | |
| import 'package:flutter/material.dart';
 | |
| import 'package:fluttertoast/fluttertoast.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/extensions/context_extension.dart';
 | |
| 
 | |
| import '../../../models/gas_refill_comments_model.dart';
 | |
| import '../../../new_views/common_widgets/app_lazy_loading.dart';
 | |
| 
 | |
| class GasRefillCommentsProvider extends ChangeNotifier {
 | |
|   // number of items call in each request
 | |
|   final pageItemNumber = 12;
 | |
| 
 | |
|   //reset provider data
 | |
|   void reset() {
 | |
|     comments = [];
 | |
|     nextPage = true;
 | |
|     stateCode = null;
 | |
|   }
 | |
| 
 | |
|   // state code of current request to defied error message
 | |
|   // like 400 customer request failed// 500 service not available
 | |
|   int? stateCode;
 | |
| 
 | |
|   // true if there is next page in product list and false if not
 | |
|   bool nextPage = true;
 | |
| 
 | |
|   // list of user requests
 | |
|   List<GasRefillComment> comments = [];
 | |
| 
 | |
|   // when requests in-process _loading = true
 | |
|   // done _loading = true
 | |
|   // failed _loading = false
 | |
|   bool isLoading = false;
 | |
| 
 | |
|   /// 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
 | |
|   Future<int> getComments({required String callId}) async {
 | |
|     if (isLoading == true) return -2;
 | |
|     isLoading = true;
 | |
|     notifyListeners();
 | |
|     late Response response;
 | |
|     try {
 | |
|       response = await ApiManager.instance.get(URLs.getGazRefillComments + "?gasRefillId=$callId");
 | |
|       stateCode = response.statusCode;
 | |
|       if (response.statusCode >= 200 && response.statusCode < 300) {
 | |
|         List requestsListJson = json.decode(response.body)["data"];
 | |
|         List<GasRefillComment> commentsPage = requestsListJson.map((request) => GasRefillComment.fromJson(request)).toList();
 | |
|         comments ??= [];
 | |
|         comments.addAll(commentsPage);
 | |
|         comments.sort((a, b) {
 | |
|           return b.createdOn!.compareTo(a.createdOn!);
 | |
|         });
 | |
|         if (commentsPage.length == pageItemNumber) {
 | |
|           nextPage = true;
 | |
|         } else {
 | |
|           nextPage = false;
 | |
|         }
 | |
|       }
 | |
|       isLoading = false;
 | |
|       notifyListeners();
 | |
|       return response.statusCode;
 | |
|     } catch (error) {
 | |
|       print(error);
 | |
|       isLoading = false;
 | |
|       stateCode = -1;
 | |
|       notifyListeners();
 | |
|       return -1;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   /// 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
 | |
|   Future<int> addComment(BuildContext context, {required String comment, required int id}) async {
 | |
|     if (isLoading == true) return -2;
 | |
|     isLoading = true;
 | |
|     late Response response;
 | |
|     try {
 | |
|       showDialog(context: context, barrierDismissible: false, builder: (context) => const AppLazyLoading());
 | |
|       response = await ApiManager.instance.post(URLs.addGazRefillComment, body: {"id": 0, "gasRefillId": id, "comment": comment});
 | |
| 
 | |
|       stateCode = response.statusCode;
 | |
|       if (response.statusCode >= 200 && response.statusCode < 300) {
 | |
|         reset(); //visit.status = pentry.ppmVisitStatus;
 | |
|         notifyListeners();
 | |
|         Fluttertoast.showToast(msg: context.translation.commentAdded);
 | |
|         Navigator.of(context).pop();
 | |
|       } else {
 | |
|         Fluttertoast.showToast(msg: "${context.translation.failedToCompleteRequest} ${jsonDecode(response.body)["message"]}");
 | |
|       }
 | |
|       isLoading = false;
 | |
|       notifyListeners();
 | |
|       Navigator.of(context).pop();
 | |
|       return response.statusCode;
 | |
|     } catch (error) {
 | |
|       print(error);
 | |
|       isLoading = false;
 | |
|       stateCode = -1;
 | |
|       notifyListeners();
 | |
|       return -1;
 | |
|     }
 | |
|   }
 | |
| }
 |