moveItem method

Future<bool> moveItem(
  1. int? fromIndex,
  2. int? toIndex
)

Implementation

Future<bool> moveItem(int? fromIndex, int? toIndex) async {
  try {
    fromIndex ??= myDataSource?.data?.indexOf(data) ?? 0;
    toIndex ??= myDataSource?.data?.indexOf(data) ?? 0;
    if (fromIndex > toIndex) {
      var index = fromIndex;
      fromIndex = toIndex;
      toIndex = index;
    }
    if (fromIndex < 0) fromIndex = 0;
    if (fromIndex > items.length) fromIndex = items.length;
    if (toIndex < 0) toIndex = 0;
    if (toIndex > items.length) toIndex = items.length;
    if (fromIndex == toIndex) return true;

    // reorder hashmap
    moveInHashmap(items, fromIndex, toIndex);

    // reorder data
    notificationsEnabled = false;
    myDataSource?.move(fromIndex, toIndex, notifyListeners: false);
    data = myDataSource?.data ?? data;
    notificationsEnabled = true;

    // notify
    data = myDataSource?.notify();
  } catch (e) {
    Log().exception(e);
  }
  return true;
}