Search in sources :

Example 16 with UsersDataModel

use of com.example.first_responder_app.dataModels.UsersDataModel in project FirstResponse by mattpost1700.

the class HomeFragment method populateResponders.

public void populateResponders() {
    db.collection("users").whereEqualTo(FirestoreDatabase.FIELD_FIRE_DEPARTMENT_ID, activeUser.getFire_department_id()).whereGreaterThanOrEqualTo(FirestoreDatabase.FIELD_RESPONDING_TIME, AppUtil.earliestTime(requireContext())).get().addOnCompleteListener(userTask -> {
        Log.d(TAG, "READ DATABASE - HOME FRAGMENT (populateResponders)");
        if (userTask.isSuccessful()) {
            ArrayList<UsersDataModel> temp = new ArrayList<>();
            for (QueryDocumentSnapshot userDoc : userTask.getResult()) {
                UsersDataModel user = userDoc.toObject(UsersDataModel.class);
                List<String> responses = user.getResponses();
                if (responses != null && responses.size() > 0 && isActive(responses.get(responses.size() - 1)))
                    temp.add(user);
            }
            respondersList.clear();
            respondersList.addAll(temp);
            respondersRecyclerViewAdapter.notifyDataSetChanged();
            // TODO: Should refresh
            Log.d("TAG", "populateResponders: ");
        }
    });
}
Also used : UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) ArrayList(java.util.ArrayList)

Example 17 with UsersDataModel

use of com.example.first_responder_app.dataModels.UsersDataModel in project FirstResponse by mattpost1700.

the class IncidentFragment method initializeIncident.

/**
 * Setup the incident data and display it
 *
 * @param incident The incident to be displayed
 */
public void initializeIncident(IncidentDataModel incident) {
    setTextViews(incident);
    Map<String, String> status = incident.getStatus();
    // Get active user id
    ActiveUser activeUser = (ActiveUser) getActivity();
    if (activeUser != null) {
        UsersDataModel user = activeUser.getActive();
        if (user != null)
            active_id = user.getDocumentId();
    }
    // Highlight active button
    if (status != null && status.containsKey(active_id)) {
        setActiveButton(status.get(active_id));
    }
    setRespondingButtonClickListener();
    // Get the Address object of the incident
    incidentAddress = addrToCoords(incident.getLocation());
    setupLocationListener();
    AtomicInteger prevLength = new AtomicInteger();
    if (listenerRegistration == null) {
        // Ensure that the incident data is updated if database is updated
        docRef = FirestoreDatabase.getInstance().getDb().collection("incident").document(incident.getDocumentId());
        listenerRegistration = docRef.addSnapshotListener((snapshot, e) -> {
            Log.d(TAG, "READ DATABASE - INCIDENT FRAGMENT");
            if (e != null) {
                System.err.println("Listen failed: " + e);
                return;
            }
            if (snapshot != null && snapshot.exists()) {
                incidentDataModel = snapshot.toObject(IncidentDataModel.class);
                if (incidentDataModel != null) {
                    setTextViews(incidentDataModel);
                    Map<String, String> statuses = incidentDataModel.getStatus();
                    if (statuses != null) {
                        String status1 = statuses.get(active_id);
                        setActiveButton(status1);
                    }
                    // Get the Address object of the incident
                    incidentAddress = addrToCoords(incidentDataModel.getLocation());
                    int currentLength = incidentDataModel.getResponding().size();
                    if (prevLength.get() != currentLength) {
                        prevLength.set(currentLength);
                        responderList.clear();
                        populateRespondersListFromDB();
                    }
                }
            } else {
                System.out.print("Current data: null");
            }
        });
    }
}
Also used : Address(android.location.Address) CameraUpdateFactory(com.google.android.gms.maps.CameraUpdateFactory) Chip(com.google.android.material.chip.Chip) LinearLayout(android.widget.LinearLayout) Bundle(android.os.Bundle) PackageManager(android.content.pm.PackageManager) NonNull(androidx.annotation.NonNull) LocationListener(android.location.LocationListener) ColorStateList(android.content.res.ColorStateList) ActiveUser(com.example.first_responder_app.interfaces.ActiveUser) Manifest(android.Manifest) FieldPath(com.google.firebase.firestore.FieldPath) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FragmentIncidentBinding(com.example.first_responder_app.databinding.FragmentIncidentBinding) Locale(java.util.Locale) ETA(com.example.first_responder_app.DirectionAPI.ETA) Map(java.util.Map) View(android.view.View) Button(android.widget.Button) ContextCompat(androidx.core.content.ContextCompat) NavHostFragment(androidx.navigation.fragment.NavHostFragment) Log(android.util.Log) DataBindingUtil(androidx.databinding.DataBindingUtil) MapView(com.google.android.gms.maps.MapView) LatLng(com.google.android.gms.maps.model.LatLng) NavDirections(androidx.navigation.NavDirections) AppUtil(com.example.first_responder_app.AppUtil) MaterialColors(com.google.android.material.color.MaterialColors) IncidentViewModel(com.example.first_responder_app.viewModels.IncidentViewModel) Geocoder(android.location.Geocoder) ViewGroup(android.view.ViewGroup) AlertDialog(android.app.AlertDialog) R(com.example.first_responder_app.R) List(java.util.List) Nullable(androidx.annotation.Nullable) TextView(android.widget.TextView) Location(android.location.Location) Marker(com.google.android.gms.maps.model.Marker) LocationManager(android.location.LocationManager) DialogFragment(androidx.fragment.app.DialogFragment) Context(android.content.Context) ChipGroup(com.google.android.material.chip.ChipGroup) Dialog(android.app.Dialog) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint) DocumentReference(com.google.firebase.firestore.DocumentReference) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) IncidentDataModel(com.example.first_responder_app.dataModels.IncidentDataModel) Toast(android.widget.Toast) ListenerRegistration(com.google.firebase.firestore.ListenerRegistration) ReportViewModel(com.example.first_responder_app.viewModels.ReportViewModel) TAG(android.content.ContentValues.TAG) DialogInterface(android.content.DialogInterface) UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) ViewModelProvider(androidx.lifecycle.ViewModelProvider) ActivityCompat(androidx.core.app.ActivityCompat) LayoutInflater(android.view.LayoutInflater) MarkerOptions(com.google.android.gms.maps.model.MarkerOptions) FirestoreDatabase(com.example.first_responder_app.FirestoreDatabase) IOException(java.io.IOException) UserViewModel(com.example.first_responder_app.viewModels.UserViewModel) OnMapReadyCallback(com.google.android.gms.maps.OnMapReadyCallback) RefreshETAs(com.example.first_responder_app.interfaces.RefreshETAs) Navigation(androidx.navigation.Navigation) GoogleMap(com.google.android.gms.maps.GoogleMap) UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActiveUser(com.example.first_responder_app.interfaces.ActiveUser) Map(java.util.Map) GoogleMap(com.google.android.gms.maps.GoogleMap)

