Skip to content

Commit

Permalink
Filter/Sorting persistence and altering navigation logic (#113)
Browse files Browse the repository at this point in the history
* persistence and slightly altering navigation logic

* Removed text field error persistence
  • Loading branch information
Sami-Jagirdar authored Dec 3, 2023
1 parent 8a4695a commit d96d9a0
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 56 deletions.
11 changes: 3 additions & 8 deletions app/src/main/java/com/example/househomey/HomeFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ public class HomeFragment extends Fragment implements FilterCallback {
private BigDecimal listSum = new BigDecimal("0.00");
private int listCount = 0;
private Map<String, Comparator<Item>> sortProperties;
private Comparator<Item> currentSort;
private String currentSortName;
private Comparator<Item> currentSort = new DescriptionComparator();
private String currentSortName = "description";
private final boolean DESC = true;
private final boolean ASC = false;
private ToggleButton toggleOrder;
private boolean sortOrder;
private boolean sortOrder = ASC;
private CollectionReference tagRef;
private ArrayList<Tag> tagList = new ArrayList<>();
private Map<String, Item> itemIdMap = new HashMap<>();
Expand Down Expand Up @@ -117,11 +117,6 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
currentSort = sortProperties.get(currentSortName);
sortOrder = received_args.getBoolean("sortOrder");
}
else {
currentSortName = "description";
currentSort = sortProperties.get("description"); //default sort property
sortOrder = ASC; //ascending order is default
}

itemRef.addSnapshotListener(this::setupItemListener);
itemListView = rootView.findViewById(R.id.item_list);
Expand Down
27 changes: 19 additions & 8 deletions app/src/main/java/com/example/househomey/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.example.househomey;

import static com.example.househomey.utils.FragmentUtils.navigateToFragmentPage;
import static com.example.househomey.utils.FragmentUtils.navigateViaBottomNavBar;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.example.househomey.form.AddItemFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
Expand All @@ -16,6 +18,8 @@
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;

import org.checkerframework.checker.units.qual.A;


/**
* MainActivity of the application, handles setting up the bottom nav fragment and the user
Expand Down Expand Up @@ -44,26 +48,32 @@ protected void onCreate(Bundle savedInstanceState) {
}


// Init home fragment
navigateToFragmentPage(this, new HomeFragment());
// Init primary fragments and show home fragment
HomeFragment homeFragment = new HomeFragment();
AddItemFragment addFragment = new AddItemFragment();
UserProfileFragment userFragment = new UserProfileFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragmentContainer, homeFragment,homeFragment.toString())
.add(R.id.fragmentContainer, addFragment,addFragment.toString())
.add(R.id.fragmentContainer, userFragment,userFragment.toString())
.replace(R.id.fragmentContainer,homeFragment)
.commit();

BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnItemSelectedListener(item -> {
int id = item.getItemId();
Fragment fragment;
if (id == R.id.action_home) {
// Go to Home page
fragment = new HomeFragment();
navigateViaBottomNavBar(homeFragment,this);
} else if (id == R.id.action_add) {
// Go to Add Item page
fragment = new AddItemFragment();
navigateViaBottomNavBar(addFragment,this);
} else {
Bundle name = new Bundle();
name.putString("username", user.getUsername());
fragment = new UserProfileFragment();
fragment.setArguments(name);
userFragment.setArguments(name);
navigateViaBottomNavBar(userFragment,this);
}
navigateToFragmentPage(this, fragment);
return true;
});
}
Expand Down Expand Up @@ -95,4 +105,5 @@ public StorageReference getImageRef(String imageId) {
.child(user.getUsername())
.child(imageId);
}

}
15 changes: 3 additions & 12 deletions app/src/main/java/com/example/househomey/SelectFragment.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.househomey;

import static com.example.househomey.utils.FragmentUtils.deletePhotosFromCloud;
import static com.example.househomey.utils.FragmentUtils.goBack;
import static com.example.househomey.utils.FragmentUtils.navigateToFragmentPage;

import android.os.Bundle;
Expand Down Expand Up @@ -92,12 +93,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
final Button cancelButton = rootView.findViewById(R.id.cancel_select_button);
cancelButton.setOnClickListener(v -> {
unselectAllItems();
HomeFragment homeFragment = new HomeFragment();
Bundle outgoing_args = new Bundle();
outgoing_args.putString("currentSortName",currentSortName);
outgoing_args.putBoolean("sortOrder", sortOrder);
homeFragment.setArguments(outgoing_args);
navigateToFragmentPage(getContext(), homeFragment);
goBack(getContext());
});

final Button deleteButton = rootView.findViewById(R.id.action_delete);
Expand Down Expand Up @@ -179,12 +175,7 @@ public void onOKPressed(ArrayList<Item> selectedItems){
Log.e("Firestore", "Failed to remove items.", error);
});

HomeFragment homeFragment = new HomeFragment();
Bundle outgoing_args = new Bundle();
outgoing_args.putString("currentSortName",currentSortName);
outgoing_args.putBoolean("sortOrder", sortOrder);
homeFragment.setArguments(outgoing_args);
navigateToFragmentPage(getContext(), homeFragment);
goBack(getContext());
}

/**
Expand Down
30 changes: 24 additions & 6 deletions app/src/main/java/com/example/househomey/ViewItemFragment.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.househomey;

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

Expand Down Expand Up @@ -39,7 +40,7 @@
* to the item
* @author Matthew Neufeld
*/
public class ViewItemFragment extends Fragment {
public class ViewItemFragment extends Fragment implements EditItemFragment.OnItemUpdateListener{
private Item item;
protected ViewPhotoAdapter viewPhotoAdapter;
private CollectionReference tagRef;
Expand Down Expand Up @@ -88,17 +89,22 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
});

// On edit button click, pass item to EditItemFragment
rootView.findViewById(R.id.edit_button).setOnClickListener(v ->
navigateToFragmentPage(getContext(), new EditItemFragment(item))
);
rootView.findViewById(R.id.edit_button).setOnClickListener(v -> {
EditItemFragment editItemFragment = new EditItemFragment();
Bundle args = new Bundle();
args.putParcelable("item",item);
args.putSerializable("listener",this);
editItemFragment.setArguments(args);
navigateToFragmentPage(getContext(),editItemFragment);
});

rootView.findViewById(R.id.delete_button).setOnClickListener(v -> {
deletePhotosFromCloud(requireActivity(), item.getPhotoIds());
((MainActivity) requireActivity()).getItemRef().document(item.getId()).delete();
navigateToFragmentPage(getContext(), new HomeFragment());
goBack(getContext());
}
);
rootView.findViewById(R.id.view_item_back_button).setOnClickListener(v -> navigateToFragmentPage(getContext(), new HomeFragment()));
rootView.findViewById(R.id.view_item_back_button).setOnClickListener(v -> goBack(getContext()));

viewPhotoAdapter = new ViewPhotoAdapter(getContext(), item.getPhotoIds(), imagePath -> viewPhotoAdapter.loadIntoImageView(mainPhoto, imagePath));
if (item.getPhotoIds().isEmpty()) {
Expand Down Expand Up @@ -126,6 +132,18 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
return rootView;
}

/**
* 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
public void onItemUpdated(Item updatedItem) {
Bundle args = new Bundle();
args.putSerializable("item", updatedItem);
this.setArguments(args);
}

/**
* This method updates the tags with changes in the firestore database and creates new
* tag objects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.househomey.Item;
import com.example.househomey.R;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;

import java.util.Map;

Expand Down Expand Up @@ -38,11 +44,22 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
// Add listeners for buttons and text validation
initDatePicker(rootView);
initTextValidators(rootView);
rootView.findViewById(R.id.add_item_back_button).setVisibility(View.GONE);
rootView.findViewById(R.id.add_item_confirm_button).setOnClickListener(v -> addItem());
rootView.findViewById(R.id.add_item_back_button).setOnClickListener(v -> navigateHomeWithIndicator(getContext()));
return rootView;
}

/**
* Sets the view functionality for when this fragment is resumed in its lifecycle
*/
@Override
public void onResume() {
super.onResume();
((TextInputLayout) getView().findViewById(R.id.add_item_description_layout)).setError(null);
((TextInputLayout) getView().findViewById(R.id.add_item_cost_layout)).setError(null);
((TextInputLayout) getView().findViewById(R.id.add_item_date_layout)).setError(null);
}

/**
* Adds a new item by validating and preparing it for storage.
*/
Expand All @@ -61,6 +78,8 @@ public void writeToFirestore() {
itemRef.add(newItem.getData()).addOnSuccessListener(documentReference -> {
Log.d("Firestore", "Successfully created new item with id:" + documentReference.getId());
navigateHomeWithIndicator(getContext());
clearDataFields();

})
.addOnFailureListener(e -> {
Log.d("Firestore", "Failed to create new item");
Expand Down
35 changes: 22 additions & 13 deletions app/src/main/java/com/example/househomey/form/EditItemFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.os.BundleCompat;

import com.example.househomey.Item;
import com.example.househomey.MainActivity;
Expand All @@ -19,6 +21,7 @@
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textview.MaterialTextView;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

Expand All @@ -28,18 +31,15 @@
* @author Owen Cooke
*/
public class EditItemFragment extends ItemFormFragment {
private final Item item;
private Item item;
private Item updatedItem;

/**
* Constructs a new EditItemFragment with the item to edit.
*
* @param item The item to be edited
*/
public EditItemFragment(Item item) {
this.item = item;
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 @@ -55,9 +55,19 @@ public EditItemFragment(Item item) {
@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);

// 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 @@ -124,10 +134,9 @@ public void writeToFirestore() {
* Sends the new item to the view item fragment
*/
private void sendItem() {
ViewItemFragment viewItemFragment = new ViewItemFragment();
Bundle args = new Bundle();
args.putSerializable("item", updatedItem);
viewItemFragment.setArguments(args);
navigateToFragmentPage(getContext(), viewItemFragment);
if (listener!=null) {
listener.onItemUpdated(updatedItem);
}
goBack(getContext());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
Expand Down Expand Up @@ -371,4 +372,25 @@ public void onDeleteButtonClicked(int position) {
photoUris.remove(position);
photoAdapter.notifyItemRemoved(position);
}

/**
* Clears all the text fields and the photos in the form
*/
protected void clearDataFields() {

View formView = getView();
int numUris = photoUris.size();
photoUris.clear();
photoAdapter.notifyItemRangeRemoved(0,numUris);
((EditText) formView.findViewById(R.id.add_item_description)).setText("");
((EditText) formView.findViewById(R.id.add_item_date)).setText("");
((EditText) formView.findViewById(R.id.add_item_cost)).setText("");
((EditText) formView.findViewById(R.id.add_item_make)).setText("");
((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("");



}
}
16 changes: 16 additions & 0 deletions app/src/main/java/com/example/househomey/utils/FragmentUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,22 @@ public static void deletePhotosFromCloud(Context context, List<String> photoIds)
}
}

/**
* Navigates to fragment without adding it to back stack, this is for moving between the 3
* primary fragments on the bottom navbar
* @param fragmentToShow page to navigate to
* @param context The AppCompatActivity context where the navigation is called
*/
public static void navigateViaBottomNavBar(Fragment fragmentToShow, Context context) {
FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
if(fragmentManager.getBackStackEntryCount()!=0) {
fragmentManager.popBackStack(fragmentToShow.toString(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
fragmentManager.beginTransaction()
.replace(R.id.fragmentContainer, fragmentToShow)
.commit();
}

/**
* Takes an unformatted string and converts it to initial case
*
Expand Down
Loading

0 comments on commit d96d9a0

Please sign in to comment.