Search in sources :

Example 1 with UserViewModel

use of com.example.first_responder_app.viewModels.UserViewModel in project FirstResponse by mattpost1700.

the class EventFragment method onCreateView.

@SuppressLint("SetTextI18n")
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // binding fragment with nav_map by using navHostFragment, throw this block of code in there and that allows you to switch to other fragments
    binding = DataBindingUtil.inflate(inflater, R.layout.fragment_event, container, false);
    NavHostFragment navHostFragment = (NavHostFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
    NavController navController = navHostFragment.getNavController();
    // initialize vars as well as fetching userID
    participants = new ArrayList<>();
    ActiveUser activeUser = (ActiveUser) getActivity();
    UsersDataModel user = activeUser.getActive();
    userID = user.getDocumentId();
    // getting data from event group
    mViewModel = new ViewModelProvider(requireActivity()).get(EventViewModel.class);
    eventInfo = mViewModel.getEventDetail();
    isParticipating = eventInfo.getParticipants().contains(user.getDocumentId());
    EventRecyclerViewAdapter.ItemClickListener listener = (view, pos) -> {
        UsersDataModel u = participants.get(pos);
        UserViewModel userViewModel = new ViewModelProvider(requireActivity()).get(UserViewModel.class);
        userViewModel.setUserDataModel(u);
        NavDirections action = EventFragmentDirections.actionEventFragmentToUserFragment();
        Navigation.findNavController(binding.getRoot()).navigate(action);
    };
    // recycler binding
    RecyclerView eventRecyclerView = binding.eventEventRecycler;
    eventRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    eventRecyclerViewAdapter = new EventRecyclerViewAdapter(getContext(), participants, isAnyParticipants);
    eventRecyclerViewAdapter.setClickListener(listener);
    eventRecyclerView.setAdapter(eventRecyclerViewAdapter);
    updateUI(false);
    final SwipeRefreshLayout pullToRefresh = binding.eventRefreshLayout;
    pullToRefresh.setOnRefreshListener(() -> {
        participants.clear();
        updateUI(true);
        pullToRefresh.setRefreshing(false);
    });
    addParticipatingEventListener();
    binding.signUp.setOnClickListener(v -> {
        if (binding.signUp.getText().equals("Withdraw")) {
            eventInfo.getParticipants().remove(userID);
            db.collection(FirestoreDatabase.EVENTS_COLLECTION_DIR).document(eventInfo.getDocumentId()).set(eventInfo).addOnSuccessListener(documentReference -> {
                isParticipating = false;
                binding.signUp.setText("Sign Up");
            }).addOnFailureListener(e -> Log.w(TAG, "onCreateView: Could not update event UI", e));
        } else {
            eventInfo.getParticipants().add(userID);
            db.collection(FirestoreDatabase.EVENTS_COLLECTION_DIR).document(eventInfo.getDocumentId()).set(eventInfo).addOnSuccessListener(documentReference -> {
                isParticipating = true;
                binding.signUp.setText("Withdraw");
            }).addOnFailureListener(e -> Log.w(TAG, "onCreateView: Could not update event UI", e));
            // sends the event that includes the following:
            // title, location, description, duration
            Intent intent = new Intent(Intent.ACTION_INSERT);
            intent.setData(CalendarContract.Events.CONTENT_URI);
            intent.putExtra(CalendarContract.Events.TITLE, eventInfo.getTitle());
            intent.putExtra(CalendarContract.Events.EVENT_LOCATION, eventInfo.getLocation());
            intent.putExtra(CalendarContract.Events.DESCRIPTION, eventInfo.getDescription());
            intent.putExtra(CalendarContract.Events.DURATION, eventInfo.getDuration_in_minutes());
            if (intent.resolveActivity(requireContext().getPackageManager()) != null) {
                startActivity(intent);
            } else {
                Log.d("EVENT INTENT: ", "Event setup Failed");
            }
        // // Calendar intent
        // Calendar cal = Calendar.getInstance();
        // Intent intent = new Intent(Intent.ACTION_EDIT);
        // long startTime = eventInfo.getEvent_time().toDate().toInstant().toEpochMilli();
        // intent.putExtra("beginTime", startTime);
        // //intent.putExtra("allDay", true);
        // intent.putExtra("endTime", startTime + AppUtil.numOfMinutesToMilliSeconds(eventInfo.getDuration_in_minutes()));
        // intent.putExtra("title", "A Test Event from android app");
        // startActivity(intent);
        }
    });
    return binding.getRoot();
}
Also used : Bundle(android.os.Bundle) PackageManager(android.content.pm.PackageManager) NonNull(androidx.annotation.NonNull) Date(java.util.Date) Uri(android.net.Uri) CalendarContract(android.provider.CalendarContract) NavController(androidx.navigation.NavController) EventsDataModel(com.example.first_responder_app.dataModels.EventsDataModel) ActiveUser(com.example.first_responder_app.interfaces.ActiveUser) Manifest(android.Manifest) FieldPath(com.google.firebase.firestore.FieldPath) ContentResolver(android.content.ContentResolver) Locale(java.util.Locale) Fragment(androidx.fragment.app.Fragment) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) Log(android.util.Log) NavHostFragment(androidx.navigation.fragment.NavHostFragment) DataBindingUtil(androidx.databinding.DataBindingUtil) Timestamp(com.google.firebase.Timestamp) NavDirections(androidx.navigation.NavDirections) AppUtil(com.example.first_responder_app.AppUtil) IncidentViewModel(com.example.first_responder_app.viewModels.IncidentViewModel) ViewGroup(android.view.ViewGroup) R(com.example.first_responder_app.R) Objects(java.util.Objects) List(java.util.List) Nullable(androidx.annotation.Nullable) ContentValues(android.content.ContentValues) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) EventViewModel(com.example.first_responder_app.viewModels.EventViewModel) SimpleDateFormat(java.text.SimpleDateFormat) Intent(android.content.Intent) FragmentEventBinding(com.example.first_responder_app.databinding.FragmentEventBinding) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint) Calendar(java.util.Calendar) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) Toast(android.widget.Toast) ListenerRegistration(com.google.firebase.firestore.ListenerRegistration) TAG(android.content.ContentValues.TAG) EventRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.EventRecyclerViewAdapter) DocumentSnapshot(com.google.firebase.firestore.DocumentSnapshot) UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) ViewModelProvider(androidx.lifecycle.ViewModelProvider) LayoutInflater(android.view.LayoutInflater) ActivityCompat(androidx.core.app.ActivityCompat) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) User(com.google.firebase.firestore.auth.User) FirestoreDatabase(com.example.first_responder_app.FirestoreDatabase) UserViewModel(com.example.first_responder_app.viewModels.UserViewModel) FirebaseFirestore(com.google.firebase.firestore.FirebaseFirestore) Navigation(androidx.navigation.Navigation) UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) NavController(androidx.navigation.NavController) Intent(android.content.Intent) EventRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.EventRecyclerViewAdapter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) NavDirections(androidx.navigation.NavDirections) EventViewModel(com.example.first_responder_app.viewModels.EventViewModel) UserViewModel(com.example.first_responder_app.viewModels.UserViewModel) ActiveUser(com.example.first_responder_app.interfaces.ActiveUser) RecyclerView(androidx.recyclerview.widget.RecyclerView) NavHostFragment(androidx.navigation.fragment.NavHostFragment) ViewModelProvider(androidx.lifecycle.ViewModelProvider) SuppressLint(android.annotation.SuppressLint)

