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

[dynamic_color] Impl. [isCorePaletteSupported] API #390 #481

Closed
wants to merge 4 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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Thank you for your interest in contributing! Your work can help many developers.
cider log changed|added|fixed|removed 'Added a schmilblick'
```
1. Make sure all the existing tests are passing with `flutter test`.
1. Make sure the repo is formatted using `flutter format .`.
1. Make sure the repo is formatted using `dart format .`.
1. Commit the changes to your branch and push.

That's it! Releasing is done by team members, see the [Releasing](#releasing) section below.
Expand Down
1 change: 1 addition & 0 deletions packages/dynamic_color/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/*
4 changes: 4 additions & 0 deletions packages/dynamic_color/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased
### Added
- Added check for dynamic color support in `getCorePalette`, returns `null` if not supported.

## 1.6.8 - 2023-10-17
### Changed
- Broaden constraint for `material_color_utilities` to support it until its `1.0.0` release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import android.content.res.Resources
import android.os.Build
import androidx.annotation.ChecksSdkIntAtLeast
import androidx.annotation.RequiresApi
import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding

Expand All @@ -21,29 +22,38 @@ class DynamicColorPlugin : FlutterPlugin, MethodCallHandler {

private lateinit var binding: FlutterPluginBinding

override fun onAttachedToEngine(
@NonNull flutterPluginBinding: FlutterPluginBinding
) {
override fun onAttachedToEngine(flutterPluginBinding: FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "io.material.plugins/dynamic_color")
channel.setMethodCallHandler(this)
this.binding = flutterPluginBinding
}

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method.equals("getCorePalette")) {
// Dynamic colors are only available on Android S and up.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val resources: Resources = binding.applicationContext.resources
result.success(getCorePalette(resources))
} else {
result.success(null)
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.S)
fun isCorePaletteSupported(): Boolean {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
}

override fun onMethodCall(call: MethodCall, result: Result) {
when (call.method) {
"getCorePalette" -> {
// Dynamic colors are only available on Android S and up.
if (isCorePaletteSupported()) {
val resources: Resources = binding.applicationContext.resources
result.success(getCorePalette(resources))
} else {
result.success(null)
}
}
} else {
result.notImplemented()

"isCorePaletteSupported" -> {
result.success(isCorePaletteSupported())
}

else -> result.notImplemented()
}
}

override fun onDetachedFromEngine(@NonNull binding: FlutterPluginBinding) {
override fun onDetachedFromEngine(binding: FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';

class IsCorePaletteSupportedApiExample extends StatefulWidget {
const IsCorePaletteSupportedApiExample({Key? key}) : super(key: key);

static const title = 'DynamicColorPlugin.isCorePaletteSupported()';

@override
State<IsCorePaletteSupportedApiExample> createState() =>
_IsCorePaletteSupportedApiExampleState();
}

class _IsCorePaletteSupportedApiExampleState
extends State<IsCorePaletteSupportedApiExample> {
late Future<bool> isCorePaletteSupported;

@override
void initState() {
super.initState();

isCorePaletteSupported = DynamicColorPlugin.isCorePaletteSupported();
}

@override
Widget build(BuildContext context) {
return Column(
children: [
Expanded(
child: ListView(
scrollDirection: Axis.horizontal,
children: [
Padding(
padding: const EdgeInsets.only(top: 8),
child: FutureBuilder<bool>(
future: DynamicColorPlugin.isCorePaletteSupported(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Text(
'isCorePaletteSupported: ${snapshot.data}',
);
} else {
return const CircularProgressIndicator();
}
},
),
)
],
),
),
],
);
}
}
14 changes: 10 additions & 4 deletions packages/dynamic_color/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:dynamic_color_example/is_core_palette_supported_api_example.dart';
import 'package:flutter/material.dart';

import 'accent_color.dart';
Expand Down Expand Up @@ -60,6 +61,10 @@ class ExampleApp extends StatelessWidget {
title: AdvancedExample2.title,
widget: AdvancedExample2(),
),
const _ExampleAppButton(
title: IsCorePaletteSupportedApiExample.title,
widget: IsCorePaletteSupportedApiExample(),
),
],
),
),
Expand Down Expand Up @@ -93,10 +98,11 @@ class _ExampleAppButton extends StatelessWidget {
title: Text(title, style: const TextStyle(fontSize: 14)),
),
body: Padding(
padding: (title == CompleteExample.title)
? EdgeInsets.zero
: contentPadding,
child: widget),
padding: (title == CompleteExample.title)
? EdgeInsets.zero
: contentPadding,
child: widget,
),
),
),
),
Expand Down
10 changes: 5 additions & 5 deletions packages/dynamic_color/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ packages:
dependency: transitive
description:
name: meta
sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3"
sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
url: "https://pub.dev"
source: hosted
version: "1.9.1"
version: "1.10.0"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -187,10 +187,10 @@ packages:
dependency: transitive
description:
name: web
sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10
sha256: "14f1f70c51119012600c5f1f60ca68efda5a9b6077748163c6af2893ec5df8fc"
url: "https://pub.dev"
source: hosted
version: "0.1.4-beta"
version: "0.2.1-beta"
sdks:
dart: ">=3.1.0-185.0.dev <4.0.0"
dart: ">=3.2.0-157.0.dev <4.0.0"
flutter: ">=3.4.0-17.0.pre"
10 changes: 10 additions & 0 deletions packages/dynamic_color/lib/src/dynamic_color_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class DynamicColorPlugin {
/// A method name that the macOS plugin listens for.
static const accentColorMethodName = 'getAccentColor';

static const isCorePaletteSupportedMethodName = 'isCorePaletteSupported';

/// Returns the Android OS' dynamic colors asynchronously in a [CorePalette].
///
/// Completes with null on pre-Android S and non-Android platforms.
Expand All @@ -30,6 +32,14 @@ class DynamicColorPlugin {
return result == null ? null : CorePalette.fromList(result.toList());
}

/// Returns whether the Android OS' dynamic colors are supported asynchronously.
///
/// Completes with false on pre-Android S and non-Android platforms.
static Future<bool> isCorePaletteSupported() async {
final result = await channel.invokeMethod(isCorePaletteSupportedMethodName);
return result ?? false;
}

/// Returns the OS' accent color asynchronously as a [Color].
///
/// Supported on macOS starting with 10.14 (Mojave), on Windows starting with
Expand Down
23 changes: 23 additions & 0 deletions packages/dynamic_color/lib/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,27 @@ class DynamicColorTestingUtils {
);
});
}

/// Initializes the dynamic color plugin with mock values for testing.
@visibleForTesting
static void setMockSupportFlag({required bool? isCorePaletteSupported}) {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(DynamicColorPlugin.channel, (
MethodCall methodCall,
) async {
if (methodCall.method ==
DynamicColorPlugin.isCorePaletteSupportedMethodName) {
return isCorePaletteSupported;
} else {
return null;
}
});
addTearDown(() {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(
DynamicColorPlugin.channel,
(MethodCall methodCall) => null,
);
});
}
}
2 changes: 1 addition & 1 deletion packages/dynamic_color/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -281,5 +281,5 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.1.0-185.0.dev <4.0.0"
dart: ">=3.2.0-157.0.dev <4.0.0"
flutter: ">=3.4.0-17.0.pre"
21 changes: 21 additions & 0 deletions packages/dynamic_color/test/dynamic_color_plugin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@ void main() {
expect(colors, sampleFromListCorePalette);
});

test('isCorePaletteSupported returns false', () async {
DynamicColorTestingUtils.setMockSupportFlag(isCorePaletteSupported: false);
final isCorePaletteSupported =
await DynamicColorPlugin.isCorePaletteSupported();
expect(isCorePaletteSupported, isFalse);
});

test('isCorePaletteSupported returns true', () async {
DynamicColorTestingUtils.setMockSupportFlag(isCorePaletteSupported: true);
final isCorePaletteSupported =
await DynamicColorPlugin.isCorePaletteSupported();
expect(isCorePaletteSupported, isTrue);
});

test('isCorePaletteSupported returns null', () async {
DynamicColorTestingUtils.setMockSupportFlag(isCorePaletteSupported: null);
final isCorePaletteSupported =
await DynamicColorPlugin.isCorePaletteSupported();
expect(isCorePaletteSupported, isFalse);
});

test('getCorePalette returns null', () async {
DynamicColorTestingUtils.setMockDynamicColors(
corePalette: null,
Expand Down