getDiaryCards method

Future<List<DiaryCard>> getDiaryCards({
  1. int limit = 10,
  2. int offset = 0,
})

Retrieves a paginated list of diary cards from Firestore.

If an error occurs (e.g., offline), it fetches entries from local storage.

Implementation

Future<List<DiaryCard>> getDiaryCards({
  int limit = 10,
  int offset = 0,
}) async {
  try {
    final snapshot =
        await _entryCollection
            .orderBy('date', descending: true)
            .limit(limit + offset)
            .get();

    final docs = snapshot.docs.skip(offset).take(limit);
    List<DiaryCard> cards = [];

    for (var doc in docs) {
      final data = doc.data();
      final id = doc.id;
      final date = DateTime.parse(data['date']);
      final title = data['title'];
      final photoUrls = List<String>.from(data['photoUrls'] ?? []);
      final place = data['location'] ?? '';

      cards.add(
        DiaryCard(
          id: id,
          date: date,
          place: place,
          title: title,
          imageUrl: photoUrls.isNotEmpty ? photoUrls.first : '',
        ),
      );
    }

    return cards;
  } catch (e) {
    return _localDb.getDiaryCardsFromLocalDb(
      userId: _userId,
      limit: limit,
      offset: offset,
    );
  }
}