Skip to content

Commit dce1a13

Browse files
committed
Hopefully fix WASM compatibility
1 parent 314f8c9 commit dce1a13

5 files changed

Lines changed: 202 additions & 2 deletions

File tree

flutter_web_auth_2/lib/flutter_web_auth_2.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import 'dart:async';
2-
import 'dart:io';
32

43
import 'package:flutter/cupertino.dart';
54
import 'package:flutter/foundation.dart';
65
import 'package:flutter_web_auth_2/src/options.dart';
6+
import 'package:flutter_web_auth_2/src/platform/platform_is.dart';
77
import 'package:flutter_web_auth_2_platform_interface/flutter_web_auth_2_platform_interface.dart';
88

99
export 'src/options.dart';
@@ -35,7 +35,7 @@ class FlutterWebAuth2 {
3535
_OnAppLifecycleResumeObserver(_cleanUpDanglingCalls);
3636

3737
static void _assertCallbackScheme(String callbackUrlScheme) {
38-
if ((kIsWeb || (!Platform.isWindows && !Platform.isLinux)) &&
38+
if ((PlatformIs.web || (!PlatformIs.windows && !PlatformIs.linux)) &&
3939
!_schemeRegExp.hasMatch(callbackUrlScheme)) {
4040
throw ArgumentError.value(
4141
callbackUrlScheme,
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Adapted from https://gist.github.com/rydmike/1771fe24c050ebfe792fa309371154d8
2+
3+
import 'package:flutter_web_auth_2/src/platform/universal_platform_none.dart'
4+
if (dart.library.io) 'package:flutter_web_auth_2/src/platform/universal_platform_vm.dart'
5+
if (dart.library.js_interop) 'package:flutter_web_auth_2/src/platform/universal_platform_web.dart';
6+
7+
/// A universal platform checker.
8+
///
9+
/// Can be used to check active physical Flutter platform on all platforms.
10+
///
11+
/// To check what host platform the app is running on use:
12+
///
13+
/// * PlatformIs.android
14+
/// * PlatformIs.iOS
15+
/// * PlatformIs.macOS
16+
/// * PlatformIs.windows
17+
/// * PlatformIs.linux
18+
/// * PlatformIs.fuchsia
19+
///
20+
/// To check the device type use:
21+
///
22+
/// * PlatformIs.mobile (Android or iOS)
23+
/// * PlatformIs.desktop (Windows, macOS or Linux)
24+
///
25+
/// Currently Fuchsia is not considered mobile nor desktop, even if it
26+
/// might be so in the future.
27+
///
28+
/// To check if the Flutter application is running on Web you can use:
29+
///
30+
/// * PlatformIs.web
31+
///
32+
/// Alternatively the Flutter foundation compile time constant kIsWeb also
33+
/// works well for that.
34+
///
35+
/// The platform checks are supported independently on web. You can use
36+
/// PlatformIs windows, iOS, macOS, Android and Linux to check what the host
37+
/// platform is when you are running a Flutter Web application.
38+
///
39+
/// Checking if we are running on a Fuchsia host in a Web browser, is not yet
40+
/// fully supported. If running in a Web browser on Fuchsia, PlatformIs.web
41+
/// will be true, but PlatformIs.fuchsia will be false. Future versions, when
42+
/// Fuchsia is released, may fix this.
43+
class PlatformIs {
44+
PlatformIs._();
45+
46+
/// Is Web?
47+
static bool get web => UniversalPlatform.web;
48+
49+
/// Is macOS?
50+
static bool get macOS => UniversalPlatform.macOS;
51+
52+
/// Is Windows?
53+
static bool get windows => UniversalPlatform.windows;
54+
55+
/// Is Linux?
56+
static bool get linux => UniversalPlatform.linux;
57+
58+
/// Is Android?
59+
static bool get android => UniversalPlatform.android;
60+
61+
/// Is iOS?
62+
static bool get iOS => UniversalPlatform.iOS;
63+
64+
/// Is Fuchsia?
65+
static bool get fuchsia => UniversalPlatform.fuchsia;
66+
67+
/// Is Mobile?
68+
static bool get mobile => PlatformIs.iOS || PlatformIs.android;
69+
70+
/// Is Desktop?
71+
static bool get desktop =>
72+
PlatformIs.macOS || PlatformIs.windows || PlatformIs.linux;
73+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Adapted from https://gist.github.com/rydmike/1771fe24c050ebfe792fa309371154d8
2+
3+
// NOTE:
4+
// Never import this library directly in the application. The PlatformIs
5+
// class and library uses conditional imports to only import this file on
6+
// VM platform builds.
7+
8+
/// UniversalPlatform for just a "none" implementation when the platform
9+
/// cannot be identified for some reason.
10+
class UniversalPlatform {
11+
UniversalPlatform._();
12+
13+
/// Is Web?
14+
static bool get web => false;
15+
16+
/// Is macOS?
17+
static bool get macOS => false;
18+
19+
/// Is Windows?
20+
static bool get windows => false;
21+
22+
/// Is Linux?
23+
static bool get linux => false;
24+
25+
/// Is Android?
26+
static bool get android => false;
27+
28+
/// Is iOS?
29+
static bool get iOS => false;
30+
31+
/// Is Fuchsia?
32+
static bool get fuchsia => false;
33+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Adapted from https://gist.github.com/rydmike/1771fe24c050ebfe792fa309371154d8
2+
3+
import 'dart:io';
4+
5+
// NOTE:
6+
// Never import this library directly in the application. The PlatformIs
7+
// class and library uses conditional imports to only import this file on
8+
// VM platform builds.
9+
10+
/// UniversalPlatform for Flutter VM builds.
11+
///
12+
/// We are using Dart VM builds, so we use dart:io Platform to
13+
/// get the current platform.
14+
class UniversalPlatform {
15+
UniversalPlatform._();
16+
17+
/// Is Web?
18+
static bool get web => false;
19+
20+
/// Is macOS?
21+
static bool get macOS => Platform.isMacOS;
22+
23+
/// Is Windows?
24+
static bool get windows => Platform.isWindows;
25+
26+
/// Is Linux?
27+
static bool get linux => Platform.isLinux;
28+
29+
/// Is Android?
30+
static bool get android => Platform.isAndroid;
31+
32+
/// Is iOS?
33+
static bool get iOS => Platform.isIOS;
34+
35+
/// Is Fuchsia?
36+
static bool get fuchsia => Platform.isFuchsia;
37+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Adapted from https://gist.github.com/rydmike/1771fe24c050ebfe792fa309371154d8
2+
3+
// ignore: avoid_web_libraries_in_flutter
4+
import 'package:web/web.dart';
5+
6+
// NOTE:
7+
// Never import this library directly in the application. The PlatformIs
8+
// class and library uses conditional imports to only import this file on
9+
// Web platform builds.
10+
11+
final Navigator _nav = window.navigator;
12+
13+
/// UniversalPlatform for Flutter WEB build.
14+
///
15+
/// We can use dart:html Navigator to get the current platform.
16+
///
17+
/// This function is borrowed, with minor modifications, from GetX utils library
18+
/// with MIT license.
19+
/// Credits for it belong to its author Jonny Borges https://github.com/jonataslaw
20+
/// https://github.com/jonataslaw/getx/blob/master/lib/get_utils/src/platform/platform_web.dart
21+
///
22+
class UniversalPlatform {
23+
UniversalPlatform._();
24+
25+
/// Is Web?
26+
static bool get web => true;
27+
28+
/// Is macOS?
29+
static bool get macOS => _nav.appVersion.contains('Mac OS') && !iOS;
30+
31+
/// Is Windows?
32+
static bool get windows => _nav.appVersion.contains('Win');
33+
34+
/// Is Linux?
35+
static bool get linux =>
36+
(_nav.appVersion.contains('Linux') || _nav.appVersion.contains('x11')) &&
37+
!android;
38+
39+
/// Is Android?
40+
/// Source: https://developer.chrome.com/multidevice/user-agent
41+
static bool get android => _nav.appVersion.contains('Android ');
42+
43+
/// Is iOS?
44+
/// maxTouchPoints is needed to separate iPad iOS13 vs new MacOS
45+
static bool get iOS =>
46+
_hasMatch(_nav.platform, '/iPad|iPhone|iPod/') ||
47+
(_nav.platform == 'MacIntel' && _nav.maxTouchPoints > 1);
48+
49+
/// Is Fuchsia?
50+
/// Theoretically we could be in a Web browser on Fuchsia too, but
51+
/// we have no info on how to get that info yet, so we return false.
52+
static bool get fuchsia => false;
53+
}
54+
55+
bool _hasMatch(String? value, String pattern) =>
56+
// ignore: avoid_bool_literals_in_conditional_expressions
57+
(value == null) ? false : RegExp(pattern).hasMatch(value);

0 commit comments

Comments
 (0)