Skip to content

Commit

Permalink
Merge pull request #266 from humhub/263-exit-the-app-does-not-work
Browse files Browse the repository at this point in the history
Fix exitApp
  • Loading branch information
marc-farre authored Jan 9, 2025
2 parents 34bf5ab + 3840b79 commit 8f373ca
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 61 deletions.
12 changes: 6 additions & 6 deletions lib/models/hum_hub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ enum OpenerState {
shown(true),
hidden(false);

final bool value;
final bool isShown;

const OpenerState(this.value);
const OpenerState(this.isShown);

String get headerValue => value ? '1' : '0';
String get headerValue => isShown ? '1' : '0';

@override
String toString() {
return value ? "shown" : "hidden";
return isShown ? "shown" : "hidden";
}
}

Expand Down Expand Up @@ -55,7 +55,7 @@ class HumHub {
Map<String, dynamic> toJson() => {
'manifest': manifest?.toJson(),
'manifestUri': manifestUrl,
'openerState': openerState.value,
'openerState': openerState.isShown,
'randomHash': randomHash,
'appVersion': appVersion,
'pushToken': pushToken,
Expand Down Expand Up @@ -130,7 +130,7 @@ class HumHub {
}

Future<RedirectAction> action(ref) async {
if (openerState.value) {
if (openerState.isShown) {
return RedirectAction.opener;
} else {
if (manifest != null) {
Expand Down
84 changes: 29 additions & 55 deletions lib/pages/web_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ class WebViewAppState extends ConsumerState<WebView> {
onProgressChanged: _onProgressChanged,
onReceivedError: _onReceivedError,
onDownloadStartRequest: _onDownloadStartRequest,
onLongPressHitTestResult:
WebViewGlobalController.onLongPressHitTestResult,
onLongPressHitTestResult: WebViewGlobalController.onLongPressHitTestResult,
),
)),
);
Expand Down Expand Up @@ -129,15 +128,12 @@ class WebViewAppState extends ConsumerState<WebView> {
}
String? payloadFromPush = InitFromUrl.usePayload();
if (payloadFromPush != null) url = payloadFromPush;
return URLRequest(
url: WebUri(url ?? _manifest.startUrl),
headers: ref.read(humHubProvider).customHeaders);
return URLRequest(url: WebUri(url ?? _manifest.startUrl), headers: ref.read(humHubProvider).customHeaders);
}

Future<NavigationActionPolicy?> _shouldOverrideUrlLoading(
InAppWebViewController controller, NavigationAction action) async {
WebViewGlobalController.ajaxSetHeaders(
headers: ref.read(humHubProvider).customHeaders);
WebViewGlobalController.ajaxSetHeaders(headers: ref.read(humHubProvider).customHeaders);
WebViewGlobalController.listenToImageOpen();
WebViewGlobalController.appendViewportFitCover();

Expand All @@ -156,18 +152,14 @@ class WebViewAppState extends ConsumerState<WebView> {
if (!url.startsWith(_manifest.baseUrl) &&
!action.isForMainFrame &&
action.navigationType == NavigationType.LINK_ACTIVATED) {
await launchUrl(action.request.url!.uriValue,
mode: LaunchMode.externalApplication);
await launchUrl(action.request.url!.uriValue, mode: LaunchMode.externalApplication);
return NavigationActionPolicy.CANCEL;
}
// 2nd Append customHeader if url is in app redirect and CANCEL the requests without custom headers
if (Platform.isAndroid ||
action.navigationType == NavigationType.LINK_ACTIVATED ||
action.navigationType == NavigationType.FORM_SUBMITTED) {
Map<String, String> mergedMap = {
...?_initialRequest.headers,
...?action.request.headers
};
Map<String, String> mergedMap = {...?_initialRequest.headers, ...?action.request.headers};
URLRequest newRequest = action.request.copyWith(headers: mergedMap);
controller.loadUrl(urlRequest: newRequest);
return NavigationActionPolicy.CANCEL;
Expand All @@ -182,8 +174,7 @@ class WebViewAppState extends ConsumerState<WebView> {
await controller.addWebMessageListener(
WebMessageListener(
jsObjectName: "flutterChannel",
onPostMessage:
(inMessage, sourceOrigin, isMainFrame, replyProxy) async {
onPostMessage: (inMessage, sourceOrigin, isMainFrame, replyProxy) async {
logInfo(inMessage);
ChannelMessage message = ChannelMessage.fromJson(inMessage!.data);
await _handleJSMessage(message, _headlessWebView!);
Expand All @@ -194,15 +185,13 @@ class WebViewAppState extends ConsumerState<WebView> {
WebViewGlobalController.setValue(controller);
}

Future<FetchRequest?> _shouldInterceptFetchRequest(
InAppWebViewController controller, FetchRequest request) async {
Future<FetchRequest?> _shouldInterceptFetchRequest(InAppWebViewController controller, FetchRequest request) async {
logDebug("_shouldInterceptFetchRequest");
request.headers?.addAll(_initialRequest.headers!);
return request;
}

Future<bool?> _onCreateWindow(InAppWebViewController controller,
CreateWindowAction createWindowAction) async {
Future<bool?> _onCreateWindow(InAppWebViewController controller, CreateWindowAction createWindowAction) async {
WebUri? urlToOpen = createWindowAction.request.url;

if (urlToOpen == null) return Future.value(false);
Expand All @@ -226,22 +215,20 @@ class WebViewAppState extends ConsumerState<WebView> {
_onLoadStop(InAppWebViewController controller, Uri? url) {
// Disable remember me checkbox on login and set def. value to true: check if the page is actually login page, if it is inject JS that hides element
if (url!.path.contains('/user/auth/login')) {
WebViewGlobalController.value!.evaluateJavascript(
source: "document.querySelector('#login-rememberme').checked=true");
WebViewGlobalController.value!
.evaluateJavascript(source: "document.querySelector('#login-rememberme').checked=true");
WebViewGlobalController.value!.evaluateJavascript(
source:
"document.querySelector('#account-login-form > div.form-group.field-login-rememberme').style.display='none';");
}
WebViewGlobalController.ajaxSetHeaders(
headers: ref.read(humHubProvider).customHeaders);
WebViewGlobalController.ajaxSetHeaders(headers: ref.read(humHubProvider).customHeaders);
WebViewGlobalController.listenToImageOpen();
WebViewGlobalController.appendViewportFitCover();
LoadingProvider.of(ref).dismissAll();
}

void _onLoadStart(InAppWebViewController controller, Uri? url) async {
WebViewGlobalController.ajaxSetHeaders(
headers: ref.read(humHubProvider).customHeaders);
WebViewGlobalController.ajaxSetHeaders(headers: ref.read(humHubProvider).customHeaders);
WebViewGlobalController.listenToImageOpen();
WebViewGlobalController.appendViewportFitCover();
}
Expand All @@ -252,8 +239,7 @@ class WebViewAppState extends ConsumerState<WebView> {
}
}

void _onReceivedError(InAppWebViewController controller,
WebResourceRequest request, WebResourceError error) {
void _onReceivedError(InAppWebViewController controller, WebResourceRequest request, WebResourceError error) {
if (error.description == 'net::ERR_INTERNET_DISCONNECTED') {
NoConnectionDialog.show(context);
}
Expand All @@ -264,21 +250,18 @@ class WebViewAppState extends ConsumerState<WebView> {
WebViewGlobalController.value!.loadUrl(urlRequest: request);
}

Future<void> _handleJSMessage(
ChannelMessage message, HeadlessInAppWebView headlessWebView) async {
Future<void> _handleJSMessage(ChannelMessage message, HeadlessInAppWebView headlessWebView) async {
switch (message.action) {
case ChannelAction.showOpener:
ref.read(humHubProvider).setOpenerState(OpenerState.shown);
Navigator.of(context).pushNamedAndRemoveUntil(
OpenerPage.path, (Route<dynamic> route) => false);
Navigator.of(context).pushNamedAndRemoveUntil(OpenerPage.path, (Route<dynamic> route) => false);
break;
case ChannelAction.hideOpener:
ref.read(humHubProvider).setOpenerState(OpenerState.hidden);
ref.read(humHubProvider).setHash(HumHub.generateHash(32));
break;
case ChannelAction.registerFcmDevice:
String? token = ref.read(pushTokenProvider).value ??
await FirebaseMessaging.instance.getToken();
String? token = ref.read(pushTokenProvider).value ?? await FirebaseMessaging.instance.getToken();
if (token != null) {
WebViewGlobalController.ajaxPost(
url: message.url!,
Expand All @@ -293,8 +276,7 @@ class WebViewAppState extends ConsumerState<WebView> {
}
break;
case ChannelAction.unregisterFcmDevice:
String? token = ref.read(pushTokenProvider).value ??
await FirebaseMessaging.instance.getToken();
String? token = ref.read(pushTokenProvider).value ?? await FirebaseMessaging.instance.getToken();
if (token != null) {
WebViewGlobalController.ajaxPost(
url: message.url!,
Expand All @@ -308,32 +290,29 @@ class WebViewAppState extends ConsumerState<WebView> {
}
}

Future<bool> exitApp(context, ref) async {
Future<bool> exitApp(BuildContext context, WidgetRef ref) async {
bool canGoBack = await WebViewGlobalController.value!.canGoBack();
if (canGoBack) {
WebViewGlobalController.value!.goBack();
return Future.value(false);
} else {
final exitConfirmed = await showDialog<bool>(
// ignore: use_build_context_synchronously
context: context,
builder: (context) => AlertDialog(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0))),
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(10.0))),
title: Text(AppLocalizations.of(context)!.web_view_exit_popup_title),
content:
Text(AppLocalizations.of(context)!.web_view_exit_popup_content),
content: Text(AppLocalizations.of(context)!.web_view_exit_popup_content),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.of(context).pop(false),
child: Text(AppLocalizations.of(context)!.no),
),
TextButton(
onPressed: () {
var isHide = ref.read(humHubProvider).isHideDialog;
isHide
? SystemNavigator.pop()
: Navigator.of(context).pushNamedAndRemoveUntil(
OpenerPage.path, (Route<dynamic> route) => false);
ref.read(humHubProvider).openerState.isShown
? Navigator.of(context).pushNamedAndRemoveUntil(OpenerPage.path, (Route<dynamic> route) => false)
: SystemNavigator.pop();
},
child: Text(AppLocalizations.of(context)!.yes),
),
Expand All @@ -344,8 +323,7 @@ class WebViewAppState extends ConsumerState<WebView> {
}
}

void _onDownloadStartRequest(InAppWebViewController controller,
DownloadStartRequest downloadStartRequest) async {
void _onDownloadStartRequest(InAppWebViewController controller, DownloadStartRequest downloadStartRequest) async {
PersistentBottomSheetController? persistentController;
//bool isBottomSheetVisible = false;

Expand All @@ -365,8 +343,7 @@ class WebViewAppState extends ConsumerState<WebView> {
isDone = true;
scaffoldMessengerStateKey.currentState?.showSnackBar(
SnackBar(
content: Text(
'${AppLocalizations.of(context)!.file_download}: $filename'),
content: Text('${AppLocalizations.of(context)!.file_download}: $filename'),
action: SnackBarAction(
label: AppLocalizations.of(context)!.open,
onPressed: () {
Expand All @@ -383,22 +360,19 @@ class WebViewAppState extends ConsumerState<WebView> {
downloadTimer = Timer(const Duration(seconds: 1), () {
// Show the persistent bottom sheet if not already shown
if (!isDone) {
persistentController =
_scaffoldKey.currentState!.showBottomSheet((context) {
persistentController = _scaffoldKey.currentState!.showBottomSheet((context) {
return Container(
width: MediaQuery.of(context).size.width,
height: 100,
color: const Color(0xff313033),
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"${AppLocalizations.of(context)!.downloading}...",
style: const TextStyle(
fontWeight: FontWeight.bold, color: Colors.white),
style: const TextStyle(fontWeight: FontWeight.bold, color: Colors.white),
),
Stack(
alignment: Alignment.center,
Expand Down

0 comments on commit 8f373ca

Please sign in to comment.