Example 2 with UserViewModel

use of com.example.first_responder_app.viewModels.UserViewModel in project FirstResponse by mattpost1700.

the class EditUserFragment method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // binding fragment with nav_map by using navHostFragment, throw this block of code in there and that allows you to switch to other fragments
    FragmentEditUserBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_edit_user, container, false);
    NavHostFragment navHostFragment = (NavHostFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
    // TODO: navCont created for side bar(still need to be implemented)
    NavController navController = navHostFragment.getNavController();
    // sharedViewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
    firestoreDatabase = new FirestoreDatabase();
    db = firestoreDatabase.getDb();
    activeUser = (ActiveUser) getActivity();
    if (activeUser != null) {
        user = activeUser.getActive();
    }
    Spinner rankSpinner = binding.userRank;
    ranksAndIds = new HashMap<>();
    populateRanks(rankSpinner);
    populateEditTexts(binding.userFirstName, binding.userLastName, binding.userPhone, binding.userAddress);
    rankSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            rankSpinner.setSelection(position);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        // can leave this empty
        }
    });
    binding.saveButton.setOnClickListener(v -> {
        String firstName = binding.userFirstName.getText().toString();
        String lastName = binding.userLastName.getText().toString();
        String phone = binding.userPhone.getText().toString();
        String address = binding.userAddress.getText().toString();
        String id = "";
        if (activeUser != null) {
            UsersDataModel user = activeUser.getActive();
            if (user != null) {
                id = user.getDocumentId();
            }
        }
        String rankName = binding.userRank.getSelectedItem().toString();
        String rankID = ranksAndIds.get(rankName);
        if (id != null) {
            String errorMsg = "";
            if (!firestoreDatabase.validateName(firstName)) {
                errorMsg = "First name has invalid format";
            } else if (!firestoreDatabase.validateName(lastName)) {
                errorMsg = "Last name has invalid format";
            } else if (!firestoreDatabase.validatePhone(phone)) {
                errorMsg = "Phone number has invalid format";
            }
            if (errorMsg.equals("")) {
                // TODO: await
                firestoreDatabase.editUser(firstName, lastName, rankID, phone, address, id, getActivity());
                user.setFirst_name(firstName);
                user.setLast_name(lastName);
                user.setRank_id(rankID);
                user.setPhone_number(phone);
                user.setAddress(address);
                UserViewModel userViewModel = new ViewModelProvider(this).get(UserViewModel.class);
                userViewModel.setUserDataModel(user);
                NavDirections action = EditUserFragmentDirections.actionEditUserFragmentToUserFragment();
                Navigation.findNavController(binding.getRoot()).navigate(action);
            } else {
                Toast.makeText(getActivity(), errorMsg, Toast.LENGTH_SHORT).show();
            }
        } else {
            Log.d("User", "No active user found");
        }
    });
    return binding.getRoot();
}
Also used : UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) Spinner(android.widget.Spinner) NavController(androidx.navigation.NavController) View(android.view.View) AdapterView(android.widget.AdapterView) NavDirections(androidx.navigation.NavDirections) EditUserViewModel(com.example.first_responder_app.viewModels.EditUserViewModel) UserViewModel(com.example.first_responder_app.viewModels.UserViewModel) FragmentEditUserBinding(com.example.first_responder_app.databinding.FragmentEditUserBinding) AdapterView(android.widget.AdapterView) NavHostFragment(androidx.navigation.fragment.NavHostFragment) FirestoreDatabase(com.example.first_responder_app.FirestoreDatabase) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 3 with UserViewModel

