如何在 Kotlin Multiplatform Mobile 中使用 Compose 改变屏幕亮度

huangapple go评论86阅读模式
英文:

How to change screen brightness in Kotlin Multiplatform Mobile using Compose

问题

I am trying to code up a way to set screen brightness to max after clicking a button in Kotlin Multiplatform Compose.

我正在尝试编写一种方法,在Kotlin Multiplatform Compose中点击按钮后将屏幕亮度设置为最大。

I have created an actual function that looks like this:

我已经创建了一个实际的函数,它看起来像这样:

  1. expect fun setBrightness(brightness: Float)

And then have implemented actual classes on both Android and iOS sides in commonMain module.

然后,在commonMain模块中,在Android和iOS两侧实现了实际的类。

Android:

Android:

  1. actual fun setBrightness(brightness: Float) {
  2. val window = requireActivity().window
  3. val layoutParams = window.attributes
  4. layoutParams.screenBrightness = brightness // set the brightness value between 0 and 1
  5. window.attributes = layoutParams
  6. }

iOS:

iOS:

  1. actual fun setBrightness(brightness: Float) {
  2. UIScreen.main.brightness = brightness
  3. }

The problem is that requireActivity() is unresolved in androidMain module in shared directory. How do I go around this? I guess this is because it is only available on the native Android side.

问题是在共享目录的androidMain模块中无法解析requireActivity()。我该如何解决这个问题?我猜这是因为它仅在原生Android端可用。

How do I set the brightness on both platforms using KMM so that is works in Compose?

如何使用KMM在两个平台上设置亮度,以使其在Compose中工作?

英文:

I am trying to code up a way to set screen brightness to max after clicking a button in Kotlin Multiplatform Compose.

I have created an actual function that looks like this:

  1. expect fun setBrightness(brightness: Float)

And then have implemented actual classes on both Android and iOS sides in commonMain module.

Android:

  1. actual fun setBrightness(brightness: Float) {
  2. val window = requireActivity().window
  3. val layoutParams = window.attributes
  4. layoutParams.screenBrightness = brightness // set the brightness value between 0 and 1
  5. window.attributes = layoutParams
  6. }

iOS:

  1. actual fun setBrightness(brightness: Float) {
  2. UIScreen.main.brightness = brightness
  3. }

The problem is that requireActivity() is unresolved in androidMain module in shared directory. How do I go around this? I guess this is because it is only available on the native Android side.

How do I set the brightness on both platforms using KMM so that is works in Compose?

答案1

得分: 1

你应该考虑这种方法:

  1. commonMain 中创建一个接口:
  1. interface BrightnessController {
  2. fun setBrightness(value: Float)
  3. }
  1. 然后在 commonMain 中创建一个 expect 类:
  1. expect class BrightnessControllerImpl : BrightnessController
  1. 接着为 Android 创建一个 actual 类:
  1. actual class BrightnessControllerImpl(val context: Context) : BrightnessController {
  2. // ...
  3. }
  1. 最后,你只需注入 context(在 Koin 中,你可以使用 androidContext())。

首先,为模块创建预期的变量:

  1. expect val brightnessModule: Module

然后为所有平台创建实际的模块(例如,用于 Android 的示例):

  1. actual val brightnessModule = module {
  2. single<BrightnessController> { BrightnessControllerImpl(androidContext()) }
  3. }

接下来,在你的 App 类中添加这个模块(你的扩展了 Application 的类的名称可能不同):

  1. override fun onCreate() {
  2. super.onCreate()
  3. startKoin {
  4. androidContext(this@App)
  5. brightnessModule
  6. }
  7. }

注意:
你也可以创建分离的模块,而不是使用 expect/actual。例如,将它们命名为 androidModuleiosModule。然后,你可以根据需要将模块添加到平台中。

英文:

You should consider this approach:

  1. Create an interface in the commonMain
  1. interface BrightnessController{
  2. fun setBrightness(value: Float)
  3. }
  1. Then create expect class in the commonMain
  1. expect class BrightnessControllerImpl: BrightnessController
  1. Then create actual class for Android
  1. actual class BrightnessControllerImpl(val context: Context): BrightnessController {
  2. ...
  3. }
  1. Then you just need to inject the context(in Koin you can use androidContext())

First, create expected variable for the module

  1. expect val brightnessModule: Module

Then create actual for all platforms (example for the Android)

  1. actual val brightnessModule = module {
  2. single&lt;BrightnessController&gt; { BrightnessControllerImpl(androidContext()) }
  3. }

Then to add the module in the App class (any name you have for class that extends Application)

  1. override fun onCreate(){
  2. super.onCreate()
  3. startKoin {
  4. androidContext(this@App)
  5. brightnessModule
  6. }
  7. }

Note:
You can also create separated modules without the expect/actual. For example call them androidModule and iosModule. Then you can add only the module you need into the platform.

huangapple
  • 本文由 发表于 2023年5月11日 19:29:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定