123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:kmpdfkit_demo/widgets/constains.dart';
- import 'package:kmpdfkit_demo/widgets/extension/color_extension.dart';
- import 'package:kmpdfkit_demo/widgets/models/annot_bean.dart';
- import 'models/annot_attribute_bean.dart';
- const _methodChannel = MethodChannel(ChannelNames.eventCPDFReaderView);
- const _modifyAnnotAttrChannel =
- MethodChannel(ChannelNames.eventModifyAnnotationAttr);
- const _readerViewCallBackEventChannel =
- EventChannel(ChannelNames.eventReaderViewCallback);
- const _contextMenuHelperEventChannel =
- EventChannel(ChannelNames.eventReaderViewContextMenuHelper);
- typedef CancelListener = void Function();
- enum eventSinkId {
- readerViewCallBack,
- readerViewContextMenuHelper,
- }
- CancelListener setReaderViewCallbackListener(
- {VoidCallback? onTapMainDocArea,
- Function(int pageIndex)? onMoveToChild,
- VoidCallback? onScrolling,
- VoidCallback? onScrollEnd,
- Function(int pageIndex)? onRecordLastJumpPageNum}) {
- var subscription = _readerViewCallBackEventChannel
- .receiveBroadcastStream(eventSinkId.readerViewCallBack.index)
- .listen((data) {
- Map<dynamic, dynamic> result = data;
- String eventType = result[EventParameters.eventType];
- switch (eventType) {
- case EventParameters.onTapMainDocArea:
- if (onTapMainDocArea != null) {
- onTapMainDocArea();
- }
- break;
- case EventParameters.onMoveToChild:
- if (onMoveToChild != null) {
- int pageIndex = result[EventParameters.pageIndex];
- onMoveToChild(pageIndex);
- }
- break;
- case EventParameters.onRecordLastJumpPageNum:
- if (onRecordLastJumpPageNum != null) {
- int pageIndex = result[EventParameters.pageIndex];
- onRecordLastJumpPageNum(pageIndex);
- }
- break;
- case EventParameters.onScrollEnd:
- if (onScrollEnd != null) {
- onScrollEnd();
- }
- break;
- case EventParameters.onScrolling:
- if (onScrolling != null) {
- onScrolling();
- }
- break;
- default:
- break;
- }
- }, cancelOnError: true);
- return () {
- subscription.cancel();
- };
- }
- void setReaderViewContextMenuHelperListener(
- Function(AnnotBean) showAnnotationAttributeOptions) {
- _contextMenuHelperEventChannel.receiveBroadcastStream({
- EventParameters.eventType: eventSinkId.readerViewContextMenuHelper.name
- }).listen((event) {
- Map<dynamic, dynamic> result = event;
- String annotType = result[EventParameters.annotType];
- AnnotationType type = AnnotationType.values.byName(annotType);
- showAnnotationAttributeOptions(AnnotBean(
- annotType: type,
- attributeBean: AnnotAttributeBean.parseMapValue(type, result),
- ));
- }, cancelOnError: true);
- }
- void modifyAnnotationAttribute(
- {required AnnotationType annotationType,
- required AnnotAttributeBean bean}) async {
- await _modifyAnnotAttrChannel.invokeMethod(
- Functions.modifyAnnotAttribute, bean.toMapValues(annotationType));
- }
- void dismissModifyAnnotationAttribute() async {
- await _modifyAnnotAttrChannel
- .invokeMethod(Functions.dismissModifyAnnotationAttr);
- }
- Future<bool> scrollDirectionIsVerticalMode() async {
- String scrollDirection =
- await _methodChannel.invokeMethod(Functions.getScrollDirection);
- return scrollDirection == ScrollDirection.vertical;
- }
- Future<bool> setScrollDirection(String direction) async {
- String scrollDirection = await _methodChannel.invokeMethod(
- Functions.setScrollDirection,
- {EventParameters.scrollDirection: direction});
- return scrollDirection == ScrollDirection.vertical;
- }
- Future<bool> isDoublePage() async {
- return await _methodChannel.invokeMethod(Functions.getPageMode);
- }
- Future<bool> setPageMode(bool doublePage) async {
- return await _methodChannel.invokeMethod(
- Functions.setPageMode, {EventParameters.isDoublePage: doublePage});
- }
- Future<bool> isContinueMode() async {
- return await _methodChannel.invokeMethod(Functions.getPageContinue);
- }
- Future<bool> setIsContinueMode(bool isContinueMode) async {
- return await _methodChannel.invokeMethod(Functions.setPageContinue,
- {EventParameters.isContinueMode: isContinueMode});
- }
- Future<bool> isCoverPageMode() async {
- return await _methodChannel.invokeMethod(Functions.isCoverPageMode);
- }
- Future<bool> setIsCoverPageMode(bool isCoverPageMode) async {
- return await _methodChannel.invokeMethod(Functions.setCoverPageMode,
- {EventParameters.isCoverPageMode: isCoverPageMode});
- }
- Future<bool> isCropPageMode() async {
- return await _methodChannel.invokeMethod(Functions.isCropPageMode);
- }
- Future<bool> setIsCropPageMode(bool isCropPageMode) async {
- return await _methodChannel.invokeMethod(Functions.setIsCropPageMode,
- {EventParameters.isCropPageMode: isCropPageMode});
- }
- Future<int> getReadBackgroundColor() async {
- return await _methodChannel.invokeMethod(Functions.getReadBackgroundColor);
- }
- Future<int> setReadBackgroundColor(int backgroundColor) async {
- return await _methodChannel.invokeMethod(Functions.setReadBackgroundColor,
- {EventParameters.readBackgroundColor: backgroundColor});
- }
- Future<AnnotAttributeBean> getAnnotAttribute(AnnotationType annotType) async {
- Map<dynamic, dynamic> annotAttrMap = await _methodChannel.invokeMethod(
- Functions.getAnnotAttribute, {EventParameters.annotType: annotType.name});
- return AnnotAttributeBean.parseMapValue(annotType, annotAttrMap);
- }
- void setAnnotAttribute(
- {required AnnotationType annotationType,
- required AnnotAttributeBean bean}) async {
- await _methodChannel.invokeMethod(Functions.setAnnotAttribute, {
- EventParameters.annotType: annotationType.name,
- EventParameters.annotAttribute: bean.toMapValues(annotationType)
- });
- }
- void setCPDFReaderViewFocusType(AnnotationType annotationType) async {
- await _methodChannel.invokeMethod(Functions.setCurrentFocusedType, {
- EventParameters.touchMode: annotationType == AnnotationType.unknown
- ? TouchMode.browse.name
- : TouchMode.add_annot.name,
- EventParameters.focusedType: annotationType.name
- });
- }
|