129 lines
4.5 KiB
Dart
129 lines
4.5 KiB
Dart
import 'package:cheminova/models/place_order_list_model.dart';
|
|
import 'package:cheminova/utils/api_urls.dart';
|
|
import '../models/oder_place_model.dart';
|
|
import '../utils/common_api_service.dart';
|
|
import '../utils/show_snackbar.dart';
|
|
|
|
class GetOrderPlacedService {
|
|
int _currentPage = 1; // Initialize with the first page
|
|
//int _limit = 100; // Start with a fixed limit, can be adjusted
|
|
final List<PlacedOrderList> _allOrders = []; // To store all fetched orders
|
|
bool _hasMoreOrders = true; // To check if there are more orders to fetch
|
|
|
|
// Function to fetch placed orders from the server
|
|
Future<List<PlacedOrderList>?> getPlacedOrders() async {
|
|
try {
|
|
// Continue fetching as long as there are more orders available
|
|
while (_hasMoreOrders) {
|
|
// Construct the API URL with pagination parameters
|
|
String url = "${ApiUrls.getPlacedOrderUrl}?page=$_currentPage";
|
|
// Call the common API service to fetch orders
|
|
final response = await commonApiService<List<PlacedOrderList>>(
|
|
method: "GET",
|
|
url: url,
|
|
fromJson: (json) {
|
|
// Parse the JSON response if it contains the 'plcaedOrders' key
|
|
if (json['plcaedOrders'] != null) {
|
|
// Convert the JSON list to a list of PlacedOrderList objects
|
|
final List<PlacedOrderList> orders = (json['plcaedOrders'] as List)
|
|
.map((orderJson) => PlacedOrderList.fromJson(orderJson as Map<String, dynamic>))
|
|
.toList();
|
|
// Check if the fetched orders list is not empty
|
|
if (orders.isNotEmpty) {
|
|
_allOrders.addAll(orders); // Add the fetched orders to the main list
|
|
_currentPage++; // Increment the page number for the next fetch
|
|
// _limit += orders.length; // Adjust limit based on the number of fetched orders
|
|
} else {
|
|
_hasMoreOrders = false; // Stop fetching if no more orders are returned
|
|
}
|
|
|
|
return orders;
|
|
} else {
|
|
_hasMoreOrders = false; // Stop if there are no orders at all
|
|
return [];
|
|
}
|
|
},
|
|
);
|
|
// Stop fetching if the response is null or empty
|
|
if (response == null || response.isEmpty) {
|
|
_hasMoreOrders = false; // Stop fetching if the response is empty
|
|
}
|
|
}
|
|
// Return all fetched orders after completing the fetch process
|
|
return _allOrders;
|
|
} catch (e) {
|
|
// Show an error message in a snackbar if an exception occurs
|
|
showSnackbar(e.toString());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// Function to reset the pagination and clear the orders list
|
|
void resetPagination() {
|
|
_currentPage = 1;
|
|
//_limit = 100; // Reset the limit to the initial value
|
|
_allOrders.clear(); // Clear the list of orders
|
|
_hasMoreOrders = true; // Reset the flag to allow fetching again
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
// import 'package:cheminova/models/place_order_list_model.dart';
|
|
//
|
|
// import '../models/oder_place_model.dart';
|
|
// import '../utils/common_api_service.dart';
|
|
// import '../utils/show_snackbar.dart';
|
|
//
|
|
// class GetOrderPlacedService {
|
|
// int _currentPage = 1; // Initialize with the first page
|
|
// int _limit = 10; // Fixed limit, you can change this as needed
|
|
// final List<PlacedOrderList> _allOrders = [];
|
|
// int? totalOrders ;
|
|
// Future<List<PlacedOrderList>?> getPlacedOrders() async {
|
|
// try {
|
|
// // Construct the API URL with pagination parameters
|
|
// String url = "/api/get-placed-order-pd?page=$_currentPage&limit=$_limit";
|
|
//
|
|
// final response = await commonApiService<List<PlacedOrderList>>(
|
|
// method: "GET",
|
|
// url: url,
|
|
// fromJson: (json) {
|
|
// if (json['plcaedOrders'] != null) {
|
|
// final List<PlacedOrderList> orders = (json['plcaedOrders'] as List)
|
|
// .map((orderJson) => PlacedOrderList.fromJson(orderJson as Map<String, dynamic>))
|
|
// .toList();
|
|
// // Automatically increase the page number for the next request
|
|
// if (orders.isNotEmpty) {
|
|
// _currentPage++;
|
|
// _limit+= orders.length;
|
|
//
|
|
// }
|
|
// return orders;
|
|
// } else {
|
|
// return [];
|
|
// }
|
|
// },
|
|
// );
|
|
//
|
|
// return response;
|
|
// } catch (e) {
|
|
// showSnackbar(e.toString());
|
|
// return null;
|
|
// }
|
|
// }
|
|
//
|
|
// // Optional: Reset the pagination when needed
|
|
// void resetPagination() {
|
|
// _currentPage = 1;
|
|
// _limit = 100;
|
|
// }
|
|
// }
|