Skip to content

Commit

Permalink
Merge pull request #245 from humhub/f-47-internal-ios-zoom
Browse files Browse the repository at this point in the history
Zoom only on image opened fullscreen
  • Loading branch information
marc-farre authored Oct 17, 2024
2 parents 7bf9257 + d8cb0fd commit 14b3459
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 58 deletions.
5 changes: 4 additions & 1 deletion lib/flavored/web_view.f.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class FlavoredWebViewState extends ConsumerState<WebViewF> {
bottom: false,
child: InAppWebView(
initialUrlRequest: _initialRequest,
initialSettings: WebViewGlobalController.settings,
initialSettings: WebViewGlobalController.settings(),
pullToRefreshController: pullToRefreshController,
shouldOverrideUrlLoading: _shouldOverrideUrlLoading,
shouldInterceptFetchRequest: _shouldInterceptFetchRequest,
Expand Down Expand Up @@ -112,6 +112,7 @@ class FlavoredWebViewState extends ConsumerState<WebViewF> {
InAppWebViewController controller, NavigationAction action) async {
// 1st check if url is not def. app url and open it in a browser or inApp.
WebViewGlobalController.ajaxSetHeaders(headers: instance.customHeaders);
WebViewGlobalController.listenToImageOpen();
final url = action.request.url!.rawValue;

/// First BLOCK everything that rules out as blocked.
Expand Down Expand Up @@ -186,11 +187,13 @@ class FlavoredWebViewState extends ConsumerState<WebViewF> {
"document.querySelector('#account-login-form > div.form-group.field-login-rememberme').style.display='none';");
}
WebViewGlobalController.ajaxSetHeaders(headers: instance.customHeaders);
WebViewGlobalController.listenToImageOpen();
LoadingProvider.of(ref).dismissAll();
}

void _onLoadStart(InAppWebViewController controller, Uri? url) async {
WebViewGlobalController.ajaxSetHeaders(headers: instance.customHeaders);
WebViewGlobalController.listenToImageOpen();
}

void _onLoadError(InAppWebViewController controller, WebResourceRequest request, WebResourceError error) async {
Expand Down
126 changes: 79 additions & 47 deletions lib/pages/web_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,27 @@ class WebViewAppState extends ConsumerState<WebView> {
key: _scaffoldKey,
backgroundColor: HexColor(_manifest.themeColor),
body: SafeArea(
bottom: false,
// ignore: deprecated_member_use
child: WillPopScope(
onWillPop: () => exitApp(context, ref),
child: InAppWebView(
initialUrlRequest: _initialRequest,
initialSettings: WebViewGlobalController.settings,
pullToRefreshController: _pullToRefreshController,
shouldOverrideUrlLoading: _shouldOverrideUrlLoading,
onWebViewCreated: _onWebViewCreated,
shouldInterceptFetchRequest: _shouldInterceptFetchRequest,
onCreateWindow: _onCreateWindow,
onLoadStop: _onLoadStop,
onLoadStart: _onLoadStart,
onProgressChanged: _onProgressChanged,
onReceivedError: _onReceivedError,
onDownloadStartRequest: _onDownloadStartRequest,
onLongPressHitTestResult: WebViewGlobalController.onLongPressHitTestResult,
),
),
),
bottom: false,
// ignore: deprecated_member_use
child: WillPopScope(
onWillPop: () => exitApp(context, ref),
child: InAppWebView(
initialUrlRequest: _initialRequest,
initialSettings: WebViewGlobalController.settings(),
pullToRefreshController: _pullToRefreshController,
shouldOverrideUrlLoading: _shouldOverrideUrlLoading,
onWebViewCreated: _onWebViewCreated,
shouldInterceptFetchRequest: _shouldInterceptFetchRequest,
onCreateWindow: _onCreateWindow,
onLoadStop: _onLoadStop,
onLoadStart: _onLoadStart,
onProgressChanged: _onProgressChanged,
onReceivedError: _onReceivedError,
onDownloadStartRequest: _onDownloadStartRequest,
onLongPressHitTestResult:
WebViewGlobalController.onLongPressHitTestResult,
),
)),
);
}

Expand All @@ -129,12 +129,16 @@ class WebViewAppState extends ConsumerState<WebView> {
}
String? payloadFromPush = InitFromPush.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();

final url = action.request.url!.rawValue;

Expand All @@ -149,14 +153,18 @@ class WebViewAppState extends ConsumerState<WebView> {
}
// For all other external links
if (!url.startsWith(_manifest.baseUrl) && !action.isForMainFrame) {
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 @@ -171,7 +179,8 @@ 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 @@ -182,13 +191,15 @@ 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 @@ -212,18 +223,22 @@ 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();
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();
}

_onProgressChanged(InAppWebViewController controller, int progress) {
Expand All @@ -232,29 +247,35 @@ class WebViewAppState extends ConsumerState<WebView> {
}
}

void _onReceivedError(InAppWebViewController controller, WebResourceRequest request, WebResourceError error) {
if (error.description == 'net::ERR_INTERNET_DISCONNECTED') NoConnectionDialog.show(context);
void _onReceivedError(InAppWebViewController controller,
WebResourceRequest request, WebResourceError error) {
if (error.description == 'net::ERR_INTERNET_DISCONNECTED') {
NoConnectionDialog.show(context);
}
}

_concludeAuth(URLRequest request) {
_authBrowser.close();
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).setIsHideOpener(false);
ref.read(humHubProvider).clearSafeStorage();
FlutterAppBadger.updateBadgeCount(0);
Navigator.of(context).pushNamedAndRemoveUntil(Opener.path, (Route<dynamic> route) => false);
Navigator.of(context).pushNamedAndRemoveUntil(
Opener.path, (Route<dynamic> route) => false);
break;
case ChannelAction.hideOpener:
ref.read(humHubProvider).setIsHideOpener(true);
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 @@ -264,10 +285,13 @@ class WebViewAppState extends ConsumerState<WebView> {
}
break;
case ChannelAction.updateNotificationCount:
if (message.count != null) FlutterAppBadger.updateBadgeCount(message.count!);
if (message.count != null) {
FlutterAppBadger.updateBadgeCount(message.count!);
}
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 @@ -290,9 +314,11 @@ class WebViewAppState extends ConsumerState<WebView> {
final exitConfirmed = await showDialog<bool>(
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),
Expand All @@ -303,7 +329,8 @@ class WebViewAppState extends ConsumerState<WebView> {
var isHide = ref.read(humHubProvider).isHideDialog;
isHide
? SystemNavigator.pop()
: Navigator.of(context).pushNamedAndRemoveUntil(Opener.path, (Route<dynamic> route) => false);
: Navigator.of(context).pushNamedAndRemoveUntil(
Opener.path, (Route<dynamic> route) => false);
},
child: Text(AppLocalizations.of(context)!.yes),
),
Expand All @@ -314,7 +341,8 @@ 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 @@ -334,7 +362,8 @@ 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 @@ -351,19 +380,22 @@ 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
Loading

0 comments on commit 14b3459

Please sign in to comment.