0

My app uses Firebase authentication and I'm implementing a typical sign-in screen with buttons to sign in with Google, email etc. It's surprisingly difficult. I have a LocalUser class to hold the user's profile (name, email etc), state (whether they're logged in or in the process of logging in etc), and provide methods to start the process when a button is clicked. This seems like an ideal candidate for a ViewModel.

The trouble is, ViewModels mustn't hold a reference to an Activity, but the Google Identity SDK needs such a reference as part of its mechanism for providing its result to my code. I can't think of a way to decouple the ViewModel that doesn't still leave the ViewModel referencing the Activity, albeit indirectly.

Is it OK to just remove the ViewModel inheritance from my LocalUser class and pass it directly to my sign in screen (and from there to each control that needs it)? Is it OK for a Composable to hold a reference to Activity?

realh
  • 962
  • 2
  • 7
  • 22
  • Don't add the context or objects that hold context inside the ViewModel. If you want to use Firebase sign-in with Google using Jetpack Compose, please check [this](https://medium.com/firebase-developers/how-to-authenticate-to-firebase-using-google-one-tap-in-jetpack-compose-60b30e621d0d) out. Here's also the corresponding [repo](https://github.com/alexmamo/FirebaseSignInWithGoogle). – Alex Mamo Jul 06 '22 at 15:03
  • I'm not using One-Tap, because I don't think it's necessary to have separate flows for sign-in and sign-up in my app. I used the [SignIn API](https://developers.google.com/identity/sign-in/android/sign-in-identity) instead. This relies on the [Activity Result API](https://developer.android.com/training/basics/intents/result). That doesn't seem to be the case in your app? – realh Jul 07 '22 at 20:32
  • I've since discovered the existence of `rememberLauncherForActivityResult` which might help in my case. I haven't got round to trying it yet though. – realh Jul 07 '22 at 20:36
  • The [Activity Result API](https://developers.google.com/identity/sign-in/android/sign-in-identity) will be deprecated, check the note, "Google Identity Services will eventually replace the existing Google Sign-In API.". So sooner or later you'll have to change that, – Alex Mamo Jul 08 '22 at 06:20
  • I am already using GIS though. As it says on that page we both linked, GIS includes a new Sign-In API to replace the old one (in addition to providing the One Tap API), but it still uses Activity Result. – realh Jul 09 '22 at 12:32
  • [OnActivityResult method is deprecated](https://stackoverflow.com/questions/62671106/onactivityresult-method-is-deprecated-what-is-the-alternative). So you should also consider implementing the new approach. – Alex Mamo Jul 10 '22 at 06:18
  • `onActivityResult` is deprecated, but I don't think [the Activity Result API](https://developer.android.com/training/basics/intents/result) is. That's what replaces the deprecated method. – realh Jul 10 '22 at 13:23
  • Indeed, registerForActivityResult is the new way of handling the result from an activity. – Alex Mamo Jul 10 '22 at 14:12

0 Answers0