performImportFlow function

Future<bool> performImportFlow(
  1. BuildContext context,
  2. Future<void> onSuccess()
)

Performs the full import flow.

Implementation

Future<bool> performImportFlow(
  BuildContext context,
  Future<void> Function() onSuccess,
) async {
  final confirmed = await showDialog<bool>(
    context: context,
    builder: (_) => const ImportFormatDialog(),
  );
  if (confirmed != true) return false;

  final result = await PlacesService.importPlaces();
  if (result.cancelled) return false;

  if (!context.mounted) return false;

  if (!result.hasPlaces && result.hasErrors) {
    await showImportFailedDialog(context, result.errors);
    return false;
  }

  if (!result.hasPlaces) {
    showNoPlacesFoundSnackbar(context);
    return false;
  }

  if (!context.mounted) return false;

  final previewResult = await showDialog<ImportPreviewResult>(
    context: context,
    builder: (_) => ImportPreviewDialog(
      places: result.places,
      errors: result.errors,
      skippedCount: result.skippedCount,
    ),
  );
  if (previewResult == null || previewResult.places.isEmpty) return false;

  final edited = previewResult.places;
  final encrypted = previewResult.encrypted;

  if (!context.mounted) return false;

  // If encrypted, check security key first.

  if (encrypted) {
    final hasKey = await EncryptedPlacesService.ensureSecurityKey(
      context,
      const LocationsPage(),
    );
    if (!hasKey) {
      if (!context.mounted) return false;
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(
          content: Text('Security key required for encrypted import'),
          backgroundColor: Colors.orange,
        ),
      );
      return false;
    }
  }

  if (!context.mounted) return false;

  final progress = ValueNotifier<String>(
    'Importing ${edited.length} places${encrypted ? ' (encrypted)' : ''}...\nFetching addresses (0/${edited.length})...',
  );
  showImportingProgressDialog(context, progress);

  bool success;
  if (encrypted) {
    // Import to encrypted storage.
    success = await EncryptedPlacesService.mergeImportedEncryptedPlaces(
      edited,
      context,
      const LocationsPage(),
      onProgress: (c, t) => progress.value =
          'Importing ${edited.length} places (encrypted)...\nFetching addresses ($c/$t)...',
    );
  } else {
    // Import to regular storage.
    success = await PlacesService.mergeImportedPlaces(
      edited,
      context,
      const LocationsPage(),
      onProgress: (c, t) => progress.value =
          'Importing ${edited.length} places...\nFetching addresses ($c/$t)...',
    );
  }

  if (!context.mounted) return success;

  Navigator.of(context).pop();

  if (success) {
    showImportSuccessSnackbar(context, edited.length);
    await onSuccess();
  } else {
    showImportFailureSnackbar(context);
  }
  return success;
}