use of com.example.first_responder_app.viewModels.UserViewModel 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)

Example 4 with UserViewModel

use of com.example.first_responder_app.viewModels.UserViewModel in project FirstResponse by mattpost1700.

the class IncidentFragment method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    FragmentIncidentBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_incident, container, false);
    NavHostFragment navHostFragment = (NavHostFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
    bindingView = binding.getRoot();
    context = getContext();
    Log.d(TAG, "onCreateView: " + bindingView.findViewById(R.id.incident_button_layout));
    Bundle mapViewBundle = null;
    if (savedInstanceState != null) {
        mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY);
    }
    mMapView = (MapView) binding.googleMapView;
    mMapView.onCreate(mapViewBundle);
    mMapView.getMapAsync(this);
    mViewModel = new ViewModelProvider(requireActivity()).get(IncidentViewModel.class);
    incident = mViewModel.getIncidentDataModel();
    responderList = new ArrayList<>();
    Log.d(TAG, "onCreate: " + getActivity().findViewById(R.id.incident_button_layout));
    initializeIncident(incident);
    // TODO: Check if user has permissions to file report (If not hide FAB)
    binding.incidentFileReportButton.setOnClickListener(v -> {
        ReportViewModel userViewModel = new ViewModelProvider(requireActivity()).get(ReportViewModel.class);
        userViewModel.setIncidentDataModel(incidentDataModel);
        NavDirections action = IncidentFragmentDirections.actionIncidentFragmentToReportFragment();
        Navigation.findNavController(binding.getRoot()).navigate(action);
    });
    // setup the dialog upon clicking the responder count icon, clicking on individual item will redirect to their profile page
    binding.incidentRespondingCount.setOnClickListener(v -> {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater layoutInflater = getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.dialog_title, null);
        builder.setCustomTitle(view).setItems(responderArr, (dialogInterface, i) -> {
            mUserViewModel = new ViewModelProvider(requireActivity()).get(UserViewModel.class);
            mUserViewModel.setUserDataModel(responderList.get(i));
            NavDirections action = IncidentFragmentDirections.actionIncidentFragmentToUserFragment();
            Navigation.findNavController(binding.getRoot()).navigate(action);
        });
        builder.setNegativeButton("Cancel", (dialogInterface, i) -> {
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    });
    return binding.getRoot();
}
Also used : ReportViewModel(com.example.first_responder_app.viewModels.ReportViewModel) AlertDialog(android.app.AlertDialog) IncidentViewModel(com.example.first_responder_app.viewModels.IncidentViewModel) Bundle(android.os.Bundle) FragmentIncidentBinding(com.example.first_responder_app.databinding.FragmentIncidentBinding) View(android.view.View) MapView(com.google.android.gms.maps.MapView) TextView(android.widget.TextView) NavDirections(androidx.navigation.NavDirections) UserViewModel(com.example.first_responder_app.viewModels.UserViewModel) LayoutInflater(android.view.LayoutInflater) NavHostFragment(androidx.navigation.fragment.NavHostFragment) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 5 with UserViewModel

