Skip to content

Commit

Permalink
Add onResourceRequestListener (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
evil159 authored Feb 5, 2024
1 parent 42af0c4 commit 177b7c6
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### main

* Add `LogConfiguration` allowing to intercept logs produced by the plugin. Pass your custom `LogWriterBackend` to `LogConfiguration.registerLogWriterBackend()` to redirect logs produced by the mapping engine to your desired destination.
* Add `MapWidget.onResourceRequestListener` that can be used to subscribe to resource requests made by the map.

### 1.0.0-beta.3

Expand Down
5 changes: 5 additions & 0 deletions example/lib/full_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class FullMapState extends State<FullMap> {
print("CameraChangedEventData: timestamp: ${data.timestamp}");
}

_onResourceRequestListener(ResourceEventData data) {
print("ResourceEventData: time: ${data.timeInterval}");
}

_onMapIdleListener(MapIdleEventData data) {
print("MapIdleEventData: timestamp: ${data.timestamp}");
}
Expand Down Expand Up @@ -133,6 +137,7 @@ class FullMapState extends State<FullMap> {
onStyleDataLoadedListener: _onStyleDataLoadedListener,
onStyleImageMissingListener: _onStyleImageMissingListener,
onStyleImageUnusedListener: _onStyleImageUnusedListener,
onResourceRequestListener: _onResourceRequestListener,
));
}
}
4 changes: 2 additions & 2 deletions ios/Classes/MapEvents+JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ extension ResponseInfo {
result["mustRevalidate"] = mustRevalidate
result["source"] = source.rawValue
result["size"] = size
result["modified"] = modified
result["expires"] = expires
result["modified"] = modified?.microsecondsSince1970
result["expires"] = expires?.microsecondsSince1970
result["etag"] = etag
result["error"] = error?.toJSON
return result
Expand Down
3 changes: 3 additions & 0 deletions lib/src/callbacks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ typedef void OnStyleImageMissingListener(
typedef void OnStyleImageUnusedListener(
StyleImageUnusedEventData styleImageUnusedEventData);

/// Definition for listener invoked when the map makes a resource request.
typedef void OnResourceRequestListener(ResourceEventData resourceEventData);

/// Gesture listener called on map tap.
typedef void OnMapTapListener(ScreenCoordinate coordinate);

Expand Down
28 changes: 17 additions & 11 deletions lib/src/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ enum RenderMode {
/// from the network or being parsed.
PARTIAL,

/// The map is fully rendered. */
/// The map is fully rendered.
FULL
}

Expand Down Expand Up @@ -295,7 +295,7 @@ class SourceRemovedEventData {

/// The class for style-data-loaded event in Observer
class StyleDataLoadedEventData {
/// The `timeInterval.begin` is when style data begins loading, and the `timeInterval.end` is when style data is loaded. */
/// The `timeInterval.begin` is when style data begins loading, and the `timeInterval.end` is when style data is loaded.
final EventTimeInterval timeInterval;

/// The 'type' property defines what kind of style has been loaded.
Expand Down Expand Up @@ -334,7 +334,7 @@ class StyleImageUnusedEventData {

/// The class for style-loaded event in Observer
class StyleLoadedEventData {
/// The `timeInterval.begin` is when the style begins loading, and the `timeInterval.end` is when the style is loaded. */
/// The `timeInterval.begin` is when the style begins loading, and the `timeInterval.end` is when the style is loaded.
final EventTimeInterval timeInterval;

StyleLoadedEventData.fromJson(Map<String, dynamic> json)
Expand Down Expand Up @@ -427,7 +427,7 @@ class Response {
final bool noContent;

/// "modified" property
final String? modified;
final DateTime? modified;

/// "source" property
final ResponseSourceType source;
Expand All @@ -436,7 +436,7 @@ class Response {
final bool notModified;

/// "expires" property
final String? expires;
final DateTime? expires;

/// "size" property
final int size;
Expand All @@ -448,10 +448,14 @@ class Response {
: eTag = json['etag'],
mustRevalidate = json['mustRevalidate'],
noContent = json['noContent'],
modified = json['modified'],
modified = json['modified'] != null
? DateTime.fromMicrosecondsSinceEpoch(json['modified'])
: null,
source = ResponseSourceType.values[json['source']],
notModified = json['notModified'],
expires = json['expires'],
expires = json['expires'] != null
? DateTime.fromMicrosecondsSinceEpoch(json['expires'])
: null,
size = json['size'],
error = json['error'] != null ? Error.fromJson(json['error']) : null;
}
Expand Down Expand Up @@ -496,9 +500,11 @@ class Request {
final RequestPriority priority;

Request.fromJson(Map<String, dynamic> json)
: loadingMethod = (json['loading-method'] as List<int>)
: loadingMethod = json['loadingMethod']
.cast<int>()
.map((e) => RequestLoadingMethodType.values[e])
.toList(),
.toList()
.cast<RequestLoadingMethodType>(),
url = json['url'],
kind = RequestType.values[json['resource']],
priority = RequestPriority.values[json['priority']];
Expand Down Expand Up @@ -538,9 +544,9 @@ enum RequestType {
/// Describes priority for request object.
/// @param value String value of this enum
enum RequestPriority {
/// Regular priority. */
/// Regular priority.
REGULAR,

/// low priority. */
/// low priority.
LOW
}
8 changes: 8 additions & 0 deletions lib/src/map_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class MapWidget extends StatefulWidget {
this.onStyleDataLoadedListener,
this.onStyleImageMissingListener,
this.onStyleImageUnusedListener,
this.onResourceRequestListener,
this.onTapListener,
this.onLongTapListener,
this.onScrollListener,
Expand Down Expand Up @@ -106,6 +107,9 @@ class MapWidget extends StatefulWidget {
if (onStyleImageUnusedListener != null) {
_eventTypes.add(_MapEvent.styleImageRemoveUnused);
}
if (onResourceRequestListener != null) {
_eventTypes.add(_MapEvent.resourceRequest);
}
}

/// Describes the map options value when using a MapWidget.
Expand Down Expand Up @@ -176,6 +180,9 @@ class MapWidget extends StatefulWidget {
/// Invoked whenever an image added to the Style is no longer needed and can be removed using StyleManager#removeStyleImage method.
final OnStyleImageUnusedListener? onStyleImageUnusedListener;

/// Invoked when map makes a request to load required resources.
final OnResourceRequestListener? onResourceRequestListener;

/// Which gestures should be consumed by the map.
///
/// It is possible for other gesture recognizers to be competing with the map on pointer
Expand Down Expand Up @@ -260,6 +267,7 @@ class _MapWidgetState extends State<MapWidget> {
onStyleDataLoadedListener: widget.onStyleDataLoadedListener,
onStyleImageMissingListener: widget.onStyleImageMissingListener,
onStyleImageUnusedListener: widget.onStyleImageUnusedListener,
onResourceRequestListener: widget.onResourceRequestListener,
onMapTapListener: widget.onTapListener,
onMapLongTapListener: widget.onLongTapListener,
onMapScrollListener: widget.onScrollListener,
Expand Down
9 changes: 9 additions & 0 deletions lib/src/mapbox_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class MapboxMap extends ChangeNotifier {
this.onStyleDataLoadedListener,
this.onStyleImageMissingListener,
this.onStyleImageUnusedListener,
this.onResourceRequestListener,
this.onMapTapListener,
this.onMapLongTapListener,
this.onMapScrollListener,
Expand Down Expand Up @@ -89,6 +90,11 @@ class MapboxMap extends ChangeNotifier {
onStyleImageUnusedListener?.call(argument);
});
}
if (onResourceRequestListener != null) {
_mapboxMapsPlatform.onResourceRequestPlatform.add((argument) {
onResourceRequestListener?.call(argument);
});
}
_setupGestures();
}

Expand Down Expand Up @@ -139,6 +145,9 @@ class MapboxMap extends ChangeNotifier {
/// Invoked whenever an image added to the Style is no longer needed and can be removed using StyleManager#removeStyleImage method.
final OnStyleImageUnusedListener? onStyleImageUnusedListener;

/// Invoked when map makes a request to load required resources.
final OnResourceRequestListener? onResourceRequestListener;

/// The currently loaded Style]object.
late StyleManager style =
StyleManager(binaryMessenger: _proxyBinaryMessenger);
Expand Down
5 changes: 5 additions & 0 deletions lib/src/mapbox_maps_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class _MapboxMapsPlatform {
ArgumentCallbacks<StyleImageMissingEventData>();
final onStyleImageUnusedPlatform =
ArgumentCallbacks<StyleImageUnusedEventData>();
final onResourceRequestPlatform = ArgumentCallbacks<ResourceEventData>();

final int _channelSuffix = _suffixesRegistry.getSuffix();
late MethodChannel _channel;
Expand Down Expand Up @@ -98,6 +99,10 @@ class _MapboxMapsPlatform {
onStyleImageUnusedPlatform(
StyleImageUnusedEventData.fromJson(jsonDecode(call.arguments)));
break;
case _MapEvent.resourceRequest:
onResourceRequestPlatform(
ResourceEventData.fromJson(jsonDecode(call.arguments)));
break;
default:
throw MissingPluginException();
}
Expand Down
67 changes: 33 additions & 34 deletions test/events_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import 'package:test/test.dart';

void main() {
test('CameraChangedEventData fromJson', () {
var cameraChangedEventData = CameraChangedEventData.fromJson(
<String, dynamic>{'timestamp': 1});
var cameraChangedEventData =
CameraChangedEventData.fromJson(<String, dynamic>{'timestamp': 1});
expect(cameraChangedEventData.timestamp, 1);
});

Expand All @@ -15,10 +15,9 @@ void main() {
});

test('MapLoadedEventData fromJson', () {
var mapLoadedEventData =
MapLoadedEventData.fromJson(<String, dynamic>{
'timeInterval': <String, dynamic>{'begin': 1, 'end': 2}
});
var mapLoadedEventData = MapLoadedEventData.fromJson(<String, dynamic>{
'timeInterval': <String, dynamic>{'begin': 1, 'end': 2}
});
expect(mapLoadedEventData.timeInterval.begin.microsecondsSinceEpoch, 1);
expect(mapLoadedEventData.timeInterval.end.microsecondsSinceEpoch, 2);
});
Expand All @@ -44,12 +43,8 @@ void main() {
});

test('MapLoadingErrorEventData fromJson with null value', () {
var mapLoadingErrorEventData =
MapLoadingErrorEventData.fromJson(<String, dynamic>{
'timestamp': 1,
'type': 0,
'message': 'message'
});
var mapLoadingErrorEventData = MapLoadingErrorEventData.fromJson(
<String, dynamic>{'timestamp': 1, 'type': 0, 'message': 'message'});
expect(mapLoadingErrorEventData.timestamp, 1);
expect(mapLoadingErrorEventData.type, MapLoadErrorType.STYLE);
expect(mapLoadingErrorEventData.message, 'message');
Expand All @@ -65,16 +60,19 @@ void main() {
'placementChanged': true,
'needsRepaint': false
});
expect(renderFrameFinishedEventData.timeInterval.begin.microsecondsSinceEpoch, 1);
expect(renderFrameFinishedEventData.timeInterval.end.microsecondsSinceEpoch, 2);
expect(
renderFrameFinishedEventData.timeInterval.begin.microsecondsSinceEpoch,
1);
expect(renderFrameFinishedEventData.timeInterval.end.microsecondsSinceEpoch,
2);
expect(renderFrameFinishedEventData.renderMode, RenderMode.FULL);
expect(renderFrameFinishedEventData.placementChanged, true);
expect(renderFrameFinishedEventData.needsRepaint, false);
});

test('RenderFrameStartedEventData fromJson', () {
var renderFrameStartedEventData = RenderFrameStartedEventData.fromJson(
<String, dynamic>{'timestamp': 1});
var renderFrameStartedEventData =
RenderFrameStartedEventData.fromJson(<String, dynamic>{'timestamp': 1});
expect(renderFrameStartedEventData.timestamp, 1);
});

Expand All @@ -84,7 +82,7 @@ void main() {
'cancelled': false,
'source': 3,
'request': <String, dynamic>{
'loading-method': [0],
'loadingMethod': [0],
'url': 'https://api.mapbox.com',
'resource': 3,
'priority': 0
Expand All @@ -93,23 +91,21 @@ void main() {
'etag': 'd8abd8d10bee6b45b4dbf5c05496587a',
'mustRevalidate': false,
'noContent': false,
'modified': 'Mon, 05 Oct 2020 14:23:52 GMT',
'modified': 3,
'source': 0,
'notModified': false,
'expires': 'Thu, 15 Oct 2020 14:32:23 GMT',
'expires': 4,
'size': 181576,
'error': <String, dynamic>{
'reason': 1,
'message': 'error message'
}
'error': <String, dynamic>{'reason': 1, 'message': 'error message'}
}
});
expect(resourceEventData.timeInterval.begin.microsecondsSinceEpoch, 1);
expect(resourceEventData.timeInterval.end.microsecondsSinceEpoch, 2);
expect(resourceEventData.cancelled, false);
expect(resourceEventData.dataSource, DataSourceType.NETWORK);
expect(resourceEventData.request.kind, RequestType.TILE);
expect(resourceEventData.request.loadingMethod, [RequestLoadingMethodType.NETWORK]);
expect(resourceEventData.request.loadingMethod,
[RequestLoadingMethodType.NETWORK]);
expect(resourceEventData.request.url, 'https://api.mapbox.com');
expect(resourceEventData.request.priority, RequestPriority.REGULAR);
expect(
Expand All @@ -118,8 +114,8 @@ void main() {
expect(resourceEventData.response!.noContent, false);
expect(resourceEventData.response!.source, ResponseSourceType.NETWORK);
expect(resourceEventData.response!.notModified, false);
expect(
resourceEventData.response!.expires, 'Thu, 15 Oct 2020 14:32:23 GMT');
expect(resourceEventData.response!.modified?.microsecondsSinceEpoch, 3);
expect(resourceEventData.response!.expires?.microsecondsSinceEpoch, 4);
expect(resourceEventData.response!.size, 181576);
expect(resourceEventData.response!.error!.reason,
ResponseErrorReason.NOT_FOUND);
Expand All @@ -142,8 +138,10 @@ void main() {
'loaded': false,
'tileId': <String, dynamic>{'x': 1, 'y': 2, 'z': 3}
});
expect(sourceDataLoadedEventData.timeInterval.begin.microsecondsSinceEpoch, 1);
expect(sourceDataLoadedEventData.timeInterval.end.microsecondsSinceEpoch, 2);
expect(
sourceDataLoadedEventData.timeInterval.begin.microsecondsSinceEpoch, 1);
expect(
sourceDataLoadedEventData.timeInterval.end.microsecondsSinceEpoch, 2);
expect(sourceDataLoadedEventData.id, 'id');
expect(sourceDataLoadedEventData.type, SourceDataType.TILE);
expect(sourceDataLoadedEventData.loaded, false);
Expand All @@ -160,11 +158,13 @@ void main() {
});

test('StyleDataLoadedEventData fromJson', () {
var styleDataLoadedEventData = StyleDataLoadedEventData.fromJson(<String, dynamic>{
var styleDataLoadedEventData =
StyleDataLoadedEventData.fromJson(<String, dynamic>{
'timeInterval': <String, dynamic>{'begin': 1, 'end': 2},
'type': 0
});
expect(styleDataLoadedEventData.timeInterval.begin.microsecondsSinceEpoch, 1);
expect(
styleDataLoadedEventData.timeInterval.begin.microsecondsSinceEpoch, 1);
expect(styleDataLoadedEventData.type, StyleDataType.STYLE);
});

Expand All @@ -183,10 +183,9 @@ void main() {
});

test('StyleLoadedEventData fromJson', () {
var styleLoadedEventData = StyleLoadedEventData.fromJson(
<String, dynamic>{
'timeInterval': <String, dynamic>{'begin': 1, 'end': 2}
});
var styleLoadedEventData = StyleLoadedEventData.fromJson(<String, dynamic>{
'timeInterval': <String, dynamic>{'begin': 1, 'end': 2}
});
expect(styleLoadedEventData.timeInterval.begin.microsecondsSinceEpoch, 1);
expect(styleLoadedEventData.timeInterval.end.microsecondsSinceEpoch, 2);
});
Expand Down

0 comments on commit 177b7c6

Please sign in to comment.