Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Fix edit photos crashing with new navigation method #121

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions app/src/main/java/com/example/househomey/form/EditItemFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.example.househomey.utils.FragmentUtils.formatDate;
import static com.example.househomey.utils.FragmentUtils.goBack;
import static java.util.Optional.ofNullable;

import android.os.Bundle;
import android.util.Log;
Expand All @@ -27,12 +28,7 @@
public class EditItemFragment extends ItemFormFragment {
private Item item;
private Item updatedItem;

private OnItemUpdateListener listener;
public interface OnItemUpdateListener extends Serializable {
void onItemUpdated(Item updatedItem);
}


/**
* This creates the view to edit an existing item and sets the button listeners.
Expand All @@ -49,19 +45,14 @@ public interface OnItemUpdateListener extends Serializable {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = super.onCreateView(inflater, container, savedInstanceState);

//Get the item and listener for item update from Bundle
Bundle args = getArguments();
assert(args!=null);
this.item = BundleCompat.getParcelable(args, "item", Item.class);
this.listener = args.getSerializable("listener", OnItemUpdateListener.class);
ofNullable(getArguments())
.map(args -> BundleCompat.getParcelable(args, "item", Item.class))
.ifPresent(item -> this.item = item);

// Change title and prefill inputs with the existing Item's data
((MaterialTextView) rootView.findViewById(R.id.add_item_title)).setText("Edit Item");
prefillInputs(rootView);



// Add listeners for buttons and text validation
initDatePicker(rootView);
initTextValidators(rootView);
Expand Down Expand Up @@ -128,9 +119,22 @@ public void writeToFirestore() {
* Sends the new item to the view item fragment
*/
private void sendItem() {
if (listener!=null) {
if (listener != null) {
listener.onItemUpdated(updatedItem);
}
goBack(getContext());
}

/**
* Sets the listener for item update events.
*
* @param listener The listener to be notified when an item is updated.
*/
public void setListener(OnItemUpdateListener listener) {
this.listener = listener;
}

public interface OnItemUpdateListener extends Serializable {
void onItemUpdated(Item updatedItem);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,6 @@ public void onDeleteButtonClicked(int position) {
* Clears all the text fields and the photos in the form
*/
protected void clearDataFields() {

View formView = getView();
int numUris = photoUris.size();
photoUris.clear();
Expand All @@ -388,8 +387,5 @@ protected void clearDataFields() {
((EditText) formView.findViewById(R.id.add_item_model)).setText("");
((EditText) formView.findViewById(R.id.add_item_serial_number)).setText("");
((EditText) formView.findViewById(R.id.add_item_comment)).setText("");



}
}
58 changes: 26 additions & 32 deletions app/src/main/java/com/example/househomey/item/ViewItemFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@
import com.example.househomey.MainActivity;
import com.example.househomey.R;
import com.example.househomey.form.EditItemFragment;

import com.example.househomey.form.ViewPhotoAdapter;
import com.example.househomey.home.HomeFragment;
import com.example.househomey.item.Item;
import com.example.househomey.tags.Tag;
import com.example.househomey.tags.ApplyTagFragment;
import com.example.househomey.tags.Tag;
import com.example.househomey.utils.FragmentUtils;
import com.google.android.material.chip.Chip;
import com.google.android.material.chip.ChipGroup;
Expand All @@ -43,32 +40,31 @@
/**
* This fragment is for the "View Item Page" - which currently displays the details and comment linked
* to the item
*
* @author Matthew Neufeld
*/
public class ViewItemFragment extends Fragment implements EditItemFragment.OnItemUpdateListener{
private Item item;
public class ViewItemFragment extends Fragment implements EditItemFragment.OnItemUpdateListener {
protected ViewPhotoAdapter viewPhotoAdapter;
private CollectionReference tagRef;
private Item item;
private ChipGroup chipGroup;
private ListenerRegistration tagListener;

/**
*
* @param inflater The LayoutInflater object that can be used to inflate
* any views in the fragment,
* @param container If non-null, this is the parent view that the fragment's
* UI should be attached to. The fragment should not add the view itself,
* but this can be used to generate the LayoutParams of the view.
* @param inflater The LayoutInflater object that can be used to inflate
* any views in the fragment,
* @param container If non-null, this is the parent view that the fragment's
* UI should be attached to. The fragment should not add the view itself,
* but this can be used to generate the LayoutParams of the view.
* @param savedInstanceState If non-null, this fragment is being re-constructed
* from a previous saved state as given here.
*
* from a previous saved state as given here.
* @return view item fragment
*/
@SuppressLint("SetTextI18n")
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_view_item, container, false);
tagRef = ((MainActivity) requireActivity()).getTagRef();
CollectionReference tagRef = ((MainActivity) requireActivity()).getTagRef();
tagListener = tagRef.addSnapshotListener(this::setupTagListener);

// Initialize TextViews
TextView title = rootView.findViewById(R.id.view_item_title);
TextView make = rootView.findViewById(R.id.view_item_make);
Expand Down Expand Up @@ -98,10 +94,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
rootView.findViewById(R.id.edit_button).setOnClickListener(v -> {
EditItemFragment editItemFragment = new EditItemFragment();
Bundle args = new Bundle();
args.putParcelable("item",item);
args.putSerializable("listener",this);
args.putParcelable("item", item);
editItemFragment.setArguments(args);
navigateToFragmentPage(getContext(),editItemFragment);
editItemFragment.setListener(this);
navigateToFragmentPage(getContext(), editItemFragment);
});

rootView.findViewById(R.id.delete_button).setOnClickListener(v -> {
Expand Down Expand Up @@ -132,7 +128,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
Bundle tagArgs = new Bundle();
tagArgs.putParcelableArrayList("itemList", selectedItem);
applyTagFragment.setArguments(tagArgs);
applyTagFragment.show(requireActivity().getSupportFragmentManager(),"tagDialog");
applyTagFragment.show(requireActivity().getSupportFragmentManager(), "tagDialog");
});

return rootView;
Expand All @@ -150,6 +146,7 @@ public void onStop() {
/**
* Whenever an item is updated (in Edit Item), this listener method adds the updated item
* to View Item's bundle arguments and displays the updated item information upon view creation
*
* @param updatedItem the Item with updated fields
*/
@Override
Expand All @@ -162,8 +159,9 @@ public void onItemUpdated(Item updatedItem) {
/**
* This method updates the tags with changes in the firestore database and creates new
* tag objects
*
* @param querySnapshots The updated information on the inventory from the database
* @param error Non-null if an error occurred in Firestore
* @param error Non-null if an error occurred in Firestore
*/
private void setupTagListener(QuerySnapshot querySnapshots, FirebaseFirestoreException error) {
if (error != null) {
Expand All @@ -173,7 +171,7 @@ private void setupTagListener(QuerySnapshot querySnapshots, FirebaseFirestoreExc
if (querySnapshots != null) {
item.clearTags();
chipGroup.removeAllViews();
for (QueryDocumentSnapshot doc: querySnapshots) {
for (QueryDocumentSnapshot doc : querySnapshots) {
Tag tag = new Tag(doc.getId(), doc.getData());
for (String id : tag.getItemIds()) {
if (Objects.equals(item.getId(), id)) item.addTag(tag);
Expand All @@ -188,18 +186,14 @@ private void setupTagListener(QuerySnapshot querySnapshots, FirebaseFirestoreExc
*/
private void addTagsToChipGroup() {
CollectionReference tagRef = ((MainActivity) requireActivity()).getTagRef();
for (Tag tag: item.getTags()) {
for (Tag tag : item.getTags()) {
final Chip chip = FragmentUtils.makeChip(tag.getTagLabel(), true, chipGroup, getContext(), R.color.creme, R.color.black, R.color.black);
final Tag finalTag = tag;
chip.setOnCloseIconClickListener(v -> {
tagRef.document(finalTag.getTagLabel()).get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
ArrayList<String> itemIds = new ArrayList<>((List<String>) task.getResult().get("items"));
itemIds.removeIf(item -> item.equals(this.item.getId()));
tagRef.document(finalTag.getTagLabel()).update("items", itemIds);
}
});
});
chip.setOnCloseIconClickListener(v -> tagRef.document(finalTag.getTagLabel()).get().addOnSuccessListener(task -> {
ArrayList<String> itemIds = new ArrayList<>((List<String>) task.getData().get("items"));
itemIds.removeIf(item -> item.equals(this.item.getId()));
tagRef.document(finalTag.getTagLabel()).update("items", itemIds);
}));
}
}
}