onInit method

  1. @override
void onInit()
override

Called immediately after the widget is allocated in memory. You might use this to initialize something for the controller.

Implementation

@override
void onInit() {
  for (var e in _profiles.values) {
    accounts.add(e);
  }
  accounts.sort(_compareAccounts);

  _profilesSubscription = _profiles.changes.listen((e) async {
    switch (e.op) {
      case OperationKind.added:
        accounts.add(e.value!);
        accounts.sort(_compareAccounts);
        break;

      case OperationKind.removed:
        accounts.removeWhere((u) => u.value.id == e.key);
        break;

      case OperationKind.updated:
        accounts.sort(_compareAccounts);
        break;
    }
  });

  login = TextFieldState(
    onFocus: (s) {
      s.error.value = null;
      password.unsubmit();
    },
    onSubmitted: (s) {
      password.focus.requestFocus();
      s.unsubmit();
    },
  );

  password = TextFieldState(
    onFocus: (s) {
      s.error.value = null;
      s.unsubmit();
    },
    onSubmitted: (_) => signIn(),
  );

  email = TextFieldState(
    onFocus: (s) {
      try {
        if (s.text.isNotEmpty) {
          UserEmail(s.text.toLowerCase());
        }

        s.error.value = null;
      } on FormatException {
        s.error.value = 'err_incorrect_email'.l10n;
      }
    },
    onSubmitted: (s) async {
      if (s.error.value != null) {
        return;
      }

      final UserEmail? email = UserEmail.tryParse(s.text.toLowerCase());

      if (email == null) {
        s.error.value = 'err_incorrect_email'.l10n;
      } else {
        emailCode.clear();
        stage.value = AccountsViewStage.signUpWithEmailCode;
        try {
          await _authService.createConfirmationCode(email: email);
          s.unsubmit();
        } on AddUserEmailException catch (e) {
          s.error.value = e.toMessage();
          _setResendEmailTimer(false);

          stage.value = AccountsViewStage.signUpWithEmail;
        } catch (_) {
          s.resubmitOnError.value = true;
          s.error.value = 'err_data_transfer'.l10n;
          _setResendEmailTimer(false);
          s.unsubmit();

          stage.value = AccountsViewStage.signUpWithEmail;
          rethrow;
        }
      }
    },
  );

  emailCode = TextFieldState(
    onSubmitted: (s) async {
      s.status.value = RxStatus.loading();
      try {
        await _authService.signIn(
          email: UserEmail(email.text),
          code: ConfirmationCode(emailCode.text),
          force: true,
        );

        // TODO: This is a hack that should be removed, as whenever the
        //       account is changed, the [HomeView] and its dependencies must
        //       be rebuilt, which may take some unidentifiable amount of time
        //       as of now.
        router.go(Routes.nowhere);
        await Future.delayed(const Duration(milliseconds: 500));
        router.home();
      } on AddUserEmailException catch (e) {
        switch (e.code) {
          case AddUserEmailErrorCode.wrongCode:
            s.error.value = e.toMessage();

            ++codeAttempts;
            if (codeAttempts >= 3) {
              codeAttempts = 0;
              _setCodeTimer();
            }
            s.status.value = RxStatus.empty();
            break;

          default:
            s.error.value = e.toMessage();
            break;
        }
      } on FormatException catch (_) {
        s.error.value = 'err_wrong_recovery_code'.l10n;
        s.status.value = RxStatus.empty();
        ++codeAttempts;
        if (codeAttempts >= 3) {
          codeAttempts = 0;
          _setCodeTimer();
        }
      } catch (_) {
        s.resubmitOnError.value = true;
        s.error.value = 'err_data_transfer'.l10n;
        s.status.value = RxStatus.empty();
        s.unsubmit();
        rethrow;
      }
    },
  );

  super.onInit();
}