// ignore_for_file: constant_identifier_names, non_constant_identifier_names, use_build_context_synchronously import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:gozap_package/components/apiservices.dart'; import 'package:gozap_package/models/admin/admin_model.dart'; import 'package:gozap_package/models/asset/asset_model.dart'; import 'package:gozap_package/models/recharge/recharge_history_model.dart'; import 'package:gozap_package/models/service/service_history_model.dart'; import 'package:gozap_package/models/swap/swap_history_model.dart'; import 'package:gozap_package/models/user/user_model.dart'; import 'package:provider/provider.dart'; enum DashboardModelStatus { Ended, Loading, Error, } class DashboardModel extends ChangeNotifier { late DashboardModelStatus _status; late String _errorCode; late String _errorMessage; late AdminModule? _adminModule; late AssetModule? _swapReturnBattery; late AssetModule? _swapBattery; String _swapReturnBatteryErrorText= ''; String _swapBatteryErrorText= ''; String get errorCode => _errorCode; String get errorMessage => _errorMessage; DashboardModelStatus get status => _status; AdminModule? get adminModule => _adminModule; AssetModule? get swapReturnBattery => _swapReturnBattery; AssetModule? get swapBattery => _swapBattery; String get swapReturnBatteryErrorText => _swapReturnBatteryErrorText; String get swapBatteryErrorText => _swapBatteryErrorText; DashboardModel(); DashboardModel.instance(BuildContext context) { getter(context); } Future getter(BuildContext context) async { _status = DashboardModelStatus.Loading; notifyListeners(); try { String UserID = Provider.of(context, listen: false) .user! .UserID .toString(); var mapData = await APIService(context).postData( "Admin/", {"UserID": UserID}, false); if (mapData != null) { UserModule userModule = UserModule.fromJson(mapData["UserData"]); List assetList = []; List rechargeHistoryList = []; List serviceHistoryList = []; List swapHistoryList = []; for (var element in mapData["SwapData"]["HistoryData"]["History"]) { swapHistoryList.add(SwapHistoryModule.fromJson(element)); } for (var element in mapData["RechargeData"]["HistoryData"]["History"]) { rechargeHistoryList.add(RechargeHistoryModule.fromJson(element)); } for (var element in mapData["ServiceData"]["HistoryData"]["History"]) { serviceHistoryList.add(ServiceHistoryModule.fromJson(element)); } for (var element in mapData["AssetData"]["HistoryData"]["History"]) { assetList.add(AssetModule.fromJson(element)); } SwapHistoryMetaModule swapHistoryMetaModule = SwapHistoryMetaModule( TotalCount: mapData["SwapData"]["HistoryData"]["TotalCount"], FreeSwapCount: mapData["SwapData"]["HistoryData"]["FreeSwapCount"], PaidSwapCount: mapData["SwapData"]["HistoryData"]["PaidSwapCount"], SwapHistoryList: swapHistoryList); RechargeHistoryMetaModule rechargeMetaModule = RechargeHistoryMetaModule( TotalRechargeCount: mapData["RechargeData"]["HistoryData"] ["TotalCount"], RechargeHistoryList: rechargeHistoryList); AssetMetaModule assetMetaModule = AssetMetaModule( TotalAssetCount: mapData["AssetData"]["HistoryData"]["TotalCount"], BatteryAssetCount: 0, //mapData["assetData"]["HistoryData"]["FreeSwapCount"], vehicleAssetCount: 0, //mapData["assetData"]["HistoryData"]["PaidSwapCount"], AssetList: assetList); ServiceHistoryMetaModule serviceHistoryMetaModule = ServiceHistoryMetaModule( TotalCount: mapData["ServiceData"]["HistoryData"]["TotalCount"], ServicedCount: mapData["ServiceData"]["HistoryData"] ["TotalCount"], InCompletedCount: mapData["ServiceData"]["HistoryData"] ["InCompletedServiceCount"], ServiceHistoryList: serviceHistoryList); _adminModule = AdminModule( User: userModule, SwapHistoryMeta: swapHistoryMetaModule, AssetMeta: assetMetaModule, RechargeHistoryMeta: rechargeMetaModule, ServiceHistoryMeta: serviceHistoryMetaModule); _swapReturnBatteryErrorText=""; _swapBatteryErrorText=""; } } catch (e) { _adminModule = null; if (kDebugMode) { print(e); } } _status = DashboardModelStatus.Ended; notifyListeners(); } Future setter(BuildContext context) async { _status = DashboardModelStatus.Loading; notifyListeners(); _status = DashboardModelStatus.Ended; notifyListeners(); } Future recharge(BuildContext context, {Map? data,int SwapRechargeCount=1}) async { _status = DashboardModelStatus.Loading; notifyListeners(); try { var postdata ={ "SwapRechargeCount": SwapRechargeCount.toString(), "PaymentType": data!["PaymentType"].toString(), "RiderID": data["RechargeRiderID"].toString(), }; var resdata = await APIService(context).postData( "Admin/Recharge/add_recharge.php", postdata, false); if (resdata != null) { getter(context); } } catch (e) { if (kDebugMode) { print(e); } } _status = DashboardModelStatus.Ended; notifyListeners(); } Future swap(BuildContext context, {Map? data}) async { _status = DashboardModelStatus.Loading; notifyListeners(); try { var resdata = await APIService(context).postData( "Admin/Swap/", { "SwapBatteryID": data!["SwapBatteryID"], "ReturnBatteryID": data["ReturnBatteryID"], "RiderID": data["RiderID"], }, false); if (resdata != null) { getter(context); } } catch (e) { if (kDebugMode) { print(e); } } _status = DashboardModelStatus.Ended; notifyListeners(); } Future service(BuildContext context, {Map? data}) async { _status = DashboardModelStatus.Loading; notifyListeners(); try { var postdata ={ "AssetID": data!["ServiceAssetID"].toString(), }; var resdata = await APIService(context).postData( "Admin/Service/add_service.php", postdata, false); if (resdata != null) { getter(context); } } catch (e) { if (kDebugMode) { print(e); } } _status = DashboardModelStatus.Ended; notifyListeners(); } Future asset(BuildContext context, {Map? data}) async { _status = DashboardModelStatus.Loading; notifyListeners(); try { var resdata = await APIService(context).postData( "Admin/Asset/add.php", { "AssetID": data!["AssetID"], "AssetName": data["AssetName"], "AssetCategoryID": data["AssetCategoryID"], }, false); if (resdata != null) { getter(context); } } catch (e) { if (kDebugMode) { print(e); } } _status = DashboardModelStatus.Ended; notifyListeners(); } Future checkAssetForReturnBattery(BuildContext context, String AssetID) async { notifyListeners(); try { var resdata = await APIService(context).postData( "Admin/Asset/check_asset.php", { "AssetID": AssetID, }, false); if (resdata != null) { _swapReturnBattery = AssetModule.fromJson(resdata); if (_swapReturnBattery!.IsInServiceCenter) { _swapReturnBatteryErrorText="Not return battery"; }else{ _swapReturnBatteryErrorText=""; } }else{ _swapReturnBatteryErrorText="Asset isn't available"; } } catch (e) { if (kDebugMode) { print(e); } } notifyListeners(); } Future checkAssetForSwapBattery(BuildContext context, String AssetID) async { notifyListeners(); try { var resdata = await APIService(context).postData( "Admin/Asset/check_asset.php", { "AssetID": AssetID, }, false); if (resdata != null) { _swapBattery = AssetModule.fromJson(resdata); if (!_swapBattery!.IsInServiceCenter) { _swapBatteryErrorText="Not swap battery"; }else{ _swapBatteryErrorText=""; } }else{ _swapBatteryErrorText="Asset isn't available"; } } catch (e) { if (kDebugMode) { print(e); } } notifyListeners(); } void update() { _status = DashboardModelStatus.Loading; notifyListeners(); // ignore: todo //TODO Add code here _status = DashboardModelStatus.Ended; notifyListeners(); } void remove() { _status = DashboardModelStatus.Loading; notifyListeners(); // ignore: todo //TODO Add code here _status = DashboardModelStatus.Ended; notifyListeners(); } }