cpdf_view_ctrl.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. var currentPageIndexStream = StreamController<int>.broadcast();
  16. var _currentPageIndex = 0;
  17. var scrollStatusController = StreamController<bool>.broadcast();
  18. var _isScrolling = false;
  19. StreamSubscription? _streamSubscription;
  20. CPDFViewCtrl(int id) {
  21. _viewId = id;
  22. document = CPDFDocument(_viewId);
  23. _methodChannel =
  24. MethodChannel('com.compdfkit.flutter.pdfviewer.settings.$_viewId');
  25. _readerViewCallbackEventChannel = EventChannel(
  26. 'com.compdfkit.flutter.pdfviewer.readerview.callback.$_viewId');
  27. }
  28. /// get cpdf_reader_widget scroll Direction
  29. /// true : scroll vertically
  30. /// false : scroll horizontally
  31. Future<bool> isVerticalMode() async {
  32. return await _methodChannel.invokeMethod('isVerticalMode');
  33. }
  34. /// set scroll direction
  35. void setVerticalMode(bool verticalMode) async {
  36. await _methodChannel
  37. .invokeMethod('settings', {'isVerticalMode': verticalMode});
  38. }
  39. void setDisplayPageIndex(int pageIndex) async {
  40. await _methodChannel.invokeMethod('settings', {
  41. 'setDisplayPageIndex' : pageIndex
  42. });
  43. }
  44. void setReaderViewCallback(
  45. {VoidCallback? onTapMainDocArea,
  46. Function(int pageIndex)? onMoveToChild,
  47. VoidCallback? onScrolling,
  48. VoidCallback? onScrollEnd,
  49. Function(int pageIndex)? onRecordLastJumpPageNum}) {
  50. _streamSubscription = _readerViewCallbackEventChannel
  51. .receiveBroadcastStream('setReaderViewCallback')
  52. .listen((data) {
  53. Map<dynamic, dynamic> result = data;
  54. String eventType = result['eventType'];
  55. switch (eventType) {
  56. case 'onTapMainDocArea':
  57. if (onTapMainDocArea != null) {
  58. onTapMainDocArea();
  59. }
  60. break;
  61. case 'onMoveToChild':
  62. int pageIndex = result['pageIndex'];
  63. if (onMoveToChild != null) {
  64. int pageIndex = result['pageIndex'];
  65. onMoveToChild(pageIndex);
  66. }
  67. _currentPageIndex = pageIndex;
  68. currentPageIndexStream.sink.add(_currentPageIndex);
  69. break;
  70. case 'onRecordLastJumpPageNum':
  71. if (onRecordLastJumpPageNum != null) {
  72. int pageIndex = result['pageIndex'];
  73. onRecordLastJumpPageNum(pageIndex);
  74. }
  75. break;
  76. case 'onScrollEnd':
  77. if (onScrollEnd != null) {
  78. onScrollEnd();
  79. }
  80. if (_isScrolling){
  81. _isScrolling = false;
  82. scrollStatusController.sink.add(_isScrolling);
  83. }
  84. break;
  85. case 'onScrolling':
  86. if (onScrolling != null) {
  87. onScrolling();
  88. }
  89. _isScrolling = true;
  90. scrollStatusController.sink.add(_isScrolling);
  91. break;
  92. default:
  93. break;
  94. }
  95. }, cancelOnError: true);
  96. }
  97. void dispose(){
  98. currentPageIndexStream.close();
  99. scrollStatusController.close();
  100. _streamSubscription?.cancel();
  101. }
  102. }