Skip to content

Commit

Permalink
Improve public POI performance and allow offline usage #1169 - Fix li…
Browse files Browse the repository at this point in the history
…nt and tests
  • Loading branch information
HarelM committed Jun 1, 2020
1 parent 013d576 commit c157113
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export class LayersSidebarComponent extends BaseMapComponent {
public lastModified: Observable<Date>;

constructor(resources: ResourcesService,
private readonly dialog: MatDialog,
private readonly httpClient: HttpClient,
private readonly dialog: MatDialog,
private readonly httpClient: HttpClient,
private readonly purchaseService: PurchaseService,
private readonly layersService: LayersService,
private readonly selectedRouteService: SelectedRouteService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class DatabaseService {

public async getPoisForClustering(): Promise<GeoJSON.Feature<GeoJSON.Point>[]> {
this.loggingService.debug("Getting POIs for clutering from DB");
let features = await this.poisDatabase.table(DatabaseService.POIS_TABLE_NAME).toArray()
let features = await this.poisDatabase.table(DatabaseService.POIS_TABLE_NAME).toArray();
let slimPois = features.map((feature: GeoJSON.Feature) => {
let geoLocation = feature.properties.poiGeolocation;
let slimFeature = {
Expand All @@ -226,10 +226,10 @@ export class DatabaseService {
properties: feature.properties
} as GeoJSON.Feature<GeoJSON.Point>;
slimFeature.properties.poiHasExtraData = {};

for (let language of Object.keys(slimFeature.properties.poiNames)) {
slimFeature.properties.poiHasExtraData[language] = (slimFeature.properties["description:" + language] != null)
|| Object.keys(slimFeature.properties).find(k => k.startsWith("image")) != null
|| Object.keys(slimFeature.properties).find(k => k.startsWith("image")) != null;
}
return slimFeature;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { ToastService } from "./toast.service";
import { GeoJsonParser } from "./geojson.parser";
import { SQLite } from "@ionic-native/sqlite/ngx";
import { Urls } from "../urls";
import { PointOfInterestExtended, ApplicationState } from "../models/models";
import { PointOfInterestExtended } from "../models/models";
import { NgReduxTestingModule, MockNgRedux } from "@angular-redux/store/testing";

describe("Poi Service", () => {
Expand All @@ -25,6 +25,9 @@ describe("Poi Service", () => {
let toastMock = new ToastServiceMockCreator();
let hashService = {};
let fileServiceMock = {};
let databaseServiceMock = {
getPoisForClustering: () => Promise.resolve([])
};
TestBed.configureTestingModule({
imports: [
HttpClientModule,
Expand All @@ -34,13 +37,13 @@ describe("Poi Service", () => {
providers: [
{ provide: ResourcesService, useValue: toastMock.resourcesService },
{ provide: HashService, useValue: hashService },
{ provide: ToastService, useValue: null },
{ provide: ToastService, useValue: toastMock.toastService },
{ provide: FileService, useValue: fileServiceMock },
{ provide: DatabaseService, useValue: databaseServiceMock },
GeoJsonParser,
RunningContextService,
WhatsAppService,
PoiService,
DatabaseService,
LoggingService,
Device,
SQLite
Expand All @@ -62,15 +65,13 @@ describe("Poi Service", () => {
let promise = poiService.initialize();
mockBackend.match(r => r.url.startsWith(Urls.poiCategories)).forEach(t => t.flush([{ icon: "icon", name: "category" }]));
await new Promise((resolve) => setTimeout(resolve, 100)); // this is in order to let the code continue to run to the next await
mockBackend.match(r => r.url === Urls.slimGeoJSON)[0].flush({ type: "FeatureCollection", features: [] });
// mockBackend.match(r => r.url === Urls.slimGeoJSON)[0].flush({ type: "FeatureCollection", features: [] });

await promise;

expect(changed).toBe(true);
})));



it("Should get a point by id and source from the server", (inject([PoiService, HttpTestingController],
async (poiService: PoiService, mockBackend: HttpTestingController) => {

Expand Down
29 changes: 15 additions & 14 deletions IsraelHiking.Web/sources/application/services/poi.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ export class PoiService {
private categoriesGroups: Observable<CategoriesGroup[]>;

constructor(private readonly resources: ResourcesService,
private readonly httpClient: HttpClient,
private readonly whatsappService: WhatsAppService,
private readonly hashService: HashService,
private readonly databaseService: DatabaseService,
private readonly runningContextService: RunningContextService,
private readonly geoJsonParser: GeoJsonParser,
private readonly loggingService: LoggingService,
private readonly toastService: ToastService,
private readonly ngRedux: NgRedux<ApplicationState>
private readonly httpClient: HttpClient,
private readonly whatsappService: WhatsAppService,
private readonly hashService: HashService,
private readonly databaseService: DatabaseService,
private readonly runningContextService: RunningContextService,
private readonly geoJsonParser: GeoJsonParser,
private readonly loggingService: LoggingService,
private readonly toastService: ToastService,
private readonly ngRedux: NgRedux<ApplicationState>
) {
this.poisCache = [];
this.poisChanged = new EventEmitter();
Expand All @@ -88,7 +88,7 @@ export class PoiService {
this.resources.languageChanged.subscribe(() => this.updatePois());
this.categoriesGroups.subscribe(() => this.updatePois());
await this.syncCategories();
await this.rebuildPois()
await this.rebuildPois();
this.toastService.progress({
action: this.downloadPOIs
});
Expand All @@ -98,7 +98,7 @@ export class PoiService {
this.poisGeojson.features = await this.databaseService.getPoisForClustering();
for (let feature of this.poisGeojson.features) {
let language = this.resources.getCurrentLanguageCodeSimplified();
if (!feature.properties.poiNames[language] || feature.properties.poiNames[language].length == 0) {
if (!feature.properties.poiNames[language] || feature.properties.poiNames[language].length === 0) {
continue;
}
for (let name of feature.properties.poiNames[language]) {
Expand Down Expand Up @@ -135,7 +135,7 @@ export class PoiService {
await this.rebuildPois();
this.loggingService.info(`Updated pois for clustering: ${this.poisGeojson.features.length}`);
progressCallback(100, "All set, POIS are up-to-date");

// HM TODO: get images? get pois.ihm file?

} catch (ex) {
Expand All @@ -148,7 +148,7 @@ export class PoiService {
if (!ids) {
return [];
}
let results = []
let results = [];
for (let id of uniq(ids)) {
let feature = await this.databaseService.getPoiById(id);
let point = this.featureToPoint(feature);
Expand All @@ -157,7 +157,8 @@ export class PoiService {
return results;
}

private getUpdatesWithProgress(lastModifiedString: string, progressCallback: Function): Promise<GeoJSON.Feature<GeoJSON.Geometry>[]> {
private getUpdatesWithProgress(lastModifiedString: string, progressCallback: (value: number) => void)
: Promise<GeoJSON.Feature<GeoJSON.Geometry>[]> {
return new Promise((resolve, reject) => {
this.httpClient.get(Urls.poiUpdates + lastModifiedString, {
observe: "events",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { HttpClientTestingModule, HttpTestingController } from "@angular/common/

import { SearchResultsProvider, ISearchResultsPointOfInterest } from "./search-results.provider";
import { GeoJsonParser } from "./geojson.parser";
import { RunningContextService } from "./running-context.service";
import { Device } from "@ionic-native/device/ngx";
import { PoiService } from "./poi.service";

describe("SearchResultsProvider", () => {
beforeEach(() => {
Expand All @@ -14,7 +17,9 @@ describe("SearchResultsProvider", () => {
],
providers: [
GeoJsonParser,
SearchResultsProvider
SearchResultsProvider,
{ provide: RunningContextService, useValue: { isOnline: true } },
{ provide: PoiService, useValue: null }
]
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export interface ISearchResultsPointOfInterest extends PointOfInterestExtended {
export class SearchResultsProvider {

constructor(private readonly httpClient: HttpClient,
private readonly runningContextService: RunningContextService,
private readonly poiService: PoiService) {
private readonly runningContextService: RunningContextService,
private readonly poiService: PoiService) {
}

public getResults = async (searchTerm: string, isHebrew: boolean): Promise<ISearchResultsPointOfInterest[]> => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MatSnackBar } from "@angular/material";
import { MatSnackBar, MatDialog } from "@angular/material";
import { ToastService } from "./toast.service";
import { ResourcesService } from "./resources.service";
import { GetTextCatalogMockCreator } from "./resources.service.spec";
Expand All @@ -8,8 +8,9 @@ export class ToastServiceMockCreator {
public resourcesService: ResourcesService;
constructor() {
let snackBar = { open: () => null } as any as MatSnackBar;
let matDialog = { open: () => null } as any as MatDialog;
this.resourcesService = new ResourcesService(new GetTextCatalogMockCreator().getTextCatalogService);
this.toastService = new ToastService(this.resourcesService, snackBar);
this.toastService = new ToastService(this.resourcesService, matDialog, snackBar);
}
}

Expand Down
1 change: 0 additions & 1 deletion IsraelHiking.Web/sources/application/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export class Urls {
public static readonly baseTilesAddress = environment.baseTilesAddress;
public static readonly apiBase = environment.baseApiAddress;
public static readonly emptyHtml = Urls.baseAddress + "/empty-for-oauth.html";
public static readonly slimGeoJSON = Urls.baseAddress + "/pois-slim.geojson";
public static readonly translations = "translations/";
public static readonly urls = Urls.apiBase + "urls/";
public static readonly elevation = Urls.apiBase + "elevation";
Expand Down

0 comments on commit c157113

Please sign in to comment.