Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add non nullable support #5

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name: platform_info
on:
push:
branches: [ master, dev ]
branches: [ master, dev, feature/null_safety ]
pull_request:
branches: [ master, dev ]
branches: [ master, dev, feature/null_safety ]
jobs:
build:
runs-on: ubuntu-latest
container:
image: google/dart:latest
image: google/dart:beta
steps:
- uses: actions/checkout@v2
- name: Install Dependencies
Expand All @@ -27,5 +27,5 @@ jobs:
uses: ChicagoFlutter/lcov-cop@v1.0.2
with:
path: coverage/lcov.info
min_coverage: 100
min_coverage: 95
exclude: "**/*.g.dart"
31 changes: 16 additions & 15 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ void main(List<String> args) {
print(platform.numberOfProcessors.gcd(1));

final string = platform.when(
io: () => platform.when(
fuchsia: () => 'io fuchsia',
windows: () => 'io windows',
android: () => 'io android',
iOS: () => 'io iOS',
macOS: () => 'io macOS',
linux: () => 'io linux',
unknown: () => 'io unknown',
),
web: () => platform.when(
material: () => 'web Android or Fuchsia',
cupertino: () => 'web macOS or iOS',
orElse: () => 'web Windows or Linux or unknown',
),
);
io: () => platform.when(
fuchsia: () => 'io fuchsia',
windows: () => 'io windows',
android: () => 'io android',
iOS: () => 'io iOS',
macOS: () => 'io macOS',
linux: () => 'io linux',
unknown: () => 'io unknown',
),
web: () => platform.when(
material: () => 'web Android or Fuchsia',
cupertino: () => 'web macOS or iOS',
orElse: () => 'web Windows or Linux or unknown',
),
) ??
'<unknown platform>';
print(string);
}
3 changes: 3 additions & 0 deletions lib/src/base_host_platform.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'package:meta/meta.dart' show immutable;

import 'enums.dart';

