0

There're 3 fragments

  • AuthFragment - is responsible for the authentication of the user. The authenticated user is stored in the AuthUser class. The 'AuthUserclass is passed to theHomeFragment` via safe args.
  • HomeFragment - is responsible for displaying some widgets, whatever, allows to logout (that's why it needs the AuthUser - the app gets back to AuthFragment on logout) and to go to the ProfileFragment. The ProfileFragment doesn't need to know anything about authentication, that's why the HomeFragment creates a HomeUser class which contains the profile information and other things. The HomeUser class is passed to the ProfileFragment via safe args
  • ProfileFragment - displays the HomeUser data and allows to get back to the HomeFragment

In short, the navigation graph looks like this:

  • AuthFragment --(AuthUser)--> HomeFragment
  • HomeFragment --> AuthFragment
  • HomeFragment --(HomeUser)--> ProfileFragment
  • ProfileFragment <--(What here???)-- HomeFragment

The AuthFragment uses the AuthViewModel and the AuthRepository to handle the firebase authentication, but it's not in the scope of this question, so I won't get deeper into it.

Here's the thing, what should I use:

  • safe args - the HomeFragment would take the AuthUser and the HomeUser as arguments, and the AuthUser would also be passed as an argument to the ProfileFragment because the HomeFragment needs to have the AuthUser valid, so to get back to the HomeFragment with a valid AuthUser the ProfileFragment has to have the valid AuthUser.
//AuthFragment to HomeFragment, HomeUser is null
AuthUser authUser = getAuthUser();
var action = AuthFragmentDirections.authToHome(authUser);
NavHostFragment.findNavController(AuthFragment.this).navigate(action);
//HomeFragment to ProfileFragment
AuthUser authUser = getAuthUser();
HomeUser homeUser = getHomeUser();
var action = AuthFragmentDirections.homeToProfile(authUser, homeUser);
NavHostFragment.findNavController(HomeFragment.this).navigate(action);
//ProfileFragmentto HomeFragment 
AuthUser authUser = getAuthUser();
HomeUser homeUser = getHomeUser();
var action = AuthFragmentDirections.profileToHome(authUser, homeUser);
NavHostFragment.findNavController(ProfileFragment.this).navigate(action);
//getting the authUser from arguments
authUser = HomeFragmentArgs.fromBundle(getArguments()).getAuthUser();

It sounds confusing, I know... that's why I thought of the HomeViewModel class that would contain the AuthUser and the HomeUser, so let's get into what I thought of

  • ViewModel to save the AuthUser or/and the HomeUser - I've got 2 ideas.
  1. The AuthViewModel will be also used in the HomeFragment, which is not ideal, because AuthViewModel contains methods strictly for authorization
  2. Create a HomeViewModel that will have the AuthUser
public class HomeViewModel extends ViewModel {
    public AuthUser authUser;
    public HomeUser homeUser;
}

and in the HomeFragment

authUser = HomeScreenArgs.fromBundle(getArguments()).getAuthUser();
homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
homeViewModel.authUser = authUser;

My question is, which method is more correct?

  1. It doesn't matter if the ProfileFragment has access to the AuthUser and I can just pass it all around using safe args
  2. I should use the AuthViewModel, which has authentication methods, in the HomeFragment.
  3. I should use a combination of safe args and a ViewModel to pass the AuthUser to the HomeFragment and save it inside a ViewModel
Edziju
  • 399
  • 11

0 Answers0