WIP: refactory localization_service

This commit is contained in:
J. A. Messias 2024-10-17 13:02:53 -03:00
parent 88715e8dd6
commit 9f76462e78
1 changed files with 122 additions and 109 deletions

View File

@ -1,4 +1,3 @@
import 'dart:developer';
import 'package:flutter/material.dart';
@ -13,129 +12,87 @@ import '../../utils/log_util.dart';
import '../../utils/snackbar_util.dart';
class LocalizationService {
static Future<void> 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<dynamic> 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<dynamic> 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<bool> processLocals(BuildContext context) async{
static Future<bool> 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<dynamic> 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<dynamic> 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);
}
@ -144,40 +101,22 @@ class LocalizationService {
log('() => catch: $e', stackTrace: s);
return await selectLocal(context);
}
return false;
}
static Future<bool> 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<void> 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: <String, dynamic>{
@ -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<dynamic> locals) {
for (var local in locals) {
final String status = local['CLU_STATUS'];
log('() => CLU_STATUS: $status');
}
}
static bool _isActive(List<dynamic> locals) {
return locals.where((local) => local['CLU_STATUS'] == 'A').isNotEmpty;
}
static bool _isInactived(List<dynamic> locals) {
return locals.where((local) => local['CLI_ID'] != StorageUtil().cliUUID && local['CLU_STATUS'] == 'A').isNotEmpty;
}
static bool _isPending(List<dynamic> 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<bool> _handleUnavailable(BuildContext context, List<dynamic> 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<bool> _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<String, dynamic> 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;
}
}