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();
}
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();
}
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;
}
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();
}
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();
}
Aggregations