initService function

Future<bool> initService(
  1. BuildContext context,
  2. {bool showLoadingDialog = true}
)

Implementation

Future<bool> initService(
  BuildContext context, {
  bool showLoadingDialog = true,
}) async {
  final SdkProvider sdkProvider = Provider.of(
    context,
    listen: false,
  );

  sdkProvider.canTransact = false;

  if (showLoadingDialog) {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) => const SdkLoadingDialog(),
    );
  }

  final Map? response = await platformRepository.initService(context);

  if (showLoadingDialog) {
    Navigator.pop(context);
  }

  if (response != null) {
    if (response.containsKey(SdkRequiredHardwareEnvironment.gpsIsNeeded)) {
      await showDialog(
        context: context,
        barrierDismissible: false,
        builder: (context) => SdkErrorDialog(
          supportSubject: SdkMailMessages.gpsNeeded,
          title: 'El Gps es necesario',
          description: response[SdkRequiredHardwareEnvironment.gpsIsNeeded],
          buttonText: 'Aceptar',
          icon: Icons.gps_fixed_rounded,
          onPressed: () => Navigator.pop(context),
        ),
      );
      return false;
    } else if (response.containsKey(
      SdkGeneralExceptionsEnvironment.onOSOrDeviceNotCompatible,
    )) {
      await showDialog(
        context: context,
        builder: (context) => SdkErrorDialog(
          supportSubject: SdkMailMessages.soNoCompatible,
          title: '"SO" o dispositivo no compatible',
          description: response[
              SdkGeneralExceptionsEnvironment.onOSOrDeviceNotCompatible],
          buttonText: 'Aceptar',
          icon: Icons.app_blocking_rounded,
          onPressed: () => Navigator.pop(context),
        ),
      );
      return false;
    } else if (response.containsKey(
      SdkGeneralExceptionsEnvironment.onCouldNotConnectService,
    )) {
      await showDialog(
        context: context,
        builder: (context) => SdkErrorDialog(
          supportSubject: SdkMailMessages.couldntConnect,
          title: 'Imposible conectarse',
          description: response[
              SdkGeneralExceptionsEnvironment.onCouldNotConnectService],
          buttonText: 'Aceptar',
          icon: Icons.security_update_warning_rounded,
          onPressed: () => Navigator.pop(context),
        ),
      );
      return false;
    } else if (response.containsKey(
      SdkGeneralExceptionsEnvironment.onOSOrDeviceNotCompatible,
    )) {
      await showDialog(
        context: context,
        builder: (context) => SdkErrorDialog(
          supportSubject: SdkMailMessages.inactiveDevice,
          title: 'Dispositivo no activo',
          description:
              response[SdkGeneralExceptionsEnvironment.isNotActiveTerminal],
          buttonText: 'Aceptar',
          icon: Icons.app_blocking_rounded,
          onPressed: () => Navigator.pop(context),
        ),
      );
      return false;
    } else if (response.containsKey(
      SdkImportantDataEnvironment.deviceId,
    )) {
      sdkProvider.deviceId = response[SdkImportantDataEnvironment.deviceId];
      sdkProvider.canTransact = true;
      return true;
    } else {
      Transitions(
        context: context,
        child: SecurityExceptionPage(
          reason: response,
        ),
        removeUntil: true,
        replacement: true,
      );
      return false;
    }
  } else {
    showSnackbar(
      context,
      message: 'No se puede inicializar el servicio',
    );
    return true;
  }
}