cpdf_view_ctrl.dart 5.3 KB

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