How to use "GoogleSignInApi" in Android Clean Architecture?

192 Views Asked by At

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.

1

There are 1 best solutions below

0
On

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