Search in sources :

Example 1 with FragmentHomeBinding

use of com.example.first_responder_app.databinding.FragmentHomeBinding in project FirstResponse by mattpost1700.

the class HomeFragment method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    DrawerLocker drawerLocker = ((DrawerLocker) getActivity());
    if (drawerLocker != null) {
        drawerLocker.setDrawerLocked(false);
    }
    FragmentHomeBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);
    NavHostFragment navHostFragment = (NavHostFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
    bindingView = binding.getRoot();
    // TODO: navCont created for side bar(still need to be implemented)
    NavController navController = navHostFragment.getNavController();
    // switch to Home fragment upon clicking it
    // also if you have any other code relates to onCreateView just add it from here
    activeUser = AppUtil.getActiveUser(getActivity());
    if (activeUser == null) {
        getActivity().getFragmentManager().popBackStack();
        Toast.makeText(getContext(), "User is not logged in!", Toast.LENGTH_SHORT).show();
    }
    // Setup click listeners for the view all incidents and view all responders buttons
    binding.viewAllIncidents.setOnClickListener(view -> {
        NavDirections action = HomeFragmentDirections.actionHomeFragmentToIncidentGroupFragment();
        Navigation.findNavController(binding.getRoot()).navigate(action);
    });
    binding.viewAllResponders.setOnClickListener(view -> {
        NavDirections action = HomeFragmentDirections.actionHomeFragmentToRespondingFragment();
        Navigation.findNavController(binding.getRoot()).navigate(action);
    });
    binding.sortIncidentsButton.setOnClickListener(view -> {
        PopupMenu popupMenu = new PopupMenu(getContext(), view);
        popupMenu.setOnMenuItemClickListener(this);
        popupMenu.inflate(R.menu.incident_popup_menu);
        popupMenu.show();
    });
    binding.sortRespondersButton.setOnClickListener(view -> {
        PopupMenu popupMenu = new PopupMenu(getContext(), view);
        popupMenu.setOnMenuItemClickListener(this);
        popupMenu.inflate(R.menu.user_popup_menu);
        popupMenu.show();
    });
    final SwipeRefreshLayout pullToRefresh = bindingView.findViewById(R.id.homeSwipeRefreshLayout);
    pullToRefresh.setOnRefreshListener(() -> {
        // your code
        refreshData();
        pullToRefresh.setRefreshing(false);
    });
    listOfIncidentDataModel = new ArrayList<>();
    respondersList = new ArrayList<>();
    listOfRanks = new ArrayList<>();
    // automatically subscribes everyone who logs in to get notifications for these topics
    FirebaseMessaging.getInstance().subscribeToTopic("events").addOnCompleteListener(new OnCompleteListener<>() {

        @Override
        public void onComplete(@NonNull Task<Void> task) {
        }
    });
    FirebaseMessaging.getInstance().subscribeToTopic("announcements").addOnCompleteListener(new OnCompleteListener<Void>() {

        @Override
        public void onComplete(@NonNull Task<Void> task) {
        }
    });
    FirebaseMessaging.getInstance().subscribeToTopic("incidents").addOnCompleteListener(new OnCompleteListener<Void>() {

        @Override
        public void onComplete(@NonNull Task<Void> task) {
        }
    });
    saveRanksCollection();
    RespondersRecyclerViewAdapter.ResponderClickListener responderClickListener = (view, position) -> {
        UserViewModel userViewModel = new ViewModelProvider(requireActivity()).get(UserViewModel.class);
        userViewModel.setUserDataModel(respondersList.get(position));
        NavDirections action = HomeFragmentDirections.actionHomeFragmentToUserFragment();
        Navigation.findNavController(binding.getRoot()).navigate(action);
    };
    IncidentRecyclerViewAdapter.IncidentClickListener incidentClickListener = (view, position) -> {
        IncidentDataModel incident = listOfIncidentDataModel.get(position);
        IncidentViewModel incidentViewModel = new ViewModelProvider(requireActivity()).get(IncidentViewModel.class);
        incidentViewModel.setIncidentDataModel(incident);
        NavDirections action = HomeFragmentDirections.actionHomeFragmentToIncidentFragment();
        Navigation.findNavController(binding.getRoot()).navigate(action);
    };
    // RecyclerViews
    RecyclerView incidentRecyclerView = bindingView.findViewById(R.id.incidents_recycler_view);
    incidentRecyclerView.setLayoutManager(new LinearLayoutManager(bindingView.getContext()));
    incidentRecyclerViewAdapter = new IncidentRecyclerViewAdapter(bindingView.getContext(), listOfIncidentDataModel);
    incidentRecyclerViewAdapter.setIncidentClickListener(incidentClickListener);
    incidentRecyclerView.setAdapter(incidentRecyclerViewAdapter);
    RecyclerView respondersRecyclerView = bindingView.findViewById(R.id.responders_recycler_view);
    respondersRecyclerView.setLayoutManager(new LinearLayoutManager(bindingView.getContext()));
    respondersRecyclerViewAdapter = new RespondersRecyclerViewAdapter(bindingView.getContext(), respondersList, listOfIncidentDataModel);
    respondersRecyclerViewAdapter.setResponderClickListener(responderClickListener);
    respondersRecyclerView.setAdapter(respondersRecyclerViewAdapter);
    // Start event listeners (live data)
    addIncidentEventListener();
    addResponderEventListener();
    return bindingView;
}
Also used : Bundle(android.os.Bundle) NonNull(androidx.annotation.NonNull) IncidentRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.IncidentRecyclerViewAdapter) DrawerLocker(com.example.first_responder_app.interfaces.DrawerLocker) Task(com.google.android.gms.tasks.Task) MenuItem(android.view.MenuItem) NavController(androidx.navigation.NavController) ArrayList(java.util.ArrayList) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) IncidentDataModel(com.example.first_responder_app.dataModels.IncidentDataModel) PopupMenu(android.widget.PopupMenu) Toast(android.widget.Toast) Fragment(androidx.fragment.app.Fragment) HomeViewModel(com.example.first_responder_app.viewModels.HomeViewModel) View(android.view.View) ListenerRegistration(com.google.firebase.firestore.ListenerRegistration) RecyclerView(androidx.recyclerview.widget.RecyclerView) TAG(android.content.ContentValues.TAG) Log(android.util.Log) NavHostFragment(androidx.navigation.fragment.NavHostFragment) DataBindingUtil(androidx.databinding.DataBindingUtil) UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) FragmentManager(androidx.fragment.app.FragmentManager) ViewModelProvider(androidx.lifecycle.ViewModelProvider) NavDirections(androidx.navigation.NavDirections) LayoutInflater(android.view.LayoutInflater) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) AppUtil(com.example.first_responder_app.AppUtil) FragmentHomeBinding(com.example.first_responder_app.databinding.FragmentHomeBinding) FirebaseMessaging(com.google.firebase.messaging.FirebaseMessaging) FirestoreDatabase(com.example.first_responder_app.FirestoreDatabase) IncidentViewModel(com.example.first_responder_app.viewModels.IncidentViewModel) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) FragmentTransaction(androidx.fragment.app.FragmentTransaction) ViewGroup(android.view.ViewGroup) UserViewModel(com.example.first_responder_app.viewModels.UserViewModel) R(com.example.first_responder_app.R) FirebaseFirestore(com.google.firebase.firestore.FirebaseFirestore) List(java.util.List) Nullable(androidx.annotation.Nullable) Navigation(androidx.navigation.Navigation) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) RespondersRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.RespondersRecyclerViewAdapter) RanksDataModel(com.example.first_responder_app.dataModels.RanksDataModel) IncidentViewModel(com.example.first_responder_app.viewModels.IncidentViewModel) RespondersRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.RespondersRecyclerViewAdapter) IncidentDataModel(com.example.first_responder_app.dataModels.IncidentDataModel) NavController(androidx.navigation.NavController) DrawerLocker(com.example.first_responder_app.interfaces.DrawerLocker) FragmentHomeBinding(com.example.first_responder_app.databinding.FragmentHomeBinding) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) NavDirections(androidx.navigation.NavDirections) UserViewModel(com.example.first_responder_app.viewModels.UserViewModel) RecyclerView(androidx.recyclerview.widget.RecyclerView) IncidentRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.IncidentRecyclerViewAdapter) NavHostFragment(androidx.navigation.fragment.NavHostFragment) PopupMenu(android.widget.PopupMenu) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Aggregations

TAG (android.content.ContentValues.TAG)1 Bundle (android.os.Bundle)1 Log (android.util.Log)1 LayoutInflater (android.view.LayoutInflater)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 PopupMenu (android.widget.PopupMenu)1 Toast (android.widget.Toast)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 DataBindingUtil (androidx.databinding.DataBindingUtil)1 Fragment (androidx.fragment.app.Fragment)1 FragmentManager (androidx.fragment.app.FragmentManager)1 FragmentTransaction (androidx.fragment.app.FragmentTransaction)1 ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 NavController (androidx.navigation.NavController)1 NavDirections (androidx.navigation.NavDirections)1 Navigation (androidx.navigation.Navigation)1 NavHostFragment (androidx.navigation.fragment.NavHostFragment)1