Files
rasadyar_application/packages/chicken/lib/presentation/pages/root/logic.dart

116 lines
3.7 KiB
Dart

import 'package:flutter/material.dart' show Text, EdgeInsets, Colors;
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_core/core.dart';
enum ErrorLocationType { serviceDisabled, permissionDenied, none }
class RootLogic extends GetxController {
RxList<ErrorLocationType> errorLocationType = RxList();
RxMap<int, dynamic> inventoryExpandedList = RxMap();
var tokenService = Get.find<TokenStorageService>();
late ChickenRepository chickenRepository;
late DioRemote dioRemote;
RxInt count = 5.obs;
RxList<InventoryModel> inventoryList = RxList();
Rxn<KillHouseDistributionInfo> killHouseDistributionInfo =
Rxn<KillHouseDistributionInfo>();
@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<void> getInventory() async {
await safeCall<List<InventoryModel>?>(
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<void> getKillHouseDistributionInfo() async {
await safeCall<KillHouseDistributionInfo?>(
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,
);
}
}