cpdf_view_ctrl.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import 'dart:async';
  2. import 'package:flutter/services.dart';
  3. import 'document/cpdf_document.dart';
  4. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  5. ///
  6. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  7. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  8. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  9. /// This notice may not be removed from this file.
  10. class CPDFViewCtrl {
  11. int _viewId = 0;
  12. late final MethodChannel _methodChannel;
  13. late final EventChannel _readerViewCallbackEventChannel;
  14. late final CPDFDocument document;
  15. // Monitor the page number currently displayed when the page slides
  16. var currentPageIndexStream = StreamController<int>.broadcast();
  17. var currentPageIndex = 0;
  18. // The sliding state of the current page, sliding or stopped
  19. var scrollStatusController = StreamController<bool>.broadcast();
  20. var _isScrolling = false;
  21. var onTapMainDocArea = StreamController<bool>.broadcast();
  22. var _onTapMainDocArea = false;
  23. StreamSubscription? _streamSubscription;
  24. CPDFViewCtrl(int id) {
  25. _viewId = id;
  26. document = CPDFDocument(_viewId);
  27. _methodChannel =
  28. MethodChannel('com.compdfkit.flutter.pdfviewer.settings.$_viewId');
  29. _readerViewCallbackEventChannel = EventChannel(
  30. 'com.compdfkit.flutter.pdfviewer.readerview.callback.$_viewId');
  31. setReaderViewCallback();
  32. }
  33. /// get cpdf_reader_widget scroll Direction
  34. /// true : scroll vertically
  35. /// false : scroll horizontally
  36. Future<bool> isVerticalMode() async {
  37. return await _methodChannel.invokeMethod('isVerticalMode');
  38. }
  39. /// set scroll direction
  40. void setVerticalMode(bool verticalMode) async {
  41. await _methodChannel.invokeMethod('setVerticalMode', verticalMode);
  42. }
  43. void setDisplayPageIndex(int pageIndex) async {
  44. await _methodChannel.invokeMethod('setDisplayPageIndex', {
  45. 'pageIndex' : pageIndex,
  46. 'animation' : true
  47. });
  48. }
  49. void setReaderViewCallback(
  50. {VoidCallback? onTapMainDocArea,
  51. Function(int pageIndex)? onMoveToChild,
  52. VoidCallback? onScrolling,
  53. VoidCallback? onScrollEnd,
  54. Function(int pageIndex)? onRecordLastJumpPageNum}) {
  55. _streamSubscription = _readerViewCallbackEventChannel
  56. .receiveBroadcastStream('setReaderViewCallback')
  57. .listen((data) {
  58. Map<dynamic, dynamic> result = data;
  59. String eventType = result['eventType'];
  60. switch (eventType) {
  61. case 'onTapMainDocArea':
  62. if (onTapMainDocArea != null) {
  63. onTapMainDocArea();
  64. }
  65. _onTapMainDocArea = true;
  66. this.onTapMainDocArea.sink.add(_onTapMainDocArea);
  67. break;
  68. case 'onMoveToChild':
  69. int pageIndex = result['pageIndex'];
  70. if (onMoveToChild != null) {
  71. int pageIndex = result['pageIndex'];
  72. onMoveToChild(pageIndex);
  73. }
  74. currentPageIndex = pageIndex;
  75. currentPageIndexStream.sink.add(currentPageIndex);
  76. break;
  77. case 'onRecordLastJumpPageNum':
  78. if (onRecordLastJumpPageNum != null) {
  79. int pageIndex = result['pageIndex'];
  80. onRecordLastJumpPageNum(pageIndex);
  81. }
  82. break;
  83. case 'onScrollEnd':
  84. if (onScrollEnd != null) {
  85. onScrollEnd();
  86. }
  87. if (_isScrolling) {
  88. _isScrolling = false;
  89. scrollStatusController.sink.add(_isScrolling);
  90. }
  91. break;
  92. case 'onScrolling':
  93. if (onScrolling != null) {
  94. onScrolling();
  95. }
  96. _isScrolling = true;
  97. scrollStatusController.sink.add(_isScrolling);
  98. break;
  99. default:
  100. break;
  101. }
  102. }, cancelOnError: true);
  103. }
  104. void dispose() {
  105. currentPageIndexStream.close();
  106. scrollStatusController.close();
  107. _streamSubscription?.cancel();
  108. }
  109. }