use of com.example.first_responder_app.interfaces.ActiveUser in project FirstResponse by mattpost1700.
the class FirestoreDatabase method editUser.
/**
* Update a user's data
*
* @param firstName The user's first name
* @param lastName The user's last name
* @param phone The user's prone number (as a String)
* @param rank The user's rank (document ID, not rank name)
* @param address The user's address
* @param id The document id of the user being updated
* @param context The context
*/
public void editUser(String firstName, String lastName, String rank, String phone, String address, String id, Context context) {
String phoneNum = parsePhone(phone);
db.collection("users").document(id).update("first_name", firstName, "last_name", lastName, "address", address, "phone_number", phoneNum, FIELD_RANK_ID, rank).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d("DB", "User successfully updated");
ActiveUser activeUser = (ActiveUser) context;
if (activeUser != null) {
UsersDataModel user = activeUser.getActive();
if (user != null && (user.getDocumentId().equals(id))) {
user.setRank_id(rank);
user.setFirst_name(firstName);
user.setLast_name(lastName);
user.setPhone_number(phoneNum);
activeUser.setActive(user);
}
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w("DB", "Error updating user " + id, e);
}
});
}
use of com.example.first_responder_app.interfaces.ActiveUser 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.interfaces.ActiveUser in project FirstResponse by mattpost1700.
the class IncidentFragment method respondingButtonClicked.
/**
* When responding button is clicked update active users responding status
*
* @param b the button that was clicked
*/
public void respondingButtonClicked(View b) {
Log.d(TAG, "respondingButtonClicked: " + ((Button) b).getText());
String text = ((Button) b).getText().toString();
String id = incidentDataModel.getDocumentId();
// Get active user id
ActiveUser active = (ActiveUser) getActivity();
UsersDataModel activeUser = null;
if (active != null) {
activeUser = active.getActive();
}
if (activeUser == null && context != null) {
Toast.makeText(context, "You must be logged in", Toast.LENGTH_LONG).show();
} else if (activeUser != null) {
List<String> responses = activeUser.getResponses();
if (!AppUtil.timeIsWithin(activeUser.getResponding_time(), requireContext())) {
responses = new ArrayList<>();
}
// TODO: remove hardcoded string
if (text.equals("Unavailable")) {
FirestoreDatabase.getInstance().responding(activeUser.getDocumentId(), incidentDataModel, text, false, responses, null);
setActiveButton(text);
} else {
FirestoreDatabase.getInstance().responding(activeUser.getDocumentId(), incidentDataModel, text, true, responses, () -> {
RefreshETAs refreshETAs = (RefreshETAs) getActivity();
refreshETAs.refresh();
});
setActiveButton(text);
}
}
}
use of com.example.first_responder_app.interfaces.ActiveUser in project FirstResponse by mattpost1700.
the class UserFragment method onCreateView.
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
FragmentUserBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_user, container, false);
NavHostFragment navHostFragment = (NavHostFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
mViewModel = new ViewModelProvider(requireActivity()).get(UserViewModel.class);
user = mViewModel.getUserDataModel();
profilePictureImageView = binding.userProfilePictureImageView;
// Get active user id
ActiveUser active = (ActiveUser) getActivity();
if (active != null) {
activeUser = active.getActive();
}
// TODO: navCont created for side bar(still need to be implemented)
NavController navController = navHostFragment.getNavController();
boolean THIS_IS_CURRENT_USER = user != null && activeUser != null && activeUser.getDocumentId().equals(user.getDocumentId());
if (THIS_IS_CURRENT_USER) {
binding.userSendMessageFab.setVisibility(View.GONE);
binding.userEditFab.setOnClickListener(v -> {
NavDirections action = UserFragmentDirections.actionUserFragmentToEditUserFragment2();
Navigation.findNavController(binding.getRoot()).navigate(action);
});
} else {
binding.userEditFab.setVisibility(View.GONE);
binding.userSendMessageFab.setOnClickListener(v -> {
Snackbar.make(getView(), "Send msg!", Snackbar.LENGTH_SHORT).show();
// TODO: Send message
});
}
setText(binding);
try {
final File localFile = File.createTempFile("Images", "bmp");
StorageReference ref = FirestoreDatabase.profilePictureRef.child(user.getRemote_path_to_profile_picture());
ref.getFile(localFile).addOnSuccessListener(bytes -> {
try {
profilePictureImageView.setImageBitmap(BitmapFactory.decodeFile(localFile.getAbsolutePath()));
} catch (Exception e) {
Log.d(TAG, "onCreateView: No profile picture found");
}
}).addOnFailureListener(e -> {
Log.w(TAG, "getUserProfile: Could not load profile picture!", e);
});
} catch (IOException e) {
Log.e(TAG, "onCreateView: Failed creating temp file", e);
} catch (IllegalArgumentException e) {
Log.d(TAG, "No profile picture");
}
return binding.getRoot();
}
use of com.example.first_responder_app.interfaces.ActiveUser in project FirstResponse by mattpost1700.
the class MainActivity method setActive.
/**
* Set the logged in user
* @param user the user who just logged in
*/
@Override
public void setActive(UsersDataModel user) {
Log.d(TAG, "setActive: " + user);
if (user == null) {
this.activeUser = null;
if (userListener != null) {
userListener.remove();
userListener = null;
}
if (incidentListener != null) {
incidentListener.remove();
incidentListener = null;
}
stopETA();
return;
}
this.activeUser = user;
NavigationView navView = findViewById(R.id.navView);
View header = navView.getHeaderView(0);
TextView name = header.findViewById(R.id.nav_name);
TextView username = header.findViewById(R.id.nav_username);
String fullName = user.getFirst_name() + " " + user.getLast_name();
name.setText(fullName);
username.setText(user.getUsername());
// Ensure that the active user data is updated if database is updated
if (userListener == null) {
DocumentReference docRef = FirestoreDatabase.getInstance().getDb().collection("users").document(user.getDocumentId());
userListener = docRef.addSnapshotListener((snapshot, err) -> {
Log.d(TAG, "READ DATABASE - MAIN ACTIVITY");
if (err != null) {
System.err.println("Listen failed: " + err);
return;
}
if (snapshot != null && snapshot.exists()) {
activeUser = snapshot.toObject(UsersDataModel.class);
// Find the address of the department and determine where they are responding
setRespAddr();
// Download profile pic
try {
if (activeUser.getRemote_path_to_profile_picture() != null) {
final File localFile = File.createTempFile("Images", "bmp");
StorageReference ref = FirestoreDatabase.profilePictureRef.child(activeUser.getRemote_path_to_profile_picture());
ref.getFile(localFile).addOnSuccessListener(bytes -> {
try {
((ImageView) findViewById(R.id.appDrawerProfilePicImageView)).setImageBitmap(BitmapFactory.decodeFile(localFile.getAbsolutePath()));
} catch (Exception e) {
Log.d(TAG, "onCreateView: No profile picture found");
}
}).addOnFailureListener(e -> {
Log.w(TAG, "getUserProfile: Could not load profile picture!", e);
});
}
} catch (IOException e) {
Log.e(TAG, "onCreateView: Failed creating temp file", e);
} catch (NullPointerException e) {
Log.e(TAG, "onCreate: Cannot get user", e);
} catch (IllegalArgumentException e) {
Log.e(TAG, "onCreate: User has no image", e);
}
} else {
System.out.print("Current data: null");
}
});
}
}
Aggregations