Search in sources :

Example 1 with FirestoreDatabase

use of com.example.first_responder_app.FirestoreDatabase in project FirstResponse by mattpost1700.

the class NewChatFragment 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
    ChatNewFragmentBinding binding = DataBindingUtil.inflate(inflater, R.layout.chat_new_fragment, 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();
    firestoreDatabase = new FirestoreDatabase();
    db = FirebaseFirestore.getInstance();
    activeUser = (ActiveUser) getActivity();
    if (activeUser != null) {
        user = activeUser.getActive();
    }
    listOfUsers = new ArrayList<>();
    populateUserList();
    listOfNewUsers = new ArrayList<>();
    NewChatRecyclerViewAdapter.ItemClickListener userClickListener = ((view, position, data) -> {
        // remove user
        listOfNewUsers.remove(data);
        newChatRecyclerViewAdapter.notifyDataSetChanged();
    });
    RecyclerView newChatRecyclerView = binding.newChatRecyclerView;
    newChatRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    newChatRecyclerViewAdapter = new NewChatRecyclerViewAdapter(getContext(), listOfNewUsers);
    newChatRecyclerViewAdapter.setClickListener(userClickListener);
    newChatRecyclerView.setAdapter(newChatRecyclerViewAdapter);
    newChatRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {

        @Override
        public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
            newChatRecyclerView.removeOnLayoutChangeListener(this);
        // newChatRecyclerView.scrollToPosition(newChatRecyclerView.getAdapter().getItemCount() - 1);
        }
    });
    binding.newChatAddUserFab.setOnClickListener(v -> {
        String newUserName = binding.newChatUsersEditText.getText().toString();
        UsersDataModel result = listOfUsers.stream().filter(u -> newUserName.equals(u.getFull_name())).findAny().orElse(null);
        // If user exists, is not the current user, and has not already been added
        if (result != null && !result.getDocumentId().equals(user.getDocumentId()) && !listOfNewUsers.contains(result)) {
            binding.newChatErrorMsg.setText("");
            listOfNewUsers.add(result);
            newChatRecyclerViewAdapter.notifyDataSetChanged();
        } else {
            binding.newChatErrorMsg.setText("User not found");
        }
    });
    binding.newChatCreateConfirm.setOnClickListener(v -> {
        String chatName = binding.newChatChatNameEditText.getText().toString();
        if (listOfNewUsers.size() == 0) {
            binding.newChatErrorMsg.setText("Must add users to the chat");
        } else if (chatName.length() == 0) {
            binding.newChatErrorMsg.setText("Chat name must not be blank");
        } else {
            listOfNewUsers.add(user);
            firestoreDatabase.addChat(chatName, listOfNewUsers);
        }
        NavDirections action = NewChatFragmentDirections.actionNewChatFragmentToChatGroupFragment();
        Navigation.findNavController(binding.getRoot()).navigate(action);
    });
    return binding.getRoot();
}
Also used : UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) NavController(androidx.navigation.NavController) NewChatRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.NewChatRecyclerViewAdapter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) ChatNewFragmentBinding(com.example.first_responder_app.databinding.ChatNewFragmentBinding) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) NavDirections(androidx.navigation.NavDirections) RecyclerView(androidx.recyclerview.widget.RecyclerView) NavHostFragment(androidx.navigation.fragment.NavHostFragment) FirestoreDatabase(com.example.first_responder_app.FirestoreDatabase)

Example 2 with FirestoreDatabase

use of com.example.first_responder_app.FirestoreDatabase in project FirstResponse by mattpost1700.

the class ChatFragment 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
    ChatFragmentBinding binding = DataBindingUtil.inflate(inflater, R.layout.chat_fragment, 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();
    firestoreDatabase = new FirestoreDatabase();
    db = FirebaseFirestore.getInstance();
    activeUser = (ActiveUser) getActivity();
    if (activeUser != null) {
        user = activeUser.getActive();
    }
    mViewModel = new ViewModelProvider(requireActivity()).get(ChatViewModel.class);
    c = mViewModel.getChatDetail();
    listOfMembers = c.getMembers();
    listOfMessages = mViewModel.getListOfMessages();
    populateMessageList();
    // getting data from chat
    mViewModel = new ViewModelProvider(requireActivity()).get(ChatViewModel.class);
    messageInfo = mViewModel.getMessageDetail();
    RecyclerView chatRecyclerView = binding.chatRecyclerView;
    chatRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    chatRecyclerViewAdapter = new ChatRecyclerViewAdapter(getContext(), listOfMessages, listOfMembers);
    // chatRecyclerViewAdapter.setClickListener(chatClickListener);
    chatRecyclerView.setAdapter(chatRecyclerViewAdapter);
    chatRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {

        @Override
        public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
            chatRecyclerView.removeOnLayoutChangeListener(this);
            chatRecyclerView.scrollToPosition(chatRecyclerView.getAdapter().getItemCount() - 1);
        }
    });
    binding.sendButton.setOnClickListener(v -> {
        String userName = "";
        if (user != null) {
            userName = user.getFirst_name() + " " + user.getLast_name();
        }
        String msg = binding.editText.getText().toString();
        if (!msg.equals("") && user != null) {
            firestoreDatabase.addMessage(c.getId(), msg, user.getDocumentId(), chatRecyclerViewAdapter, mViewModel);
        }
    });
    binding.leaveChat.setOnClickListener(v -> {
        firestoreDatabase.removeUserFromChat(user.getDocumentId(), c.getId(), listOfMembers);
        NavDirections action = ChatFragmentDirections.actionChatFragmentToChatGroupFragment();
        Navigation.findNavController(binding.getRoot()).navigate(action);
    });
    return binding.getRoot();
}
Also used : NavController(androidx.navigation.NavController) ChatRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.ChatRecyclerViewAdapter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) NavDirections(androidx.navigation.NavDirections) ChatViewModel(com.example.first_responder_app.viewModels.ChatViewModel) RecyclerView(androidx.recyclerview.widget.RecyclerView) ChatFragmentBinding(com.example.first_responder_app.databinding.ChatFragmentBinding) NavHostFragment(androidx.navigation.fragment.NavHostFragment) FirestoreDatabase(com.example.first_responder_app.FirestoreDatabase) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 3 with FirestoreDatabase

