Skip to content

Commit

Permalink
Merge branch 'main' into radhi/messages-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-radhika-s committed Dec 27, 2024
2 parents 795f53a + 0be178b commit 4cf765d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 39 deletions.
2 changes: 2 additions & 0 deletions data/lib/api/auth/api_user_service.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:data/api/network/client.dart';
import 'package:data/service/device_service.dart';
import 'package:data/storage/location_caches.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand Down Expand Up @@ -259,6 +260,7 @@ class ApiUserService {
}

void clearPreference() {
LocationCache.instance.clear();
locationManager.stopTrackingService();
userJsonNotifier.state = null;
userSessionJsonNotifier.state = null;
Expand Down
34 changes: 13 additions & 21 deletions data/lib/repository/journey_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ class JourneyRepository {
lastKnownJourney: lastKnownJourney,
userId: userId);

// to get all route position between location a -> b for moving user journey
locationCache.addLocation(extractedLocation, userId);

// Check add add extracted location to last five locations to calculate geometric median
await _checkAndSaveLastFiveLocations(extractedLocation, userId);

Expand Down Expand Up @@ -83,9 +80,6 @@ class JourneyRepository {
_steadyLocationTimer = Timer(const Duration(minutes: 5), () async {
try {
await _saveSteadyLocation(position, userId);
// removing previous journey routes to get latest location route for next journey from start point to end
locationCache.clearLocationCache();
LocationManager.instance.updateLocationRequest(false);
} catch (e, stack) {
logger.e('Error saving steady location for user $userId: $e',
stackTrace: stack);
Expand Down Expand Up @@ -199,8 +193,9 @@ class JourneyRepository {
lastKnownJourney.toLocationFromMovingJourney());
}

final timeDifference = (extractedLocation.timestamp.millisecondsSinceEpoch) -
(lastKnownJourney.update_at ?? 0);
final timeDifference =
(extractedLocation.timestamp.millisecondsSinceEpoch) -
(lastKnownJourney.update_at ?? 0);

if (lastKnownJourney.isSteady()) {
if (distance > MIN_DISTANCE) {
Expand Down Expand Up @@ -231,6 +226,8 @@ class JourneyRepository {
ApiLocationJourney lastKnownJourney,
double distance,
int duration) async {
final lastFiveLocations = locationCache.getLastFiveLocations(userId);

await journeyService.updateLastLocationJourney(
userId,
lastKnownJourney.copyWith(
Expand All @@ -242,7 +239,10 @@ class JourneyRepository {
fromLongitude: lastKnownJourney.from_longitude,
toLatitude: extractedLocation.latitude,
toLongitude: extractedLocation.longitude,
routes: _getRoute(userId),
routes: lastFiveLocations
.map((location) => JourneyRoute(
latitude: location.latitude, longitude: location.longitude))
.toList(),
routeDistance: distance,
routeDuration: duration,
type: JOURNEY_TYPE_MOVING);
Expand Down Expand Up @@ -306,6 +306,8 @@ class JourneyRepository {
],
);

LocationManager.instance.updateLocationRequest(false);

journeyService.updateLastLocationJourney(userId, movingJourney);

// Save journey for steady user and update cache as well:
Expand All @@ -319,17 +321,6 @@ class JourneyRepository {
locationCache.putLastJourney(newJourney, userId);
}

List<JourneyRoute> _getRoute(String userId) {
var locations = locationCache.getLocations(userId);

return locations.map((location) {
return JourneyRoute(
latitude: location.latitude,
longitude: location.longitude,
);
}).toList();
}

bool _isOnlyOneDayChanged(
LocationData? extractedLocation, ApiLocationJourney lastKnownJourney) {
final lastKnownDate = DateTime.fromMillisecondsSinceEpoch(
Expand Down Expand Up @@ -374,7 +365,8 @@ class JourneyRepository {
LocationData extractedLocation,
String userId,
) async {
var lastFiveLocations = locationCache.getLastFiveLocations(userId).take(4).toList();
var lastFiveLocations =
locationCache.getLastFiveLocations(userId).take(4).toList();
lastFiveLocations.insert(0, extractedLocation);

locationCache.putLastFiveLocations(lastFiveLocations, userId);
Expand Down
19 changes: 1 addition & 18 deletions data/lib/storage/location_caches.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:data/api/location/location.dart';
class LocationCache {
final Cache<String, ApiLocationJourney> _lastJourneyCache;
final Cache<String, List<LocationData>> _lastFiveLocationCache;
final Cache<String, List<LocationData>> _locationCache;

static LocationCache? _instance;

Expand All @@ -17,8 +16,7 @@ class LocationCache {

LocationCache({int cacheSize = 5, int locationCacheSize = 200})
: _lastJourneyCache = Cache<String, ApiLocationJourney>(cacheSize),
_lastFiveLocationCache = Cache<String, List<LocationData>>(cacheSize),
_locationCache = Cache<String, List<LocationData>>(locationCacheSize);
_lastFiveLocationCache = Cache<String, List<LocationData>>(cacheSize);

void putLastJourney(ApiLocationJourney journey, String userId) {
_lastJourneyCache.put(userId, journey);
Expand All @@ -36,24 +34,9 @@ class LocationCache {
return _lastFiveLocationCache.get(userId) ?? [];
}

void clearLocationCache() {
_locationCache.clear();
}

void addLocation(LocationData location, String userId) {
var locations = getLocations(userId);
locations.add(location);
_locationCache.put(userId, locations);
}

List<LocationData> getLocations(String userId) {
return _locationCache.get(userId) ?? [];
}

void clear() {
_lastJourneyCache.clear();
_lastFiveLocationCache.clear();
_locationCache.clear();
}
}

Expand Down

0 comments on commit 4cf765d

Please sign in to comment.