123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- import 'dart:async';
- import 'package:compdfkit_flutter/common/util/cpdf_color_extension.dart';
- import 'package:flutter/services.dart';
- import 'document/cpdf_document.dart';
- /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
- ///
- /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
- /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
- /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
- /// This notice may not be removed from this file.
- class CPDFViewCtrl {
- int _viewId = 0;
- late final MethodChannel _methodChannel;
- late final EventChannel _readerViewCallbackEventChannel;
- late final CPDFDocument document;
- // Monitor the page number currently displayed when the page slides
- var currentPageIndexStream = StreamController<int>.broadcast();
- var currentPageIndex = 0;
- // The sliding state of the current page, sliding or stopped
- var scrollStatusController = StreamController<bool>.broadcast();
- var _isScrolling = false;
- var onTapMainDocArea = StreamController<bool>.broadcast();
- var _onTapMainDocArea = false;
- StreamSubscription? _streamSubscription;
- CPDFViewCtrl(int id) {
- _viewId = id;
- document = CPDFDocument(_viewId);
- _methodChannel =
- MethodChannel('com.compdfkit.flutter.pdfviewer.settings.$_viewId');
- _readerViewCallbackEventChannel = EventChannel(
- 'com.compdfkit.flutter.pdfviewer.readerview.callback.$_viewId');
- setReaderViewCallback();
- }
- /// get cpdf_reader_widget scroll Direction
- /// true : scroll vertically
- /// false : scroll horizontally
- Future<bool> isVerticalMode() async {
- return await _methodChannel.invokeMethod('isVerticalMode');
- }
- /// set scroll direction
- void setVerticalMode(bool verticalMode) async {
- await _methodChannel.invokeMethod('setVerticalMode', verticalMode);
- }
- Future<bool> isDoublePageMode() async {
- return await _methodChannel.invokeMethod('isDoublePage');
- }
- void setDoublePageMode(bool doublePage) async {
- await _methodChannel.invokeMethod('setDoublePage', doublePage);
- }
- Future<bool> isCoverPageMode() async {
- return await _methodChannel.invokeMethod('isCoverPageMode');
- }
- void setCoverPageMode(bool coverPage) async {
- await _methodChannel.invokeMethod('setCoverPageMode', coverPage);
- }
- Future<bool> isContinueMode() async {
- return await _methodChannel.invokeMethod('isContinueMode');
- }
- void setContinueMode(bool continueMode) async {
- await _methodChannel.invokeMethod('setContinueMode', continueMode);
- }
- Future<bool> isCropMode() async {
- return await _methodChannel.invokeMethod('isCropMode');
- }
- void setCropMode(bool cropMode) async {
- await _methodChannel.invokeMethod('setCropMode', cropMode);
- }
- Future<Color> getReadBackgroundColor() async {
- String hexColor = await _methodChannel.invokeMethod('getReadBackgroundColor');
- return HexColor.fromHex(hexColor);
- }
- void setReadBackgroundColor(Color color) async {
- await _methodChannel.invokeMethod('setReadBackgroundColor', color.toHex());
- }
- void setDisplayPageIndex(int pageIndex) async {
- await _methodChannel.invokeMethod('setDisplayPageIndex', {
- 'pageIndex' : pageIndex,
- 'animation' : true
- });
- }
- void setReaderViewCallback(
- {VoidCallback? onTapMainDocArea,
- Function(int pageIndex)? onMoveToChild,
- VoidCallback? onScrolling,
- VoidCallback? onScrollEnd,
- Function(int pageIndex)? onRecordLastJumpPageNum}) {
- _streamSubscription = _readerViewCallbackEventChannel
- .receiveBroadcastStream('setReaderViewCallback')
- .listen((data) {
- Map<dynamic, dynamic> result = data;
- String eventType = result['eventType'];
- switch (eventType) {
- case 'onTapMainDocArea':
- if (onTapMainDocArea != null) {
- onTapMainDocArea();
- }
- _onTapMainDocArea = true;
- this.onTapMainDocArea.sink.add(_onTapMainDocArea);
- break;
- case 'onMoveToChild':
- int pageIndex = result['pageIndex'];
- if (onMoveToChild != null) {
- int pageIndex = result['pageIndex'];
- onMoveToChild(pageIndex);
- }
- currentPageIndex = pageIndex;
- currentPageIndexStream.sink.add(currentPageIndex);
- break;
- case 'onRecordLastJumpPageNum':
- if (onRecordLastJumpPageNum != null) {
- int pageIndex = result['pageIndex'];
- onRecordLastJumpPageNum(pageIndex);
- }
- break;
- case 'onScrollEnd':
- if (onScrollEnd != null) {
- onScrollEnd();
- }
- if (_isScrolling) {
- _isScrolling = false;
- scrollStatusController.sink.add(_isScrolling);
- }
- break;
- case 'onScrolling':
- if (onScrolling != null) {
- onScrolling();
- }
- _isScrolling = true;
- scrollStatusController.sink.add(_isScrolling);
- break;
- default:
- break;
- }
- }, cancelOnError: true);
- }
- void invalidateAllChildren() {
- _methodChannel.invokeMethod('invalidateAllChildren');
- }
- void dispose() {
- currentPageIndexStream.close();
- scrollStatusController.close();
- _streamSubscription?.cancel();
- }
- }
|