57 lines
1.4 KiB
Dart
57 lines
1.4 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
|
|
class RideSessionModel {
|
|
final String id;
|
|
final String title;
|
|
final DateTime startedAt;
|
|
final DateTime endedAt;
|
|
final int durationSec;
|
|
final double distanceKmRaw;
|
|
|
|
const RideSessionModel({
|
|
required this.id,
|
|
required this.title,
|
|
required this.startedAt,
|
|
required this.endedAt,
|
|
required this.durationSec,
|
|
required this.distanceKmRaw,
|
|
});
|
|
|
|
factory RideSessionModel.fromFirestore(
|
|
DocumentSnapshot<Map<String, dynamic>> doc,
|
|
) {
|
|
final data = doc.data() ?? <String, dynamic>{};
|
|
|
|
return RideSessionModel(
|
|
id: doc.id,
|
|
title: (data['title'] as String?)?.trim().isNotEmpty == true
|
|
? (data['title'] as String).trim()
|
|
: 'Ride Session',
|
|
startedAt: _readDateTime(data['startedAt']) ?? DateTime.now(),
|
|
endedAt: _readDateTime(data['endedAt']) ?? DateTime.now(),
|
|
durationSec: (data['durationSec'] as num?)?.toInt() ?? 0,
|
|
distanceKmRaw: (data['distanceKmRaw'] as num?)?.toDouble() ?? 0.0,
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toFirestoreMap() {
|
|
return <String, dynamic>{
|
|
'title': title,
|
|
'startedAt': startedAt,
|
|
'endedAt': endedAt,
|
|
'durationSec': durationSec,
|
|
'distanceKmRaw': distanceKmRaw,
|
|
};
|
|
}
|
|
|
|
static DateTime? _readDateTime(Object? raw) {
|
|
if (raw is Timestamp) {
|
|
return raw.toDate();
|
|
}
|
|
if (raw is DateTime) {
|
|
return raw;
|
|
}
|
|
return null;
|
|
}
|
|
}
|