createFeed method

Future<void> createFeed({
  1. required PeamanFeed feed,
  2. dynamic onSuccess(
    1. PeamanFeed
    )?,
  3. dynamic onError(
    1. dynamic
    )?,
})

Implementation

Future<void> createFeed({
  required final PeamanFeed feed,
  final Function(PeamanFeed)? onSuccess,
  final Function(dynamic)? onError,
}) async {
  try {
    final _currentMillis = DateTime.now().millisecondsSinceEpoch;

    final _feedRef = PeamanReferenceHelper.feedsCol.doc(feed.id);
    final _myFeedRef = PeamanReferenceHelper.myFeedsCol(
      uid: feed.ownerId!,
    ).doc(_feedRef.id);

    final hashtags = PeamanCommonHelper.getHashtagsFromText(
      text: feed.caption ?? '',
    );

    final _feed = feed.copyWith(
      id: _feedRef.id,
      createdAt: feed.createdAt ?? _currentMillis,
      updatedAt: feed.updatedAt ?? _currentMillis,
      searchKeys: [...hashtags, ...feed.searchKeys],
    );
    final _myFeed = PeamanMyFeed(
      id: _feedRef.id,
      createdAt: _currentMillis,
      updatedAt: _currentMillis,
    );

    final _futures = <Future>[];

    final _createFeedFuture = _feedRef.set(_feed.toJson());
    _futures.add(_createFeedFuture);

    final _myFeedFuture = _myFeedRef.set(_myFeed.toJson());
    _futures.add(_myFeedFuture);

    final _hashTagFuture = addFeedToHashtags(
      feedId: _feed.id!,
      hashtags: hashtags,
    );
    _futures.add(_hashTagFuture);

    final _updatePhotosFuture = _updateUserStatusCount(
      uid: feed.ownerId!,
      feeds: 1,
      photos: feed.files
          .where((element) => element.type == PeamanFileType.image)
          .length,
      videos: feed.files
          .where((element) => element.type == PeamanFileType.video)
          .length,
    );
    _futures.add(_updatePhotosFuture);

    await Future.wait(_futures);
    print('Success: Creating feed ${_feed.id}');
    onSuccess?.call(_feed);
  } catch (e) {
    print(e);
    print('Error!!!: Creating feed');
    onError?.call(e);
  }
}