use of com.example.first_responder_app.FirestoreDatabase in project FirstResponse by mattpost1700.

the class ChatGroupFragment 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
    ChatGroupFragmentBinding binding = DataBindingUtil.inflate(inflater, R.layout.chat_group_fragment, 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();
    firestoreDatabase = new FirestoreDatabase();
    db = firestoreDatabase.getDb();
    activeUser = (ActiveUser) getActivity();
    if (activeUser != null) {
        user = activeUser.getActive();
    }
    listOfChats = new ArrayList<>();
    populateChatList();
    ChatGroupRecyclerViewAdapter.ItemClickListener chatClickListener = ((view, position, data) -> {
        // passing data to chat
        mViewModel = new ViewModelProvider(requireActivity()).get(ChatViewModel.class);
        mViewModel.setChatDetail(data);
        List<Message> temp = new ArrayList<>();
        mViewModel.setListOfMessages(temp);
        NavDirections action = ChatGroupFragmentDirections.actionChatGroupFragmentToChatFragment();
        Navigation.findNavController(binding.getRoot()).navigate(action);
    });
    RecyclerView chatGroupRecyclerView = binding.chatGroupRecyclerView;
    chatGroupRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    chatGroupRecyclerViewAdapter = new ChatGroupRecyclerViewAdapter(getContext(), listOfChats);
    chatGroupRecyclerViewAdapter.setClickListener(chatClickListener);
    chatGroupRecyclerView.setAdapter(chatGroupRecyclerViewAdapter);
    binding.chatTextView.setOnClickListener(v -> {
        NavDirections action = ChatGroupFragmentDirections.actionChatGroupFragmentToChatFragment();
        Navigation.findNavController(binding.getRoot()).navigate(action);
    });
    binding.addChatTextView.setOnClickListener(v -> {
        NavDirections action = ChatGroupFragmentDirections.actionChatGroupFragmentToNewChatFragment();
        Navigation.findNavController(binding.getRoot()).navigate(action);
    });
    return binding.getRoot();
}
Also used : Message(com.example.first_responder_app.messaging.Message) ArrayList(java.util.ArrayList) NavController(androidx.navigation.NavController) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) NavDirections(androidx.navigation.NavDirections) ChatGroupRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.ChatGroupRecyclerViewAdapter) ChatViewModel(com.example.first_responder_app.viewModels.ChatViewModel) RecyclerView(androidx.recyclerview.widget.RecyclerView) NavHostFragment(androidx.navigation.fragment.NavHostFragment) FirestoreDatabase(com.example.first_responder_app.FirestoreDatabase) ChatGroupFragmentBinding(com.example.first_responder_app.databinding.ChatGroupFragmentBinding) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 4 with FirestoreDatabase

use of com.example.first_responder_app.FirestoreDatabase 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();
}
Also used : UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) Spinner(android.widget.Spinner) NavController(androidx.navigation.NavController) View(android.view.View) AdapterView(android.widget.AdapterView) NavDirections(androidx.navigation.NavDirections) EditUserViewModel(com.example.first_responder_app.viewModels.EditUserViewModel) UserViewModel(com.example.first_responder_app.viewModels.UserViewModel) FragmentEditUserBinding(com.example.first_responder_app.databinding.FragmentEditUserBinding) AdapterView(android.widget.AdapterView) NavHostFragment(androidx.navigation.fragment.NavHostFragment) FirestoreDatabase(com.example.first_responder_app.FirestoreDatabase) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 5 with FirestoreDatabase

use of com.example.first_responder_app.FirestoreDatabase 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

NavController (androidx.navigation.NavController)5 NavDirections (androidx.navigation.NavDirections)5 NavHostFragment (androidx.navigation.fragment.NavHostFragment)5 FirestoreDatabase (com.example.first_responder_app.FirestoreDatabase)5 ViewModelProvider (androidx.lifecycle.ViewModelProvider)4 View (android.view.View)3 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)3 RecyclerView (androidx.recyclerview.widget.RecyclerView)3 UsersDataModel (com.example.first_responder_app.dataModels.UsersDataModel)2 ChatViewModel (com.example.first_responder_app.viewModels.ChatViewModel)2 Intent (android.content.Intent)1 AdapterView (android.widget.AdapterView)1 Spinner (android.widget.Spinner)1 ChatFragmentBinding (com.example.first_responder_app.databinding.ChatFragmentBinding)1 ChatGroupFragmentBinding (com.example.first_responder_app.databinding.ChatGroupFragmentBinding)1 ChatNewFragmentBinding (com.example.first_responder_app.databinding.ChatNewFragmentBinding)1 FragmentEditUserBinding (com.example.first_responder_app.databinding.FragmentEditUserBinding)1 FragmentEventNewBinding (com.example.first_responder_app.databinding.FragmentEventNewBinding)1 ActiveUser (com.example.first_responder_app.interfaces.ActiveUser)1 Message (com.example.first_responder_app.messaging.Message)1