I have an application which fetches the data from api and caches in db. I want to be able to show this data (from db) in the widget and also add a button to update the data, which when pressed will fetch the data and update the db and hence update the widget. The problem is since I'm using mvvm, Im not sure if at all it is possible to make use of livedata and fancy supports of jetpack components.
So I just created a Dao reference in widget update fun and fetched the db. That does fetch the data but it is not updating the textview, i logged it and it showed me the data correctly.
companion object {
internal fun updateAppWidget(
context: Context, appWidgetManager: AppWidgetManager,
appWidgetId: Int
) {
val db = AppDatabase.getInstance(context.applicationContext).weatherDao()
val views = RemoteViews(context.packageName, R.layout.weather_widget)
GlobalScope.launch {
val weather = db.getCurrentWeatherMetricAsync()
Log.d("TAG_TAG_TAG", "weather: " + weather.temperature);
withContext(Dispatchers.Main) {
views.setTextViewText(R.id.tv_counter, " asd " + weather.temperature)
}
}
appWidgetManager.updateAppWidget(appWidgetId, views)
}
}
the dao has a suspended fun for this.
@Query("select * from current_weather where id = 0")
suspend fun getCurrentWeatherMetricAsync(): CurrentMetric
Can you please tell me how to interact with the db and the proper means to interact with the repository of application?