Search in sources :

Example 1 with ReportViewModel

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

the class ReportFragment method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    binding = DataBindingUtil.inflate(inflater, R.layout.fragment_report, container, false);
    NavHostFragment navHostFragment = (NavHostFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
    View bindingView = binding.getRoot();
    activeUser = AppUtil.getActiveUser(getActivity());
    if (activeUser == null) {
        getActivity().getFragmentManager().popBackStack();
        Toast.makeText(getContext(), "User is not logged in!", Toast.LENGTH_SHORT).show();
    }
    ReportViewModel mViewModel = new ViewModelProvider(requireActivity()).get(ReportViewModel.class);
    incident = mViewModel.getIncidentDataModel();
    setText();
    binding.floatingActionButton.setOnClickListener(view -> {
        // Send to db
        // String fire_department_id, String incident_id, String user_created_id, String address, String units, String box_number, String incident_type, String narrative) {
        ReportDataModel report = null;
        try {
            report = new ReportDataModel(activeUser.getFire_department_id(), incident.getDocumentId(), activeUser.getDocumentId(), binding.addressTextView.getText().toString(), binding.unitsTextView.getText().toString(), binding.boxNumberTextView.getText().toString(), binding.incidentTypeTextView.getText().toString(), binding.reportOfficerText.getText().toString());
        } catch (NullPointerException nullPointerException) {
            report = null;
            Toast.makeText(getActivity(), "You must fill out the whole form", Toast.LENGTH_SHORT).show();
        }
        if (report != null) {
            FirestoreDatabase.getInstance().getDb().collection(FirestoreDatabase.REPORTS_COLLECTION_DIR).add(report).addOnSuccessListener(documentReference -> {
                Log.d(TAG, "onCreateView: added " + documentReference);
                Toast.makeText(getActivity(), "Report saved!", Toast.LENGTH_SHORT).show();
                NavDirections action = ReportFragmentDirections.actionReportFragmentToIncidentFragment();
                Navigation.findNavController(binding.getRoot()).navigate(action);
            }).addOnFailureListener(e -> Log.e(TAG, "onCreateView: Failed to upload document!", e));
        }
    });
    return bindingView;
}
Also used : ReportViewModel(com.example.first_responder_app.viewModels.ReportViewModel) Bundle(android.os.Bundle) NonNull(androidx.annotation.NonNull) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) IncidentDataModel(com.example.first_responder_app.dataModels.IncidentDataModel) Locale(java.util.Locale) Toast(android.widget.Toast) Fragment(androidx.fragment.app.Fragment) View(android.view.View) ReportViewModel(com.example.first_responder_app.viewModels.ReportViewModel) TAG(android.content.ContentValues.TAG) FragmentReportBinding(com.example.first_responder_app.databinding.FragmentReportBinding) Log(android.util.Log) NavHostFragment(androidx.navigation.fragment.NavHostFragment) DataBindingUtil(androidx.databinding.DataBindingUtil) ReportDataModel(com.example.first_responder_app.dataModels.ReportDataModel) UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) ViewModelProvider(androidx.lifecycle.ViewModelProvider) NavDirections(androidx.navigation.NavDirections) LayoutInflater(android.view.LayoutInflater) Timestamp(java.sql.Timestamp) AppUtil(com.example.first_responder_app.AppUtil) FirestoreDatabase(com.example.first_responder_app.FirestoreDatabase) ViewGroup(android.view.ViewGroup) R(com.example.first_responder_app.R) Nullable(androidx.annotation.Nullable) Navigation(androidx.navigation.Navigation) ReportDataModel(com.example.first_responder_app.dataModels.ReportDataModel) NavHostFragment(androidx.navigation.fragment.NavHostFragment) View(android.view.View) ViewModelProvider(androidx.lifecycle.ViewModelProvider) NavDirections(androidx.navigation.NavDirections)

Example 2 with ReportViewModel

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

Aggregations

Bundle (android.os.Bundle)2 LayoutInflater (android.view.LayoutInflater)2 View (android.view.View)2 ViewModelProvider (androidx.lifecycle.ViewModelProvider)2 NavDirections (androidx.navigation.NavDirections)2 NavHostFragment (androidx.navigation.fragment.NavHostFragment)2 ReportViewModel (com.example.first_responder_app.viewModels.ReportViewModel)2 AlertDialog (android.app.AlertDialog)1 TAG (android.content.ContentValues.TAG)1 Log (android.util.Log)1 ViewGroup (android.view.ViewGroup)1 TextView (android.widget.TextView)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 Navigation (androidx.navigation.Navigation)1 AppUtil (com.example.first_responder_app.AppUtil)1 FirestoreDatabase (com.example.first_responder_app.FirestoreDatabase)1