Files
fitness/mobile/lib/utils/formatters.dart
T
2026-10-04 00:19:45 +03:00

104 lines
2.8 KiB
Dart

/// أدوات تنسيق الوقت والمسافة والسرعة
class Formatters {
/// Format seconds to HH:MM:SS or MM:SS
static String time(int seconds) {
int h = seconds ~/ 3600;
int m = (seconds % 3600) ~/ 60;
int s = seconds % 60;
String pad(int n) => n.toString().padLeft(2, '0');
if (h > 0) return '${pad(h)}:${pad(m)}:${pad(s)}';
return '${pad(m)}:${pad(s)}';
}
/// Format meters to km or m
static String distance(double meters) {
if (meters >= 1000) {
return '${(meters / 1000).toStringAsFixed(2)} km';
}
return '${meters.toStringAsFixed(0)} m';
}
/// Format distance short (e.g., "5.2")
static String distanceKm(double meters) {
return (meters / 1000).toStringAsFixed(2);
}
/// Format speed (m/s) to pace (min/km)
static String pace(double mps) {
if (mps <= 0) return '--:--';
double kmh = mps * 3.6;
if (kmh <= 0) return '--:--';
double paceMinKm = 60.0 / kmh;
int min = paceMinKm.toInt();
int sec = ((paceMinKm - min) * 60).toInt();
return '${min.toString().padLeft(2, '0')}:${sec.toString().padLeft(2, '0')}';
}
/// Format pace with unit
static String paceWithUnit(double mps) {
return '${pace(mps)} min/km';
}
/// Format speed m/s to km/h
static String speedKmh(double mps) {
return '${(mps * 3.6).toStringAsFixed(1)} km/h';
}
/// Format calories
static String calories(double cal) {
return '${cal.toStringAsFixed(0)} kcal';
}
/// Format elevation
static String elevation(double meters) {
return '${meters.toStringAsFixed(0)} m';
}
/// Format date
static String date(DateTime dt) {
final months = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',
];
return '${dt.day} ${months[dt.month - 1]} ${dt.year}';
}
/// Format time of day
static String timeOfDay(DateTime dt) {
String pad(int n) => n.toString().padLeft(2, '0');
return '${pad(dt.hour)}:${pad(dt.minute)}';
}
/// Format date and time
static String dateTime(DateTime dt) {
return '${date(dt)} ${timeOfDay(dt)}';
}
/// Format duration in human readable form
static String durationHuman(int seconds) {
if (seconds < 60) return '${seconds}s';
if (seconds < 3600) return '${seconds ~/ 60}m ${seconds % 60}s';
int h = seconds ~/ 3600;
int m = (seconds % 3600) ~/ 60;
return '${h}h ${m}m';
}
/// Get workout type display name
static String workoutType(String type) {
switch (type) {
case 'running':
return '🏃 Running';
case 'walking':
return '🚶 Walking';
default:
return type;
}
}
/// Format improvement percentage
static String improvement(double percent) {
String sign = percent >= 0 ? '+' : '';
return '$sign${percent.toStringAsFixed(1)}%';
}
}