showFailure static method

Future<void> showFailure({
  1. required BuildContext context,
  2. required MojoResult? result,
})

Implementation

static Future<void> showFailure(
    {required BuildContext context, required MojoResult? result}) async {
  final AppCenterService _appCenterService = locator<AppCenterService>();
  final MojoInterface _mojo = locator<MojoInterface>();

  var resultToLog = <String, String>{};
  if (result != null) {
    String userId;
    try {
      userId = _mojo.getUserId();
    } catch (e) {
      userId = 'Unable to retrieve User Id';
    }

    resultToLog = {
      'Type': result.resultType.toString(),
      'Message': result.message ?? 'No message',
      'Body': result.body ?? 'No body',
      'User': userId
    };
  }

  await _appCenterService.logEvent('Dialog - Failure', resultToLog);

  String message;
  String dialogTitle = 'Oops!';
  switch (result!.resultType) {
    case ResultType.LoginFailed:
      {
        message =
            'Please check the email address and password you entered and try again.';
        dialogTitle = 'Login Failed';
      }
      break;
    case ResultType.LocalLoginFailed:
      {
        message =
            'Your credentials are out of date, please re-enable biometric login in settings.';
        dialogTitle = 'Login Failed';
      }
      break;
    case ResultType.OutOfSync:
      {
        message =
            'Your session has expired, please log back in and try again';
      }
      break;
    case ResultType.Unauthorized:
      {
        message =
            'Your session has expired, please log back in and try again';
      }
      break;
    default:
      {
        message =
            'There seems to be a problem processing your request, please try again.';
      }
  }

  await showDialog(
    context: context,
    builder: (BuildContext context) => Dialog(
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(20.0))),
      child: Container(
        decoration: BoxDecoration(
            color: Theme.of(context).brightness == Brightness.dark
                ? Theme.of(context).primaryColorDark
                : Theme.of(context).backgroundColor,
            borderRadius: BorderRadius.all(Radius.circular(20.0))),
        height: Screen.heightOf(55, context),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: EdgeInsets.only(top: Screen.heightOf(7, context)),
              child: Icon(
                FontAwesomeIcons.exclamation,
                color: Colors.amber,
                size: 80,
              ),
            ),
            SizedBox(
              height: Screen.heightOf(3, context),
            ),
            Text(
              dialogTitle,
              style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: Screen.heightOf(3, context),
                  color: Theme.of(context).primaryColorDark),
              textAlign: TextAlign.center,
            ),
            SizedBox(
              height: Screen.heightOf(3, context),
            ),
            Padding(
              padding: EdgeInsets.only(
                  left: Screen.widthOf(5, context),
                  right: Screen.widthOf(5, context)),
              child: Text(
                message,
                style: TextStyle(
                    color: Theme.of(context).textTheme.caption?.color),
                textAlign: TextAlign.center,
              ),
            ),
            SizedBox(
              height: Screen.heightOf(3, context),
            ),
            Button(
              text: 'OK',
              onPressed: () {
                Navigator.pop(context);
              },
              minWidth: Screen.widthOf(25, context),
              color: Theme.of(context).primaryColor,
            ),
          ],
        ),
      ),
    ),
  );

  if (result.resultType == ResultType.OutOfSync ||
      result.resultType == ResultType.Unauthorized) {
    var authService = locator<AuthService>();
    await authService.logoutAsync();
    await Navigator.pushNamedAndRemoveUntil(context, '/login', (_) => false);
  }
}