Skip to content

Commit

Permalink
pass location null from main
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-ishita-g committed Oct 23, 2024
1 parent 086a784 commit 8b442c6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
7 changes: 5 additions & 2 deletions app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,16 @@ void updateSpaceUserNetworkState(RemoteMessage message, NetworkService networkSe
}
}

void updateCurrentUserState(RemoteMessage message, NetworkService networkService) {
void updateCurrentUserState(RemoteMessage message, NetworkService networkService) async {
final String? userId = message.data[NotificationUpdateStateConst.KEY_USER_ID];
final bool isTypeUpdateState = message.data[NotificationUpdateStateConst.NOTIFICATION_TYPE_UPDATE_STATE];
if (userId != null && isTypeUpdateState) {
networkService.updateUserNetworkState(userId);
}
if (userId != null) journeyRepository.addJourneyOnDayChange(null, userId);
if (userId != null) {
final lastKnownJourney = await journeyRepository.getLastKnownLocation(userId, null);
journeyRepository.addJourneyOnDayChange(null, lastKnownJourney, userId);
}
}

Future<ProviderContainer> _initContainer() async {
Expand Down
28 changes: 15 additions & 13 deletions data/lib/repository/journey_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class JourneyRepository {
_cancelSteadyLocationTimer();
_startSteadyLocationTimer(extractedLocation, userId);

var lastKnownJourney = await _getLastKnownLocation(userId, extractedLocation);
addJourneyOnDayChange(extractedLocation, userId);
var lastKnownJourney = await getLastKnownLocation(userId, extractedLocation);
addJourneyOnDayChange(extractedLocation, lastKnownJourney, userId);

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

Expand Down Expand Up @@ -72,7 +72,7 @@ class JourneyRepository {
}

Future<void> _saveSteadyLocation(LocationData position, String userId) async {
var lastKnownJourney = await _getLastKnownLocation(userId, position);
var lastKnownJourney = await getLastKnownLocation(userId, position);
await _saveJourneyOnJourneyStopped(userId, position, lastKnownJourney, 0);
}

Expand All @@ -84,15 +84,13 @@ class JourneyRepository {
}
}

Future<void> addJourneyOnDayChange(LocationData? extractedLocation,String userId) async {
var lastKnownJourney = locationCache.getLastJourney(userId);
if (lastKnownJourney == null) return;
bool isDayChanged = this.isDayChanged(extractedLocation, lastKnownJourney);
Future<void> addJourneyOnDayChange(LocationData? extractedLocation, ApiLocationJourney lasKnownJourney, String userId) async {
bool isDayChanged = this.isDayChanged(extractedLocation, lasKnownJourney);

if (isDayChanged) {
// Day is changed between last known journey and current location
// Just save again the last known journey in remote database with updated day i.e., current time
await _saveJourneyOnDayChanged(userId, lastKnownJourney);
await _saveJourneyOnDayChanged(userId, lasKnownJourney);
return;
}
}
Expand Down Expand Up @@ -132,8 +130,8 @@ class JourneyRepository {
/// If not available in remote database as well, save extracted location as new location journey
/// with steady state in cache as well as remote database
///
Future<ApiLocationJourney> _getLastKnownLocation(
String userId, LocationData extractedLocation) async {
Future<ApiLocationJourney> getLastKnownLocation(
String userId, LocationData? extractedLocation) async {
var lastKnownJourney = locationCache.getLastJourney(userId);

if (lastKnownJourney != null) {
Expand All @@ -155,12 +153,16 @@ class JourneyRepository {

String newJourneyId = await journeyService.saveCurrentJourney(
userId: userId,
fromLatitude: extractedLocation.latitude,
fromLongitude: extractedLocation.longitude,
fromLatitude: extractedLocation?.latitude ?? 0,
fromLongitude: extractedLocation?.longitude ?? 0,
created_at: DateTime.now().millisecondsSinceEpoch,
);
var locationJourney = ApiLocationJourney.fromPosition(
extractedLocation, userId, newJourneyId);
extractedLocation ??
LocationData(
latitude: 0, longitude: 0, timestamp: DateTime.now()),
userId,
newJourneyId);
locationCache.putLastJourney(locationJourney, userId);
return locationJourney;
}
Expand Down

0 comments on commit 8b442c6

Please sign in to comment.