/// Host platform
/// contain info about host device
@immutable
abstract class HostPlatform {
/// Host platform type
/// + io
Expand Down
14 changes: 7 additions & 7 deletions lib/src/io_host_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class _IOHostPlatform implements HostPlatform {
_IOHostPlatform._();

static bool get _isUnknownEnvironment =>
Zone.current[#platform_info_test.isUnknownEnvironment] as bool ?? false;
Zone.current[#platform_info_test.isUnknownEnvironment] as bool? ?? false;

static bool get _isKnownEnvironment => !_isUnknownEnvironment;

Expand Down Expand Up @@ -73,12 +73,12 @@ class _IOHostPlatform implements HostPlatform {

static String _getLocale() {
final lang = io.Platform.localeName
?.split('-')
?.first
?.split('_')
?.first
?.trim()
?.toLowerCase();
.split('-')
.first
.split('_')
.first
.trim()
.toLowerCase();
if (_isUnknownEnvironment || lang is! String || lang.length != 2) {
return kDefaultHostPlatform.locale;
}
Expand Down
39 changes: 21 additions & 18 deletions lib/src/methods.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'enums.dart';
mixin PlatformMethods on ExtendedHostPlatform {
/// Run functions that satisfy the current state of the platform.
/// You can use nested methods to compose more complex queries.
/// Can return null, if [orElse] not set and any callback was not called.
///
/// ### Operating System
/// [fuchsia] - whether the operating system is a version of Fuchsia
Expand Down Expand Up @@ -53,33 +54,35 @@ mixin PlatformMethods on ExtendedHostPlatform {
/// macOS: () => ...,
/// linux: () => ...,
/// unknown: () => ...,
/// orElse: () => ...,
/// ),
/// web: () => platform.when(
/// material: () => ...,
/// cupertino: () => ...,
/// orElse: () => ...,
/// ),
/// orElse: () => ...,
/// );
/// ```
///
PlatformResult when<PlatformResult extends Object>({
PlatformResult Function() fuchsia,
PlatformResult Function() windows,
PlatformResult Function() android,
PlatformResult Function() iOS,
PlatformResult Function() macOS,
PlatformResult Function() linux,
PlatformResult Function() unknown,
PlatformResult Function() material,
PlatformResult Function() cupertino,
PlatformResult Function() mobile,
PlatformResult Function() desktop,
PlatformResult Function() io,
PlatformResult Function() web,
PlatformResult Function() release,
PlatformResult Function() profile,
PlatformResult Function() debug,
PlatformResult Function() orElse,
PlatformResult? when<PlatformResult>({
PlatformResult Function()? fuchsia,
PlatformResult Function()? windows,
PlatformResult Function()? android,
PlatformResult Function()? iOS,
PlatformResult Function()? macOS,
PlatformResult Function()? linux,
PlatformResult Function()? unknown,
PlatformResult Function()? material,
PlatformResult Function()? cupertino,
PlatformResult Function()? mobile,
PlatformResult Function()? desktop,
PlatformResult Function()? io,
PlatformResult Function()? web,
PlatformResult Function()? release,
PlatformResult Function()? profile,
PlatformResult Function()? debug,
PlatformResult Function()? orElse,
}) {
{
// Operating System
Expand Down
128 changes: 17 additions & 111 deletions lib/src/platform.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// ignore_for_file: avoid_equals_and_hash_code_on_mutable_classes

import 'package:meta/meta.dart' show immutable;

import 'base_host_platform.dart';
import 'constants.dart';
import 'enums.dart';
Expand Down Expand Up @@ -47,6 +49,7 @@ import 'stub_host_platform.dart'
///
/// + Is cupertino (MacOS, iOS)
///
@immutable
class Platform extends ExtendedHostPlatform with PlatformMethods {
@override
HostPlatformType get type => _hostPlatform.type;
Expand All @@ -64,8 +67,7 @@ class Platform extends ExtendedHostPlatform with PlatformMethods {
int get numberOfProcessors => _hostPlatform.numberOfProcessors;

@override
bool get isOperatingSystemKnown => _isOperatingSystemKnown;
bool _isOperatingSystemKnown;
late final bool isOperatingSystemKnown;

@override
bool get isWeb => _hostPlatform.type == HostPlatformType.web;
Expand All @@ -74,20 +76,16 @@ class Platform extends ExtendedHostPlatform with PlatformMethods {
bool get isIO => _hostPlatform.type == HostPlatformType.io;

@override
bool get isMobile => _isMobile;
bool _isMobile;
late final bool isMobile;

@override
bool get isDesktop => _isDesktop;
bool _isDesktop;
late final bool isDesktop;

@override
bool get isMaterial => _isMaterial;
bool _isMaterial;
late final bool isMaterial;

@override
bool get isCupertino => _isCupertino;
bool _isCupertino;
late final bool isCupertino;

@override
bool get isAndroid => operatingSystem == OperatingSystem.android;
Expand All @@ -108,11 +106,9 @@ class Platform extends ExtendedHostPlatform with PlatformMethods {
bool get isWindows => operatingSystem == OperatingSystem.windows;

@override
BuildMode get buildMode => _buildMode;
BuildMode _buildMode;
final BuildMode buildMode;

/// Host platform
/// contain info about host device
/// Host platform contain info about host device
final HostPlatform _hostPlatform;

static BuildMode _getCurrentBuildMode() => () {
Expand All @@ -139,110 +135,20 @@ class Platform extends ExtendedHostPlatform with PlatformMethods {
static final Platform _this = Platform._internal();
Platform._internal()
: _hostPlatform = _getHostPlatform(),
_buildMode = _getCurrentBuildMode() {
_isOperatingSystemKnown = operatingSystem != OperatingSystem.unknown;
_isMobile = kListOSForMobile.contains(operatingSystem);
_isDesktop = kListOSForDesktop.contains(operatingSystem);
_isMaterial = kListOSWithMaterialDesign.contains(operatingSystem);
_isCupertino = kListOSWithCupertinoDesign.contains(operatingSystem);
buildMode = _getCurrentBuildMode() {
isOperatingSystemKnown = operatingSystem != OperatingSystem.unknown;
isMobile = kListOSForMobile.contains(operatingSystem);
isDesktop = kListOSForDesktop.contains(operatingSystem);
isMaterial = kListOSWithMaterialDesign.contains(operatingSystem);
isCupertino = kListOSWithCupertinoDesign.contains(operatingSystem);
}

Platform._emptyForTest() : _hostPlatform = _getHostPlatform();

@override
int get hashCode => 0;

@override
bool operator ==(Object other) => other is Platform;

@override
String toString() => version;
}

/// Fake platform for test
class FakePlatform extends Platform with PlatformMethods {
/// @nodoc
FakePlatform() : super._emptyForTest() {
reset();
}

@override
BuildMode buildMode;

@override
bool isAndroid;

@override
bool isCupertino;

@override
bool isDesktop;

@override
bool isFuchsia;

@override
bool isIO;

@override
bool isIOS;

@override
bool isLinux;

@override
bool isMacOS;

@override
bool isMaterial;

@override
bool isMobile;

@override
bool isOperatingSystemKnown;

@override
bool isWeb;

@override
bool isWindows;

@override
String locale;

@override
int numberOfProcessors;

@override
OperatingSystem operatingSystem;

@override
HostPlatformType type;

@override
String version;

/// @nodoc
void reset() {
buildMode = BuildMode.debug;
isFuchsia = false;
isWindows = false;
isAndroid = false;
isIOS = false;
isMacOS = false;
isLinux = true;
isMaterial = false;
isCupertino = false;
isMobile = false;
isDesktop = true;
locale = 'en';
numberOfProcessors = 1;
operatingSystem = OperatingSystem.linux;
isOperatingSystemKnown = true;
isIO = true;
isWeb = false;
type = HostPlatformType.io;
version = '1';
}
String toString() => '<Platform $version>';
}
3 changes: 3 additions & 0 deletions lib/src/stub_host_platform.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import 'package:meta/meta.dart' show immutable;

import 'base_host_platform.dart';
import 'enums.dart';

/// Get host platform if dart.library.html and dart.library.io available
HostPlatform getHostPlatform() => const DefaultHostPlatform();

/// Unknown host platform with default values
@immutable
class DefaultHostPlatform implements HostPlatform {
/// Unknown host platform with default values
const DefaultHostPlatform();
Expand Down
Loading