flutter-freaccess-hub/lib/shared/components/molecules/locals/utils/local_util.dart

136 lines
6.4 KiB
Dart

import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:hub/backend/api_requests/api_calls.dart';
import 'package:hub/flutter_flow/internationalization.dart';
import 'package:hub/shared/components/molecules/locals/index.dart';
import 'package:hub/shared/helpers/storage/base_storage.dart';
import 'package:hub/shared/helpers/storage/storage_helper.dart';
import 'package:hub/shared/utils/dialog_util.dart';
import 'package:hub/shared/utils/log_util.dart';
class LocalUtil {
static void handleError(BuildContext context, String errorMsg) async {
final String devUUID = await StorageHelper().get(KeychainStorageKey.devUUID.value) ?? '';
final String userUUID = await StorageHelper().get(KeychainStorageKey.userUUID.value) ?? '';
final bool isAuthenticated = userUUID.isNotEmpty && devUUID.isNotEmpty;
final bool isDevLinked = !errorMsg.contains('Esse dispositivo nao pertence a esse usuario');
log('() => isLinked: $errorMsg');
if (!isAuthenticated) {
errorMsg = FFLocalizations.of(context).getVariableText(
ptText: 'Erro ao obter credenciais de autenticação',
enText: 'Error getting authentication credentials',
);
await DialogUtil.error(context, errorMsg);
return;
// await DialogUtil.error(context, errorMsg).whenComplete(() async => await AuthenticationService.signOut(context));
}
if (!isDevLinked) {
errorMsg = FFLocalizations.of(context).getVariableText(
ptText: 'Não foi possível vincular o dispositivo, tente novamente.',
enText: 'Unable to link device, try again',
);
await DialogUtil.warning(context, errorMsg);
return;
}
// await DialogUtil.error(context, errorMsg).whenComplete(() async => await LocalsRemoteDataSourceImpl().selectLocal(context, null));
}
static Future<bool> handleUnavailable(BuildContext context, List<dynamic> locals) async {
log('() => isUnavailable');
try {
await StorageHelper().set(KeychainStorageKey.clientUUID.value, locals[0]['CLI_ID']);
await StorageHelper().set(KeychainStorageKey.ownerUUID.value, locals[0]['CLU_OWNER_ID']);
await StorageHelper().set(KeychainStorageKey.clientName.value, locals[0]['CLI_NOME']);
await StorageHelper().set(KeychainStorageKey.ownerName.value, locals[0]['CLU_OWNER_DSC']);
var response = await PhpGroup.resopndeVinculo.call(tarefa: 'A');
if (response.jsonBody['error'] == true) {
await StorageHelper().set(KeychainStorageKey.clientUUID.value, '');
await StorageHelper().set(KeychainStorageKey.ownerUUID.value, '');
await StorageHelper().set(KeychainStorageKey.clientName.value, '');
await StorageHelper().set(KeychainStorageKey.ownerName.value, '');
return false;
}
if (response.jsonBody['error'] == false) return await LocalsRemoteDataSourceImpl().processData(context).then((value) => value);
} catch (e, s) {
await 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');
await StorageHelper().set(KeychainStorageKey.clientUUID.value, local['CLI_ID']);
await StorageHelper().set(KeychainStorageKey.ownerUUID.value, local['CLU_OWNER_ID']);
await StorageHelper().set(KeychainStorageKey.clientName.value, local['CLI_NOME']);
await StorageHelper().set(KeychainStorageKey.ownerName.value, local['CLU_OWNER_DSC']);
await StorageHelper().set(KeychainStorageKey.userName.value, local['USU_NOME']);
return await LocalsRemoteDataSourceImpl().processData(context);
}
static void logLocalsStatus(List<dynamic> locals) {
for (var local in locals) {
final String status = local['CLU_STATUS'];
log('() => CLU_STATUS: $status');
}
}
static Future<bool> updateStorageUtil(Map<String, dynamic> jsonBody) async {
try {
await StorageHelper().set(KeychainStorageKey.whatsapp.value, jsonBody['whatsapp'] ?? false);
await StorageHelper().set(KeychainStorageKey.provisional.value, jsonBody['provisional'] ?? false);
await StorageHelper().set(KeychainStorageKey.pets.value, jsonBody['pet'] ?? false);
await StorageHelper().set(KeychainStorageKey.petAmount.value,
jsonBody['petAmountRegister']?.toString().isEmpty ?? true ? '0' : jsonBody['petAmountRegister'].toString());
await StorageHelper().set(KeychainStorageKey.userName.value, jsonBody['visitado']['VDO_NOME'] ?? '');
await StorageHelper().set(KeychainStorageKey.userEmail.value, jsonBody['visitado']['VDO_EMAIL'] ?? '');
final bool isNewVersion = jsonBody['newVersion'] ?? false;
await StorageHelper().set(KeychainStorageKey.isNewVersion.value, isNewVersion);
return isNewVersion;
} catch (e, s) {
log('Error in _updateStorageUtil: $e', stackTrace: s);
return false;
}
}
static bool isActive(List<dynamic> locals) {
return locals.where((local) => local['CLU_STATUS'] == 'A').isNotEmpty;
}
static Future<bool> isInactived(List<dynamic> locals) async {
String cliUUID = (await StorageHelper().get(KeychainStorageKey.clientUUID.value)) ?? '';
return locals.where((local) => local['CLI_ID'] != 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 Future<bool> isUnselected() async {
String cliUUID = (await StorageHelper().get(KeychainStorageKey.clientUUID.value)) ?? '';
String cliName = (await StorageHelper().get(KeychainStorageKey.clientName.value)) ?? '';
String ownerUUID = (await StorageHelper().get(KeychainStorageKey.ownerUUID.value)) ?? '';
return cliUUID.isEmpty && cliName.isEmpty && ownerUUID.isEmpty;
}
static Future<bool> isSelected(bool isInactived) async {
String cliUUID = (await StorageHelper().get(KeychainStorageKey.clientUUID.value)) ?? '';
String cliName = (await StorageHelper().get(KeychainStorageKey.clientName.value)) ?? '';
return cliUUID.isNotEmpty && cliName.isNotEmpty && isInactived;
}
static Future<bool> isAvailable() async {
String cliUUID = (await StorageHelper().get(KeychainStorageKey.clientUUID.value)) ?? '';
String cliName = (await StorageHelper().get(KeychainStorageKey.clientName.value)) ?? '';
return cliUUID.isNotEmpty && cliName.isNotEmpty;
}
}