57 lines
1.7 KiB
Dart
57 lines
1.7 KiB
Dart
import 'package:cheminova/controller/product_mannual_service.dart';
|
|
import 'package:cheminova/controller/product_stock_service.dart';
|
|
import 'package:cheminova/models/product_stock_model.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import '../models/product_mannual_model.dart'; // Your model import
|
|
// Your service import
|
|
|
|
class ProductStockController extends GetxController {
|
|
|
|
var productStockList = <ProductStockModel>[].obs;
|
|
|
|
// Service to fetch data
|
|
final ProductStockService productStockService = ProductStockService();
|
|
|
|
// Loading state
|
|
var isLoading = false.obs;
|
|
|
|
// Method to fetch product manuals from the service
|
|
Future<void>fetchProductStock() async {
|
|
try {
|
|
// Set loading to true
|
|
isLoading.value = true;
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
String? token = prefs.getString('token');
|
|
var manuals = await productStockService.getProductStock(token!);
|
|
|
|
// If data is returned, update the list
|
|
if (manuals != null) {
|
|
productStockList.value = manuals;
|
|
} else {
|
|
productStockList.value = []; // If no data, set an empty list
|
|
}
|
|
} catch (e) {
|
|
// Handle error here, for example logging or showing an error message
|
|
print("Error fetching product stock: $e");
|
|
} finally {
|
|
// Set loading to false
|
|
isLoading.value = false;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
void updateProductInventory(int index, ProductStockModel newInventory) {
|
|
productStockList[index] = newInventory ;
|
|
productStockList.refresh(); // Refresh the list to reflect changes
|
|
}
|
|
@override
|
|
void onInit() {
|
|
// Fetch product manuals when the controller is initialized
|
|
fetchProductStock();
|
|
super.onInit();
|
|
}
|
|
}
|
|
|