From 360606242e90c03171371a08d0e02ffe090152d4 Mon Sep 17 00:00:00 2001 From: cp-ishita-g Date: Thu, 12 Dec 2024 15:10:42 +0530 Subject: [PATCH] add type of journey --- app/ios/Runner/LocationManager.swift | 4 - .../timeline/journey_timeline_screen.dart | 4 +- .../location/journey/api_journey_service.dart | 2 + data/lib/api/location/journey/journey.dart | 11 +-- .../api/location/journey/journey.freezed.dart | 77 +++++++++++++++---- data/lib/api/location/journey/journey.g.dart | 2 + data/lib/repository/journey_repository.dart | 14 +++- 7 files changed, 84 insertions(+), 30 deletions(-) diff --git a/app/ios/Runner/LocationManager.swift b/app/ios/Runner/LocationManager.swift index fba5e56d..1b1271f7 100644 --- a/app/ios/Runner/LocationManager.swift +++ b/app/ios/Runner/LocationManager.swift @@ -32,11 +32,9 @@ class LocationsHandler: NSObject, ObservableObject, CLLocationManagerDelegate { func startLocationUpdates() { if self.manager.authorizationStatus == .notDetermined { - print("startLocationUpdates: permission notDetermined") return } updatesStarted = true - print("startLocationUpdates: startUpdatingLocation......") manager.startUpdatingLocation() } @@ -65,8 +63,6 @@ class LocationsHandler: NSObject, ObservableObject, CLLocationManagerDelegate { return } - print("Got location update: \(currentLocation.coordinate.latitude):\(currentLocation.coordinate.longitude)") - lastLocation = currentLocation } diff --git a/app/lib/ui/flow/journey/timeline/journey_timeline_screen.dart b/app/lib/ui/flow/journey/timeline/journey_timeline_screen.dart index 90f09bfc..7102605d 100644 --- a/app/lib/ui/flow/journey/timeline/journey_timeline_screen.dart +++ b/app/lib/ui/flow/journey/timeline/journey_timeline_screen.dart @@ -127,7 +127,7 @@ class _JourneyTimelineScreenState extends ConsumerState { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - journey.isSteadyLocation() + journey.type == JOURNEY_TYPE_STEADY ? _steadyLocationItem( journey, state.sortedJourney.first.id == journey.id, @@ -199,7 +199,7 @@ class _JourneyTimelineScreenState extends ConsumerState { _buildPlaceInfo( location, formattedTime, - journey.isSteadyLocation(), + journey.type == JOURNEY_TYPE_STEADY, steadyDuration, isFirstItem), const SizedBox(height: 16), diff --git a/data/lib/api/location/journey/api_journey_service.dart b/data/lib/api/location/journey/api_journey_service.dart index 01eb71a6..8774cf16 100644 --- a/data/lib/api/location/journey/api_journey_service.dart +++ b/data/lib/api/location/journey/api_journey_service.dart @@ -38,6 +38,7 @@ class ApiJourneyService { int? routeDuration, int? created_at, int? updated_at, + String? type, }) async { final fromLatLng = LatLng(fromLatitude, fromLongitude); final toLatLng = (toLatitude != null && toLongitude != null) @@ -58,6 +59,7 @@ class ApiJourneyService { route_duration: routeDuration, created_at: created_at ?? DateTime.now().millisecondsSinceEpoch, update_at: updated_at ?? DateTime.now().millisecondsSinceEpoch, + type: type ?? (toLatLng == null ? JOURNEY_TYPE_STEADY : JOURNEY_TYPE_MOVING) ); await docRef.set(journey.toJson()); return docRef.id; diff --git a/data/lib/api/location/journey/journey.dart b/data/lib/api/location/journey/journey.dart index 224b364a..c2562655 100644 --- a/data/lib/api/location/journey/journey.dart +++ b/data/lib/api/location/journey/journey.dart @@ -1,3 +1,4 @@ +// ignore_for_file: constant_identifier_names import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:data/api/location/location.dart'; import 'package:data/domain/journey_lat_lng_entension.dart'; @@ -7,6 +8,9 @@ import 'package:google_maps_flutter/google_maps_flutter.dart'; part 'journey.freezed.dart'; part 'journey.g.dart'; +const JOURNEY_TYPE_STEADY = 'steady'; +const JOURNEY_TYPE_MOVING = 'moving'; + @freezed class ApiLocationJourney with _$ApiLocationJourney { const ApiLocationJourney._(); @@ -23,6 +27,7 @@ class ApiLocationJourney with _$ApiLocationJourney { int? route_duration, int? created_at, int? update_at, + String? type, }) = _LocationJourney; factory ApiLocationJourney.fromJson(Map json) => @@ -36,7 +41,7 @@ class ApiLocationJourney with _$ApiLocationJourney { } List toRoute() { - if (isSteadyLocation()) { + if (type == JOURNEY_TYPE_STEADY) { return []; } else { List result = [LatLng(from_latitude, from_longitude)]; @@ -48,10 +53,6 @@ class ApiLocationJourney with _$ApiLocationJourney { } } - bool isSteadyLocation() { - return to_latitude == null && to_longitude == null; - } - LocationData toLocationFromSteadyJourney() { return LocationData(latitude: from_latitude, longitude: from_longitude, timestamp: DateTime.now()); } diff --git a/data/lib/api/location/journey/journey.freezed.dart b/data/lib/api/location/journey/journey.freezed.dart index b9189b66..e40d3d7a 100644 --- a/data/lib/api/location/journey/journey.freezed.dart +++ b/data/lib/api/location/journey/journey.freezed.dart @@ -31,9 +31,14 @@ mixin _$ApiLocationJourney { int? get route_duration => throw _privateConstructorUsedError; int? get created_at => throw _privateConstructorUsedError; int? get update_at => throw _privateConstructorUsedError; + String? get type => throw _privateConstructorUsedError; + /// Serializes this ApiLocationJourney to a JSON map. Map toJson() => throw _privateConstructorUsedError; - @JsonKey(ignore: true) + + /// Create a copy of ApiLocationJourney + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) $ApiLocationJourneyCopyWith get copyWith => throw _privateConstructorUsedError; } @@ -55,7 +60,8 @@ abstract class $ApiLocationJourneyCopyWith<$Res> { double? route_distance, int? route_duration, int? created_at, - int? update_at}); + int? update_at, + String? type}); } /// @nodoc @@ -68,6 +74,8 @@ class _$ApiLocationJourneyCopyWithImpl<$Res, $Val extends ApiLocationJourney> // ignore: unused_field final $Res Function($Val) _then; + /// Create a copy of ApiLocationJourney + /// with the given fields replaced by the non-null parameter values. @pragma('vm:prefer-inline') @override $Res call({ @@ -82,6 +90,7 @@ class _$ApiLocationJourneyCopyWithImpl<$Res, $Val extends ApiLocationJourney> Object? route_duration = freezed, Object? created_at = freezed, Object? update_at = freezed, + Object? type = freezed, }) { return _then(_value.copyWith( id: freezed == id @@ -128,6 +137,10 @@ class _$ApiLocationJourneyCopyWithImpl<$Res, $Val extends ApiLocationJourney> ? _value.update_at : update_at // ignore: cast_nullable_to_non_nullable as int?, + type: freezed == type + ? _value.type + : type // ignore: cast_nullable_to_non_nullable + as String?, ) as $Val); } } @@ -151,7 +164,8 @@ abstract class _$$LocationJourneyImplCopyWith<$Res> double? route_distance, int? route_duration, int? created_at, - int? update_at}); + int? update_at, + String? type}); } /// @nodoc @@ -162,6 +176,8 @@ class __$$LocationJourneyImplCopyWithImpl<$Res> _$LocationJourneyImpl _value, $Res Function(_$LocationJourneyImpl) _then) : super(_value, _then); + /// Create a copy of ApiLocationJourney + /// with the given fields replaced by the non-null parameter values. @pragma('vm:prefer-inline') @override $Res call({ @@ -176,6 +192,7 @@ class __$$LocationJourneyImplCopyWithImpl<$Res> Object? route_duration = freezed, Object? created_at = freezed, Object? update_at = freezed, + Object? type = freezed, }) { return _then(_$LocationJourneyImpl( id: freezed == id @@ -222,6 +239,10 @@ class __$$LocationJourneyImplCopyWithImpl<$Res> ? _value.update_at : update_at // ignore: cast_nullable_to_non_nullable as int?, + type: freezed == type + ? _value.type + : type // ignore: cast_nullable_to_non_nullable + as String?, )); } } @@ -240,7 +261,8 @@ class _$LocationJourneyImpl extends _LocationJourney { this.route_distance, this.route_duration, this.created_at, - this.update_at}) + this.update_at, + this.type}) : _routes = routes, super._(); @@ -276,10 +298,12 @@ class _$LocationJourneyImpl extends _LocationJourney { final int? created_at; @override final int? update_at; + @override + final String? type; @override String toString() { - return 'ApiLocationJourney(id: $id, user_id: $user_id, from_latitude: $from_latitude, from_longitude: $from_longitude, to_latitude: $to_latitude, to_longitude: $to_longitude, routes: $routes, route_distance: $route_distance, route_duration: $route_duration, created_at: $created_at, update_at: $update_at)'; + return 'ApiLocationJourney(id: $id, user_id: $user_id, from_latitude: $from_latitude, from_longitude: $from_longitude, to_latitude: $to_latitude, to_longitude: $to_longitude, routes: $routes, route_distance: $route_distance, route_duration: $route_duration, created_at: $created_at, update_at: $update_at, type: $type)'; } @override @@ -305,10 +329,11 @@ class _$LocationJourneyImpl extends _LocationJourney { (identical(other.created_at, created_at) || other.created_at == created_at) && (identical(other.update_at, update_at) || - other.update_at == update_at)); + other.update_at == update_at) && + (identical(other.type, type) || other.type == type)); } - @JsonKey(ignore: true) + @JsonKey(includeFromJson: false, includeToJson: false) @override int get hashCode => Object.hash( runtimeType, @@ -322,9 +347,12 @@ class _$LocationJourneyImpl extends _LocationJourney { route_distance, route_duration, created_at, - update_at); + update_at, + type); - @JsonKey(ignore: true) + /// Create a copy of ApiLocationJourney + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) @override @pragma('vm:prefer-inline') _$$LocationJourneyImplCopyWith<_$LocationJourneyImpl> get copyWith => @@ -351,7 +379,8 @@ abstract class _LocationJourney extends ApiLocationJourney { final double? route_distance, final int? route_duration, final int? created_at, - final int? update_at}) = _$LocationJourneyImpl; + final int? update_at, + final String? type}) = _$LocationJourneyImpl; const _LocationJourney._() : super._(); factory _LocationJourney.fromJson(Map json) = @@ -380,7 +409,12 @@ abstract class _LocationJourney extends ApiLocationJourney { @override int? get update_at; @override - @JsonKey(ignore: true) + String? get type; + + /// Create a copy of ApiLocationJourney + /// with the given fields replaced by the non-null parameter values. + @override + @JsonKey(includeFromJson: false, includeToJson: false) _$$LocationJourneyImplCopyWith<_$LocationJourneyImpl> get copyWith => throw _privateConstructorUsedError; } @@ -394,8 +428,12 @@ mixin _$JourneyRoute { double get latitude => throw _privateConstructorUsedError; double get longitude => throw _privateConstructorUsedError; + /// Serializes this JourneyRoute to a JSON map. Map toJson() => throw _privateConstructorUsedError; - @JsonKey(ignore: true) + + /// Create a copy of JourneyRoute + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) $JourneyRouteCopyWith get copyWith => throw _privateConstructorUsedError; } @@ -419,6 +457,8 @@ class _$JourneyRouteCopyWithImpl<$Res, $Val extends JourneyRoute> // ignore: unused_field final $Res Function($Val) _then; + /// Create a copy of JourneyRoute + /// with the given fields replaced by the non-null parameter values. @pragma('vm:prefer-inline') @override $Res call({ @@ -457,6 +497,8 @@ class __$$JourneyRouteImplCopyWithImpl<$Res> _$JourneyRouteImpl _value, $Res Function(_$JourneyRouteImpl) _then) : super(_value, _then); + /// Create a copy of JourneyRoute + /// with the given fields replaced by the non-null parameter values. @pragma('vm:prefer-inline') @override $Res call({ @@ -505,11 +547,13 @@ class _$JourneyRouteImpl implements _JourneyRoute { other.longitude == longitude)); } - @JsonKey(ignore: true) + @JsonKey(includeFromJson: false, includeToJson: false) @override int get hashCode => Object.hash(runtimeType, latitude, longitude); - @JsonKey(ignore: true) + /// Create a copy of JourneyRoute + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) @override @pragma('vm:prefer-inline') _$$JourneyRouteImplCopyWith<_$JourneyRouteImpl> get copyWith => @@ -535,8 +579,11 @@ abstract class _JourneyRoute implements JourneyRoute { double get latitude; @override double get longitude; + + /// Create a copy of JourneyRoute + /// with the given fields replaced by the non-null parameter values. @override - @JsonKey(ignore: true) + @JsonKey(includeFromJson: false, includeToJson: false) _$$JourneyRouteImplCopyWith<_$JourneyRouteImpl> get copyWith => throw _privateConstructorUsedError; } diff --git a/data/lib/api/location/journey/journey.g.dart b/data/lib/api/location/journey/journey.g.dart index 68ff00f5..7efeefe8 100644 --- a/data/lib/api/location/journey/journey.g.dart +++ b/data/lib/api/location/journey/journey.g.dart @@ -23,6 +23,7 @@ _$LocationJourneyImpl _$$LocationJourneyImplFromJson( route_duration: (json['route_duration'] as num?)?.toInt(), created_at: (json['created_at'] as num?)?.toInt(), update_at: (json['update_at'] as num?)?.toInt(), + type: json['type'] as String?, ); Map _$$LocationJourneyImplToJson( @@ -39,6 +40,7 @@ Map _$$LocationJourneyImplToJson( 'route_duration': instance.route_duration, 'created_at': instance.created_at, 'update_at': instance.update_at, + 'type': instance.type, }; _$JourneyRouteImpl _$$JourneyRouteImplFromJson(Map json) => diff --git a/data/lib/repository/journey_repository.dart b/data/lib/repository/journey_repository.dart index 52da6342..6cd8da2e 100644 --- a/data/lib/repository/journey_repository.dart +++ b/data/lib/repository/journey_repository.dart @@ -77,7 +77,7 @@ class JourneyRepository { var lastLocation = locationCache.getLastJourney(userId)?.toLocationFromSteadyJourney(); var lastLocationJourney = await getLastKnownLocation(userId, position); - if (lastLocation == null || lastLocationJourney.isSteadyLocation()) { + if (lastLocation == null || lastLocationJourney.type == JOURNEY_TYPE_STEADY) { return; } @@ -96,7 +96,7 @@ class JourneyRepository { Future _saveSteadyLocation(LocationData position, String userId) async { var lastKnownJourney = await getLastKnownLocation(userId, position); - if (lastKnownJourney.isSteadyLocation()) return; + if (lastKnownJourney.type == JOURNEY_TYPE_STEADY) return; await _saveJourneyOnJourneyStopped(userId, position, lastKnownJourney); } @@ -138,6 +138,7 @@ class JourneyRepository { userId: userId, fromLatitude: extractedLocation.latitude, fromLongitude: extractedLocation.longitude, + type: JOURNEY_TYPE_STEADY ); var newJourney = extractedLocation @@ -177,6 +178,7 @@ class JourneyRepository { fromLatitude: extractedLocation?.latitude ?? 0, fromLongitude: extractedLocation?.longitude ?? 0, created_at: DateTime.now().millisecondsSinceEpoch, + type: JOURNEY_TYPE_STEADY ); var locationJourney = extractedLocation!.toLocationJourney(userId, newJourneyId); @@ -195,7 +197,7 @@ class JourneyRepository { var geometricMedian = locations.isNotEmpty ? _geometricMedianCalculation(locations) : null; - double distance = lastKnownJourney.isSteadyLocation() + double distance = lastKnownJourney.type == JOURNEY_TYPE_STEADY ? _distanceBetween(geometricMedian ?? extractedLocation, lastKnownJourney.toLocationFromSteadyJourney()) : _distanceBetween(geometricMedian ?? extractedLocation, @@ -205,7 +207,7 @@ class JourneyRepository { extractedLocation.timestamp.millisecondsSinceEpoch) - (lastKnownJourney.update_at ?? 0); - if (lastKnownJourney.isSteadyLocation()) { + if (lastKnownJourney.type == JOURNEY_TYPE_STEADY) { if (distance > MIN_DISTANCE) { // Here, means last known journey is steady and and now user has started moving // Save journey for moving user and update cache as well: @@ -245,6 +247,7 @@ class JourneyRepository { toLongitude: extractedLocation.longitude, routeDistance: distance, routeDuration: duration, + type: JOURNEY_TYPE_MOVING ); var journey = ApiLocationJourney( @@ -259,6 +262,7 @@ class JourneyRepository { route_duration: duration, created_at: DateTime.now().millisecondsSinceEpoch, update_at: DateTime.now().millisecondsSinceEpoch, + type: JOURNEY_TYPE_MOVING ); locationCache.putLastJourney(journey, userId); @@ -294,6 +298,7 @@ class JourneyRepository { ], created_at: lastKnownJourney.created_at, update_at: DateTime.now().millisecondsSinceEpoch, + type: JOURNEY_TYPE_MOVING ); final lastJourneyUpdatedTime = @@ -339,6 +344,7 @@ class JourneyRepository { fromLatitude: extractedLocation.latitude, fromLongitude: extractedLocation.longitude, created_at: lastKnownJourney.update_at, + type: JOURNEY_TYPE_STEADY ); var steadyJourney = ApiLocationJourney(