Merge pull request #74 from ClaytonWWilson/total-price-bar

Floating total price bar
This commit is contained in:
Clayton Wilson 2020-10-31 20:35:43 -04:00 committed by GitHub
commit d7a86aac7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 3 deletions

View File

@ -36,6 +36,7 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver {
Button incrQuan; Button incrQuan;
Button decrQuan; Button decrQuan;
Button removeItem; Button removeItem;
TextView tvTotalPrice;
ProgressBar loadingListItems; ProgressBar loadingListItems;
ArrayList<String> pNames = new ArrayList<>(); ArrayList<String> pNames = new ArrayList<>();
@ -46,6 +47,8 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver {
ArrayList<ListEntry> pListItemPair = new ArrayList<>(); ArrayList<ListEntry> pListItemPair = new ArrayList<>();
double totalPrice = 0;
Map<String, Double> totalPriceByStore = new HashMap<>(); Map<String, Double> totalPriceByStore = new HashMap<>();
Map<String, Integer> storeHeaderIndex = new HashMap<>(); Map<String, Integer> storeHeaderIndex = new HashMap<>();
@ -83,7 +86,7 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver {
e.printStackTrace(); e.printStackTrace();
} }
requestor = new Requestor(am, configs.getProperty("apiKey")); requestor = new Requestor(am, configs.getProperty("apiKey"));
requestor.getObject(Integer.toString(listID), List.class, this); requestor.getObject(Integer.toString(listID), List.class, this);
/*pNames.add("Half-gallon organic whole milk"); /*pNames.add("Half-gallon organic whole milk");
@ -166,17 +169,23 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver {
} }
} }
} }
// Increment total price
totalPrice += (item.getPrice().doubleValue() * entry.getQuantity());
} }
} }
tvTotalPrice = (TextView) findViewById(R.id.total_price);
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
tvTotalPrice.setText(String.format("$%.2f", totalPrice));
loadingListItems.setVisibility(View.GONE); loadingListItems.setVisibility(View.GONE);
myAdapter.notifyDataSetChanged(); myAdapter.notifyDataSetChanged();
} }
}); });
} }
} }
class MyAdapter extends ArrayAdapter<String> { class MyAdapter extends ArrayAdapter<String> {
@ -219,6 +228,10 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver {
ListEntry le = pListItemPair.remove(position); ListEntry le = pListItemPair.remove(position);
le.setQuantity(le.getQuantity() - 1); le.setQuantity(le.getQuantity() - 1);
pListItemPair.add(position, le); pListItemPair.add(position, le);
totalPrice -= Double.parseDouble(pPrices.get(position));
tvTotalPrice.setText(String.format("$%.2f", totalPrice));
SynchronousReceiver<Integer> synchronousenforcer = new SynchronousReceiver<>(); SynchronousReceiver<Integer> synchronousenforcer = new SynchronousReceiver<>();
requestor.deleteObject(le, synchronousenforcer, synchronousenforcer); requestor.deleteObject(le, synchronousenforcer, synchronousenforcer);
try { try {
@ -252,6 +265,10 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver {
ListEntry le = pListItemPair.remove(position); ListEntry le = pListItemPair.remove(position);
le.setQuantity(le.getQuantity() + 1); le.setQuantity(le.getQuantity() + 1);
pListItemPair.add(position, le); pListItemPair.add(position, le);
totalPrice += Double.parseDouble(pPrices.get(position));
tvTotalPrice.setText(String.format("$%.2f", totalPrice));
SynchronousReceiver<Integer> synchronousenforcer = new SynchronousReceiver<>(); SynchronousReceiver<Integer> synchronousenforcer = new SynchronousReceiver<>();
requestor.deleteObject(le, synchronousenforcer, synchronousenforcer); requestor.deleteObject(le, synchronousenforcer, synchronousenforcer);
try { try {
@ -276,6 +293,10 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver {
removeItem.setOnClickListener(new View.OnClickListener() { removeItem.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
totalPrice -= (Double.parseDouble(pPrices.get(position)) *
Double.parseDouble(pQuantity.get(position)));
tvTotalPrice.setText(String.format("$%.2f", totalPrice));
if(position == 0) { if(position == 0) {
pNames.clear(); pNames.clear();
pStores.clear(); pStores.clear();

View File

@ -155,6 +155,7 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
requestor.getObject(query, ItemSearch.class, this); requestor.getObject(query, ItemSearch.class, this);
} }
// TODO: Scroll the list back to the top when a search, sort, or filter is performed
// Sorts the search results // Sorts the search results
private void sortResults() { private void sortResults() {
// Reset the filtered list // Reset the filtered list

View File

@ -17,8 +17,32 @@
<ListView <ListView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:id="@+id/listView"> android:id="@+id/listView"
android:paddingBottom="20dp">
</ListView> </ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:background="@color/colorPrimaryDark">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="16sp"
android:text="Total: "/>
<TextView
android:id="@+id/total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="16sp"
android:text="@string/default__00_00"/>
</LinearLayout>
</RelativeLayout> </RelativeLayout>