gps_speed.dart 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import 'package:flutter/foundation.dart';
  2. /// Native GPS speed snapshot. Speed is in km/h; a stationary fix is 0.
  3. @immutable
  4. class GpsSpeed {
  5. const GpsSpeed({
  6. this.hasPermission = false,
  7. this.hasFix = false,
  8. this.speedKmh,
  9. this.updatedAt = 0,
  10. });
  11. final bool hasPermission;
  12. final bool hasFix;
  13. final double? speedKmh;
  14. final int updatedAt;
  15. bool isFresh(DateTime now) {
  16. if (!hasFix || speedKmh == null || updatedAt <= 0) return false;
  17. final age = now.millisecondsSinceEpoch - updatedAt;
  18. return age >= 0 && age < 5000;
  19. }
  20. factory GpsSpeed.fromMap(Map<dynamic, dynamic> map) {
  21. return GpsSpeed(
  22. hasPermission: map['hasPermission'] == true,
  23. hasFix: map['hasFix'] == true,
  24. speedKmh: (map['speedKmh'] as num?)?.toDouble(),
  25. updatedAt: (map['updatedAt'] as num?)?.toInt() ?? 0,
  26. );
  27. }
  28. @override
  29. bool operator ==(Object other) {
  30. return other is GpsSpeed &&
  31. other.hasPermission == hasPermission &&
  32. other.hasFix == hasFix &&
  33. other.speedKmh == speedKmh &&
  34. other.updatedAt == updatedAt;
  35. }
  36. @override
  37. int get hashCode => Object.hash(hasPermission, hasFix, speedKmh, updatedAt);
  38. }