简体   繁体   中英

How to use "GoogleSignInApi" in Android Clean Architecture?

How to handle "Google Sign in" using android CLEAN architecture? As we know that we should avoid android code in presentation layer.

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". Also having some issue on returning values as "Google Sign In" can be triggered any time from the user.

The presentation layer is a good place for android code such as activities and fragments.

Activities and fragments have a lifecycle where they are active or not, and this allows them to save energy when in the background. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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