问题描述
我正在尝试创建一个小部件,以显示当前天气的信息(数据和图片),该小部件将在我的应用中使用MVVM(从SQLite或Weather API进行存储库获取/获取数据)检索到。我设法从数据库中检索信息,但我认为与控件库交互会更好。当小部件使用AppWidetProvider时,我对如何使用Kodein将我的存储库绑定到WidgetProvider中感到困惑。我试图创建ViewModel&ViewModelFactory(就像我对Fragments所做的那样),但是没有成功。
class CurrentWeatherWidget() :AppWidgetProvider(),CurrentWidgetRefresher/*,KodeinAware*/ {(...)}
我想使用存储库方法刷新数据,而无需用户启动我的应用程序
override fun onReceive(context: Context?,intent: Intent?) {
super.onReceive(context,intent)
when (intent?.action) {
ONCLICKTAG -> {
Toast.makeText(context,"Trying to refresh the data",Toast.LENGTH_SHORT).show()
CoroutineScope(Dispatchers.IO).launch {
// I want to use my repo method here
//currentForecastRepository.requestRefreshOfData() // <---
context?.refreshWidgetData()
}
} (...)
}
}
此外,这就是我直接从数据库中检索信息的方式:
(...)
val weather =
ForecastDatabase.invoke(context.applicationContext).currentWeatherDao()
.getCurrentWeatherAsync()
val location =
ForecastDatabase.invoke(context.applicationContext).currentLocationDao()
.getLocationNotLive()
(...)
这是我的存储库界面:
interface Repository {
suspend fun requestRefreshOfData()
suspend fun getWeatherLocation() : LiveData<out WeatherLocation>
var requireRefreshOfData: Boolean
}
interface CurrentForecastRepository : Repository {
suspend fun getCurrentWeather() : LiveData<CurrentWeatherEntry>
}
非常感谢您!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)