-
-
Save nielsvanvelzen/e7d7d288e0e6e152bec9295579dfc32d to your computer and use it in GitHub Desktop.
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) | |
} | |
} |
Nice!
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:
java.lang.RuntimeException: Cannot create an instance of class [ViewModel class]
Caused by: java.lang.NoSuchMethodException: <init> []
This happens because the DreamServiceCompat class doesn't implement HasDefaultViewModelProviderFactory
, which Compose's hiltViewModel()
relies on to create ViewModels with dependencies.
This throws the error.
@HiltViewModel
class DaydreamViewModel
@Inject
constructor(
private val getImagesUseCase: GetImagesUseCase // if the constructor is not empty the app crashes ❌
) : ViewModel()
This works fine.
@HiltViewModel
class DaydreamViewModel
@Inject
constructor(
// no parameters ✅
) : ViewModel()
My current workaround:
I've managed to solve this by directly injecting dependencies and manually creating the ViewModel:
@AndroidEntryPoint
class MyTvDaydream : DreamServiceCompat() {
@Inject
lateinit var useCase: GetImagesUseCase
override fun onAttachedToWindow() {
super.onAttachedToWindow()
val viewModel = DaydreamViewModel(useCase)
setContent {
val screenState by viewModel.screenState.collectAsState()
// ...
}
}
}
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
and HasDefaultViewModelProviderFactory
interfaces, similar to how ComponentActivity does. This would allow for proper integration with Jetpack Compose's hiltViewModel()
and the standard ViewModel architecture.
Thank you!