Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow blank connections applications use map-layers widget 's UserPreferences #3256

Merged
merged 11 commits into from
Mar 2, 2022
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/map-layers",
"comment": "User Preferences is now supported for Blank Connection configurations.",
"type": "none"
}
],
"packageName": "@itwin/map-layers"
}
30 changes: 15 additions & 15 deletions extensions/map-layers/src/MapLayerPreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class MapLayerPreferences {
* @param source source to be stored on the setting service
* @param storeOnIModel if true store the settings object on the model, if false store it on the project
*/
public static async storeSource(source: MapLayerSource, storeOnIModel: boolean, iTwinId: GuidString, iModelId: GuidString): Promise<boolean> {
public static async storeSource(source: MapLayerSource, iTwinId: GuidString, iModelId?: GuidString, storeOnIModel?: boolean): Promise<boolean> {
if (!MapLayersUI.iTwinConfig)
return false;
const accessToken = undefined !== IModelApp.authorizationClient ? (await IModelApp.authorizationClient.getAccessToken()) : undefined;
Expand Down Expand Up @@ -88,10 +88,10 @@ export class MapLayerPreferences {
*
* @param oldSource
* @param newSource
* @param projectId
* @param iTwinId
* @param iModelId
*/
public static async replaceSource(oldSource: MapLayerSource, newSource: MapLayerSource, projectId: GuidString, iModelId: GuidString): Promise<void> {
public static async replaceSource(oldSource: MapLayerSource, newSource: MapLayerSource, iTwinId: GuidString, iModelId?: GuidString): Promise<void> {
if (!MapLayersUI.iTwinConfig)
return;
const accessToken = undefined !== IModelApp.authorizationClient ? (await IModelApp.authorizationClient.getAccessToken()) : undefined;
Expand All @@ -102,15 +102,15 @@ export class MapLayerPreferences {
accessToken,
namespace: MapLayerPreferences._preferenceNamespace,
key: oldSource.name,
iTwinId: projectId,
iTwinId,
iModelId,
});
} catch (_err) {
await MapLayersUI.iTwinConfig.delete({
accessToken,
namespace: MapLayerPreferences._preferenceNamespace,
key: oldSource.name,
iTwinId: projectId,
iTwinId,
});
storeOnIModel = true;
}
Expand All @@ -125,7 +125,7 @@ export class MapLayerPreferences {
await MapLayersUI.iTwinConfig.save({
accessToken,
key: `${MapLayerPreferences._preferenceNamespace}.${newSource.name}`,
iTwinId: projectId,
iTwinId,
iModelId: storeOnIModel ? iModelId : undefined,
content: mapLayerSetting,
});
Expand All @@ -139,7 +139,7 @@ export class MapLayerPreferences {
* @param iTwinId
* @param iModelId
*/
public static async deleteByName(source: MapLayerSource, iTwinId: GuidString, iModelId: GuidString): Promise<void> {
public static async deleteByName(source: MapLayerSource, iTwinId: GuidString, iModelId?: GuidString): Promise<void> {
if (!MapLayersUI.iTwinConfig)
return;
const accessToken = undefined !== IModelApp.authorizationClient ? (await IModelApp.authorizationClient.getAccessToken()) : undefined;
Expand Down Expand Up @@ -177,7 +177,7 @@ export class MapLayerPreferences {
* @param iModelId
* @param storeOnIModel
*/
private static async delete(url: string, name: string, iTwinId: GuidString, iModelId: GuidString, storeOnIModel: boolean): Promise<boolean> {
private static async delete(url: string, name: string, iTwinId: GuidString, iModelId?: GuidString, storeOnIModel?: boolean): Promise<boolean> {
if (!MapLayersUI.iTwinConfig)
return true;
const accessToken = undefined !== IModelApp.authorizationClient ? (await IModelApp.authorizationClient.getAccessToken()) : undefined;
Expand Down Expand Up @@ -258,10 +258,10 @@ export class MapLayerPreferences {

/** Attempts to get a map layer based off a specific url.
* @param url
* @param projectId
* @param iTwinId
* @param iModelId
*/
public static async getByUrl(url: string, projectId: string, iModelId?: string): Promise<MapLayerPreferencesContent | undefined> {
public static async getByUrl(url: string, iTwinId: string, iModelId?: string): Promise<MapLayerPreferencesContent | undefined> {
if (!MapLayersUI.iTwinConfig)
return undefined;

Expand All @@ -271,7 +271,7 @@ export class MapLayerPreferences {
accessToken,
namespace: MapLayerPreferences._preferenceNamespace,
key: "",
iTwinId: projectId,
iTwinId,
iModelId,
});

Expand All @@ -288,11 +288,11 @@ export class MapLayerPreferences {
}

/** Get all MapLayerSources from the user's preferences, iTwin setting and iModel settings.
* @param projectId id of the project
* @param iTwinId id of the iTwiin
aruniverse marked this conversation as resolved.
Show resolved Hide resolved
* @param iModelId id of the iModel
* @throws if any of the calls to grab settings fail.
*/
public static async getSources(projectId: GuidString, iModelId: GuidString): Promise<MapLayerSource[]> {
public static async getSources(iTwinId: GuidString, iModelId?: GuidString): Promise<MapLayerSource[]> {
if (!MapLayersUI.iTwinConfig)
return [];
const accessToken = undefined !== IModelApp.authorizationClient ? (await IModelApp.authorizationClient.getAccessToken()) : undefined;
Expand All @@ -304,7 +304,7 @@ export class MapLayerPreferences {
accessToken,
namespace: MapLayerPreferences._preferenceNamespace,
key: "",
iTwinId: projectId,
iTwinId,
});
if (undefined !== userResultByProject)
mapLayerList.push(userResultByProject);
Expand All @@ -317,7 +317,7 @@ export class MapLayerPreferences {
accessToken,
namespace: MapLayerPreferences._preferenceNamespace,
key: "",
iTwinId: projectId,
iTwinId,
iModelId,
});
if (undefined !== userResultByIModel)
Expand Down
68 changes: 61 additions & 7 deletions extensions/map-layers/src/test/MapLayerSettingsSerivce.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,50 @@ describe("MapLayerPreferences", () => {
let sources = await MapLayerPreferences.getSources(iTwinId, iModelId);
let foundSource = sources.some((value) => { return value.name === testName; });
chai.assert.isFalse(foundSource, "expect not to find the source as it has not been stored yet");
const success = await MapLayerPreferences.storeSource(layer!, false, iTwinId, iModelId);

sources = await MapLayerPreferences.getSources(iTwinId);
foundSource = sources.some((value) => { return value.name === testName; });
chai.assert.isFalse(foundSource, "expect not to find the source as it has not been stored yet");

const success = await MapLayerPreferences.storeSource(layer!, iTwinId, iModelId, false);
chai.assert.isTrue(success);

sources = await MapLayerPreferences.getSources(iTwinId, iModelId);
foundSource = sources.some((value) => { return value.name === testName; });
chai.assert.isTrue(foundSource);
});

it("should store and retrieve layer without ModelId", async () => {
const layer = MapLayerSource.fromJSON({
url: "test12345",
name: testName,
formatId: "test12345",
transparentBackground: true,
});

chai.assert.isDefined(layer);
let sources = await MapLayerPreferences.getSources(iTwinId);
let foundSource = sources.some((value) => { return value.name === testName; });
chai.assert.isFalse(foundSource, "expect not to find the source as it has not been stored yet");

const success = await MapLayerPreferences.storeSource(layer!, iTwinId);
chai.assert.isTrue(success);

sources = await MapLayerPreferences.getSources(iTwinId);
foundSource = sources.some((value) => { return value.name === testName; });
chai.assert.isTrue(foundSource);
});

it("should not be able to store model setting if same setting exists as project setting", async () => {
const layer = MapLayerSource.fromJSON({
url: "test12345",
name: testName,
formatId: "test12345",
transparentBackground: true,
});
let success = await MapLayerPreferences.storeSource(layer!, false, iTwinId, iModelId);
let success = await MapLayerPreferences.storeSource(layer!, iTwinId, iModelId, false);
chai.assert.isTrue(success);
success = await MapLayerPreferences.storeSource(layer!, true, iTwinId, iModelId);
success = await MapLayerPreferences.storeSource(layer!, iTwinId, iModelId, true);
chai.assert.isFalse(success, "cannot store the iModel setting that conflicts with an iTwin setting");
});

Expand All @@ -66,9 +92,22 @@ describe("MapLayerPreferences", () => {
formatId: "test12345",
transparentBackground: true,
});
let success = await MapLayerPreferences.storeSource(layer!, true, iTwinId, iModelId);
let success = await MapLayerPreferences.storeSource(layer!, iTwinId, iModelId, true);
chai.assert.isTrue(success);
success = await MapLayerPreferences.storeSource(layer!, false, iTwinId, iModelId);
success = await MapLayerPreferences.storeSource(layer!, iTwinId, iModelId, false);
chai.assert.isTrue(success);
});

it("should be able to store the same settings twice without iTwinId and iModelId", async () => {
const layer = MapLayerSource.fromJSON({
url: "test12345",
name: testName,
formatId: "test12345",
transparentBackground: true,
});
let success = await MapLayerPreferences.storeSource(layer!, iTwinId);
chai.assert.isTrue(success);
success = await MapLayerPreferences.storeSource(layer!, iTwinId);
chai.assert.isTrue(success);
});

Expand All @@ -82,12 +121,27 @@ describe("MapLayerPreferences", () => {

chai.assert.isDefined(layer);

chai.assert.isTrue(await MapLayerPreferences.storeSource(layer!, true, iTwinId, iModelId));
chai.assert.isTrue(await MapLayerPreferences.storeSource(layer!, iTwinId, iModelId, true));
await MapLayerPreferences.deleteByName(layer!, iTwinId, iModelId);
chai.assert.isUndefined(await MapLayerPreferences.getByUrl(layer!.url, iTwinId, iModelId));

chai.assert.isTrue(await MapLayerPreferences.storeSource(layer!, false, iTwinId, iModelId));
chai.assert.isTrue(await MapLayerPreferences.storeSource(layer!, iTwinId, iModelId, true));
await MapLayerPreferences.deleteByName(layer!, iTwinId, iModelId);
chai.assert.isUndefined(await MapLayerPreferences.getByUrl(layer!.url, iTwinId, iModelId));
});

it("should be able to delete a mapSource stored without iTwinId and iModelId", async () => {
const layer = MapLayerSource.fromJSON({
url: "test12345",
name: testName,
formatId: "test12345",
transparentBackground: true,
});

chai.assert.isDefined(layer);

chai.assert.isTrue(await MapLayerPreferences.storeSource(layer!, iTwinId));
await MapLayerPreferences.deleteByName(layer!, iTwinId);
chai.assert.isUndefined(await MapLayerPreferences.getByUrl(layer!.url, iTwinId));
});
});
27 changes: 27 additions & 0 deletions extensions/map-layers/src/test/MapUrlDialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/

import { Guid } from "@itwin/core-bentley";
import { EmptyLocalization, ImageMapLayerSettings, MapSubLayerProps } from "@itwin/core-common";
import { DisplayStyle3dState, IModelApp, IModelConnection, MapLayerAuthType, MapLayerSource, MapLayerSourceStatus, MockRender, NotifyMessageDetails, OutputMessagePriority, ScreenViewport, ViewState3d } from "@itwin/core-frontend";
import { Select } from "@itwin/itwinui-react";
Expand All @@ -11,6 +12,7 @@ import * as enzyme from "enzyme";
import * as React from "react";
import * as sinon from "sinon";
import * as moq from "typemoq";
import { MapLayersUI } from "../mapLayers";
import { MapUrlDialog } from "../ui/widget/MapUrlDialog";
import { TestUtils } from "./TestUtils";

Expand Down Expand Up @@ -116,11 +118,17 @@ describe("MapUrlDialog", () => {
beforeEach(() => {
displayStyleMock.reset();
displayStyleMock.setup((ds) => ds.attachMapLayerSettings(moq.It.isAny(), moq.It.isAny(), moq.It.isAny()));
imodelMock.reset();
imodelMock.setup((iModel) => iModel.iModelId).returns(() => "fakeGuid");
imodelMock.setup((iModel) => iModel.iTwinId).returns(() => "fakeGuid");

viewMock.reset();
viewMock.setup((view) => view.iModel).returns(() => imodelMock.object);
viewportMock.reset();
viewportMock.setup((viewport) => viewport.iModel).returns(() => viewMock.object.iModel);
viewportMock.setup((viewport) => viewport.view).returns(() => viewMock.object);
viewportMock.setup((viewport) => viewport.displayStyle).returns(() => displayStyleMock.object);

});

const mockModalUrlDialogOk = () => {
Expand Down Expand Up @@ -180,4 +188,23 @@ describe("MapUrlDialog", () => {
it("attach a layer requiring EsriToken", async () => {
await testAddAuthLayer(MapLayerAuthType.EsriToken);
});

it("should not display user preferences options if iTwinConfig is undefined ", () => {

const component = enzyme.mount(<MapUrlDialog activeViewport={viewportMock.object} isOverlay={false} onOkResult={mockModalUrlDialogOk} />);
const allRadios = component.find('input[type="radio"]');
expect(allRadios.length).to.equals(0);
});

it("should display user preferences options if iTwinConfig is defined ", () => {
sandbox.stub(MapLayersUI, "iTwinConfig").get(() => ({
get: undefined,
save: undefined,
delete: undefined,
}));
const component = enzyme.mount(<MapUrlDialog activeViewport={viewportMock.object} isOverlay={false} onOkResult={mockModalUrlDialogOk} />);
const allRadios= component.find('input[type="radio"]');
expect(allRadios.length).to.equals(2);
});

});
17 changes: 9 additions & 8 deletions extensions/map-layers/src/ui/widget/MapLayerManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,15 @@ export function MapLayerManager(props: MapLayerManagerProps) {
const sourceLayers = await MapLayerSources.create(undefined, (fetchPublicMapLayerSources && !hideExternalMapLayersSection));

const iModel = IModelApp.viewManager.selectedView ? IModelApp.viewManager.selectedView.iModel : undefined;
if (iModel && iModel.iTwinId && iModel.iModelId) {
try {
const preferenceSources = await MapLayerPreferences.getSources(iModel.iTwinId, iModel.iModelId);
for (const source of preferenceSources)
await MapLayerSources.addSourceToMapLayerSources(source);
} catch (err) {
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Error, IModelApp.localization.getLocalizedString("mapLayers:CustomAttach.ErrorLoadingLayers"), BentleyError.getErrorMessage(err)));
}
try {
const preferenceSources = ( iModel?.iTwinId === undefined
? []
: await MapLayerPreferences.getSources(iModel?.iTwinId, iModel?.iModelId)
);
for (const source of preferenceSources)
await MapLayerSources.addSourceToMapLayerSources(source);
} catch (err) {
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Error, IModelApp.localization.getLocalizedString("mapLayers:CustomAttach.ErrorLoadingLayers"), BentleyError.getErrorMessage(err)));
}

if (!isMounted.current) {
Expand Down
Loading