Example 18 with UsersDataModel

use of com.example.first_responder_app.dataModels.UsersDataModel in project FirstResponse by mattpost1700.

the class IncidentFragment method populateResponders.

public void populateResponders(int startIdx, int endIdx) {
    FirestoreDatabase.getInstance().getDb().collection("users").whereIn(FieldPath.documentId(), incidentDataModel.getResponding().subList(startIdx, endIdx)).get().addOnCompleteListener(responderTask -> {
        Log.d(TAG, "READ DATABASE - EVENT FRAGMENT");
        if (responderTask.isSuccessful()) {
            List<UsersDataModel> tempList = new ArrayList<>();
            for (QueryDocumentSnapshot userDoc : responderTask.getResult()) {
                UsersDataModel userData = userDoc.toObject(UsersDataModel.class);
                tempList.add(userData);
            }
            responderList.addAll(tempList);
            responderArr = new CharSequence[responderList.size()];
            for (int i = 0; i < responderList.size(); i++) {
                responderArr[i] = responderList.get(i).getFull_name();
            }
        } else {
            Log.w(TAG, "populateResponderList: Responder data failed to query", responderTask.getException());
        }
    });
}
Also used : UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint)

Example 19 with UsersDataModel

use of com.example.first_responder_app.dataModels.UsersDataModel in project FirstResponse by mattpost1700.

