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

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

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:

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

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:

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

iOS:

iOS:

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

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:

expect fun setBrightness(brightness: Float)

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

Android:

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

iOS:

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

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

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

    expect val brightnessModule: Module

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

    actual val brightnessModule = module {
        single<BrightnessController> { BrightnessControllerImpl(androidContext()) }
    }

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

    override fun onCreate() {
        super.onCreate()
        startKoin {
            androidContext(this@App)
            brightnessModule
        }
    }

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

英文:

You should consider this approach:

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

First, create expected variable for the module

    expect val brightnessModule: Module

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

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

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

    override fun onCreate(){
        super.onCreate()
        startKoin {
            androidContext(this@App)
            brightnessModule
        }
    }

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:

确定