use of com.example.first_responder_app.viewModels.UserViewModel in project FirstResponse by mattpost1700.

the class RespondingFragment method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    binding = DataBindingUtil.inflate(inflater, R.layout.fragment_responding, container, false);
    listOfRespondingDataModel = new ArrayList<>();
    listOfIncidentDataModel = new ArrayList<>();
    activeUser = AppUtil.getActiveUser(getActivity());
    if (activeUser == null) {
        getActivity().getFragmentManager().popBackStack();
        Toast.makeText(getContext(), "User is not logged in!", Toast.LENGTH_SHORT).show();
    }
    final SwipeRefreshLayout pullToRefresh = binding.respondingSwipeRefreshLayout;
    pullToRefresh.setOnRefreshListener(() -> {
        refreshData();
        pullToRefresh.setRefreshing(false);
    });
    // onclick
    RespondersGroupRecyclerViewAdapter.ResponderClickListener responderClickListener = (view, position) -> {
        UserViewModel userViewModel = new ViewModelProvider(requireActivity()).get(UserViewModel.class);
        userViewModel.setUserDataModel(listOfRespondingDataModel.get(position));
        NavDirections action = RespondingFragmentDirections.actionRespondingFragmentToUserFragment();
        Navigation.findNavController(binding.getRoot()).navigate(action);
    };
    binding.sortRespondersButton.setOnClickListener(view -> {
        PopupMenu popupMenu = new PopupMenu(getContext(), view);
        popupMenu.setOnMenuItemClickListener(this);
        popupMenu.inflate(R.menu.user_popup_menu);
        popupMenu.show();
    });
    // Recycler view
    RecyclerView respondingRecyclerView = binding.respondingRecyclerView;
    respondingRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    respondingRecyclerViewAdapter = new RespondersGroupRecyclerViewAdapter(getContext(), listOfRespondingDataModel, listOfIncidentDataModel);
    respondingRecyclerViewAdapter.setResponderClickListener(responderClickListener);
    respondingRecyclerView.setAdapter(respondingRecyclerViewAdapter);
    addResponderEventListener();
    addIncidentEventListener();
    return binding.getRoot();
}
Also used : Bundle(android.os.Bundle) NonNull(androidx.annotation.NonNull) MenuItem(android.view.MenuItem) 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) View(android.view.View) ListenerRegistration(com.google.firebase.firestore.ListenerRegistration) RecyclerView(androidx.recyclerview.widget.RecyclerView) TAG(android.content.ContentValues.TAG) Log(android.util.Log) DataBindingUtil(androidx.databinding.DataBindingUtil) UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) Timestamp(com.google.firebase.Timestamp) ViewModelProvider(androidx.lifecycle.ViewModelProvider) NavDirections(androidx.navigation.NavDirections) LayoutInflater(android.view.LayoutInflater) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) AppUtil(com.example.first_responder_app.AppUtil) FirestoreDatabase(com.example.first_responder_app.FirestoreDatabase) FragmentRespondingBinding(com.example.first_responder_app.databinding.FragmentRespondingBinding) 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) RespondingViewModel(com.example.first_responder_app.viewModels.RespondingViewModel) Navigation(androidx.navigation.Navigation) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) RespondersGroupRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.RespondersGroupRecyclerViewAdapter) UserViewModel(com.example.first_responder_app.viewModels.UserViewModel) RespondersGroupRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.RespondersGroupRecyclerViewAdapter) RecyclerView(androidx.recyclerview.widget.RecyclerView) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) ViewModelProvider(androidx.lifecycle.ViewModelProvider) NavDirections(androidx.navigation.NavDirections) PopupMenu(android.widget.PopupMenu)

Aggregations

View (android.view.View)6 ViewModelProvider (androidx.lifecycle.ViewModelProvider)6 UserViewModel (com.example.first_responder_app.viewModels.UserViewModel)6 NavDirections (androidx.navigation.NavDirections)5 NavHostFragment (androidx.navigation.fragment.NavHostFragment)5 Bundle (android.os.Bundle)4 LayoutInflater (android.view.LayoutInflater)4 FirestoreDatabase (com.example.first_responder_app.FirestoreDatabase)4 UsersDataModel (com.example.first_responder_app.dataModels.UsersDataModel)4 TAG (android.content.ContentValues.TAG)3 Log (android.util.Log)3 ViewGroup (android.view.ViewGroup)3 Toast (android.widget.Toast)3 NonNull (androidx.annotation.NonNull)3 Nullable (androidx.annotation.Nullable)3 DataBindingUtil (androidx.databinding.DataBindingUtil)3 Fragment (androidx.fragment.app.Fragment)3 NavController (androidx.navigation.NavController)3 Navigation (androidx.navigation.Navigation)3 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)3