the class CreateUserFragment method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    DrawerLocker drawerLocker = ((DrawerLocker) getActivity());
    if (drawerLocker != null) {
        drawerLocker.setDrawerLocked(true);
    }
    binding = DataBindingUtil.inflate(inflater, R.layout.create_user_fragment, container, false);
    mViewModel = new ViewModelProvider(requireActivity()).get(CreateUserViewModel.class);
    binding.createUserFireDepartmentIdEditText.setText(mViewModel.getFireDepartmentId());
    binding.createUserCreateButton.setOnClickListener(v -> {
        if (validInput()) {
            FirestoreDatabase.getInstance().getDb().collection(FirestoreDatabase.FIRE_DEPARTMENT_COLLECTION_DIR).document(binding.createUserFireDepartmentIdEditText.getText().toString()).get().addOnSuccessListener(documentSnapshot -> {
                if (documentSnapshot.toObject(UsersDataModel.class) != null) {
                    UsersDataModel newUser = new UsersDataModel();
                    fillUser(newUser);
                    FirestoreDatabase.getInstance().getDb().collection(FirestoreDatabase.USERS_COLLECTION_DIR).add(newUser).addOnSuccessListener(documentReference -> {
                        // Success, login
                        if (FirestoreDatabase.getInstance().setActiveUser(newUser)) {
                            newUser.setDocumentId(documentReference.getId());
                            ActiveUser activeUser = ((ActiveUser) getActivity());
                            activeUser.setActive(newUser);
                            NavDirections action = CreateUserFragmentDirections.actionCreateUserFragmentToHomeFragment();
                            Navigation.findNavController(binding.getRoot()).navigate(action);
                        } else {
                            Toast.makeText(getContext(), "User does not have a department", Toast.LENGTH_SHORT).show();
                        }
                    }).addOnFailureListener(e -> Toast.makeText(getContext(), "Failed to upload user. Network error", Toast.LENGTH_SHORT).show());
                } else {
                    Toast.makeText(getContext(), "Fire department does not exist", Toast.LENGTH_SHORT).show();
                }
            }).addOnFailureListener(e -> Toast.makeText(getContext(), "Cannot check for fire department. Network error", Toast.LENGTH_SHORT).show());
        } else {
            Toast.makeText(getContext(), "You must fill all of the text fields", Toast.LENGTH_SHORT).show();
        }
    });
    return binding.getRoot();
}
Also used : UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) Bundle(android.os.Bundle) CreateUserViewModel(com.example.first_responder_app.viewModels.CreateUserViewModel) ViewModelProvider(androidx.lifecycle.ViewModelProvider) NavDirections(androidx.navigation.NavDirections) LayoutInflater(android.view.LayoutInflater) NonNull(androidx.annotation.NonNull) FirestoreDatabase(com.example.first_responder_app.FirestoreDatabase) ViewGroup(android.view.ViewGroup) DrawerLocker(com.example.first_responder_app.interfaces.DrawerLocker) R(com.example.first_responder_app.R) ActiveUser(com.example.first_responder_app.interfaces.ActiveUser) Nullable(androidx.annotation.Nullable) Toast(android.widget.Toast) Fragment(androidx.fragment.app.Fragment) View(android.view.View) Navigation(androidx.navigation.Navigation) CreateUserFragmentBinding(com.example.first_responder_app.databinding.CreateUserFragmentBinding) DataBindingUtil(androidx.databinding.DataBindingUtil) UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) ActiveUser(com.example.first_responder_app.interfaces.ActiveUser) DrawerLocker(com.example.first_responder_app.interfaces.DrawerLocker) CreateUserViewModel(com.example.first_responder_app.viewModels.CreateUserViewModel) ViewModelProvider(androidx.lifecycle.ViewModelProvider) NavDirections(androidx.navigation.NavDirections)

Example 20 with UsersDataModel

use of com.example.first_responder_app.dataModels.UsersDataModel in project FirstResponse by mattpost1700.

the class NewChatFragment method populateUserList.

private void populateUserList() {
    db.collection("users").get().addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot doc : task.getResult()) {
                UsersDataModel u = new UsersDataModel(doc.getId(), (String) doc.get("first_name"), (String) doc.get("last_name"));
                listOfUsers.add(u);
            }
            UsersDataModel currentU = new UsersDataModel(user.getDocumentId(), user.getFirst_name(), user.getLast_name());
            listOfUsers.add(currentU);
        } else {
            Log.d(TAG, "db get failed in new chat page " + task.getException());
        }
    });
}
Also used : UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot)

Aggregations

UsersDataModel (com.example.first_responder_app.dataModels.UsersDataModel)26 ArrayList (java.util.ArrayList)10 QueryDocumentSnapshot (com.google.firebase.firestore.QueryDocumentSnapshot)9 ActiveUser (com.example.first_responder_app.interfaces.ActiveUser)8 SuppressLint (android.annotation.SuppressLint)6 View (android.view.View)6 ViewModelProvider (androidx.lifecycle.ViewModelProvider)6 NavDirections (androidx.navigation.NavDirections)6 NonNull (androidx.annotation.NonNull)5 NavHostFragment (androidx.navigation.fragment.NavHostFragment)5 FirestoreDatabase (com.example.first_responder_app.FirestoreDatabase)5 TAG (android.content.ContentValues.TAG)4 Context (android.content.Context)4 Bundle (android.os.Bundle)4 Log (android.util.Log)4 Toast (android.widget.Toast)4 Fragment (androidx.fragment.app.Fragment)4 UserViewModel (com.example.first_responder_app.viewModels.UserViewModel)4 List (java.util.List)4 Manifest (android.Manifest)3