CPDFReaderWidgetController constructor

CPDFReaderWidgetController(
  1. int id, {
  2. CPDFPageChangedCallback? onPageChanged,
  3. CPDFDocumentSaveCallback? saveCallback,
  4. CPDFPageEditDialogBackPressCallback? onPageEditBackPress,
  5. CPDFFillScreenChangedCallback? onFillScreenChanged,
  6. CPDFIOSClickBackPressedCallback? onIOSClickBackPressed,
  7. CPDFOnTapMainDocAreaCallback? onTapMainDocArea,
  8. CPDFOnCustomToolbarItemTappedCallback? onCustomToolbarItemTapped,
  9. CPDFOnAnnotationCreationPreparedCallback? onAnnotationCreationPrepared,
  10. CPDFOnCustomContextMenuItemTappedCallback? onCustomContextMenuItemTapped,
})

Implementation

CPDFReaderWidgetController(int id,
    {CPDFPageChangedCallback? onPageChanged,
    CPDFDocumentSaveCallback? saveCallback,
    CPDFPageEditDialogBackPressCallback? onPageEditBackPress,
    CPDFFillScreenChangedCallback? onFillScreenChanged,
    CPDFIOSClickBackPressedCallback? onIOSClickBackPressed,
    CPDFOnTapMainDocAreaCallback? onTapMainDocArea,
    CPDFOnCustomToolbarItemTappedCallback? onCustomToolbarItemTapped,
    CPDFOnAnnotationCreationPreparedCallback? onAnnotationCreationPrepared,
    CPDFOnCustomContextMenuItemTappedCallback?
        onCustomContextMenuItemTapped}) {
  _channel = MethodChannel('com.compdfkit.flutter.ui.pdfviewer.$id');
  _annotationHistoryManager = CPDFAnnotationHistoryManager(_channel);
  _editManager = CPDFEditManager(_channel);
  _channel.setMethodCallHandler((call) async {
    switch (call.method) {
      case 'onPageChanged':
        var pageIndex = call.arguments['pageIndex'];
        onPageChanged?.call(pageIndex);
        break;
      case 'saveDocument':
        saveCallback?.call();
        break;
      case 'onPageEditDialogBackPress':
        onPageEditBackPress?.call();
        break;
      case 'onFullScreenChanged':
        bool isFillScreen = call.arguments;
        onFillScreenChanged?.call(isFillScreen);
        break;
      case 'onIOSClickBackPressed':
        onIOSClickBackPressed?.call();
        break;
      case 'onDocumentIsReady':
        _readyCompleter.complete();
        break;
      case 'onAnnotationHistoryChanged':
        _annotationHistoryManager.handleMethodCall(call);
        break;
      case 'onTapMainDocArea':
        onTapMainDocArea?.call();
        break;
      case 'onContentEditorHistoryChanged':
        _editManager.historyManager.handleMethodCall(call);
        break;
      case 'onCustomToolbarItemTapped':
        onCustomToolbarItemTapped?.call(call.arguments);
        break;
      case 'annotationsCreated':
        dynamic annotationData = call.arguments;
        final map = Map<String, dynamic>.from(annotationData);
        final annotation = CPDFAnnotationRegistry.fromJson(map);
        _eventListeners[CPDFEvent.annotationsCreated]
            ?.forEach((cb) => cb(annotation));
        break;
      case 'annotationsSelected':
        dynamic annotationData = call.arguments;
        final map = Map<String, dynamic>.from(annotationData);
        final annotation = CPDFAnnotationRegistry.fromJson(map);
        _eventListeners[CPDFEvent.annotationsSelected]
            ?.forEach((cb) => cb(annotation));
        break;
      case 'annotationsDeselected':
        dynamic annotationData = call.arguments;
        dynamic data;
        if (annotationData != null) {
          final map = Map<String, dynamic>.from(annotationData);
          data = CPDFAnnotationRegistry.fromJson(map);
        }
        _eventListeners[CPDFEvent.annotationsDeselected]
            ?.forEach((cb) => cb(data));
        break;
      case 'formFieldsCreated':
        dynamic widgetData = call.arguments;
        final map = Map<String, dynamic>.from(widgetData);
        final widget = CPDFWidgetRegistry.fromJson(map);
        _eventListeners[CPDFEvent.formFieldsCreated]
            ?.forEach((cb) => cb(widget));
        break;
      case 'formFieldsSelected':
        dynamic widgetData = call.arguments;
        final map = Map<String, dynamic>.from(widgetData);
        final widget = CPDFWidgetRegistry.fromJson(map);
        _eventListeners[CPDFEvent.formFieldsSelected]
            ?.forEach((cb) => cb(widget));
        break;
      case 'formFieldsDeselected':
        dynamic widgetData = call.arguments;
        dynamic data;
        if (widgetData != null) {
          final map = Map<String, dynamic>.from(widgetData);
          data = CPDFWidgetRegistry.fromJson(map);
        }
        _eventListeners[CPDFEvent.formFieldsDeselected]
            ?.forEach((cb) => cb(data));
        break;
      case 'editorSelectionDeselected':
        _eventListeners[CPDFEvent.editorSelectionDeselected]
            ?.forEach((cb) => cb(null));
        break;
      case 'editorSelectionSelected':
        dynamic type = call.arguments['type'];
        dynamic editAreaData = call.arguments;
        final map = Map<String, dynamic>.from(editAreaData);
        CPDFEditArea? editArea;
        if (type == 'text') {
          editArea = CPDFEditTextArea.fromJson(map);
        } else if (type == 'image') {
          editArea = CPDFEditImageArea.fromJson(map);
        } else if (type == 'path') {
          editArea = CPDFEditArea.fromJson(map);
        }
        _eventListeners[CPDFEvent.editorSelectionSelected]
            ?.forEach((cb) => cb(editArea));
        break;
      case 'onAnnotationCreationPrepared':
        String typeName = call.arguments['type'];
        CPDFAnnotationType type =
            CPDFAnnotationType.values.firstWhere((e) => e.name == typeName);
        if (call.arguments['annotation'] != null) {
          dynamic annotationData = call.arguments['annotation'];
          final map = Map<String, dynamic>.from(annotationData);
          final annotation = CPDFAnnotationRegistry.fromJson(map);
          onAnnotationCreationPrepared?.call(type, annotation);
        } else {
          onAnnotationCreationPrepared?.call(type, null);
        }
        break;
      case 'onCustomContextMenuItemTapped':
        dynamic data = call.arguments;
        final dataMap = Map<String, dynamic>.from(data);
        Map<String, dynamic> resultMap = {};
        for (var element in dataMap.entries) {
          try {
            switch (element.key) {
              case 'rect':
                final rectF = CPDFRectF.fromJson(
                    Map<String, dynamic>.from(element.value));
                resultMap['rect'] = rectF;
                break;
              case 'annotation':
                final annotation = CPDFAnnotationRegistry.fromJson(
                    Map<String, dynamic>.from(element.value));
                resultMap['annotation'] = annotation;
                break;
              case 'widget':
                final widget = CPDFWidgetRegistry.fromJson(
                    Map<String, dynamic>.from(element.value));
                resultMap['widget'] = widget;
                break;
              case 'editArea':
                final editAreaMap = Map<String, dynamic>.from(element.value);
                if (editAreaMap['type'] == 'text') {
                  final editTextArea = CPDFEditTextArea.fromJson(editAreaMap);
                  resultMap['editArea'] = editTextArea;
                } else if (editAreaMap['type'] == 'image') {
                  final editImageArea =
                      CPDFEditImageArea.fromJson(editAreaMap);
                  resultMap['editArea'] = editImageArea;
                } else {
                  final editArea = CPDFEditArea.fromJson(editAreaMap);
                  resultMap['editArea'] = editArea;
                }
                break;
              case 'point':
                double x = (element.value['x'] as num).toDouble();
                double y = (element.value['y'] as num).toDouble();
                resultMap['point'] = {
                  'x': double.parse(x.toStringAsFixed(2)),
                  'y': double.parse(y.toStringAsFixed(2))
                };
                break;
              default:
                if (element.key != 'customEventType') {
                  resultMap[element.key] = element.value;
                }
                break;
            }
          } catch (e) {
            debugPrint(
                'ComPDFKit-Flutter: onCustomContextMenuItemTapped parse error: $e');
          }
        }
        final identifier = call.arguments["identifier"];
        onCustomContextMenuItemTapped?.call(identifier, resultMap);
        break;
    }
  });
  _document = CPDFDocument.withController(id);
}