如何使用 Jetpack Compose 在 GlanceAppWidget 类中创建 Room Dao 或 Repository 或 Viewmodel 的实例

发布于 2025-01-12 07:31:29 字数 103 浏览 0 评论 0原文

我正在尝试使用 jetpack compose 加载 App Widget 中的数据列表,并且我已存储在 Room Local 数据库中,如何检索 GlanceAppWidget 类中的数据。

I am trying to load list of data in App Widget using jetpack compose and i have stored in Room Local database, how i can retrive the data in GlanceAppWidget class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野生奥特曼 2025-01-19 07:31:30

您需要使用 GlanceAppWidgetReceiver 类。您可以创建协程并访问您的域层。之后,您可以找到 GlanceAppWidget 类并将数据发送到您的小部件类。请查看这篇文章的完整示例:https:/ /medium.com/better-programming/android-jetpack-glance-for-app-widgets-bd7a704624ba

@AndroidEntryPoint
class MarketWidgetReceiver : GlanceAppWidgetReceiver() {

    override val glanceAppWidget: GlanceAppWidget = MarketWidget()

    private val coroutineScope = MainScope()

    @Inject
    lateinit var marketInformationUseCase: MarketInformationUseCase

    override fun onUpdate(
        context: Context,
        appWidgetManager: AppWidgetManager,
        appWidgetIds: IntArray
    ) {
        super.onUpdate(context, appWidgetManager, appWidgetIds)
        observeData(context)
    }

    override fun onReceive(context: Context, intent: Intent) {
        super.onReceive(context, intent)
        if (intent.action == MarketRefreshCallback.UPDATE_ACTION) {
            observeData(context)
        }
    }
    
    private fun observeData(context: Context) {
        coroutineScope.launch {

            val marketInformation =
                marketInformationUseCase.getMarketInformation(MarketInformationTimespan.TIMESPAN_1DAYS)

            val glanceId =
                GlanceAppWidgetManager(context).getGlanceIds(MarketWidget::class.java).firstOrNull()

            glanceId?.let {
                updateAppWidgetState(context, PreferencesGlanceStateDefinition, it) { pref ->
                    pref.toMutablePreferences().apply {
                        this[currentPrice] =
                            marketInformation.currentPrice
                        this[changeRate] =
                            marketInformation.changeRate
                        this[isChangeRatePositive] =
                            marketInformation.changeStatus == MarketInformationChangeStatus.POSITIVE
                    }
                }
                glanceAppWidget.update(context, it)
            }
        }
    }

    companion object {
        val currentPrice = stringPreferencesKey("currentPrice")
        val changeRate = stringPreferencesKey("changeRate")
        val isChangeRatePositive = booleanPreferencesKey("isChangeRatePositive")
    }

}

You need to work with GlanceAppWidgetReceiver class. You can create coroutine and access your domain layer. After that, you can find your GlanceAppWidget class and send your data your widget class. Please have look this article to full example : https://medium.com/better-programming/android-jetpack-glance-for-app-widgets-bd7a704624ba

@AndroidEntryPoint
class MarketWidgetReceiver : GlanceAppWidgetReceiver() {

    override val glanceAppWidget: GlanceAppWidget = MarketWidget()

    private val coroutineScope = MainScope()

    @Inject
    lateinit var marketInformationUseCase: MarketInformationUseCase

    override fun onUpdate(
        context: Context,
        appWidgetManager: AppWidgetManager,
        appWidgetIds: IntArray
    ) {
        super.onUpdate(context, appWidgetManager, appWidgetIds)
        observeData(context)
    }

    override fun onReceive(context: Context, intent: Intent) {
        super.onReceive(context, intent)
        if (intent.action == MarketRefreshCallback.UPDATE_ACTION) {
            observeData(context)
        }
    }
    
    private fun observeData(context: Context) {
        coroutineScope.launch {

            val marketInformation =
                marketInformationUseCase.getMarketInformation(MarketInformationTimespan.TIMESPAN_1DAYS)

            val glanceId =
                GlanceAppWidgetManager(context).getGlanceIds(MarketWidget::class.java).firstOrNull()

            glanceId?.let {
                updateAppWidgetState(context, PreferencesGlanceStateDefinition, it) { pref ->
                    pref.toMutablePreferences().apply {
                        this[currentPrice] =
                            marketInformation.currentPrice
                        this[changeRate] =
                            marketInformation.changeRate
                        this[isChangeRatePositive] =
                            marketInformation.changeStatus == MarketInformationChangeStatus.POSITIVE
                    }
                }
                glanceAppWidget.update(context, it)
            }
        }
    }

    companion object {
        val currentPrice = stringPreferencesKey("currentPrice")
        val changeRate = stringPreferencesKey("changeRate")
        val isChangeRatePositive = booleanPreferencesKey("isChangeRatePositive")
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文