removeUserPhone method

  1. @override
Future<void> removeUserPhone(
  1. UserPhone phone, {
  2. UserPassword? password,
  3. ConfirmationCode? confirmation,
})
override

Deletes the given phone from MyUser.phones for the authenticated MyUser.

Implementation

@override
Future<void> removeUserPhone(
  UserPhone phone, {
  UserPassword? password,
  ConfirmationCode? confirmation,
}) async {
  Log.debug(
    'removeUserPhone($phone, password: ${password?.obscured}, confirmation: $confirmation)',
    '$runtimeType',
  );

  if (myUser.value?.phones.unconfirmed == phone) {
    await _debounce(
      field: MyUserField.phone,
      current: () => myUser.value?.phones.unconfirmed,
      saved: () async => (await _active)?.value.phones.unconfirmed,
      value: null,
      mutation: (value, previous) async {
        if (previous != null) {
          return await _graphQlProvider.removeUserPhone(
            previous,
            confirmation: confirmation == null && password == null
                ? null
                : MyUserCredentials(code: confirmation, password: password),
          );
        } else if (value != null) {
          return await _graphQlProvider.addUserPhone(
            value,
            confirmation: confirmation,
          );
        }

        return null;
      },
      update: (v, p) => myUser.update(
        (u) => p != null
            ? u?.phones.unconfirmed = null
            : u?.phones.unconfirmed = v,
      ),
    );
  } else {
    int i = myUser.value?.phones.confirmed.indexOf(phone) ?? -1;

    if (i != -1) {
      myUser.update((u) => u?.phones.confirmed.remove(phone));
    }

    try {
      await _graphQlProvider.removeUserPhone(
        phone,
        confirmation: confirmation == null && password == null
            ? null
            : MyUserCredentials(code: confirmation, password: password),
      );
    } catch (_) {
      if (i != -1) {
        i = min(i, myUser.value?.phones.confirmed.length ?? 0);
        myUser.update((u) => myUser.value?.phones.confirmed.insert(i, phone));
      }
      rethrow;
    }
  }
}