From 9f76462e7888a9da845fd8f540d444d8b863c418 Mon Sep 17 00:00:00 2001 From: "J. A. Messias" Date: Thu, 17 Oct 2024 13:02:53 -0300 Subject: [PATCH] WIP: refactory localization_service --- .../localization/localization_service.dart | 231 +++++++++--------- 1 file changed, 122 insertions(+), 109 deletions(-) diff --git a/lib/shared/services/localization/localization_service.dart b/lib/shared/services/localization/localization_service.dart index d5a5f29c..7fc17118 100644 --- a/lib/shared/services/localization/localization_service.dart +++ b/lib/shared/services/localization/localization_service.dart @@ -1,4 +1,3 @@ - import 'dart:developer'; import 'package:flutter/material.dart'; @@ -13,171 +12,111 @@ import '../../utils/log_util.dart'; import '../../utils/snackbar_util.dart'; class LocalizationService { - - - - static Future checkLocals(BuildContext context) async { try { final GetLocalsCall callback = PhpGroup.getLocalsCall; - var response = await callback.call(); final bool isError = response.jsonBody['error']; if (isError) { - final List locals = response.jsonBody['locais'] ?? []; - final bool isEmpty = locals.isEmpty; - final bool isActive = locals - .where((local) => local['CLU_STATUS'] != 'B') - .toList() - .isNotEmpty; - final bool isEnable = !isEmpty && isActive; - - if (isEnable) { - StorageUtil().haveLocal = true; - StorageUtil().isLogged = true; - await WidgetsBinding.instance.endOfFrame; - StorageUtil().cliUUID = ''; - StorageUtil().ownerUUID = ''; - StorageUtil().context?.go('/homePage'); - } - } else { - final String content = response.jsonBody['error_msg']; - if(isError) StorageUtil().isLogged = false; - if(isError) context.go('/welcomePage'); - return DialogUtil.error(context, content); + _handleError(context, response.jsonBody['error_msg']); + return; } + final List locals = response.jsonBody['locais'] ?? []; + final bool isEmpty = locals.isEmpty; + final bool isActive = locals.where((local) => local['CLU_STATUS'] != 'B').isNotEmpty; + final bool isEnable = !isEmpty && isActive; + if (isEnable) { + StorageUtil().haveLocal = true; + StorageUtil().isLogged = true; + await WidgetsBinding.instance.endOfFrame; + StorageUtil().cliUUID = ''; + StorageUtil().ownerUUID = ''; + StorageUtil().context?.go('/homePage'); + } } catch (e, s) { log(e.toString(), stackTrace: s); } } - static Future processLocals(BuildContext context) async{ + static Future processLocals(BuildContext context) async { try { final GetLocalsCall callback = PhpGroup.getLocalsCall; - final ApiCallResponse response = await callback.call(); - - final bool isError = response.jsonBody['error']; - if(isError) StorageUtil().isLogged = false; - if(isError) context.go('/welcomePage'); - if(isError) DialogUtil.error(context, response.jsonBody['error_msg']); - if(isError) return false; - - - final List locals = response.jsonBody['locais'].toList() ?? []; - for (var local in locals) { - final String status = local['CLU_STATUS']; - log('() => CLU_STATUS: $status'); + if (isError) { + _handleError(context, response.jsonBody['error_msg']); + return false; } + final List locals = response.jsonBody['locais'].toList() ?? []; + _logLocalsStatus(locals); - - final bool isActive = locals.where((local) => local['CLU_STATUS'] == 'A').toList().isNotEmpty; - final bool isInactived = locals.where((local) => local['CLI_ID'] != StorageUtil().cliUUID && local['CLU_STATUS'] == 'A').toList().isNotEmpty; - final bool isPending = locals.where((local) => local['CLU_STATUS'] != 'B' && local['CLU_STATUS'] != 'A').toList().isNotEmpty; + final bool isActive = _isActive(locals); + final bool isInactived = _isInactived(locals); + final bool isPending = _isPending(locals); final bool isUnique = locals.length == 1; final bool isBlocked = locals[0]['CLU_STATUS'] == 'B'; final bool isEnabled = isUnique && isActive; final bool isDisabled = isUnique && isBlocked; - final bool isUnselected = StorageUtil().cliUUID.isEmpty && StorageUtil().cliName.isEmpty & StorageUtil().ownerUUID.isEmpty; - final bool isSelected = StorageUtil().cliUUID.isNotEmpty && StorageUtil().cliName.isNotEmpty && isInactived; + final bool isUnselected = _isUnselected(); + final bool isSelected = _isSelected(isInactived); final bool isUnavailable = isPending && isUnselected && isUnique; - final bool isAvailable = StorageUtil().cliUUID.isNotEmpty && StorageUtil().cliName.isNotEmpty; - - - + final bool isAvailable = _isAvailable(); if (isDisabled) { context.go('/welcomePage'); return true; } else if (isUnavailable) { - log('() => isUnavailable'); - try { - StorageUtil().cliUUID = locals[0]['CLI_ID']; - StorageUtil().ownerUUID = locals[0]['CLU_OWNER_ID']; - var response = await PhpGroup.resopndeVinculo.call(tarefa: 'A'); - if (response.jsonBody['error'] == true) { - StorageUtil().cliUUID = ''; - StorageUtil().cliName = ''; - StorageUtil().ownerUUID = ''; - return false; - } - if (response.jsonBody['error'] == false) return await processData(context).then( (value) => value); - } catch (e, s) { - DialogUtil.errorDefault(context); - LogUtil.requestAPIFailed( - 'responderVinculo.php', '', 'Responder VĂ­nculo', e, s); - return false; - } + return await _handleUnavailable(context, locals); } else if (isEnabled) { - log('() => isEnabled'); - final dynamic local = locals[0]; - StorageUtil().cliName = local['CLI_NOME']; - StorageUtil().userName = local['USU_NOME']; - StorageUtil().cliUUID = local['CLI_ID']; - StorageUtil().ownerUUID = local['CLU_OWNER_ID']; - - return await processData(context); - - + return await _handleEnabled(context, locals[0]); } else if (isUnselected || isAvailable) { - log('() => isUnselected'); return await selectLocal(context); } else if (isSelected) { - log('() => isSelected'); return await processData(context); } else if (isAvailable) { - log('() => isAvailable'); return await processData(context); + } else if (!isUnique && !isActive) { + log('() => not unique and not active'); + return false; + } else if (!isUnique && isBlocked) { + log('() => not unique and blocked'); + return false; + } else if (!isUnique && isPending) { + log('() => not unique and pending'); + return false; + } else if (!isUnique && isInactived) { + log('() => not unique and inactived'); + return false; } else { - log('() => else'); await StorageUtil().ensureInitialization(); return await selectLocal(context); - } + } } catch (e, s) { log('() => stack: $s'); log('() => catch: $e', stackTrace: s); return await selectLocal(context); } - return false; } static Future processData(BuildContext context) async { - try { final GetDadosCall callback = PhpGroup.getDadosCall; - var response = await callback.call(); - final error = response.jsonBody['error']; if (error == false) { - final bool whatsapp = response.jsonBody['whatsapp'] ?? false; - final bool provisional = response.jsonBody['provisional'] ?? false; - final bool pets = response.jsonBody['pet'] ?? false; - final String petAmountRegister = - response.jsonBody['petAmountRegister'].toString().isEmpty - ? '0' - : response.jsonBody['petAmountRegister'].toString(); - final String name = response.jsonBody['visitado']['VDO_NOME']; - StorageUtil().whatsapp = whatsapp; - StorageUtil().provisional = provisional; - StorageUtil().pets = pets; - StorageUtil().petAmountRegister = petAmountRegister; - StorageUtil().userName = name; + _updateStorageUtil(response.jsonBody); return true; - } else if (error == true) { + } else { log('() => error: $error'); DialogUtil.warningDefault(context).whenComplete(() => selectLocal(context)); return false; } - - return false; } catch (e, s) { log('() => stack: $s'); log('() => error: $e', stackTrace: s); @@ -202,7 +141,7 @@ class LocalizationService { child: const BottomArrowLinkedLocalsComponentWidget(), ), ), - ).then((_) async => await processData(context)); + ).then((_) async => await processData(context)); } static Future unlinkLocal(BuildContext context) async { @@ -217,10 +156,7 @@ class LocalizationService { if (value.jsonBody['error'] == false) { StorageUtil().cliName = ''; StorageUtil().cliUUID = ''; - // StorageUtil().ownerUUID = ''; - context.pop(); - context.go( '/homePage', extra: { @@ -231,7 +167,6 @@ class LocalizationService { ), }, ); - SnackBarUtil.showSnackBar(context, content); } }).catchError((err, stack) { @@ -253,5 +188,83 @@ class LocalizationService { } } + static void _handleError(BuildContext context, String errorMsg) { + StorageUtil().isLogged = false; + context.go('/welcomePage'); + DialogUtil.error(context, errorMsg); + } + static void _logLocalsStatus(List locals) { + for (var local in locals) { + final String status = local['CLU_STATUS']; + log('() => CLU_STATUS: $status'); + } + } + + static bool _isActive(List locals) { + return locals.where((local) => local['CLU_STATUS'] == 'A').isNotEmpty; + } + + static bool _isInactived(List locals) { + return locals.where((local) => local['CLI_ID'] != StorageUtil().cliUUID && local['CLU_STATUS'] == 'A').isNotEmpty; + } + + static bool _isPending(List locals) { + return locals.where((local) => local['CLU_STATUS'] != 'B' && local['CLU_STATUS'] != 'A').isNotEmpty; + } + + static bool _isUnselected() { + return StorageUtil().cliUUID.isEmpty && StorageUtil().cliName.isEmpty && StorageUtil().ownerUUID.isEmpty; + } + + static bool _isSelected(bool isInactived) { + return StorageUtil().cliUUID.isNotEmpty && StorageUtil().cliName.isNotEmpty && isInactived; + } + + static bool _isAvailable() { + return StorageUtil().cliUUID.isNotEmpty && StorageUtil().cliName.isNotEmpty; + } + + static Future _handleUnavailable(BuildContext context, List locals) async { + log('() => isUnavailable'); + try { + StorageUtil().cliUUID = locals[0]['CLI_ID']; + StorageUtil().ownerUUID = locals[0]['CLU_OWNER_ID']; + var response = await PhpGroup.resopndeVinculo.call(tarefa: 'A'); + if (response.jsonBody['error'] == true) { + StorageUtil().cliUUID = ''; + StorageUtil().cliName = ''; + StorageUtil().ownerUUID = ''; + return false; + } + if (response.jsonBody['error'] == false) return await processData(context).then((value) => value); + } catch (e, s) { + DialogUtil.errorDefault(context); + LogUtil.requestAPIFailed('responderVinculo.php', '', 'Responder VĂ­nculo', e, s); + return false; + } + return false; + } + + static Future _handleEnabled(BuildContext context, dynamic local) async { + log('() => isEnabled'); + StorageUtil().cliName = local['CLI_NOME']; + StorageUtil().userName = local['USU_NOME']; + StorageUtil().cliUUID = local['CLI_ID']; + StorageUtil().ownerUUID = local['CLU_OWNER_ID']; + return await processData(context); + } + + static void _updateStorageUtil(Map jsonBody) { + final bool whatsapp = jsonBody['whatsapp'] ?? false; + final bool provisional = jsonBody['provisional'] ?? false; + final bool pets = jsonBody['pet'] ?? false; + final String petAmountRegister = jsonBody['petAmountRegister'].toString().isEmpty ? '0' : jsonBody['petAmountRegister'].toString(); + final String name = jsonBody['visitado']['VDO_NOME']; + StorageUtil().whatsapp = whatsapp; + StorageUtil().provisional = provisional; + StorageUtil().pets = pets; + StorageUtil().petAmountRegister = petAmountRegister; + StorageUtil().userName = name; + } } \ No newline at end of file