Android Widgets,Kodein,从存储库中检索数据

问题描述

我正在尝试创建一个小部件,以显示当前天气的信息(数据和图片),该小部件将在我的应用中使用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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...