Skip to content

Commit

Permalink
Javadocs (#33)
Browse files Browse the repository at this point in the history
* Javadocs and removal of uneeded classes

* More Javadocs

* More Javadocs

* Consolidate listener

* Added first unit test

* Item Unit tests
  • Loading branch information
ldbonkowski authored Nov 5, 2023
1 parent 75b536d commit eb8bdd2
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 108 deletions.
34 changes: 34 additions & 0 deletions app/src/main/java/com/example/househomey/AddItemFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,34 @@
import java.util.HashMap;
import java.util.Objects;

/**
* This fragment is responsible for creating and loading to the database a new item
* @author Owen Cooke
*/
public class AddItemFragment extends Fragment {
private Date dateAcquired;
private TextInputEditText dateTextView;
private final CollectionReference itemRef;

/**
* Constructs a new AddItemFragment with a firestore reference
* @param itemRef A reference to a firestore collection of items
*/
public AddItemFragment(CollectionReference itemRef) {
this.itemRef = itemRef;
}

/**
* This creates the view to add an item to a user's inventory and set's the button listeners
* @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.
* @return The fragment_add_item view with the correct listeners added
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_add_item, container, false);
Expand All @@ -44,6 +63,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
return rootView;
}

/**
* This shows a datePicker for the user to select the date of the item acquisition
*/
private void showDatePicker() {
MaterialDatePicker<Long> datePicker = MaterialDatePicker.Builder.datePicker()
.setInputMode(MaterialDatePicker.INPUT_MODE_CALENDAR)
Expand All @@ -57,6 +79,9 @@ private void showDatePicker() {
datePicker.show(getParentFragmentManager(), "Date Picker");
}

/**
* Adds the user input data to the firestore database
*/
private void addItem() {
// TODO: add input validation

Expand All @@ -81,10 +106,19 @@ private void addItem() {
});
}

/**
* Gets the user input as a string from a given TextInputEditText
* @param id Id of the TextInputEditText object
* @return The user input String
* @throws NullPointerException if the input text is null
*/
private String getInputText(int id) {
return Objects.requireNonNull(((TextInputEditText) requireView().findViewById(id)).getText()).toString();
}

/**
* Changes the fragment back to the home screen
*/
private void returnToHomeScreen() {
FragmentManager fragmentManager = getParentFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
Expand Down

This file was deleted.

62 changes: 56 additions & 6 deletions app/src/main/java/com/example/househomey/HomeFragment.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.househomey;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
Expand All @@ -19,38 +20,87 @@
import com.example.househomey.Filters.MakeFilterFragment;
import com.example.househomey.Filters.TagFilterFragment;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;

public class HomeFragment extends Fragment implements FirestoreUpdateListener {
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
* This fragment represents the home screen containing the primary list of the user's inventory
* @author Owen Cooke, Jared Drueco, Lukas Bonkowski
*/
public class HomeFragment extends Fragment {
private CollectionReference itemRef;
private ListView itemListView;
private PopupMenu filterView;

private ArrayList<Item> itemList = new ArrayList<>();
private ArrayAdapter<Item> itemAdapter;

/**
* This constructs a new HomeFragment with the appropriate list of items
* @param itemRef A reference to the firestore collection containing the items to display
*/
public HomeFragment(CollectionReference itemRef) {
this.itemRef = itemRef;
}

/**
* @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.
* @return the home fragment view containing the inventory list
*/
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// Inflate the fragment's layout
View rootView = inflater.inflate(R.layout.fragment_home, container, false);

itemListView = rootView.findViewById(R.id.item_list);
itemAdapter = new ItemAdapter(getContext(), new ItemList(this, itemRef).getItems());
itemAdapter = new ItemAdapter(getContext(), itemList);
itemListView.setAdapter(itemAdapter);

itemRef.addSnapshotListener(this::setupItemListener);

View filterButton = rootView.findViewById(R.id.filter_dropdown_button);
filterButton.setOnClickListener(v -> showFilterMenu(v));
filterButton.setOnClickListener(this::showFilterMenu);

return rootView;

}

@Override
public void notifyDataSetChanged() {
itemAdapter.notifyDataSetChanged();
/**
* This method updates the itemAdapter with changes in the firestore database and creates new
* item objects
* @param querySnapshots The updated information on the inventory from the database
* @param error Non-null if an error occurred in Firestore
*/
private void setupItemListener(QuerySnapshot querySnapshots, FirebaseFirestoreException error) {
if (error != null) {
Log.e("Firestore", error.toString());
return;
}
if (querySnapshots != null) {
itemList.clear();
for (QueryDocumentSnapshot doc: querySnapshots) {
Map<String, Object> data = new HashMap<>(doc.getData());
itemList.add(new Item(doc.getId(), data));
itemAdapter.notifyDataSetChanged();
}
}
}

/**
* Displays the filter menu with options to select the appropriate filter
* @param view The view to set the filter menu on
*/
private void showFilterMenu(View view) {
PopupMenu popupMenu = new PopupMenu(requireContext(), view);
MenuInflater inflater = popupMenu.getMenuInflater();
Expand Down
55 changes: 34 additions & 21 deletions app/src/main/java/com/example/househomey/Item.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.example.househomey;

import androidx.annotation.NonNull;

import com.google.firebase.Timestamp;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* This class represents an inventory item with a variety of properties
Expand All @@ -17,21 +18,26 @@ public class Item {
public String id;
private String description;
private Date acquisitionDate;
private String make;
private String model;
private String serialNumber;
private String comment;

private String make = "";
private String model = "";
private String serialNumber = "";
private String comment = "";
private BigDecimal cost;

private Map<String, Object> data;

public Item(String id, Map<String, Object> data) {
this.data = data;
this.id = id;
this.description = (String) data.get("description");
this.acquisitionDate = ((Timestamp) data.get("acquisitionDate")).toDate();

/**
* This constructs a new item from a Map of data with a reference to it's firestore docunebt
* @param id The id of this object's document in the firestore database
* @param data The data from that document to initialize the instance
* @throws NullPointerException if a null required field is given
*/
public Item(String id, @NonNull Map<String, Object> data) {
// Required fields
this.id = Objects.requireNonNull(id);
this.description = (String) Objects.requireNonNull(data.get("description"));
this.acquisitionDate = ((Timestamp) Objects.requireNonNull(data.get("acquisitionDate"))).toDate();
this.cost = new BigDecimal((String) Objects.requireNonNull(data.get("cost"))).setScale(2);

// Optional fields
if (data.containsKey("make")) {
this.make = (String) data.get("make");
}
Expand All @@ -44,22 +50,29 @@ public Item(String id, Map<String, Object> data) {
if (data.containsKey("comment")) {
this.comment = (String) data.get("comment");
}

this.cost = new BigDecimal((String) data.get("cost")).setScale(2);
}

public HashMap<String, Object> data() {
return new HashMap<>();
}

/**
* Getter for acquisitionDate
* @return The acquisition date of this item
*/
public Date getAcquisitionDate() {
return acquisitionDate;
}

/**
* Getter for description
* @return The brief description of this item
*/
public String getDescription() {
return description;
}


/**
* Getter for cost
* @return The cost of this item
*/
public BigDecimal getCost() {
return cost;
}
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/example/househomey/ItemAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,31 @@ public class ItemAdapter extends ArrayAdapter<Item> {
private ArrayList<Item> items;
private Context context;

/**
* Constructs a new ItemAdapter with an ArrayList of items
* @param context The context containing this adapter
* @param items ArrayList of items to display
*/
public ItemAdapter(Context context, ArrayList<Item> items){
super(context, 0, items);
this.items = items;
this.context = context;
}


/**
* Gets a view to display an items from this adapters ArrayList.
* @param position The position of the item within the adapter's data set of the item whose view
* we want.
* @param convertView The old view to reuse, if possible. Note: You should check that this view
* is non-null and of an appropriate type before using. If it is not possible to convert
* this view to display the correct data, this method can create a new view.
* Heterogeneous lists can specify their number of view types, so that this View is
* always of the right type (see {@link #getViewTypeCount()} and
* {@link #getItemViewType(int)}).
* @param parent The parent that this view will eventually be attached to
* @return The view with all data from the items displayed
*/
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Expand Down
49 changes: 0 additions & 49 deletions app/src/main/java/com/example/househomey/ItemList.java

This file was deleted.

19 changes: 18 additions & 1 deletion app/src/main/java/com/example/househomey/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,22 @@
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;


/**
* MainActivity of the application, handles setting up the bottom nav fragment and the user
* @author Owen Cooke, Lukas Bonkowski
*/
public class MainActivity extends AppCompatActivity {
// Define constants for filter items
private User user;

/**
* Method to run on creation of the activity. Handles user setup and creates the bottom
* nav fragment
* @param savedInstanceState If the activity is being re-initialized after
* previously being shut down then this Bundle contains the data it most
* recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -53,12 +66,16 @@ protected void onCreate(Bundle savedInstanceState) {
});
}

/**
* Changes the current fragment to the passed fragment
* @param fragment a Fragment to navigate to and replace the current fragment with
*/
private void navigateToFragment(Fragment fragment) {
// Get the FragmentManager and start a transaction
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();

// Replace the current fragment with the addItemFragment
// Replace the current fragment with the new Fragment
transaction.replace(R.id.fragmentContainer, fragment);

// Commit the transaction
Expand Down
Loading

0 comments on commit eb8bdd2

Please sign in to comment.