Skip to content

Commit

Permalink
Created Dialog for Adding Tags (#74)
Browse files Browse the repository at this point in the history
* created TagFragment

* made addTagButton final

* Created and applied  util function for adding a chip to a chip group

* more consolidation and updated tag colours to resemble mockup

* removed unused imports

* updated Javadocs

* cleaned up a few more things
  • Loading branch information
MatthewNeufeld authored Nov 21, 2023
1 parent f720b03 commit 4981f1f
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 12 deletions.
15 changes: 15 additions & 0 deletions app/src/main/java/com/example/househomey/SelectFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import androidx.core.os.BundleCompat;
import androidx.fragment.app.Fragment;

import com.example.househomey.tags.TagFragment;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.WriteBatch;
Expand All @@ -39,6 +40,7 @@ public class SelectFragment extends Fragment implements DeleteItemsFragment.Dele
private boolean sortOrder;
private ArrayList<Item> itemList;
private ItemAdapter itemAdapter;

/**
*
* @param inflater The LayoutInflater object that can be used to inflate
Expand Down Expand Up @@ -99,9 +101,22 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
}
});

final Button actionTagsButton = rootView.findViewById(R.id.action_tags);
actionTagsButton.setOnClickListener(v -> {
openTagDialog();
});

return rootView;
}

/**
* Opens the TagFragment dialog.
*/
private void openTagDialog() {
TagFragment tagsDialogFragment = new TagFragment();
tagsDialogFragment.show(getChildFragmentManager(), "tagDialog");
}

/**
* This is an interface method that handles the deletion of selected items from the user database
* This is done upon user confirmation in the delete items dialog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import android.widget.Button;
import android.widget.EditText;

import androidx.core.content.ContextCompat;

import com.example.househomey.R;
import com.example.househomey.filter.model.FilterCallback;
import com.example.househomey.filter.model.KeywordFilter;
import com.example.househomey.utils.FragmentUtils;
import com.google.android.material.chip.Chip;
import com.google.android.material.chip.ChipGroup;

Expand Down Expand Up @@ -77,13 +76,7 @@ public void getFilterInput() {
public void autoFillFilter(List<String> keyWordArray) {
for (String label : keyWordArray) {
Context context = contentView.getContext();
Chip chip = new Chip(context);
chip.setText(label);
chip.setCloseIconVisible(true);
chip.setChipBackgroundColorResource(R.color.white);
chip.setChipStrokeColorResource(R.color.brown);
chip.setTextColor(ContextCompat.getColor(context , R.color.brown));
chipGroup.addView(chip);
Chip chip = FragmentUtils.makeChip(label, true, chipGroup, context, R.color.white, R.color.brown, R.color.brown);
chipTextVals.add(chip.getText().toString());
chip.setOnCloseIconClickListener(v -> {
chipGroup.removeView(chip);
Expand Down
75 changes: 75 additions & 0 deletions app/src/main/java/com/example/househomey/tags/TagFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.example.househomey.tags;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.fragment.app.DialogFragment;

import com.example.househomey.R;
import com.example.househomey.utils.FragmentUtils;
import com.google.android.material.chip.Chip;
import com.google.android.material.chip.ChipGroup;

/**
* DialogFragment that allows users to add new tags
* @author Matthew Neufeld
*/
public class TagFragment extends DialogFragment {

// Declare UI components
private ChipGroup chipGroup;
private EditText tagEditText;
private Button addTagButton;
private Chip chip;

/**
* Called to create the dialog, initializing UI components and setting up button listeners.
*
* @param savedInstanceState Bundle containing the saved state of the fragment.
* @return The created AlertDialog.
*/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

// Initialize AlertDialog builder
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

// Inflate the layout for this fragment
LayoutInflater inflater = requireActivity().getLayoutInflater();
View rootView = inflater.inflate(R.layout.fragment_tags, null);

// Initialize UI components
chipGroup = rootView.findViewById(R.id.chip_group_labels);
tagEditText = rootView.findViewById(R.id.tag_edit_text);
addTagButton = rootView.findViewById(R.id.add_tag_button);

// Set up button click listener to add tags
addTagButton.setOnClickListener(v -> {
String tagLabel = tagEditText.getText().toString().trim();
if (!tagLabel.isEmpty())
addTag(tagLabel);
});

return builder
.setView(rootView)
.setTitle("Tags")
.setNeutralButton("Cancel", null)
.setPositiveButton("Apply Tags", null)
.create();
}

/**
* Adds a new tag to the ChipGroup when addTagButton is clicked.
* @param tagLabel the label that will go on the tag
*/
private void addTag(String tagLabel) {
chip = FragmentUtils.makeChip(tagLabel, false, chipGroup, requireContext(), R.color.white, R.color.black, R.color.black);
chip.setOnCloseIconClickListener(v -> chipGroup.removeView(chip)); // for deleting tags - leave for now - we will discuss what we want to do
tagEditText.getText().clear();
}
}
34 changes: 31 additions & 3 deletions app/src/main/java/com/example/househomey/utils/FragmentUtils.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.example.househomey.utils;

