Search in sources :

Example 1 with FragmentEventNewBinding

use of com.example.first_responder_app.databinding.FragmentEventNewBinding in project FirstResponse by mattpost1700.

the class NewEventFragment method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    FragmentEventNewBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_event_new, 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();
    // switch to Home fragment upon clicking it
    // also if you have any other code relates to onCreateView just add it from here
    mViewModel = new ViewModelProvider(this).get(NewEventViewModel.class);
    firestoreDatabase = new FirestoreDatabase();
    // Open Timepicker when the timepicker button is pressed
    binding.eventTimePicker.setOnClickListener(v -> {
        Log.d("TAG", "onCreateView: CLICKED");
        TimePickerFragment fragment = new TimePickerFragment();
        fragment.setListener(binding.newEventTime::setText);
        fragment.show(getActivity().getSupportFragmentManager(), "Timepicker");
    });
    // Open Datepicker when the datepicker button is pressed
    binding.eventDatePicker.setOnClickListener(v -> {
        Log.d("TAG", "onCreateView: CLICKED");
        DatePickerFragment fragment = new DatePickerFragment();
        fragment.setListener(binding.newEventDate::setText);
        fragment.show(getActivity().getSupportFragmentManager(), "Datepicker");
    });
    binding.eventCreateConfirm.setOnClickListener(v -> {
        // TODO: validate input if needed
        NavDirections action = NewEventFragmentDirections.actionNewEventFragmentToEventGroupFragment();
        String title = binding.newEventTitle.getText().toString();
        String description = binding.newEventDescription.getText().toString();
        String location = binding.newEventLocation.getText().toString();
        String eventDate = binding.newEventDate.getText().toString();
        String eventTime = binding.newEventTime.getText().toString();
        String duration = binding.newEventDurationText.getText().toString();
        // 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, title);
        intent.putExtra(CalendarContract.Events.EVENT_LOCATION, location);
        intent.putExtra(CalendarContract.Events.DESCRIPTION, description);
        intent.putExtra(CalendarContract.Events.DURATION, duration);
        if (intent.resolveActivity(requireContext().getPackageManager()) != null) {
            startActivity(intent);
        } else {
            Log.d("EVENT INTENT: ", "Event setup Failed");
        }
        if (TextUtils.isEmpty(title) || TextUtils.isEmpty(description) || location.equals("") || eventDate.equals("MM/DD/YYYY") || eventTime.equals("HH:MM") || duration.equals("")) {
            binding.newEventLog.setText(R.string.event_title_description_is_empty);
            binding.newEventLog.setVisibility(View.VISIBLE);
        } else {
            try {
                Date d = new SimpleDateFormat("MM/dd/yyyy hh:mm aa", Locale.getDefault()).parse(eventDate + " " + eventTime);
                ActiveUser a = (ActiveUser) getActivity();
                firestoreDatabase.setActiveUser(a.getActive());
                firestoreDatabase.addEvent(location, title, description, d, Integer.parseInt(duration));
                try {
                    _notificationService.notifyPostReq(getContext(), "events", "New Event", title);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Navigation.findNavController(binding.getRoot()).navigate(action);
            } catch (Exception e) {
                Toast.makeText(requireContext(), "Error Creating Event", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }
    });
    return binding.getRoot();
}
Also used : NewEventViewModel(com.example.first_responder_app.viewModels.NewEventViewModel) NavController(androidx.navigation.NavController) JSONException(org.json.JSONException) Intent(android.content.Intent) Date(java.util.Date) JSONException(org.json.JSONException) NavDirections(androidx.navigation.NavDirections) ActiveUser(com.example.first_responder_app.interfaces.ActiveUser) FragmentEventNewBinding(com.example.first_responder_app.databinding.FragmentEventNewBinding) NavHostFragment(androidx.navigation.fragment.NavHostFragment) SimpleDateFormat(java.text.SimpleDateFormat) FirestoreDatabase(com.example.first_responder_app.FirestoreDatabase) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Aggregations

Intent (android.content.Intent)1 ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 NavController (androidx.navigation.NavController)1 NavDirections (androidx.navigation.NavDirections)1 NavHostFragment (androidx.navigation.fragment.NavHostFragment)1 FirestoreDatabase (com.example.first_responder_app.FirestoreDatabase)1 FragmentEventNewBinding (com.example.first_responder_app.databinding.FragmentEventNewBinding)1 ActiveUser (com.example.first_responder_app.interfaces.ActiveUser)1 NewEventViewModel (com.example.first_responder_app.viewModels.NewEventViewModel)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 JSONException (org.json.JSONException)1