saveWith method

Future<ISunnyContact> saveWith(
  1. MKey? key,
  2. FutureOr modification(
    1. ISunnyContact input
    )
)
inherited

Implementation

Future<V> saveWith(MKey? key, FutureOr modification(V input)) async {
  final record = get(key) as ObservableRecord<V>;
  final existing = await record.future;
  final cloned = repository.clone(existing);
  await modification(cloned);
  await beforeSave(cloned);

  V result;
  bool updated = true;
  if (cloned.mkey?.mxid == null) {
    result = await repository.create(cloned);
    updated = false;
  } else {
    await repository.update(repository.keyToId(key)!, cloned);
    result = await repository.load(repository.keyToId(key)!);
//      result = cloned;
  }

  if (updated) await afterUpdate(result);
  if (!updated) await afterCreate(result);
  await afterSave(result);
  applyChanges((_) {
    record.update(result, force: true);
    _.change(key, record);
  });

  return result;
}