From 0be178be6ee20f8d29c243e9bf218645b5521e7b Mon Sep 17 00:00:00 2001 From: Radhika Saliya <74301808+cp-radhika-s@users.noreply.github.com> Date: Fri, 27 Dec 2024 11:55:15 +0530 Subject: [PATCH] Fix journey cache and cleanup (#182) --- app/pubspec.lock | 30 +++++++++--------- data/lib/api/auth/api_user_service.dart | 2 ++ data/lib/repository/journey_repository.dart | 34 ++++++++------------- data/lib/storage/location_caches.dart | 19 +----------- 4 files changed, 31 insertions(+), 54 deletions(-) diff --git a/app/pubspec.lock b/app/pubspec.lock index 8990694e..5b6c1004 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -269,10 +269,10 @@ packages: dependency: transitive description: name: collection - sha256: a1ace0a119f20aabc852d165077c036cd864315bd99b7eaa10a60100341941bf + sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a url: "https://pub.dev" source: hosted - version: "1.19.0" + version: "1.18.0" color: dependency: transitive description: @@ -1169,18 +1169,18 @@ packages: dependency: transitive description: name: leak_tracker - sha256: "7bb2830ebd849694d1ec25bf1f44582d6ac531a57a365a803a6034ff751d2d06" + sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05" url: "https://pub.dev" source: hosted - version: "10.0.7" + version: "10.0.5" leak_tracker_flutter_testing: dependency: transitive description: name: leak_tracker_flutter_testing - sha256: "9491a714cca3667b60b5c420da8217e6de0d1ba7a5ec322fab01758f6998f379" + sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806" url: "https://pub.dev" source: hosted - version: "3.0.8" + version: "3.0.5" leak_tracker_testing: dependency: transitive description: @@ -1577,7 +1577,7 @@ packages: dependency: transitive description: flutter source: sdk - version: "0.0.0" + version: "0.0.99" smooth_page_indicator: dependency: "direct main" description: @@ -1638,10 +1638,10 @@ packages: dependency: transitive description: name: stack_trace - sha256: "9f47fd3630d76be3ab26f0ee06d213679aa425996925ff3feffdec504931c377" + sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b" url: "https://pub.dev" source: hosted - version: "1.12.0" + version: "1.11.1" state_notifier: dependency: transitive description: @@ -1670,10 +1670,10 @@ packages: dependency: transitive description: name: string_scanner - sha256: "688af5ed3402a4bde5b3a6c15fd768dbf2621a614950b17f04626c431ab3c4c3" + sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde" url: "https://pub.dev" source: hosted - version: "1.3.0" + version: "1.2.0" style: dependency: "direct main" description: @@ -1701,10 +1701,10 @@ packages: dependency: transitive description: name: test_api - sha256: "664d3a9a64782fcdeb83ce9c6b39e78fd2971d4e37827b9b06c3aa1edc5e760c" + sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb" url: "https://pub.dev" source: hosted - version: "0.7.3" + version: "0.7.2" time: dependency: transitive description: @@ -1861,10 +1861,10 @@ packages: dependency: transitive description: name: vm_service - sha256: f6be3ed8bd01289b34d679c2b62226f63c0e69f9fd2e50a6b3c1c729a961041b + sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d" url: "https://pub.dev" source: hosted - version: "14.3.0" + version: "14.2.5" watcher: dependency: transitive description: diff --git a/data/lib/api/auth/api_user_service.dart b/data/lib/api/auth/api_user_service.dart index 117ca176..687a1bdf 100644 --- a/data/lib/api/auth/api_user_service.dart +++ b/data/lib/api/auth/api_user_service.dart @@ -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'; @@ -242,6 +243,7 @@ class ApiUserService { } void clearPreference() { + LocationCache.instance.clear(); locationManager.stopTrackingService(); userJsonNotifier.state = null; userSessionJsonNotifier.state = null; diff --git a/data/lib/repository/journey_repository.dart b/data/lib/repository/journey_repository.dart index cd3eb57e..89d63021 100644 --- a/data/lib/repository/journey_repository.dart +++ b/data/lib/repository/journey_repository.dart @@ -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); @@ -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); @@ -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) { @@ -231,6 +226,8 @@ class JourneyRepository { ApiLocationJourney lastKnownJourney, double distance, int duration) async { + final lastFiveLocations = locationCache.getLastFiveLocations(userId); + await journeyService.updateLastLocationJourney( userId, lastKnownJourney.copyWith( @@ -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); @@ -306,6 +306,8 @@ class JourneyRepository { ], ); + LocationManager.instance.updateLocationRequest(false); + journeyService.updateLastLocationJourney(userId, movingJourney); // Save journey for steady user and update cache as well: @@ -319,17 +321,6 @@ class JourneyRepository { locationCache.putLastJourney(newJourney, userId); } - List _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( @@ -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); diff --git a/data/lib/storage/location_caches.dart b/data/lib/storage/location_caches.dart index 63a8cd37..3687c51a 100644 --- a/data/lib/storage/location_caches.dart +++ b/data/lib/storage/location_caches.dart @@ -6,7 +6,6 @@ import 'package:data/api/location/location.dart'; class LocationCache { final Cache _lastJourneyCache; final Cache> _lastFiveLocationCache; - final Cache> _locationCache; static LocationCache? _instance; @@ -17,8 +16,7 @@ class LocationCache { LocationCache({int cacheSize = 5, int locationCacheSize = 200}) : _lastJourneyCache = Cache(cacheSize), - _lastFiveLocationCache = Cache>(cacheSize), - _locationCache = Cache>(locationCacheSize); + _lastFiveLocationCache = Cache>(cacheSize); void putLastJourney(ApiLocationJourney journey, String userId) { _lastJourneyCache.put(userId, journey); @@ -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 getLocations(String userId) { - return _locationCache.get(userId) ?? []; - } - void clear() { _lastJourneyCache.clear(); _lastFiveLocationCache.clear(); - _locationCache.clear(); } }