While developing my note-taking app and as a new feature I wanna create a widget for my app by jetpack lib glance.
At first, I thought it would be like a normal database implementation, but it didn't work, And I have no idea how it does, even when I made some searches I get only network and API stuff.
But in my case, I wanted the local data.
And here are my classes.
@HiltViewModel
class AppWidgetVM @Inject constructor(
private val repo: EntityRepoImp
): ViewModel() {
private val _allNotesById = MutableStateFlow<List<Entity>>(emptyList())
val allNotesById: StateFlow<List<Entity>>
get() = _allNotesById.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(),
initialValue = listOf()
)
init {
viewModelScope.launch(Dispatchers.IO) {
repo.getAllNotesById.collect {
_allNotesById.value = it
}
}
}
}
@AndroidEntryPoint
class AppWidgetReceiver: GlanceAppWidgetReceiver() {
override val glanceAppWidget: GlanceAppWidget = AppWidget()
}
class AppWidget: GlanceAppWidget() {
@Composable
override fun Content() {
...
}
}
I know its looks like empty code, But like I said I have no idea how it does.
The variable allNotesById
holds all my local notes, And that is exactly what I want to display in AppWidget class.