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 the
HomeFragment` 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 toAuthFragment
on logout) and to go to theProfileFragment
. TheProfileFragment
doesn't need to know anything about authentication, that's why theHomeFragment
creates aHomeUser
class which contains the profile information and other things. TheHomeUser
class is passed to theProfileFragment
via safe args - ProfileFragment - displays the
HomeUser
data and allows to get back to theHomeFragment
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 theAuthUser
and theHomeUser
as arguments, and theAuthUser
would also be passed as an argument to theProfileFragment
because theHomeFragment
needs to have theAuthUser
valid, so to get back to theHomeFragment
with a validAuthUser
theProfileFragment
has to have the validAuthUser
.
//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 theHomeUser
- I've got 2 ideas.
- The
AuthViewModel
will be also used in theHomeFragment
, which is not ideal, becauseAuthViewModel
contains methods strictly for authorization - Create a
HomeViewModel
that will have theAuthUser
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?
- It doesn't matter if the
ProfileFragment
has access to theAuthUser
and I can just pass it all around using safe args - I should use the
AuthViewModel
, which has authentication methods, in theHomeFragment
. - I should use a combination of safe args and a ViewModel to pass the
AuthUser
to theHomeFragment
and save it inside a ViewModel