importAnnotations method

Future<Map<int, List<Annotation>>> importAnnotations(
  1. String filePath
)

Import annotations from a custom file path

Implementation

Future<Map<int, List<Annotation>>> importAnnotations(String filePath) async {
  try {
    final file = File(filePath);

    if (!await file.exists()) {
      throw Exception('File does not exist: $filePath');
    }

    final jsonString = await file.readAsString();
    final Map<String, dynamic> data = jsonDecode(jsonString);

    // Check if it's a versioned export format
    Map<String, dynamic> annotationsData;
    if (data.containsKey('annotations')) {
      annotationsData = data['annotations'];
    } else {
      // Legacy format - treat the whole data as annotations
      annotationsData = data;
    }

    final Map<int, List<Annotation>> annotations = {};

    for (final entry in annotationsData.entries) {
      final pageNumber = int.parse(entry.key);
      final annotationsJson = entry.value as List;

      final pageAnnotations = annotationsJson
          .map((annotationJson) => Annotation.fromJson(annotationJson))
          .toList();

      annotations[pageNumber] = pageAnnotations;
    }

    return annotations;
  } catch (e) {
    throw Exception('Failed to import annotations: $e');
  }
}