updateCalibreFromForm<A extends ArticleRawAbstract> method
Future<CalibreWeebi<A> >
updateCalibreFromForm<A extends ArticleRawAbstract>(
- CalibreWeebi<
A> calibre, { - bool isLinkedAndIsInternetAvailable = true,
inherited
Implementation
Future<CalibreWeebi<A>> updateCalibreFromForm<A extends ArticleRawAbstract>(
CalibreWeebi<A> calibre,
{bool isLinkedAndIsInternetAvailable = true}) async {
final now = DateTime.now();
final calibreEdited = calibre.copyWith(
title: name.trim(),
stockUnit: stockUnit,
updateDate: now,
);
if (photoPath != initialPhotoPath) {
if (photoPath.isNotEmpty) {
// check it is not a demo product
if (calibre.creationDate != DatesWeebi.defaultDate) {
final localPhotoFilePath =
photoSaver.copyPhotoFile(calibreEdited.title, photoPath).path;
final newPhoto = ArticlePhoto(
calibreId: calibreEdited.id,
id: 0,
path: localPhotoFilePath,
source: PhotoSource.file,
dateUTC: now.toUtc());
try {
await _articlesStore.upsertPhoto(newPhoto,
isInternetAvailable: isLinkedAndIsInternetAvailable);
} catch (e) {
print(e);
}
}
} else {
// photo has been removed
print('in the delete photo');
if (_articlesStore.photos
.any((p) => p.calibreId == calibreEdited.id && p.id == 0)) {
final photo = _articlesStore.photos.firstWhereOrNull(
(p) => p.calibreId == calibreEdited.id && p.id == 0);
if (photo != null) {
await _articlesStore.deletePhoto(photo, isInternetAvailable: false);
}
}
}
}
final temp = await _articlesStore.updateCalibre<A>(calibreEdited);
for (final categoryStore in _articlesStore.categories) {
// calibre was included in category
if (categoryStore.calibresIds.contains(calibre.id)) {
// but removed in update view so we need to update category
if (categories.none((c) => c.title == categoryStore.title)) {
final calibreIds = categoryStore.calibresIds..remove(calibre.id);
final c = categoryStore.copyWith(calibresIds: calibreIds);
await _articlesStore.updateCategory(c);
}
// not changed do nothing
}
// add the ones added in update view
}
if (categories.isNotEmpty) {
for (final category in categories) {
final calibreIds = category.calibresIds..add(calibre.id);
final c = category.copyWith(calibresIds: calibreIds);
await _articlesStore.updateCategory(c);
}
}
return temp;
}