From 742c0df75d8501c087ffda1b3344b4d21ac471fd Mon Sep 17 00:00:00 2001 From: Alex <51419598+alexrintt@users.noreply.github.com> Date: Mon, 18 Dec 2023 12:45:30 -0300 Subject: [PATCH] [dynamic_color] Impr. dynamic color detection on Android (#522) * [dynamic_color] use `DynamicColors.isDynamicColorAvailable` verification * [dynamic_color] update changelog * [global] fix contributing step * [dynamic_color] revert the example pubspec.lock file * [dynamic_color] revert pubspec.lock from dynamic_color pkg * [dynamic_color] Fix changelog lower case at start * [dynamic_color] Set release version `1.6.9` and date --- CONTRIBUTING.md | 2 +- packages/dynamic_color/CHANGELOG.md | 4 +++ packages/dynamic_color/android/build.gradle | 3 ++ .../dynamic_color/DynamicColorPlugin.kt | 30 +++++++++---------- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index adcb526c..4fd447a7 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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. diff --git a/packages/dynamic_color/CHANGELOG.md b/packages/dynamic_color/CHANGELOG.md index 26860a6f..28093cb4 100644 --- a/packages/dynamic_color/CHANGELOG.md +++ b/packages/dynamic_color/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.6.9 - 2023-12-16 +### Changed +- Use `DynamicColors.isDynamicColorAvailable` from `com.google.android.material:material` to verify if dynamic colors are available on Android + ## 1.6.8 - 2023-10-17 ### Changed - Broaden constraint for `material_color_utilities` to support it until its `1.0.0` release diff --git a/packages/dynamic_color/android/build.gradle b/packages/dynamic_color/android/build.gradle index 914da68a..f309a151 100644 --- a/packages/dynamic_color/android/build.gradle +++ b/packages/dynamic_color/android/build.gradle @@ -49,4 +49,7 @@ android { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + + def material_version = '1.7.0' + implementation "com.google.android.material:material:$material_version" } diff --git a/packages/dynamic_color/android/src/main/kotlin/io/material/plugins/dynamic_color/DynamicColorPlugin.kt b/packages/dynamic_color/android/src/main/kotlin/io/material/plugins/dynamic_color/DynamicColorPlugin.kt index 0ca0306f..d9292130 100644 --- a/packages/dynamic_color/android/src/main/kotlin/io/material/plugins/dynamic_color/DynamicColorPlugin.kt +++ b/packages/dynamic_color/android/src/main/kotlin/io/material/plugins/dynamic_color/DynamicColorPlugin.kt @@ -1,7 +1,5 @@ package io.material.plugins.dynamic_color -import androidx.annotation.NonNull - import io.flutter.embedding.engine.plugins.FlutterPlugin import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel @@ -10,6 +8,7 @@ import io.flutter.plugin.common.MethodChannel.Result import android.content.res.Resources import android.os.Build import androidx.annotation.RequiresApi +import com.google.android.material.color.DynamicColors import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding class DynamicColorPlugin : FlutterPlugin, MethodCallHandler { @@ -21,29 +20,28 @@ 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) + override fun onMethodCall(call: MethodCall, result: Result) { + when (call.method) { + "getCorePalette" -> { + if (DynamicColors.isDynamicColorAvailable()) { + val resources: Resources = binding.applicationContext.resources + result.success(getCorePalette(resources)) + } else { + result.success(null) + } } - } else { - result.notImplemented() + + else -> result.notImplemented() } } - override fun onDetachedFromEngine(@NonNull binding: FlutterPluginBinding) { + override fun onDetachedFromEngine(binding: FlutterPluginBinding) { channel.setMethodCallHandler(null) }