Last active
April 25, 2025 07:46
-
-
Save nielsvanvelzen/e7d7d288e0e6e152bec9295579dfc32d to your computer and use it in GitHub Desktop.
Extension to DreamService to allow usage of Jetpack Compose inside
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.service.dreams.DreamService | |
import androidx.annotation.CallSuper | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.platform.ComposeView | |
import androidx.compose.ui.platform.ViewCompositionStrategy | |
import androidx.lifecycle.Lifecycle | |
import androidx.lifecycle.LifecycleRegistry | |
import androidx.lifecycle.ViewModelStore | |
import androidx.lifecycle.ViewModelStoreOwner | |
import androidx.lifecycle.setViewTreeLifecycleOwner | |
import androidx.lifecycle.setViewTreeViewModelStoreOwner | |
import androidx.savedstate.SavedStateRegistry | |
import androidx.savedstate.SavedStateRegistryController | |
import androidx.savedstate.SavedStateRegistryOwner | |
import androidx.savedstate.setViewTreeSavedStateRegistryOwner | |
abstract class DreamServiceCompat : DreamService(), SavedStateRegistryOwner, ViewModelStoreOwner { | |
@Suppress("LeakingThis") | |
private val lifecycleRegistry = LifecycleRegistry(this) | |
@Suppress("LeakingThis") | |
private val savedStateRegistryController = SavedStateRegistryController.create(this).apply { | |
performAttach() | |
} | |
override val lifecycle: Lifecycle get() = lifecycleRegistry | |
override val viewModelStore = ViewModelStore() | |
override val savedStateRegistry: SavedStateRegistry get() = savedStateRegistryController.savedStateRegistry | |
@CallSuper | |
override fun onCreate() { | |
super.onCreate() | |
savedStateRegistryController.performRestore(null) | |
lifecycleRegistry.currentState = Lifecycle.State.CREATED | |
} | |
override fun onDreamingStarted() { | |
super.onDreamingStarted() | |
lifecycleRegistry.currentState = Lifecycle.State.STARTED | |
} | |
override fun onDreamingStopped() { | |
super.onDreamingStopped() | |
lifecycleRegistry.currentState = Lifecycle.State.CREATED | |
} | |
fun setContent(content: @Composable () -> Unit) { | |
val view = ComposeView(this) | |
// Set composition strategy | |
view.setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) | |
// Inject dependencies normally added by appcompat activities | |
view.setViewTreeLifecycleOwner(this) | |
view.setViewTreeViewModelStoreOwner(this) | |
view.setViewTreeSavedStateRegistryOwner(this) | |
// Set content composable | |
view.setContent(content) | |
// Set content view | |
setContentView(view) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @nielsvanvelzen ,
I've been implementing a daydream service using DreamServiceCompat and encountered an issue with ViewModels that have dependencies injected via Hilt. I wanted to report this issue and see if there might be a more elegant solution than my current workaround.
The issue:
When trying to use
hiltViewModel()
in a Compose UI inside DreamServiceCompat, I'm getting this error:This happens because the DreamServiceCompat class doesn't implement
HasDefaultViewModelProviderFactory
, which Compose'shiltViewModel()
relies on to create ViewModels with dependencies.This throws the error.
This works fine.
My current workaround:
I've managed to solve this by directly injecting dependencies and manually creating the ViewModel:
This works, but it bypasses the standard ViewModel lifecycle management and other benefits of the ViewModelProvider system.
Potential solution:
It would be great if DreamServiceCompat implemented
ViewModelStoreOwner
andHasDefaultViewModelProviderFactory
interfaces, similar to how ComponentActivity does. This would allow for proper integration with Jetpack Compose'shiltViewModel()
and the standard ViewModel architecture.