简体   繁体   English

如何在 Android Clean Architecture 中使用“GoogleSignInApi”?

[英]How to use "GoogleSignInApi" in Android Clean Architecture?

How to handle "Google Sign in" using android CLEAN architecture?如何使用 android CLEAN架构处理“Google 登录” As we know that we should avoid android code in presentation layer.正如我们所知,我们应该避免在表示层中使用 android 代码。

i have tried to start an "Activity" where i have managed all of my login related codes.我试图启动一个“活动”,在那里我管理了所有与登录相关的代码。 But i tried to pass callback from this activity to my "Data Layer" using Kotlin Coroutine but can not pass this values form "Data Layer" to "Presentation Layer".但是我尝试使用 Kotlin Coroutine 将此活动的回调传递到我的“数据层”,但无法将此值从“数据层”传递到“表示层”。 Also having some issue on returning values as "Google Sign In" can be triggered any time from the user.在返回值方面也存在一些问题,因为用户可以随时触发“Google 登录”。

The presentation layer is a good place for android code such as activities and fragments.表示层是 android 活动和片段等代码的好地方。

Activities and fragments have a lifecycle where they are active or not, and this allows them to save energy when in the background. Activity 和 fragments 有一个生命周期,在这个生命周期中它们是活跃的还是不活跃的,这允许它们在后台时节省能量。 So callbacks should not be passed to the data layer, instead data from the data layer can be observed going from the repo to the viewmodel, becoming LiveData, and then you can take actions from their in the Activity/Fragment:所以回调不应该传递到数据层,而是可以观察到数据层的数据从 repo 到 viewmodel,成为 LiveData,然后你可以在 Activity/Fragment 中从它们采取行动:

public class MyFragment : Fragment() {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val myPriceListener: LiveData<BigDecimal> = ...
        myPriceListener.observe(viewLifecycleOwner, Observer<BigDecimal> { price: BigDecimal? ->
            // Update the UI.
        })
    }
}

https://developer.android.com/topic/libraries/architecture/livedata https://developer.android.com/topic/libraries/architecture/livedata

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM