requestWikiInfo static method

EventHandler? requestWikiInfo(
  1. Landmark landmark, {
  2. required void onComplete(
    1. GemError err,
    2. ExternalInfo? extraInfo
    ),
})

Requests Wikipedia info for a landmark

The result will be provided as a ExternalInfo object

Parameters

  • IN landmark Landmark for which the request will be done.
  • IN onWikiDataAvailable callback to be triggered after the wikipedia request is done.
    • It will be called with GemError.success and the external info object on success.
    • It will be called with GemError.invalidInput error and null info if the landmark does not have Wikipedia info
    • It will be called with GemError.connection error and null info if connection is not available or restricted
    • It will be called with GemError.notFound error and null info if wikipedia info is not found for the landmark
    • It will be called with GemError.general error and null info on other errors

Returns

  • An EventHandler that can be used to cancel the request.

Throws

  • An exception if it fails.

Implementation

static EventHandler? requestWikiInfo(
  Landmark landmark, {
  required void Function(GemError err, ExternalInfo? extraInfo) onComplete,
}) {
  final (bool hasWikiInfo, ExternalInfo externalInfo) = _checkWikiInfo(landmark);
  if (!hasWikiInfo) {
    onComplete(GemError.invalidInput, null);
    return null;
  }

  final ExternalInfoHandler wikiListener = ExternalInfoHandler(externalInfo: externalInfo);

  wikiListener.registerOnCompleteWithDataCallback((
    final int err,
    final String hint,
    final Map<dynamic, dynamic> json,
  ) {
    GemKitPlatform.instance.unregisterEventHandler(wikiListener.id);
    final GemError error = GemErrorExtension.fromCode(err);
    if (error == GemError.success) {
      onComplete(error, externalInfo);
    } else {
      onComplete(error, null);
    }
  });
  GemKitPlatform.instance.registerEventHandler(
    wikiListener.id,
    wikiListener,
  );

  Future<void>.delayed(const Duration(milliseconds: 100), () async {
    for (int i = 0; i < 30; i++) {
      if (externalInfo.wikiPageUrl.isNotEmpty) {
        break;
      }

      await Future<void>.delayed(const Duration(milliseconds: 50));
    }

    objectMethod(
      externalInfo.pointerId,
      'ExternalInfo',
      'requestWikiInfo',
      args: <String, dynamic>{
        'first': landmark.pointerId,
        'second': wikiListener.id,
      },
    );
  });

  return wikiListener;
}