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

Webview panel new behavior #140

Merged
merged 3 commits into from
Dec 20, 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
15 changes: 1 addition & 14 deletions lib/features/dapps/subfeatures/open_dapp/open_dapp_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,13 @@ class OpenAppPage extends HookConsumerWidget {
chainId: state.network?.chainId,
rpcUrl: state.network?.web3RpcHttpUrl,
walletAddress: state.account!.address,
pullToRefreshController: state.pullToRefreshController,
// onOverScrolled: (controller, x, y, clampedX, clampedY) =>
// presenter.handleOverScroll(y.toDouble()),
onScrollChanged: (controller, x, y) =>
presenter.handleScroll(y.toDouble()),
isDebug: false,
initialUrlRequest: URLRequest(
url: Uri.parse(url),
),
onLoadStop: (controller, url) {
presenter.injectCopyHandling();
presenter.injectScrollDetector();
},
onLoadError: (controller, url, code, message) =>
collectLog('onLoadError: $code: $message'),
Expand Down Expand Up @@ -214,12 +210,3 @@ class OpenAppPage extends HookConsumerWidget {
);
}
}

Widget getContainer() {
return Container(
margin: EdgeInsets.symmetric(vertical: 10),
height: 100,
width: double.infinity,
color: Colors.black,
);
}
118 changes: 94 additions & 24 deletions lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert';
import 'package:clipboard/clipboard.dart';
import 'package:datadashwallet/common/common.dart';
Expand Down Expand Up @@ -39,14 +40,6 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
void initState() {
super.initState();

state.pullToRefreshController = PullToRefreshController(
options: PullToRefreshOptions(
backgroundColor: Colors.transparent,
color: Colors.transparent,
enabled: true),
onRefresh: showPanel,
);

listen(
_accountUseCase.account,
(value) {
Expand All @@ -61,14 +54,8 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
});
}

@override
Future<void> dispose() {
return super.dispose();
}

void onWebViewCreated(InAppWebViewController controller) async {
notify(() => state.webviewController = controller);

updateCurrentUrl(null);
}

Expand Down Expand Up @@ -549,26 +536,107 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
final cancelDuration = const Duration(milliseconds: 400);
final settleDuration = const Duration(milliseconds: 400);

void handleScroll(double scroll) {
// If opposite direction then get back
final isScrollDownward = scroll > 0;
if (isScrollDownward) {
hidePanel();
} else {
showPanel();
}
injectScrollDetector() {
String jsCode = """
var pStart = { x: 0, y: 0 };
var pStop = { x: 0, y: 0 };

function swipeStart(e) {
if (typeof e["targetTouches"] !== "undefined") {
var touch = e.targetTouches[0];
pStart.x = touch.screenX;
pStart.y = touch.screenY;
} else {
pStart.x = e.screenX;
pStart.y = e.screenY;
}
}

function swipeEnd(e) {
if (typeof e["changedTouches"] !== "undefined") {
var touch = e.changedTouches[0];
pStop.x = touch.screenX;
pStop.y = touch.screenY;
} else {
pStop.x = e.screenX;
pStop.y = e.screenY;
}

swipeCheck();
}

function swipeCheck() {
var changeY = pStart.y - pStop.y;
var changeX = pStart.x - pStop.x;
if (isPullDown(changeY, changeX)) {
window.flutter_inappwebview?.callHandler("axs-scroll-detector", true);
} else if (isPullUp(changeY, changeX)) {
window.flutter_inappwebview?.callHandler("axs-scroll-detector", false);
}
}

function isPullDown(dY, dX) {
// methods of checking slope, length, direction of line created by swipe action
console.log(dY);
console.log(dX );
return (
dY < 0 &&
((Math.abs(dX) <= 100 && Math.abs(dY) >= 100 ) ||
(Math.abs(dX) / Math.abs(dY) <= 0.1 && dY >= 60))
);
}

function isPullUp(dY, dX) {
// Check if the gesture is a pull-up
console.log(dY);
console.log(dX);
return (
dY > 0 &&
((Math.abs(dX) <= 100 && Math.abs(dY) >= 100) ||
(Math.abs(dX) / Math.abs(dY) <= 0.1 && dY >= 60))
);
}

document.addEventListener(
"touchstart",
function (e) {
swipeStart(e);
},
false
);
document.addEventListener(
"touchend",
function (e) {
swipeEnd(e);
},
false
);
""";
state.webviewController!.evaluateJavascript(source: jsCode);

state.webviewController!.addJavaScriptHandler(
handlerName: 'axs-scroll-detector',
callback: (args) {
if (args[0] is bool) {
args[0] == true ? showPanel() : hidePanel();
}
},
);
}

Timer? panelTimer;

void showPanel() async {
state.pullToRefreshController!.endRefreshing();
final status = state.animationController!.status;
if (state.animationController!.value != 1 &&
status == AnimationStatus.completed ||
status == AnimationStatus.dismissed) {
await state.animationController!.animateTo(
1.0,
duration: settleDuration,
curve: Curves.ease,
);
panelTimer = Timer(const Duration(seconds: 3), hidePanel);
}
}

Expand All @@ -581,6 +649,9 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
duration: cancelDuration,
curve: Curves.easeInExpo,
);
if (panelTimer != null) {
panelTimer!.cancel();
}
}
}

Expand All @@ -594,7 +665,6 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
doubleTapTime = DateTime.now();
}


void showNetworkDetailsBottomSheet() {
showNetworkDetailsDialog(context!, network: state.network!);
}
Expand Down
2 changes: 0 additions & 2 deletions lib/features/dapps/subfeatures/open_dapp/open_dapp_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class OpenDAppState with EquatableMixin {
Account? account;
InAppWebViewController? webviewController;
AnimationController? animationController;
PullToRefreshController? pullToRefreshController;
int progress = 0;
Network? network;
int panelHideTimer = 5;
Expand All @@ -21,7 +20,6 @@ class OpenDAppState with EquatableMixin {
progress,
network,
animationController,
pullToRefreshController,
currentUrl,
isSecure
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ class AllowMultipleDoubleTap extends DoubleTapGestureRecognizer {
void rejectGesture(int pointer) {
acceptGesture(pointer);
}


}


class AllowMultipleVerticalDrag extends VerticalDragGestureRecognizer {
@override
void rejectGesture(int pointer) {
acceptGesture(pointer);
}
}
Loading
Loading