Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Webview gesture conflicts fix #67

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 40 additions & 21 deletions lib/features/dapps/subfeatures/open_dapp/open_dapp_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:web3_provider/web3_provider.dart';
import 'open_dapp_presenter.dart';
import 'open_dapp_state.dart';
import 'widgets/bridge_params.dart';
import 'widgets/allow_multiple_gestures.dart';

class OpenAppPage extends HookConsumerWidget {
const OpenAppPage({Key? key, required this.url}) : super(key: key);
Expand All @@ -19,37 +20,52 @@ class OpenAppPage extends HookConsumerWidget {
Widget build(BuildContext context, WidgetRef ref) {
final presenter = ref.read(openDAppPageContainer.actions);
final state = ref.watch(openDAppPageContainer.state);
const primaryVelocity = 500;

const primaryVelocity = 2000;
return Scaffold(
backgroundColor: ColorsTheme.of(context).screenBackground,
body: SafeArea(
child: PresenterHooks(
presenter: presenter,
child: Stack(
children: [
GestureDetector(
onHorizontalDragEnd: (details) async {
final webViewController = state.webviewController!;

if (details.primaryVelocity! < 0 - primaryVelocity &&
(await webViewController.canGoForward())) {
webViewController.goForward();
}
RawGestureDetector(
behavior: HitTestBehavior.opaque,
gestures: {
AllowMultipleHorizontalDrag:
GestureRecognizerFactoryWithHandlers<
AllowMultipleHorizontalDrag>(
() => AllowMultipleHorizontalDrag(),
(AllowMultipleHorizontalDrag instance) {
instance.onEnd = (details) async {
final webViewController = state.webviewController!;
if (details.primaryVelocity! < 0 - primaryVelocity &&
(await webViewController.canGoForward())) {
webViewController.goForward();
}

if (details.primaryVelocity! > primaryVelocity &&
(await webViewController.canGoBack())) {
webViewController.goBack();
}
if (details.primaryVelocity! > primaryVelocity &&
(await webViewController.canGoBack())) {
webViewController.goBack();
}

if (details.primaryVelocity! > primaryVelocity &&
!(await webViewController.canGoBack())) {
if (BottomFlowDialog.maybeOf(context) != null) {
BottomFlowDialog.of(context).close();
}
}
if (details.primaryVelocity! > primaryVelocity &&
!(await webViewController.canGoBack())) {
if (BottomFlowDialog.maybeOf(context) != null) {
BottomFlowDialog.of(context).close();
}
}
};
},
),
AllowMultipleDoubleTap: GestureRecognizerFactoryWithHandlers<
AllowMultipleDoubleTap>(
() => AllowMultipleDoubleTap(),
(AllowMultipleDoubleTap instance) {
instance.onDoubleTap =
() => state.webviewController!.reload();
},
)
},
onDoubleTap: () => state.webviewController!.reload(),
child: InAppWebViewEIP1193(
chainId: state.network?.chainId,
rpcUrl: state.network?.web3RpcHttpUrl,
Expand Down Expand Up @@ -122,6 +138,9 @@ class OpenAppPage extends HookConsumerWidget {
Factory<VerticalDragGestureRecognizer>(
() => VerticalDragGestureRecognizer(),
),
Factory<HorizontalDragGestureRecognizer>(
() => HorizontalDragGestureRecognizer(),
),
},
androidOnPermissionRequest:
(controller, origin, resources) async {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:flutter/gestures.dart';

// Custom Gesture Recognizer.
// rejectGesture() is overridden. When a gesture is rejected, this is the function that is called. By default, it disposes of the
// Recognizer and runs clean up. However we modified it so that instead the Recognizer is disposed of, it is actually manually added.
// The result is instead you have one Recognizer winning the Arena, you have two. It is a win-win.
class AllowMultipleHorizontalDrag extends HorizontalDragGestureRecognizer {
@override
void rejectGesture(int pointer) {
acceptGesture(pointer);
}

}

class AllowMultipleDoubleTap extends DoubleTapGestureRecognizer {
@override
void rejectGesture(int pointer) {
acceptGesture(pointer);
}
}
15 changes: 5 additions & 10 deletions lib/features/portfolio/presentation/portfolio_page_presenter.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:datadashwallet/common/common.dart';
import 'package:datadashwallet/core/core.dart';
import 'package:url_launcher/url_launcher.dart';
import 'portfolio_page_state.dart';

final portfolioContainer =
Expand Down Expand Up @@ -80,20 +79,16 @@ class PortfolioPresenter extends CompletePresenter<PortfolioState> {
}
}

void buyNFt() {
String? getNftUrl() {
final enabledNetwork = _chainConfigurationUseCase.networks.value
.where((element) => element.enabled)
.toList()[0];
if (enabledNetwork.chainId == Config.mxcTestnetChainId) {
openUrl(Urls.mxcTestnetNftMarketPlace);
return Urls.mxcTestnetNftMarketPlace;
} else if (enabledNetwork.chainId == Config.mxcMainnetChainId) {
openUrl(Urls.mxcMainnetNftMarketPlace);
return Urls.mxcMainnetNftMarketPlace;
} else {
return null;
}
}

void openUrl(String url) async {
(await canLaunchUrl(Uri.parse(url))) == true
? launchUrl(Uri.parse(url), mode: LaunchMode.inAppWebView)
: null;
}
}
14 changes: 13 additions & 1 deletion lib/features/portfolio/subfeatures/nft/nft_list/nft_list.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:io';

import 'package:datadashwallet/core/core.dart';
import 'package:datadashwallet/features/dapps/dapps.dart';
import 'package:datadashwallet/features/portfolio/presentation/portfolio_page_presenter.dart';
import 'package:datadashwallet/features/portfolio/subfeatures/nft/nft_list/utils.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -50,7 +52,17 @@ class NFTList extends HookConsumerWidget {
key: const Key('buyNFTButton'),
contentPadding:
const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
onTap: () => presenter.buyNFt(),
onTap: () {
final launchUrl = presenter.getNftUrl();
if (launchUrl != null) {
Navigator.of(context).push(route.featureDialog(
maintainState: false,
OpenAppPage(
url: launchUrl,
),
));
}
},
title: translate('buy_x').replaceFirst('{0}', 'NFT'),
iconData: Icons.add,
alignIconStart: true,
Expand Down
Loading