import android.content.Context;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.househomey.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.chip.Chip;
import com.google.android.material.chip.ChipGroup;
import com.google.android.material.datepicker.CalendarConstraints;
import com.google.android.material.datepicker.DateValidatorPointBackward;
import com.google.android.material.datepicker.MaterialDatePicker;
Expand All @@ -19,13 +21,14 @@

/**
* This is a utility class for fragment page navigation within an Android application.
* @author Owen Cooke
* @author Owen Cooke, Matthew Neufeld
*/
public class FragmentUtils {

/**
* Navigates to a specified fragment page by replacing the fragment
* within the provided AppCompatActivity's fragment container.
*
* @param context The AppCompatActivity instance used for accessing the FragmentManager.
* @param page The Fragment to navigate to and replace the current fragment with.
*/
Expand All @@ -40,6 +43,7 @@ public static void navigateToFragmentPage(Context context, Fragment page) {
/**
* Specialized method for navigating back to the HomeFragment
* while also setting the selected item in the BottomNavigationView
*
* @param context The AppCompatActivity context from which the navigation is initiated.
*/
public static void navigateHomeWithIndicator(Context context) {
Expand All @@ -49,6 +53,7 @@ public static void navigateHomeWithIndicator(Context context) {

/**
* Navigates the fragment manager back to the previous fragment if there is a fragment in the back stack.
*
* @param context The AppCompatActivity context where the navigation is called.
*/
public static void goBack(Context context) {
Expand All @@ -60,6 +65,7 @@ public static void goBack(Context context) {

/**
* Enables date picking for the calendar
*
* @return datePicker
*/
public static MaterialDatePicker<Long> createDatePicker() {
Expand All @@ -77,6 +83,7 @@ public static MaterialDatePicker<Long> createDatePicker() {

/**
* Formats given date as a string in "yyyy-MM-dd" format.
*
* @param date date to be formatted
* @return formatted date
*/
Expand All @@ -85,6 +92,27 @@ public static String formatDate(Date date) {
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat.format(date);
}
}

/**
* Makes a chip and adds it to a chip group
* @param label label that will go on the chip
* @param closeIconVisibility boolean: if true, closeIcon is visible, if false, not visible
* @param chipGroup group of chips the chip will be added to
* @param context the given context
* @param backgroundColour background colour of the chip
* @param strokeColour stroke colour of the chip
* @param textColour text colour of the chip
* @return a new chip
*/
public static Chip makeChip(String label, Boolean closeIconVisibility, ChipGroup chipGroup, Context context, int backgroundColour, int strokeColour, int textColour) {
Chip chip = new Chip(context);
chip.setText(label);
chip.setCloseIconVisible(closeIconVisibility);
chip.setChipBackgroundColorResource(backgroundColour);
chip.setChipStrokeColorResource(strokeColour);
chip.setTextColor(ContextCompat.getColor(context, textColour));
chipGroup.addView(chip);
return chip;
}
}

48 changes: 48 additions & 0 deletions app/src/main/res/layout/fragment_tags.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<com.google.android.material.textfield.TextInputLayout
style="@style/TextInputLayoutStyle"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="New tag"
app:hintEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tag_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.button.MaterialButton
android:id="@+id/add_tag_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="30dp"
android:layout_height="30dp"
android:padding="0dp"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
app:icon="@drawable/baseline_add_24"
app:iconSize="30dp"
app:iconTint="@color/black" />
</LinearLayout>

<com.google.android.material.chip.ChipGroup
android:id="@+id/chip_group_labels"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:singleLine="false">
</com.google.android.material.chip.ChipGroup>

</LinearLayout>

0 comments on commit 4981f1f

Please sign in to comment.