onTapUpdate method Null safety

void onTapUpdate()

Implementation

void onTapUpdate() async {
  // String username = usernameController.text.trim();
  String email = emailController.text.trim();
  String name = nameController.text.trim();
  String phoneNumber = phoneNumberController.text.trim();
  String birth = birthController.text.trim();
  String oldPassword = oldPasswordController.text.trim();
  String newPassword = newPasswordController.text.trim();
  String newPasswordConfirm = newPasswordConfirmController.text.trim();

  String errTitle = "";
  String errContent = "";

  // if (phoneNumber.isEmpty) {
  //   errTitle = "올바른 핸드폰 번호";
  //   errContent = "올바른 핸드폰 번호를 입력하세요. 계정 인증에 꼭 필요합니다.";
  // }

  // if (Validators.phoneNumberValidator(phoneNumber)) {
  //   phoneNumber = "+82" + phoneNumber.substring(1);
  //   phoneNumber = phoneNumber.replaceAll("-", "");
  // } else {
  //   errTitle = "올바른 핸드폰 번호";
  //   errContent = "올바른 핸드폰 번호를 입력하세요.";
  // }

  if (oldPassword.isNotEmpty) {
    if (newPassword.isEmpty) {
      errTitle = "오류";
      errContent = "새로운 비밀번호를 입력하세요.";
    }
    if (newPassword.length < 8) {
      errTitle = "오류";
      errContent = "새로운 비밀번호는 최소 8자 이상이어야 합니다.";
    }
    if (newPasswordConfirm.isEmpty) {
      errTitle = "오류";
      errContent = "새로운 비밀번호를 입력하세요.";
    }
    if (newPasswordConfirm.length < 8) {
      errTitle = "오류";
      errContent = "새로운 비밀번호 확인은 최소 8자 이상이어야 합니다.";
    }
    if (newPassword != newPasswordConfirm) {
      errTitle = "오류";
      errContent = "새로운 비밀번호와 새로운 비밀번호 확인이 일치하지 않습니다.";
    }
  }

  if (errTitle.isNotEmpty) {
    EventService().eventBus.fire(
          PopupEvent(
            type: PopupType.alert,
            action: PopupAction.show,
            data: {"title": errTitle, "content": errContent},
            onClose: () {
              Navigator.of(ApiService().context, rootNavigator: true).popUntil((route) => !route.settings.name!.startsWith(PopupScreen.routeName));
            },
          ),
        );
    return;
  }

  List<AuthUserAttribute> authUserAttributes = await Amplify.Auth.fetchUserAttributes();
  if (authUserAttributes.indexWhere((element) => (element.userAttributeKey as CognitoUserAttributeKey) == CognitoUserAttributeKey.email) == -1)
    authUserAttributes.add(AuthUserAttribute(userAttributeKey: CognitoUserAttributeKey.email, value: ''));
  if (authUserAttributes.indexWhere((element) => (element.userAttributeKey as CognitoUserAttributeKey) == CognitoUserAttributeKey.name) == -1)
    authUserAttributes.add(AuthUserAttribute(userAttributeKey: CognitoUserAttributeKey.name, value: ''));
  if (authUserAttributes.indexWhere((element) => (element.userAttributeKey as CognitoUserAttributeKey) == CognitoUserAttributeKey.phoneNumber) == -1)
    authUserAttributes.add(AuthUserAttribute(userAttributeKey: CognitoUserAttributeKey.phoneNumber, value: ''));
  if (authUserAttributes.indexWhere((element) => (element.userAttributeKey as CognitoUserAttributeKey) == CognitoUserAttributeKey.birthdate) == -1)
    authUserAttributes.add(AuthUserAttribute(userAttributeKey: CognitoUserAttributeKey.birthdate, value: ''));

  List<AuthUserAttribute> updateUserAttributes = [];
  for (AuthUserAttribute e in authUserAttributes) {
    if ((e.userAttributeKey as CognitoUserAttributeKey) == CognitoUserAttributeKey.name) {
      if (name.isNotEmpty) e = AuthUserAttribute(userAttributeKey: CognitoUserAttributeKey.name, value: name);
    } else if ((e.userAttributeKey as CognitoUserAttributeKey) == CognitoUserAttributeKey.email) {
      if (email.isNotEmpty) e = AuthUserAttribute(userAttributeKey: CognitoUserAttributeKey.email, value: email);
    } else if ((e.userAttributeKey as CognitoUserAttributeKey) == CognitoUserAttributeKey.birthdate) {
      if (birth.isNotEmpty) e = AuthUserAttribute(userAttributeKey: CognitoUserAttributeKey.birthdate, value: birth);
    }
    updateUserAttributes.add(e);
  }

  try {
    await Amplify.Auth.updateUserAttributes(attributes: updateUserAttributes);
    if (oldPassword.isNotEmpty) await Amplify.Auth.updatePassword(oldPassword: oldPassword, newPassword: newPassword);

    if (email.isNotEmpty) currentUser.email = email;
    if (name.isNotEmpty) currentUser.name = name;
    if (phoneNumber.isNotEmpty) currentUser.phoneNumber = phoneNumber;
    if (birth.isNotEmpty) currentUser.birth = DateTime.parse(birth);

    oldPasswordController.text = "";
    newPasswordController.text = "";
    newPasswordConfirmController.text = "";
    EventService().eventBus.fire(PopupEvent(type: PopupType.alert, action: PopupAction.show, data: {"title": "회원정보 수정", "content": "회원정보 수정에 성공하였습니다."}));
  } on AmplifyException catch (e) {
    LogManager().addLog(e.toString(), screen: runtimeType.toString());
    EventService().eventBus.fire(PopupEvent(type: PopupType.alert, action: PopupAction.show, data: {"title": "회원정보 수정", "content": "회원정보 수정에 실패하였습니다."}));
  }
}