tm-android-app/lib/provider/product_provider.dart
2024-10-17 13:39:27 +05:30

102 lines
2.6 KiB
Dart

import 'package:cheminova/constants/constant.dart';
import 'package:cheminova/models/product_model.dart';
import 'package:cheminova/services/api_client.dart';
import 'package:cheminova/services/api_urls.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
class ProductProvider extends ChangeNotifier {
ProductProvider() {
getProducts();
}
final _apiClient = ApiClient();
ProductResponse? productResponse;
List<Product> productList = [];
final List<Product> _selectedProducts = [];
bool _isLoading = false;
bool get isLoading => _isLoading;
List<Product> get selectedProducts => _selectedProducts;
void setLoading(bool loading) {
_isLoading = loading;
notifyListeners();
}
Future<void> getProducts() async {
setLoading(true);
try {
Response response = await _apiClient.get(ApiUrls.getProducts);
setLoading(false);
if (response.statusCode == 200) {
productResponse = ProductResponse.fromJson(response.data);
productList = productResponse!.products;
notifyListeners();
}
} catch (e) {
setLoading(false);
String error = "Something went wrong";
if (e is DioException) {
error = e.response!.data['message'] ?? "Something went wrong";
}
ScaffoldMessenger.of(
navigatorKey.currentContext!,
).showSnackBar(
SnackBar(
content: Text(
error,
),
),
);
}
}
Future<bool> submitSelectedProducts(
String addedFor, String addedForId) async {
setLoading(true);
try {
final data = {
"products": selectedProducts.map((product) {
return {
"SKU": product.SKU,
"ProductName": product.name,
"Sale": product.sale,
"Inventory": product.inventory,
"productid": product.id,
};
}).toList(),
"addedFor": addedFor,
"addedForId": addedForId,
};
Response response =
await _apiClient.post(ApiUrls.submitProducts, data: data);
setLoading(false);
if (response.statusCode == 201) {
return true;
} else {
return false;
}
} catch (e) {
setLoading(false);
String error = "Something went wrong";
if (e is DioException) {
error = e.response!.data['message'] ?? "Something went wrong";
}
ScaffoldMessenger.of(
navigatorKey.currentContext!,
).showSnackBar(
SnackBar(
content: Text(
error,
),
),
);
return false;
}
}
}