reorderContact method

Future<void> reorderContact(
  1. int from,
  2. int to
)

Reorders a ChatContact from the from position to the to position.

Implementation

Future<void> reorderContact(int from, int to) async {
  final List<ContactEntry> favorites =
      contacts
          .where((e) => e.contact.value.favoritePosition != null)
          .toList();

  double position;

  if (to <= 0) {
    position = favorites.first.contact.value.favoritePosition!.val * 2;
  } else if (to >= favorites.length) {
    position = favorites.last.contact.value.favoritePosition!.val / 2;
  } else {
    position =
        (favorites[to].contact.value.favoritePosition!.val +
            favorites[to - 1].contact.value.favoritePosition!.val) /
        2;
  }

  if (to > from) {
    to--;
  }

  final ChatContactId contactId = favorites[from].id;
  favorites.insert(to, favorites.removeAt(from));

  await favoriteContact(contactId, ChatContactFavoritePosition(position));
}