downloadOfflineRegion function

Future<OfflineRegion> downloadOfflineRegion(
  1. OfflineRegionDefinition definition, {
  2. Map<String, dynamic> metadata = const {},
  3. dynamic onEvent(
    1. DownloadRegionStatus event
    )?,
})

Implementation

Future<OfflineRegion> downloadOfflineRegion(
  OfflineRegionDefinition definition, {
  Map<String, dynamic> metadata = const {},
  Function(DownloadRegionStatus event)? onEvent,
}) async {
  final channelName =
      'downloadOfflineRegion_${DateTime.now().microsecondsSinceEpoch}';

  await _globalChannel
      .invokeMethod('downloadOfflineRegion#setup', <String, dynamic>{
    'channelName': channelName,
  });

  if (onEvent != null) {
    EventChannel(channelName).receiveBroadcastStream().handleError((error) {
      if (error is PlatformException) {
        onEvent(Error(error));
        return Error(error);
      }
      final unknownError = Error(
        PlatformException(
          code: 'UnknowException',
          message:
              'This error is unhandled by plugin. Please contact us if needed.',
          details: error,
        ),
      );
      onEvent(unknownError);
      return unknownError;
    }).listen((data) {
      final Map<String, Object?> jsonData = json.decode(data);
      final status = switch (jsonData['status']) {
        'start' => InProgress(0.0),
        'progress' => InProgress((jsonData['progress']! as num).toDouble()),
        'success' => Success(),
        _ => throw Exception('Invalid event status ${jsonData['status']}'),
      };
      onEvent(status);
    });
  }

  final result = await _globalChannel
      .invokeMethod('downloadOfflineRegion', <String, dynamic>{
    'definition': definition.toMap(),
    'metadata': metadata,
  });

  return OfflineRegion.fromMap(json.decode(result));
}