From 681454b6a849e5f2b795648cace34a0a384ec61c Mon Sep 17 00:00:00 2001 From: phuoc Date: Fri, 14 Jun 2024 16:20:44 +0700 Subject: [PATCH] fix branch link canvas Signed-off-by: phuoc --- lib/screen/scan_qr/scan_qr_page.dart | 99 ---------------------------- lib/service/deeplink_service.dart | 26 +++++--- lib/view/stream_device_view.dart | 2 +- 3 files changed, 17 insertions(+), 110 deletions(-) diff --git a/lib/screen/scan_qr/scan_qr_page.dart b/lib/screen/scan_qr/scan_qr_page.dart index 9c156ad96..85f6cc023 100644 --- a/lib/screen/scan_qr/scan_qr_page.dart +++ b/lib/screen/scan_qr/scan_qr_page.dart @@ -6,7 +6,6 @@ // import 'dart:async'; -import 'dart:convert'; import 'dart:io'; import 'package:autonomy_flutter/common/injector.dart'; @@ -18,10 +17,8 @@ import 'package:autonomy_flutter/screen/bloc/accounts/accounts_bloc.dart' import 'package:autonomy_flutter/screen/bloc/ethereum/ethereum_bloc.dart'; import 'package:autonomy_flutter/screen/bloc/persona/persona_bloc.dart'; import 'package:autonomy_flutter/screen/bloc/tezos/tezos_bloc.dart'; -import 'package:autonomy_flutter/screen/detail/preview/canvas_device_bloc.dart'; import 'package:autonomy_flutter/screen/global_receive/receive_page.dart'; import 'package:autonomy_flutter/service/audit_service.dart'; -import 'package:autonomy_flutter/service/canvas_client_service_v2.dart'; import 'package:autonomy_flutter/service/deeplink_service.dart'; import 'package:autonomy_flutter/service/metric_client_service.dart'; import 'package:autonomy_flutter/service/navigation_service.dart'; @@ -31,21 +28,17 @@ import 'package:autonomy_flutter/util/constants.dart'; import 'package:autonomy_flutter/util/log.dart'; import 'package:autonomy_flutter/util/route_ext.dart'; import 'package:autonomy_flutter/util/style.dart'; -import 'package:autonomy_flutter/util/ui_helper.dart'; import 'package:autonomy_flutter/view/back_appbar.dart'; import 'package:autonomy_flutter/view/header.dart'; import 'package:autonomy_flutter/view/primary_button.dart'; -import 'package:autonomy_flutter/view/stream_device_view.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:feralfile_app_theme/feral_file_app_theme.dart'; -import 'package:feralfile_app_tv_proto/feralfile_app_tv_proto.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:qr_code_scanner/qr_code_scanner.dart'; -import 'package:synchronized/synchronized.dart'; // ignore_for_file: constant_identifier_names @@ -280,7 +273,6 @@ enum ScannerItem { BEACON_CONNECT, ETH_ADDRESS, XTZ_ADDRESS, - CANVAS_DEVICE, GLOBAL } @@ -304,8 +296,6 @@ class QRScanViewState extends State bool? _cameraPermission; String? currentCode; final metricClient = injector(); - final _navigationService = injector(); - late Lock _lock; Timer? _timer; late bool _shouldPop; @@ -315,7 +305,6 @@ class QRScanViewState extends State super.initState(); _shouldPop = !(widget.scannerItem == ScannerItem.GLOBAL); unawaited(_checkPermission()); - _lock = Lock(); } @override @@ -634,8 +623,6 @@ class QRScanViewState extends State Text('scan_qr'.tr(), style: theme.primaryTextTheme.labelLarge), ], ); - case ScannerItem.CANVAS_DEVICE: - return const SizedBox(); } } @@ -688,7 +675,6 @@ class QRScanViewState extends State } else { _handleError(code); } - break; case ScannerItem.BEACON_CONNECT: if (code.startsWith('tezos://')) { @@ -696,7 +682,6 @@ class QRScanViewState extends State } else { _handleError(code); } - break; case ScannerItem.ETH_ADDRESS: case ScannerItem.XTZ_ADDRESS: @@ -711,40 +696,14 @@ class QRScanViewState extends State Navigator.pop(context, code); } await Future.delayed(const Duration(milliseconds: 300)); - break; case ScannerItem.GLOBAL: if (code.startsWith('wc:')) { await _handleAutonomyConnect(code); } else if (code.startsWith('tezos:')) { await _handleBeaconConnect(code); - /* TODO: Remove or support for multiple wallets - } else if (code.startsWith("tz1")) { - Navigator.of(context).popAndPushNamed(SendCryptoPage.tag, - arguments: SendData(CryptoType.XTZ, code)); - } else { - try { - final _ = EthereumAddress.fromHex(code); - Navigator.of(context).popAndPushNamed(SendCryptoPage.tag, - arguments: SendData(CryptoType.ETH, code)); - } catch (err) { - log(err.toString()); - } - */ - } else if (_isCanvasQrCode(code)) { - unawaited(_lock.synchronized(() async { - await _handleCanvasQrCode(code); - })); - } else { - _handleError(code); - } - break; - case ScannerItem.CANVAS_DEVICE: - if (_isCanvasQrCode(code)) { - unawaited(_lock.synchronized(() => _handleCanvasQrCode(code))); } else { _handleError(code); } - break; } if (mounted) { await resumeCamera(); @@ -759,64 +718,6 @@ class QRScanViewState extends State }); } - bool _isCanvasQrCode(String code) { - try { - CanvasDevice.fromJson(jsonDecode(code)); - return true; - } catch (err) { - return false; - } - } - - Future _handleCanvasQrCode(String code) async { - log.info('Canvas device scanned: $code'); - setState(() { - _isLoading = true; - }); - await pauseCamera(); - if (!mounted) { - return false; - } - try { - final device = CanvasDevice.fromJson(jsonDecode(code)); - final canvasClient = injector(); - final result = await canvasClient.addQrDevice(device); - final isSuccessful = result != null; - if (!mounted) { - return false; - } - if (_shouldPop) { - Navigator.pop(context, isSuccessful); - } else { - await UIHelper.showFlexibleDialog( - context, - BlocProvider.value( - value: injector(), - child: const StreamDeviceView(), - ), - isDismissible: true, - autoDismissAfter: 3, - ); - } - injector().add(CanvasDeviceAppendDeviceEvent(device)); - return isSuccessful; - } catch (e) { - if (mounted) { - if (_shouldPop) { - Navigator.pop(context, false); - } - if (e.toString().contains('DEADLINE_EXCEEDED') || true) { - await UIHelper.showInfoDialog( - _navigationService.navigatorKey.currentContext!, - 'failed_to_connect'.tr(), - 'canvas_ip_fail'.tr(), - closeButton: 'close'.tr()); - } - } - } - return false; - } - void _handleError(String data) { setState(() { isScanDataError = true; diff --git a/lib/service/deeplink_service.dart b/lib/service/deeplink_service.dart index 3738d9075..7e42ba925 100644 --- a/lib/service/deeplink_service.dart +++ b/lib/service/deeplink_service.dart @@ -28,6 +28,7 @@ import 'package:autonomy_flutter/service/remote_config_service.dart'; import 'package:autonomy_flutter/service/tezos_beacon_service.dart'; import 'package:autonomy_flutter/service/wc2_service.dart'; import 'package:autonomy_flutter/util/constants.dart'; +import 'package:autonomy_flutter/util/custom_route_observer.dart'; import 'package:autonomy_flutter/util/dio_exception_ext.dart'; import 'package:autonomy_flutter/util/log.dart'; import 'package:autonomy_flutter/util/string_ext.dart'; @@ -424,17 +425,22 @@ class DeeplinkServiceImpl extends DeeplinkService { return; } if (isSuccessful) { - await UIHelper.showFlexibleDialog( - _navigationService.context, - BlocProvider.value( - value: injector(), - child: const StreamDeviceView(), - ), - isDismissible: true, - autoDismissAfter: 3, - ); + if (CustomRouteObserver.currentRoute?.settings.name == + AppRouter.scanQRPage) { + /// in case scan when open scanQRPage, + /// scan with navigation home page does not go to this flow + _navigationService.goBack(result: device); + } else { + await UIHelper.showFlexibleDialog( + _navigationService.context, + BlocProvider.value( + value: injector(), + child: const StreamDeviceView(), + ), + isDismissible: true, + ); + } } - injector().add(CanvasDeviceAppendDeviceEvent(device)); default: memoryValues.branchDeeplinkData.value = null; diff --git a/lib/view/stream_device_view.dart b/lib/view/stream_device_view.dart index 0ccbcd803..178b271ae 100644 --- a/lib/view/stream_device_view.dart +++ b/lib/view/stream_device_view.dart @@ -176,7 +176,7 @@ class _StreamDeviceViewState extends State { Future scanToAddMore(BuildContext context) async { final device = await Navigator.of(context) - .pushNamed(AppRouter.scanQRPage, arguments: ScannerItem.CANVAS_DEVICE); + .pushNamed(AppRouter.scanQRPage, arguments: ScannerItem.GLOBAL); log.info('device selected: $device'); _canvasDeviceBloc.add(CanvasDeviceGetDevicesEvent()); }