Skip to content

Commit

Permalink
Refactor to directly call Platform.is and make .isWeb constant
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Hua authored and tqdv committed Mar 4, 2022
1 parent 5ec9968 commit e38186e
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
.packages
.pub-cache/
.pub/
/pubspec.lock
/build/

# Web related
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Changelog

## 1.1.0

- Refactor to call Platform.is* in a more direct way
- Make UniversalPlatform.isWeb constant
- Add UniversalPlatformType.Other and .None

## 1.0.0-nullsafety
- Migrate to nnbd

Expand Down
17 changes: 12 additions & 5 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0"
collection:
dependency: transitive
description:
Expand All @@ -20,13 +20,20 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -45,13 +52,13 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.0-nullsafety"
version: "1.1.0"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.1"
sdks:
dart: ">=2.12.0 <3.0.0"
dart: ">=2.14.0 <3.0.0"
4 changes: 3 additions & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: sized_context_example
description: A new Flutter application.
version: 1.0.0+1
version: 1.1.0

environment:
sdk: ">=2.12.0 <3.0.0"
Expand All @@ -14,3 +14,5 @@ dependencies:

flutter:
uses-material-design: true

publish_to: none
12 changes: 0 additions & 12 deletions lib/src/platform_io.dart

This file was deleted.

3 changes: 3 additions & 0 deletions lib/src/universal_platform.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export 'universal_platform_none.dart'
if (dart.library.io) 'universal_platform_io.dart'
if (dart.library.html) 'universal_platform_html.dart';
19 changes: 19 additions & 0 deletions lib/src/universal_platform_html.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'universal_platform_type.dart';

abstract class UniversalPlatform {
static UniversalPlatformType get value => UniversalPlatform.Web;

static bool get isAndroid => false;
static bool get isFuchsia => false;
static bool get isIOS => false;
static bool get isLinux => false;
static bool get isMacOS => false;
static bool get isWindows => false;
static bool get isOther => false;

static const bool isWeb = true;
static const bool isNone = false;

static bool get isDesktop => isLinux || isMacOS || isWindows;
static bool get isDesktopOrWeb => isDesktop || isWeb;
}
40 changes: 40 additions & 0 deletions lib/src/universal_platform_io.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'dart:io' show Platform;

import 'universal_platform_type.dart';

abstract class UniversalPlatform {
static UniversalPlatformType get value {
UniversalPlatformType result;
if (Platform.isAndroid) {
result = UniversalPlatformType.Android;
} else if (Platform.isIOS) {
result = UniversalPlatformType.IOS;
} else if (Platform.isFuchsia) {
result = UniversalPlatformType.Fuchsia;
} else if (Platform.isLinux) {
result = UniversalPlatformType.Linux;
} else if (Platform.isMacOS) {
result = UniversalPlatformType.MacOS;
} else if (Platform.isWindows) {
result = UniversalPlatformType.Windows;
} else {
result = UniversalPlatformType.Other;
}
return result;
}

static bool get isAndroid => Platform.isAndroid;
static bool get isFuchsia => Platform.isFuchsia;
static bool get isIOS => Platform.isIOS;
static bool get isLinux => Platform.isLinux;
static bool get isMacOS => Platform.isMacOS;
static bool get isWindows => Platform.isWindows;
static bool get isOther =>
UniversalPlatform.current == UniversalPlatformType.Other;

static const bool isWeb = false;
static const bool isNone = false;

static bool get isDesktop => isLinux || isMacOS || isWindows;
static bool get isDesktopOrWeb => isDesktop || isWeb;
}
4 changes: 0 additions & 4 deletions lib/src/universal_platform_locator.dart

This file was deleted.

19 changes: 19 additions & 0 deletions lib/src/universal_platform_none.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'universal_platform_type.dart';

abstract class UniversalPlatform {
static UniversalPlatformType get value => UniversalPlatform.None;

static bool get isAndroid => false;
static bool get isFuchsia => false;
static bool get isIOS => false;
static bool get isLinux => false;
static bool get isMacOS => false;
static bool get isWindows => false;
static bool get isOther => false;

static const bool isWeb = false;
static const bool isNone = true;

static bool get isDesktop => isLinux || isMacOS || isWindows;
static bool get isDesktopOrWeb => isDesktop || isWeb;
}
11 changes: 11 additions & 0 deletions lib/src/universal_platform_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
enum UniversalPlatformType {
Web,
Windows,
Linux,
MacOS,
Android,
Fuchsia,
IOS,
Other,
None,
}
30 changes: 2 additions & 28 deletions lib/universal_platform.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,2 @@
library universal_platform;
import 'src/universal_platform_locator.dart' if(dart.library.io) 'src/platform_io.dart';

export 'src/universal_platform_locator.dart' if(dart.library.io) 'src/platform_io.dart';

abstract class UniversalPlatform {

static UniversalPlatformType get value => currentUniversalPlatform;

static bool get isWeb => currentUniversalPlatform == UniversalPlatformType.Web;
static bool get isMacOS => currentUniversalPlatform == UniversalPlatformType.MacOS;
static bool get isWindows => currentUniversalPlatform == UniversalPlatformType.Windows;
static bool get isLinux => currentUniversalPlatform == UniversalPlatformType.Linux;
static bool get isAndroid => currentUniversalPlatform == UniversalPlatformType.Android;
static bool get isIOS => currentUniversalPlatform == UniversalPlatformType.IOS;
static bool get isFuchsia => currentUniversalPlatform == UniversalPlatformType.Fuchsia;

}

enum UniversalPlatformType {
Web,
Windows,
Linux,
MacOS,
Android,
Fuchsia,
IOS
}
export 'src/universal_platform.dart' show UniversalPlatform;
export 'src/universal_platform_type.dart' show UniversalPlatformType;
5 changes: 0 additions & 5 deletions pubspec.lock

This file was deleted.

4 changes: 1 addition & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
name: universal_platform
description: Replacement for dart.io.Platform class which works on Web as well as Desktop and Mobile. Allows platform checks in your view/model layer easily.
version: 1.0.0-nullsafety
version: 1.1.0
homepage: https://github.com/gskinnerTeam/flutter-universal-platform

environment:
sdk: '>=2.12.0-dev <3.0.0'

dependencies:

0 comments on commit e38186e

Please sign in to comment.