updateEmotion static method

Future<void> updateEmotion(
  1. String stateId,
  2. String emoji
)

Update emotion for the most recent session of a given state.

Implementation

static Future<void> updateEmotion(String stateId, String emoji) async {
  final box = await _openBox();
  // Find most recent session for this state.
  final entries = box.toMap().entries
      .where((e) => e.key is String && !(e.key as String).startsWith('_'))
      .where((e) {
        final map = e.value;
        return map is Map && map['stateId'] == stateId;
      })
      .toList();
  if (entries.isEmpty) return;

  entries.sort((a, b) {
    final aTime = (a.value as Map)['completedAt'] as int? ?? 0;
    final bTime = (b.value as Map)['completedAt'] as int? ?? 0;
    return bTime.compareTo(aTime);
  });

  final latest = entries.first;
  final updated = Map<String, dynamic>.from(latest.value as Map);
  updated['emotion'] = emoji;
  await box.put(latest.key, updated);
}