Skip to content

Commit

Permalink
Fix annoying reocurring warnings when editing
Browse files Browse the repository at this point in the history
  • Loading branch information
HarelM committed Nov 7, 2024
1 parent 8c686d7 commit 806fcb3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ export class LocationComponent {
return;
}
this.updateLocationFeatureCollection(location.center, location.accuracy, location.bearing);
if (this.locationSerivce.isFollowing()) {
const selectedRoute = this.selectedRouteService.getSelectedRoute();
if (selectedRoute != null && (selectedRoute.state === "Poi" || selectedRoute.state === "Route")) {
this.toastService.warning(this.resources.editingRouteWhileTracking);
}
}
});

this.store.select((state: ApplicationState) => state.inMemoryState.distance).pipe(takeUntilDestroyed()).subscribe(distance => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ import { Injectable } from "@angular/core";

@Injectable()
export class CancelableTimeoutService {
private idsByGroup: Map<string, number[]>;

constructor() {
this.idsByGroup = new Map<string, number[]>();
}
private idsByGroup = new Map<string, ReturnType<typeof setTimeout>[]>;

public setTimeoutByGroup(action: () => void, timeout: number, type: string) {
if (!this.idsByGroup.has(type)) {
this.idsByGroup.set(type, []);
}
const id = setTimeout(action, timeout);
this.idsByGroup.get(type).push(id as any);
this.idsByGroup.get(type).push(id);
}

public clearTimeoutByGroup(type: string) {
Expand Down
25 changes: 25 additions & 0 deletions IsraelHiking.Web/src/application/services/location.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { DeviceOrientationService } from "./device-orientation.service";
import { FitBoundsService } from "./fit-bounds.service";
import { MapService } from "./map.service";
import { LoggingService } from "./logging.service";
import { ToastService } from "./toast.service";
import { ResourcesService } from "./resources.service";
import { SelectedRouteService } from "./selected-route.service";
import { GpsReducer, SetCurrentPositionAction } from "../reducers/gps.reducer";
import { InMemoryReducer, SetPannedAction } from "../reducers/in-memory.reducer";

Expand Down Expand Up @@ -36,6 +39,9 @@ describe("LocationService", () => {
getZoom: () => 0
}
};
const toastService = {
warning: jasmine.createSpy()
}
TestBed.configureTestingModule({
imports: [NgxsModule.forRoot([InMemoryReducer, GpsReducer])],
providers: [
Expand All @@ -44,6 +50,9 @@ describe("LocationService", () => {
{ provide: FitBoundsService, useValue: fitBoundsService },
{ provide: MapService, useValue: mapService },
{ provide: LoggingService, useValue: { warning: () => {} } },
{ provide: ToastService, useValue: toastService },
{ provide: ResourcesService, useValue: {} },
{ provide: SelectedRouteService, useValue: { getSelectedRoute: jasmine.createSpy().and.returnValue({ state: "Poi" }) } },
LocationService
]
});
Expand Down Expand Up @@ -247,4 +256,20 @@ describe("LocationService", () => {
store.dispatch(new SetPannedAction(new Date()));
expect(service.isFollowing()).toBeFalsy();
}));

it("Should raise a toast when editing and panned changed from true to false", inject([LocationService, Store, ToastService],
(service: LocationService, store: Store, toastService: ToastService) => {
store.reset({
gpsState: {
currentPosition: null,
tracking: "tracking"
},
inMemoryState: { following: true, pannedTimestamp: new Date() }
});
service.initialize();

store.dispatch(new SetPannedAction(null));

expect(toastService.warning).toHaveBeenCalled();
}));
});
11 changes: 11 additions & 0 deletions IsraelHiking.Web/src/application/services/location.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { DeviceOrientationService } from "./device-orientation.service";
import { FitBoundsService } from "./fit-bounds.service";
import { MapService } from "./map.service";
import { LoggingService } from "./logging.service";
import { ResourcesService } from "./resources.service";
import { ToastService } from "./toast.service";
import { SelectedRouteService } from "./selected-route.service";
import { SetFollowingAction, SetPannedAction, ToggleDistanceAction } from "../reducers/in-memory.reducer";
import type { ApplicationState, LatLngAlt } from "../models/models";

Expand All @@ -17,10 +20,13 @@ export type LocationWithBearing = {

@Injectable()
export class LocationService {
private readonly resources: ResourcesService = inject(ResourcesService);
private readonly toastService = inject(ToastService);
private readonly geoLocationService = inject(GeoLocationService);
private readonly deviceOrientationService = inject(DeviceOrientationService);
private readonly fitBoundsService = inject(FitBoundsService);
private readonly mapService = inject(MapService);
private readonly selectedRouteService = inject(SelectedRouteService);
private readonly loggingService = inject(LoggingService);
private readonly store = inject(Store);

Expand Down Expand Up @@ -53,6 +59,7 @@ export class LocationService {
});

this.store.select((state: ApplicationState) => state.inMemoryState.pannedTimestamp).subscribe(pannedTimestamp => {
const wasPanned = this.isPanned;
this.isPanned = pannedTimestamp != null;
if (this.isPanned) {
return;
Expand All @@ -65,6 +72,10 @@ export class LocationService {
}
if (this.isFollowing()) {
this.moveMapToGpsPosition();
const selectedRoute = this.selectedRouteService.getSelectedRoute();
if (wasPanned && selectedRoute != null && (selectedRoute.state === "Poi" || selectedRoute.state === "Route")) {
this.toastService.warning(this.resources.editingRouteWhileTracking);
}
}
});

Expand Down

0 comments on commit 806fcb3

Please sign in to comment.