Skip to content

Commit

Permalink
Merge pull request #256 from humhub/f-internal-64-v2
Browse files Browse the repository at this point in the history
Add viewport-fit=cover to every viewport meta tag
  • Loading branch information
marc-farre authored Nov 19, 2024
2 parents 2d8134f + 760f049 commit 2677e66
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
4 changes: 3 additions & 1 deletion lib/flavored/models/humhub.f.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class HumHubF extends HumHub {
'x-humhub-app-token': randomHash!,
'x-humhub-app': appVersion ?? '1.0.0',
'x-humhub-app-bundle-id': bundleId,
'x-humhub-app-ostate': isHideOpener ? '1' : '0'
'x-humhub-app-ostate': isHideOpener ? '1' : '0',
'x-humhub-app-is-ios': isIos ? '1' : '0',
'x-humhub-app-is-android': isAndroid ? '1' : '0'
};

static Future<HumHubF> initialize() async {
Expand Down
3 changes: 3 additions & 0 deletions lib/flavored/web_view.f.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class FlavoredWebViewState extends ConsumerState<WebViewF> {
// 1st check if url is not def. app url and open it in a browser or inApp.
WebViewGlobalController.ajaxSetHeaders(headers: instance.customHeaders);
WebViewGlobalController.listenToImageOpen();
WebViewGlobalController.appendViewportFitCover();
final url = action.request.url!.rawValue;

/// First BLOCK everything that rules out as blocked.
Expand Down Expand Up @@ -188,12 +189,14 @@ class FlavoredWebViewState extends ConsumerState<WebViewF> {
}
WebViewGlobalController.ajaxSetHeaders(headers: instance.customHeaders);
WebViewGlobalController.listenToImageOpen();
WebViewGlobalController.appendViewportFitCover();
LoadingProvider.of(ref).dismissAll();
}

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

void _onLoadError(InAppWebViewController controller, WebResourceRequest request, WebResourceError error) async {
Expand Down
7 changes: 6 additions & 1 deletion lib/models/hum_hub.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:io';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
Expand All @@ -15,6 +16,8 @@ class HumHub {
String? randomHash;
String? appVersion;
String? pushToken;
final bool isIos = Platform.isIOS || Platform.isMacOS;
final bool isAndroid = Platform.isAndroid;

HumHub(
{this.manifest, this.manifestUrl, this.isHideOpener = false, this.randomHash, this.appVersion, this.pushToken});
Expand Down Expand Up @@ -65,7 +68,9 @@ class HumHub {
Map<String, String> get customHeaders => {
'x-humhub-app-token': randomHash!,
'x-humhub-app': appVersion ?? '1.0.0',
'x-humhub-app-ostate': isHideOpener ? '1' : '0'
'x-humhub-app-ostate': isHideOpener ? '1' : '0',
'x-humhub-app-is-ios': isIos ? '1' : '0',
'x-humhub-app-is-android': isAndroid ? '1' : '0'
};

static Future<Widget> app(String bundleId) async {
Expand Down
3 changes: 3 additions & 0 deletions lib/pages/web_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class WebViewAppState extends ConsumerState<WebView> {
WebViewGlobalController.ajaxSetHeaders(
headers: ref.read(humHubProvider).customHeaders);
WebViewGlobalController.listenToImageOpen();
WebViewGlobalController.appendViewportFitCover();

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

Expand Down Expand Up @@ -232,13 +233,15 @@ class WebViewAppState extends ConsumerState<WebView> {
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.listenToImageOpen();
WebViewGlobalController.appendViewportFitCover();
}

_onProgressChanged(InAppWebViewController controller, int progress) {
Expand Down
36 changes: 22 additions & 14 deletions lib/util/web_view_global_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class WebViewGlobalController {
/// [ref] is reference to the app state.
/// [url] is the URL to evaluate.
/// @return `true` if the URL should open in a new window, `false` otherwise.
static bool openCreateWindowInWebView(
{required String url, required Manifest manifest}) {
static bool openCreateWindowInWebView({required String url, required Manifest manifest}) {
String? baseUrl = manifest.baseUrl;
if (url.startsWith('$baseUrl/file/file/download')) return true;
if (url.startsWith('$baseUrl/u')) return true;
Expand All @@ -33,10 +32,7 @@ class WebViewGlobalController {
_value = newValue;
}

static void ajaxPost(
{required String url,
required String data,
Map<String, String>? headers}) {
static void ajaxPost({required String url, required String data, Map<String, String>? headers}) {
String jsonHeaders = jsonEncode(headers);
String jsCode4 = """
\$.ajax({
Expand All @@ -51,18 +47,14 @@ class WebViewGlobalController {
}

static void ajaxSetHeaders({Map<String, String>? headers}) {
String jsCode =
"\$.ajaxSetup({headers: ${jsonEncode(headers).toString()}});";
String jsCode = "\$.ajaxSetup({headers: ${jsonEncode(headers).toString()}});";
value?.evaluateJavascript(source: jsCode);
}

static void onLongPressHitTestResult(InAppWebViewController controller,
InAppWebViewHitTestResult hitResult) async {
static void onLongPressHitTestResult(InAppWebViewController controller, InAppWebViewHitTestResult hitResult) async {
if (hitResult.extra != null &&
([
InAppWebViewHitTestResultType.SRC_ANCHOR_TYPE,
InAppWebViewHitTestResultType.EMAIL_TYPE
].contains(hitResult.type))) {
([InAppWebViewHitTestResultType.SRC_ANCHOR_TYPE, InAppWebViewHitTestResultType.EMAIL_TYPE]
.contains(hitResult.type))) {
Clipboard.setData(
ClipboardData(text: hitResult.extra!),
);
Expand Down Expand Up @@ -119,6 +111,22 @@ class WebViewGlobalController {
);
}

static void appendViewportFitCover() {
value?.evaluateJavascript(source: """
(function() {
var metaTags = document.getElementsByTagName('meta');
for (var i = 0; i < metaTags.length; i++) {
if (metaTags[i].name.toLowerCase() === 'viewport') {
var content = metaTags[i].content;
if (!content.includes('viewport-fit=cover')) {
metaTags[i].content = content + ', viewport-fit=cover';
}
}
}
})();
""");
}

static InAppWebViewSettings settings({bool zoom = false}) {
return InAppWebViewSettings(
useShouldOverrideUrlLoading: true,
Expand Down

0 comments on commit 2677e66

Please sign in to comment.