Skip to content

Commit 96660b4

Browse files
authored
Merge pull request #6 from nativescript-community/fix/xml-event-listeners
ref: Event listener improvements and cleanup
2 parents edd0c17 + 14bc7e7 commit 96660b4

File tree

8 files changed

+167
-167
lines changed

8 files changed

+167
-167
lines changed

.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<projectDescription>
3-
<name>nativescript-carto</name>
3+
<name>ui-carto</name>
44
<comment>Project nativescript-carto created by Buildship.</comment>
55
<projects>
66
</projects>

src/ui-carto/core/index.android.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,12 @@ import { BaseNative } from '../BaseNative';
22
import { AltitudeKey, DefaultLatLonKeys, GenericMapPos, LatitudeKey, LongitudeKey, MapVec, ScreenBounds, ScreenPos } from './index.common';
33
export * from './index.common';
44

5-
export const ClickType = {
6-
get SINGLE() {
7-
return com.carto.ui.ClickType.CLICK_TYPE_SINGLE;
8-
},
9-
get LONG() {
10-
return com.carto.ui.ClickType.CLICK_TYPE_LONG;
11-
},
12-
get DOUBLE() {
13-
return com.carto.ui.ClickType.CLICK_TYPE_DOUBLE;
14-
},
15-
get DUAL() {
16-
return com.carto.ui.ClickType.CLICK_TYPE_DUAL;
17-
}
18-
};
19-
205
export class MapBounds<T = DefaultLatLonKeys> extends BaseNative<com.carto.core.MapBounds, {}> {
21-
constructor(public northeast?: GenericMapPos<T>, public southwest?: GenericMapPos<T>, native?: com.carto.core.MapBounds) {
6+
constructor(
7+
public northeast?: GenericMapPos<T>,
8+
public southwest?: GenericMapPos<T>,
9+
native?: com.carto.core.MapBounds
10+
) {
2211
super(undefined, native);
2312
}
2413
createNative() {

src/ui-carto/core/index.common.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ export class ScreenBounds {
3232
min: ScreenPos;
3333
max: ScreenPos;
3434
}
35+
36+
export enum ClickType {
37+
SINGLE,
38+
LONG,
39+
DOUBLE,
40+
DUAL
41+
}
42+
3543
// export namespace MapBounds {
3644
// function fromCoordinates(southwest: MapPos, northeast: MapPos): MapBounds;
3745
// }

src/ui-carto/core/index.ios.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ import { BaseNative } from '../BaseNative';
22
import { AltitudeKey, DefaultLatLonKeys, GenericMapPos, LatitudeKey, LongitudeKey, MapVec, ScreenBounds, ScreenPos } from './index.common';
33
export * from './index.common';
44

5-
export enum ClickType {
6-
SINGLE = NTClickType.T_CLICK_TYPE_SINGLE,
7-
LONG = NTClickType.T_CLICK_TYPE_LONG,
8-
DOUBLE = NTClickType.T_CLICK_TYPE_DOUBLE,
9-
DUAL = NTClickType.T_CLICK_TYPE_DUAL
10-
}
11-
125
export class MapBounds<T = DefaultLatLonKeys> extends BaseNative<NTMapBounds, {}> {
13-
constructor(public northeast?: GenericMapPos<T>, public southwest?: GenericMapPos<T>, native?: NTMapBounds) {
6+
constructor(
7+
public northeast?: GenericMapPos<T>,
8+
public southwest?: GenericMapPos<T>,
9+
native?: NTMapBounds
10+
) {
1411
super(undefined, native);
1512
}
1613
createNative() {

src/ui-carto/ui/index.android.ts

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ClickType,
23
DefaultLatLonKeys,
34
MapBounds,
45
MapPos,
@@ -16,19 +17,10 @@ import {
1617
import { Layer, TileLayer } from '../layers';
1718
import { IProjection } from '../projections';
1819
import { restrictedPanningProperty } from './cssproperties';
19-
import {
20-
Layers as BaseLayers,
21-
CartoViewBase,
22-
MapClickedEvent,
23-
MapIdleEvent,
24-
MapInteractionEvent,
25-
MapMovedEvent,
26-
MapReadyEvent,
27-
MapStableEvent
28-
} from './index.common';
20+
import { Layers as BaseLayers, CartoViewBase, MapClickedEvent, MapIdleEvent, MapInteractionEvent, MapMovedEvent, MapReadyEvent, MapStableEvent } from './index.common';
2921

3022
import { ImageSource, Property, Utils, booleanConverter } from '@nativescript/core';
31-
import { MapOptions } from '.';
23+
import { MapClickInfo, MapGestureInfo, MapInteractionInfo, MapOptions } from '.';
3224
import { EPSG4326 } from '../projections/epsg4326';
3325
export { MapClickedEvent, MapIdleEvent, MapMovedEvent, MapReadyEvent, MapStableEvent };
3426

@@ -111,63 +103,54 @@ export class CartoMap<T = DefaultLatLonKeys> extends CartoViewBase {
111103
}
112104
const listener = new com.akylas.carto.additions.AKMapEventListener({
113105
onMapIdle: () => {
114-
if (this.hasListeners(MapIdleEvent)) {
115-
this.sendEvent(MapIdleEvent);
116-
}
106+
this.sendEvent(MapIdleEvent);
117107
},
118108
onMapMoved: (userAction: boolean) => {
119-
if (this.hasListeners(MapMovedEvent)) {
120-
this.sendEvent(MapMovedEvent, { userAction });
121-
}
109+
this.sendEvent<MapGestureInfo>(MapMovedEvent, { userAction });
122110
},
123111
onMapInteraction: (interaction: com.carto.ui.MapInteractionInfo, userAction: boolean) => {
124-
if (this.hasListeners(MapInteractionEvent)) {
125-
this.sendEvent(MapInteractionEvent, {
126-
interaction: {
127-
userAction,
128-
get isAnimationStarted() {
129-
return interaction.isAnimationStarted();
130-
},
131-
get isPanAction() {
132-
return interaction.isPanAction();
133-
},
134-
get isRotateAction() {
135-
return interaction.isRotateAction();
136-
},
137-
get isTiltAction() {
138-
return interaction.isTiltAction();
139-
},
140-
get isZoomAction() {
141-
return interaction.isZoomAction();
142-
}
112+
this.sendEvent<MapInteractionInfo>(MapInteractionEvent, {
113+
userAction,
114+
interaction: {
115+
get isAnimationStarted() {
116+
return interaction.isAnimationStarted();
117+
},
118+
get isPanAction() {
119+
return interaction.isPanAction();
120+
},
121+
get isRotateAction() {
122+
return interaction.isRotateAction();
123+
},
124+
get isTiltAction() {
125+
return interaction.isTiltAction();
126+
},
127+
get isZoomAction() {
128+
return interaction.isZoomAction();
143129
}
144-
});
145-
}
130+
}
131+
});
146132
},
147133
onMapStable: (userAction: boolean) => {
148-
if (this.hasListeners(MapStableEvent)) {
149-
this.sendEvent(MapStableEvent, { userAction });
150-
}
134+
this.sendEvent<MapGestureInfo>(MapStableEvent, { userAction });
151135
},
152136
onMapClicked: (mapClickInfo: com.carto.ui.MapClickInfo) => {
153-
if (this.hasListeners(MapClickedEvent)) {
154-
this.sendEvent(MapClickedEvent, {
155-
android: mapClickInfo,
156-
get clickInfo() {
157-
return {
158-
get duration() {
159-
return mapClickInfo.getClickInfo().getDuration();
160-
}
161-
};
162-
},
163-
get clickType() {
164-
return mapClickInfo.getClickType();
165-
},
166-
get position() {
167-
return fromNativeMapPos(mapClickInfo.getClickPos());
168-
}
169-
});
170-
}
137+
this.sendEvent<MapClickInfo>(MapClickedEvent, {
138+
android: mapClickInfo,
139+
get clickInfo() {
140+
return {
141+
get duration(): number {
142+
return mapClickInfo.getClickInfo().getDuration();
143+
}
144+
};
145+
},
146+
get clickType(): ClickType {
147+
// This will return an integer value that can be compared with the actual enum
148+
return mapClickInfo.getClickType().swigValue();
149+
},
150+
get position() {
151+
return fromNativeMapPos(mapClickInfo.getClickPos());
152+
}
153+
});
171154
}
172155
});
173156
this.nativeViewProtected.listener = listener;

src/ui-carto/ui/index.common.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { CSSType, ContentView } from '@nativescript/core';
33
import { BaseNative } from '../BaseNative';
44
import { LatitudeKey, MapPos, fromNativeMapPos } from '../core';
55
import { Layer } from '../layers';
6-
import { nativeVectorToArray } from '../utils';
76
import { bearingProperty, focusPosProperty, tiltProperty, zoomProperty } from './cssproperties';
7+
import { MapInfo } from '.';
88

99
export const MapReadyEvent = 'mapReady';
1010
export const MapStableEvent = 'mapStable';
@@ -95,6 +95,13 @@ export abstract class Layers<T = any> extends BaseNative<T, {}> {
9595

9696
@CSSType('CartoMap')
9797
export abstract class CartoViewBase extends ContentView {
98+
public static mapReadyEvent = MapReadyEvent;
99+
public static mapStableEvent = MapStableEvent;
100+
public static mapIdleEvent = MapIdleEvent;
101+
public static mapMovedEvent = MapMovedEvent;
102+
public static mapInteractionEvent = MapInteractionEvent;
103+
public static mapClickedEvent = MapClickedEvent;
104+
98105
public mapReady = false;
99106
nativeProjection: any;
100107
@mapProperty({
@@ -116,7 +123,7 @@ export abstract class CartoViewBase extends ContentView {
116123
@mapProperty maxZoom: number;
117124
@mapProperty restrictedPanning: boolean;
118125

119-
public sendEvent(eventName: string, data?) {
126+
public sendEvent<T extends MapInfo = MapInfo>(eventName: string, data?: T) {
120127
if (this.hasListeners(eventName)) {
121128
this.notify({
122129
eventName,

src/ui-carto/ui/index.d.ts

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EventData, ImageSource, Style, View } from '@nativescript/core';
2-
import { DefaultLatLonKeys, GenericMapPos, MapBounds, ScreenBounds, ScreenPos } from '../core';
2+
import { ClickType, DefaultLatLonKeys, GenericMapPos, MapBounds, ScreenBounds, ScreenPos } from '../core';
33
import { Layer } from '../layers';
44
import { Projection } from '../projections';
55
import { Layers } from './index.common';
@@ -31,27 +31,53 @@ export const MapIdleEvent: string;
3131
export const MapMovedEvent: string;
3232
export const MapClickedEvent: string;
3333

34+
export interface MapInfo {}
35+
36+
export interface MapGestureInfo extends MapInfo {
37+
userAction: boolean;
38+
}
39+
40+
export interface MapInteractionInfo extends MapGestureInfo {
41+
interaction: {
42+
isAnimationStarted: boolean;
43+
isPanAction: boolean;
44+
isRotateAction: boolean;
45+
isTiltAction: boolean;
46+
isZoomAction: boolean;
47+
};
48+
}
49+
50+
export interface MapClickInfo<T = DefaultLatLonKeys> extends MapInfo {
51+
android?: any;
52+
ios?: any;
53+
clickInfo: {
54+
duration: number;
55+
};
56+
clickType: ClickType;
57+
position: GenericMapPos<T>;
58+
}
59+
3460
export interface MapEventData extends EventData {
35-
data: any;
61+
data?: MapInfo;
3662
}
3763
export interface MapPosEventData<T = DefaultLatLonKeys> extends EventData {
3864
MapPos: GenericMapPos<T>;
3965
}
4066

41-
export interface MapClickInfo<T = DefaultLatLonKeys> {
42-
clickType: number,
43-
clickInfo:{
44-
duration:number
45-
},
46-
position: GenericMapPos<T>;
67+
export interface MapMovedEventData extends MapEventData {
68+
data: MapGestureInfo;
4769
}
48-
export interface MapInteractionInfo {
49-
userAction: boolean;
50-
isAnimationStarted: boolean;
51-
isPanAction: boolean;
52-
isRotateAction: boolean;
53-
isTiltAction: boolean;
54-
isZoomAction: boolean;
70+
71+
export interface MapStableEventData extends MapEventData {
72+
data: MapGestureInfo;
73+
}
74+
75+
export interface MapInteractionEventData extends MapEventData {
76+
data: MapInteractionInfo;
77+
}
78+
79+
export interface MapClickedEventData extends MapEventData {
80+
data: MapClickInfo;
5581
}
5682

5783
export class MapOptions {
@@ -177,7 +203,7 @@ export class MapOptions {
177203
setLongClickDuration(param0: number): void;
178204
getDoubleClickMaxDuration(): number;
179205
setDoubleClickMaxDuration(param0: number): void;
180-
setLayersLabelsProcessedInReverseOrder(enabled: boolean): void;
206+
setLayersLabelsProcessedInReverseOrder(enabled: boolean): void;
181207
isLayersLabelsProcessedInReverseOrder(): boolean;
182208
}
183209

@@ -192,6 +218,13 @@ interface CartoMapStyle extends Style {
192218
}
193219

194220
export class CartoMap<T = DefaultLatLonKeys> extends View {
221+
public static mapReadyEvent = 'mapReady';
222+
public static mapStableEvent = 'mapStable';
223+
public static mapIdleEvent = 'mapIdle';
224+
public static mapMovedEvent = 'mapMoved';
225+
public static mapInteractionEvent = 'mapInteraction';
226+
public static mapClickedEvent = 'mapClicked';
227+
195228
public static setRunOnMainThread(value: boolean);
196229
public projection: Projection;
197230
focusPos: GenericMapPos<T>;
@@ -225,4 +258,10 @@ export class CartoMap<T = DefaultLatLonKeys> extends View {
225258
clearPreloadingCaches();
226259
cancelAllTasks();
227260
captureRendering(wait?: boolean): Promise<ImageSource>;
261+
262+
on(event: 'mapReady' | 'mapIdle', callback: (args: EventData) => void, thisArg?: any): void;
263+
on(event: 'mapStable', callback: (args: MapStableEventData) => void, thisArg?: any): void;
264+
on(event: 'mapMoved', callback: (args: MapMovedEventData) => void, thisArg?: any): void;
265+
on(event: 'mapInteraction', callback: (args: MapInteractionEventData) => void, thisArg?: any): void;
266+
on(event: 'mapClicked', callback: (args: MapClickedEventData) => void, thisArg?: any): void;
228267
}

0 commit comments

Comments
 (0)