import 'package:flutter/material.dart'; import '../models/message.dart'; import 'package:intl/intl.dart'; class MessageUtils { // Format date for message headers static String formatDateHeader(DateTime date, {String locale = 'id_ID'}) { final now = DateTime.now(); final today = DateTime(now.year, now.month, now.day); final yesterday = today.subtract(const Duration(days: 1)); final messageDate = DateTime(date.year, date.month, date.day); if (messageDate == today) { return 'Hari ini'; } else if (messageDate == yesterday) { return 'Kemarin'; } else { return DateFormat('EEEE, d MMMM y', locale).format(messageDate); } } // Format time for message timestamps static String formatMessageTime(DateTime date, {String locale = 'id_ID'}) { return DateFormat.Hm(locale).format(date); } // Format relative time (e.g., "2 hours ago") static String formatRelativeTime(DateTime date, {String locale = 'id_ID'}) { final now = DateTime.now(); final difference = now.difference(date); if (difference.inSeconds < 60) { return 'Baru saja'; } else if (difference.inMinutes < 60) { return '${difference.inMinutes} menit yang lalu'; } else if (difference.inHours < 24) { return '${difference.inHours} jam yang lalu'; } else if (difference.inDays < 7) { return '${difference.inDays} hari yang lalu'; } else { return DateFormat('d MMM', locale).format(date); } } // Check if a message is expired (older than 30 days) static bool isMessageExpired(Message message) { final cutoffDate = DateTime.now().subtract(const Duration(days: 30)); return message.createdAt.isBefore(cutoffDate); } // Get a color for a user based on their ID (for consistent colors) static Color getUserColor(String userId) { // Generate a color based on the hash of the user ID final hash = userId.hashCode; // List of pleasant colors for user avatars final colors = [ Colors.blue.shade700, Colors.green.shade700, Colors.amber.shade700, Colors.red.shade700, Colors.purple.shade700, Colors.teal.shade700, Colors.pink.shade700, Colors.indigo.shade700, ]; return colors[hash.abs() % colors.length]; } // Extract the first letter of a username for avatar static String getAvatarLetter(String username) { if (username.isEmpty) return '?'; return username[0].toUpperCase(); } // Truncate text with ellipsis static String truncateText(String text, int maxLength) { if (text.length <= maxLength) return text; return '${text.substring(0, maxLength)}...'; } }