deletePhoto method

  1. @action
Future<bool> deletePhoto(
  1. ArticlePhoto data, {
  2. bool isLocalFileToBeDeleted = false,
  3. bool isInternetAvailable = true,
})

handles both sides (memory service and mobx obs photos) + server

Implementation

@action
Future<bool> deletePhoto(ArticlePhoto data,
    {bool isLocalFileToBeDeleted = false,
    bool isInternetAvailable = true}) async {
  /// delete in local db
  final isDeleted =
      await _articlesService.deleteForeverPhotoRpc.request(data);

  /// grap tight, 'cause it's tricky
  /// we do not bother tracking photos update offline, because they are linked with an article
  /// so if a photo create/upsert/deletion fails then we note that

  /// delete in cloud db
  if (isDeleted) {
    if (_gatekeeper.isLinked) {
      if (isInternetAvailable == false) {
        await _addCalibreInQueue(
            data.calibreId, data.dateUTC ?? DateTime.now().toUtc());
      } else {
        // no need to try catch, already handled, throw StatusResponse
        final statusResponse = await _deletePhotoServer(data);
        if (statusResponse.type != StatusResponse_Type.SUCCESS) {
          await _addCalibreInQueue(
              data.calibreId, data.dateUTC ?? DateTime.now().toUtc());
        }
      }
    }
    //
    try {
      if (isLocalFileToBeDeleted) {
        if (await File(data.path).exists()) {
          await File(data.path).delete();
        }
      }
    } on FileSystemException catch (e) {
      print('deletePhoto $e');
    }
    final index = photos
        .indexWhere((e) => e.calibreId == data.calibreId && e.id == data.id);
    if (index != -1) {
      photos.removeAt(index);
    }
    return true;
  } else {
    return false;
  }
}