public class ProjectsFragment extends Fragment {
private ProjectsViewModel projectsViewModel;
private ProjectsAdapter.OnItemClickListener onItemClickListener = username -> {
Intent intent = new Intent(getActivity(), ProfileActivity.class);
Bundle args = new Bundle();
args.putString(ProfileFragment.PROFILE_KEY, username);
intent.putExtra(ProfileActivity.USERNAME_KEY, args);
startActivity(intent);
};
public static ProjectsFragment newInstance() {
return new ProjectsFragment();
}
@Override public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Storage.StorageOwner) {
Storage storage = ((Storage.StorageOwner) context).obtainStorage();
CustomFactory customFactory = new CustomFactory(storage, onItemClickListener);
projectsViewModel = ViewModelProviders.of(this, customFactory)
.get(ProjectsViewModel.class);
}
}
@Nullable @Override public View onCreateView(
@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
ProjectsBinding projectsBinding = ProjectsBinding.inflate(inflater, container, false);
projectsBinding.setViewModel(projectsViewModel);
projectsBinding.setLifecycleOwner(this);
return projectsBinding.getRoot();
}
}