dashboard_model.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // ignore_for_file: constant_identifier_names, non_constant_identifier_names
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:gozap_package/components/apiservices.dart';
  5. import 'package:gozap_package/models/swap/swap_count_model.dart';
  6. import 'package:gozap_package/models/swap/swap_history_model.dart';
  7. import 'package:gozap_package/models/swap/swap_model.dart';
  8. import 'package:gozap_package/models/user/user_model.dart';
  9. import 'package:provider/provider.dart';
  10. enum DashboardModelStatus {
  11. Ended,
  12. Loading,
  13. Error,
  14. }
  15. class DashboardModel extends ChangeNotifier {
  16. late DashboardModelStatus _status;
  17. late String _errorCode;
  18. late String _errorMessage;
  19. late SwapModule? _swapModule;
  20. String get errorCode => _errorCode;
  21. String get errorMessage => _errorMessage;
  22. DashboardModelStatus get status => _status;
  23. SwapModule? get swapModule => _swapModule;
  24. DashboardModel();
  25. DashboardModel.instance(BuildContext context) {
  26. // ignore: todo
  27. //TODO Add code here
  28. getter(context);
  29. }
  30. Future<void> getter(BuildContext context) async {
  31. _status = DashboardModelStatus.Loading;
  32. notifyListeners();
  33. try {
  34. String UserID = Provider.of<userChangeNotifier>(context, listen: false)
  35. .user!
  36. .UserID
  37. .toString();
  38. var mapData = await APIService(context).postData(
  39. "User/",
  40. {"UserID": UserID},
  41. false);
  42. if (mapData != null) {
  43. UserModule userModule = UserModule.fromJson(mapData["UserData"]);
  44. SwapCountModule remaingSwapModule =
  45. SwapCountModule.fromJson(mapData["SwapData"]["RemainingData"]);
  46. SwapCountModule referSwapModule =
  47. SwapCountModule.fromJson(mapData["SwapData"]["ReferData"]);
  48. SwapCountModule rechargeSwapModule =
  49. SwapCountModule.fromJson(mapData["SwapData"]["RechargeData"]);
  50. SwapCountModule freeSwapModule =
  51. SwapCountModule.fromJson(mapData["SwapData"]["FreeData"]);
  52. List<SwapHistoryModule> swapHistoryList = [];
  53. for (var element in mapData["SwapData"]["HistoryData"]["History"]) {
  54. swapHistoryList.add(SwapHistoryModule.fromJson(element));
  55. }
  56. SwapHistoryMetaModule swapHistoryMetaModule = SwapHistoryMetaModule(
  57. TotalCount: mapData["SwapData"]["HistoryData"]["TotalCount"],
  58. FreeSwapCount: mapData["SwapData"]["HistoryData"]["FreeSwapCount"],
  59. PaidSwapCount: mapData["SwapData"]["HistoryData"]["PaidSwapCount"],
  60. SwapHistoryList: swapHistoryList);
  61. _swapModule = SwapModule(
  62. User: userModule,
  63. RemaingSwap: remaingSwapModule,
  64. ReferSwap: referSwapModule,
  65. RechargeSwap: rechargeSwapModule,
  66. FreeSwap: freeSwapModule,
  67. SwapHistoryMeta: swapHistoryMetaModule);
  68. }
  69. } catch (e) {
  70. _swapModule = null;
  71. if (kDebugMode) {
  72. print(e);
  73. }
  74. }
  75. _status = DashboardModelStatus.Ended;
  76. notifyListeners();
  77. }
  78. void setter() {
  79. _status = DashboardModelStatus.Loading;
  80. notifyListeners();
  81. // ignore: todo
  82. //TODO Add code here
  83. _status = DashboardModelStatus.Ended;
  84. notifyListeners();
  85. }
  86. void update() {
  87. _status = DashboardModelStatus.Loading;
  88. notifyListeners();
  89. // ignore: todo
  90. //TODO Add code here
  91. _status = DashboardModelStatus.Ended;
  92. notifyListeners();
  93. }
  94. void remove() {
  95. _status = DashboardModelStatus.Loading;
  96. notifyListeners();
  97. // ignore: todo
  98. //TODO Add code here
  99. _status = DashboardModelStatus.Ended;
  100. notifyListeners();
  101. }
  102. }