英文:
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
你应该考虑这种方法:
- 在
commonMain
中创建一个接口:
interface BrightnessController {
fun setBrightness(value: Float)
}
- 然后在
commonMain
中创建一个expect
类:
expect class BrightnessControllerImpl : BrightnessController
- 接着为 Android 创建一个
actual
类:
actual class BrightnessControllerImpl(val context: Context) : BrightnessController {
// ...
}
- 最后,你只需注入
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
。例如,将它们命名为 androidModule
和 iosModule
。然后,你可以根据需要将模块添加到平台中。
英文:
You should consider this approach:
- Create an interface in the
commonMain
interface BrightnessController{
fun setBrightness(value: Float)
}
- Then create
expect
class in thecommonMain
expect class BrightnessControllerImpl: BrightnessController
- Then create
actual
class for Android
actual class BrightnessControllerImpl(val context: Context): BrightnessController {
...
}
- Then you just need to inject the
context
(in Koin you can useandroidContext()
)
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<BrightnessController> { 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论