-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(*): use GPS sensor data to set a location field (#2651)
closes #1579
- Loading branch information
Showing
13 changed files
with
266 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/app/features/location/address-gps-location/address-gps-location.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<button | ||
mat-icon-button | ||
(click)="updateLocationFromGps()" | ||
matTooltip="To accurately log your current address, please allow GPS access." | ||
i18n-matTooltip | ||
> | ||
<fa-icon *ngIf="!gpsLoading" icon="location-crosshairs"></fa-icon> | ||
<mat-spinner *ngIf="gpsLoading" diameter="24"></mat-spinner> | ||
</button> |
Empty file.
35 changes: 35 additions & 0 deletions
35
src/app/features/location/address-gps-location/address-gps-location.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { ComponentFixture, TestBed } from "@angular/core/testing"; | ||
|
||
import { AddressGpsLocationComponent } from "./address-gps-location.component"; | ||
import { of } from "rxjs"; | ||
import { GeoService } from "../geo.service"; | ||
import { FontAwesomeTestingModule } from "@fortawesome/angular-fontawesome/testing"; | ||
|
||
describe("AddressGpsLocationComponent", () => { | ||
let component: AddressGpsLocationComponent; | ||
let fixture: ComponentFixture<AddressGpsLocationComponent>; | ||
|
||
let mockGeoService: jasmine.SpyObj<GeoService>; | ||
|
||
beforeEach(async () => { | ||
mockGeoService = jasmine.createSpyObj(["lookup", "reverseLookup"]); | ||
mockGeoService.reverseLookup.and.returnValue( | ||
of({ | ||
error: "Unable to geocode", | ||
} as any), | ||
); | ||
|
||
await TestBed.configureTestingModule({ | ||
imports: [AddressGpsLocationComponent, FontAwesomeTestingModule], | ||
providers: [{ provide: GeoService, useValue: mockGeoService }], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(AddressGpsLocationComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it("should create", () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
src/app/features/location/address-gps-location/address-gps-location.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Component, EventEmitter, Output } from "@angular/core"; | ||
import { Logging } from "app/core/logging/logging.service"; | ||
import { GpsService } from "../gps.service"; | ||
import { MatTooltip } from "@angular/material/tooltip"; | ||
import { FaIconComponent } from "@fortawesome/angular-fontawesome"; | ||
import { MatProgressSpinnerModule } from "@angular/material/progress-spinner"; | ||
import { MatIconButton } from "@angular/material/button"; | ||
import { NgIf } from "@angular/common"; | ||
import { AlertService } from "app/core/alerts/alert.service"; | ||
import { GeoResult, GeoService } from "../geo.service"; | ||
import { firstValueFrom } from "rxjs"; | ||
|
||
@Component({ | ||
selector: "app-address-gps-location", | ||
standalone: true, | ||
imports: [ | ||
MatTooltip, | ||
FaIconComponent, | ||
MatProgressSpinnerModule, | ||
NgIf, | ||
MatTooltip, | ||
MatIconButton, | ||
], | ||
templateUrl: "./address-gps-location.component.html", | ||
styleUrl: "./address-gps-location.component.scss", | ||
}) | ||
export class AddressGpsLocationComponent { | ||
@Output() locationSelected = new EventEmitter<GeoResult>(); | ||
|
||
public gpsLoading = false; | ||
|
||
constructor( | ||
private gpsService: GpsService, | ||
private alertService: AlertService, | ||
private geoService: GeoService, | ||
) {} | ||
|
||
async updateLocationFromGps() { | ||
this.gpsLoading = true; | ||
try { | ||
const location = await this.gpsService.getGpsLocationCoordinates(); | ||
if (location) { | ||
const geoResult: GeoResult = await firstValueFrom( | ||
this.geoService.reverseLookup(location), | ||
); | ||
this.locationSelected.emit(geoResult); | ||
this.alertService.addInfo( | ||
`Selected address based on GPS coordinate lookup as ${geoResult?.display_name}`, | ||
); | ||
} | ||
} catch (error) { | ||
Logging.error(error); | ||
this.alertService.addInfo( | ||
$localize`Failed to access device location. Please check if location permission is enabled in your device settings`, | ||
); | ||
} finally { | ||
this.gpsLoading = false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
export interface Coordinates { | ||
lat: number; | ||
lon: number; | ||
|
||
/** optional accuracy (e.g. from GPS location sensor) */ | ||
accuracy?: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { TestBed } from "@angular/core/testing"; | ||
import { GpsService } from "./gps.service"; | ||
|
||
function mockGeolocationPosition() { | ||
return { | ||
coords: { | ||
latitude: 51.5074, | ||
longitude: -0.1278, | ||
accuracy: 5, | ||
}, | ||
}; | ||
} | ||
|
||
describe("GpsService", () => { | ||
let service: GpsService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(GpsService); | ||
}); | ||
|
||
it("should be created", () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it("should return coordinates if permission is granted", async () => { | ||
spyOn(navigator.permissions, "query").and.returnValue( | ||
Promise.resolve({ state: "granted" } as PermissionStatus), | ||
); | ||
|
||
spyOn(navigator.geolocation, "getCurrentPosition").and.callFake( | ||
(successCallback) => { | ||
successCallback(mockGeolocationPosition() as GeolocationPosition); | ||
}, | ||
); | ||
|
||
const coordinates = await service.getGpsLocationCoordinates(); | ||
expect(coordinates).toEqual({ | ||
lat: 51.5074, | ||
lon: -0.1278, | ||
accuracy: 5, | ||
}); | ||
}); | ||
|
||
it("should throw an error if permission is denied", async () => { | ||
spyOn(navigator.permissions, "query").and.returnValue( | ||
Promise.resolve({ state: "denied" } as PermissionStatus), | ||
); | ||
|
||
await expectAsync( | ||
service.getGpsLocationCoordinates(), | ||
).toBeRejectedWithError( | ||
"GPS permission denied or blocked. Please enable it in your device settings.", | ||
); | ||
}); | ||
|
||
it("should handle error when geolocation is not supported", async () => { | ||
spyOnProperty(navigator, "geolocation").and.returnValue(undefined); | ||
|
||
const coordinates = await service.getGpsLocationCoordinates(); | ||
|
||
expect(coordinates).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Injectable } from "@angular/core"; | ||
import { Coordinates } from "./coordinates"; | ||
|
||
/** | ||
* Access the device's GPS sensor to get the current location. | ||
*/ | ||
@Injectable({ | ||
providedIn: "root", | ||
}) | ||
export class GpsService { | ||
constructor() {} | ||
|
||
async getGpsLocationCoordinates(): Promise<Coordinates> { | ||
if (!("geolocation" in navigator) || !navigator.geolocation) { | ||
return; | ||
} | ||
|
||
const permissionStatus = await navigator.permissions.query({ | ||
name: "geolocation", | ||
}); | ||
|
||
if ( | ||
permissionStatus.state !== "granted" && | ||
permissionStatus.state !== "prompt" | ||
) { | ||
throw new Error( | ||
"GPS permission denied or blocked. Please enable it in your device settings.", | ||
); | ||
} | ||
|
||
const position: GeolocationPosition = await new Promise( | ||
(resolve, reject) => { | ||
navigator.geolocation.getCurrentPosition( | ||
(position) => resolve(position), | ||
(error) => reject(error), | ||
); | ||
}, | ||
); | ||
|
||
return { | ||
lat: position.coords.latitude, | ||
lon: position.coords.longitude, | ||
accuracy: position.coords.accuracy, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters