import 'package:flutter/material.dart' show Text, EdgeInsets, Colors; import 'package:flutter/widgets.dart'; import 'package:rasadyar_auth/data/models/local/user_local/user_local_model.dart'; import 'package:rasadyar_auth/data/services/token_storage_service.dart'; import 'package:rasadyar_auth/data/utils/safe_call.dart'; import 'package:rasadyar_auth/presentation/routes/pages.dart'; import 'package:rasadyar_chicken/data/models/response/inventory/inventory_model.dart'; import 'package:rasadyar_chicken/data/models/response/kill_house_distribution_info/kill_house_distribution_info.dart'; import 'package:rasadyar_chicken/data/repositories/chicken_repository.dart'; import 'package:rasadyar_chicken/data/repositories/chicken_repository_imp.dart'; import 'package:rasadyar_chicken/presentation/pages/home/view.dart'; import 'package:rasadyar_chicken/presentation/pages/sales_out_of_province/view.dart'; import 'package:rasadyar_core/core.dart'; enum ErrorLocationType { serviceDisabled, permissionDenied, none } class RootLogic extends GetxController { RxInt currentPage = 2.obs; List pages = [ Container(color: Colors.red), SalesOutOfProvincePage(), HomePage(), Container(color: Colors.blue), Container(color: Colors.amber), ]; RxList errorLocationType = RxList(); RxMap inventoryExpandedList = RxMap(); var tokenService = Get.find(); late ChickenRepository chickenRepository; late DioRemote dioRemote; RxInt count = 5.obs; RxList inventoryList = RxList(); Rxn killHouseDistributionInfo = Rxn(); @override void onInit() { super.onInit(); dioRemote = DioRemote(baseUrl: tokenService.baseurl.value); dioRemote.init(); chickenRepository = ChickenRepositoryImpl(dioRemote); getInventory(); getKillHouseDistributionInfo(); } void toggleExpanded(int index) { if (inventoryExpandedList.keys.contains(index)) { inventoryExpandedList.remove(index); } else { inventoryExpandedList[index] = false; } } Future getInventory() async { await safeCall?>( call: () async => await chickenRepository.getInventory(token: tokenService.accessToken.value!), onSuccess: (result) { if (result != null) { iLog(result); inventoryList.clear(); inventoryList.addAll(result); iLog(inventoryList); } }, onError: (error, stackTrace) { switch (error.response?.statusCode) { case 401: _handleGeneric(error); break; case 403: _handleGeneric(error); break; default: _handleGeneric(error); } }, ); } Future getKillHouseDistributionInfo() async { await safeCall( call: () async => await chickenRepository.getIKillHouseDistributionInfo(token: tokenService.accessToken.value!), onSuccess: (result) { if (result != null) { iLog(result); killHouseDistributionInfo.value = result; iLog(killHouseDistributionInfo.value); } }, onError: (error, stackTrace) {}, ); } void _handleGeneric(DioException error) { Get.showSnackbar(_errorSnackBar('اعتبار توکن شما منقضی شده است لطفا دوباره وارد شوید')); tokenService.deleteTokens(); Get.offAllNamed(AuthPaths.auth, arguments: Module.chicken); } GetSnackBar _errorSnackBar(String message) { return GetSnackBar( titleText: Text('خطا', style: AppFonts.yekan14.copyWith(color: Colors.white)), messageText: Text(message, style: AppFonts.yekan12.copyWith(color: Colors.white)), backgroundColor: AppColor.error, margin: EdgeInsets.symmetric(horizontal: 12, vertical: 8), borderRadius: 12, duration: Duration(milliseconds: 3500), snackPosition: SnackPosition.TOP, ); } void changePage(int index) { currentPage.value = index; } }