mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2025-12-16 10:48:46 +00:00
commit
19fdd96b76
@ -1,5 +1,8 @@
|
|||||||
package com.example.listify;
|
package com.example.listify;
|
||||||
|
|
||||||
|
import android.app.Dialog;
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.graphics.drawable.ColorDrawable;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
||||||
import com.amplifyframework.auth.AuthException;
|
import com.amplifyframework.auth.AuthException;
|
||||||
@ -11,6 +14,8 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
|||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.view.Window;
|
||||||
|
import android.view.WindowManager;
|
||||||
import android.widget.ImageButton;
|
import android.widget.ImageButton;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
@ -71,6 +76,8 @@ public class ItemDetails extends AppCompatActivity implements ListPickerDialogFr
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
closeFABMenu();
|
closeFABMenu();
|
||||||
|
LoadingCircleDialog loadingDialog = new LoadingCircleDialog(ItemDetails.this);
|
||||||
|
loadingDialog.show();
|
||||||
|
|
||||||
Properties configs = new Properties();
|
Properties configs = new Properties();
|
||||||
try {
|
try {
|
||||||
@ -81,21 +88,57 @@ public class ItemDetails extends AppCompatActivity implements ListPickerDialogFr
|
|||||||
|
|
||||||
Requestor requestor = new Requestor(am, configs.getProperty("apiKey"));
|
Requestor requestor = new Requestor(am, configs.getProperty("apiKey"));
|
||||||
SynchronousReceiver<Integer[]> listIdsReceiver = new SynchronousReceiver<>();
|
SynchronousReceiver<Integer[]> listIdsReceiver = new SynchronousReceiver<>();
|
||||||
SynchronousReceiver<List> listReceiver = new SynchronousReceiver<>();
|
|
||||||
|
|
||||||
requestor.getListOfIds(List.class, listIdsReceiver, listIdsReceiver);
|
requestor.getListOfIds(List.class, listIdsReceiver, listIdsReceiver);
|
||||||
try {
|
|
||||||
Integer[] listIds = listIdsReceiver.await();
|
|
||||||
for (int i = 0; i < listIds.length; i++) {
|
|
||||||
requestor.getObject(Integer.toString(listIds[i]), List.class, listReceiver, listReceiver);
|
|
||||||
shoppingLists.add(listReceiver.await());
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
ListPickerDialogFragment listPickerDialog = new ListPickerDialogFragment(shoppingLists);
|
Thread t = new Thread(new Runnable() {
|
||||||
listPickerDialog.show(getSupportFragmentManager(), "User Lists");
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Integer[] listIds = null;
|
||||||
|
try {
|
||||||
|
listIds = listIdsReceiver.await();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create threads and add them to a list
|
||||||
|
Thread[] threads = new Thread[listIds.length];
|
||||||
|
List[] results = new List[listIds.length];
|
||||||
|
for (int i = 0; i < listIds.length; i++) {
|
||||||
|
SynchronousReceiver<List> listReceiver = new SynchronousReceiver<>();
|
||||||
|
requestor.getObject(Integer.toString(listIds[i]), List.class, listReceiver, listReceiver);
|
||||||
|
final int finalI = i;
|
||||||
|
Thread l = new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
results[finalI] = listReceiver.await();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
threads[i] = l;
|
||||||
|
l.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
shoppingLists.clear();
|
||||||
|
// Wait for each thread to finish and add results to shoppingLists
|
||||||
|
for (int i = 0; i < threads.length; i++) {
|
||||||
|
try {
|
||||||
|
threads[i].join();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
shoppingLists.add(results[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
loadingDialog.cancel();
|
||||||
|
ListPickerDialogFragment listPickerDialog = new ListPickerDialogFragment(shoppingLists);
|
||||||
|
listPickerDialog.show(getSupportFragmentManager(), "User Lists");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
t.start();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -157,7 +200,7 @@ public class ItemDetails extends AppCompatActivity implements ListPickerDialogFr
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Add the viewed item to the selected list
|
// Add the selected item to the selected list
|
||||||
@Override
|
@Override
|
||||||
public void sendListSelection(int selectedListIndex, int quantity) {
|
public void sendListSelection(int selectedListIndex, int quantity) {
|
||||||
|
|
||||||
@ -184,6 +227,8 @@ public class ItemDetails extends AppCompatActivity implements ListPickerDialogFr
|
|||||||
// Create a new list and add the item to it
|
// Create a new list and add the item to it
|
||||||
@Override
|
@Override
|
||||||
public void sendNewListName(String name, int quantity) {
|
public void sendNewListName(String name, int quantity) {
|
||||||
|
LoadingCircleDialog loadingDialog = new LoadingCircleDialog(this);
|
||||||
|
loadingDialog.show();
|
||||||
|
|
||||||
Properties configs = new Properties();
|
Properties configs = new Properties();
|
||||||
try {
|
try {
|
||||||
@ -196,16 +241,34 @@ public class ItemDetails extends AppCompatActivity implements ListPickerDialogFr
|
|||||||
|
|
||||||
com.example.listify.data.List newList = new List(-1, name, "user filled by lambda", Instant.now().toEpochMilli());
|
com.example.listify.data.List newList = new List(-1, name, "user filled by lambda", Instant.now().toEpochMilli());
|
||||||
|
|
||||||
try {
|
Thread t = new Thread(new Runnable() {
|
||||||
requestor.postObject(newList, idReceiver, idReceiver);
|
@Override
|
||||||
int newListId = idReceiver.await();
|
public void run() {
|
||||||
ListEntry entry = new ListEntry(newListId, curProduct.getItemId(), quantity, Instant.now().toEpochMilli(),false);
|
try {
|
||||||
requestor.postObject(entry);
|
requestor.postObject(newList, idReceiver, idReceiver);
|
||||||
|
int newListId = idReceiver.await();
|
||||||
|
ListEntry entry = new ListEntry(newListId, curProduct.getItemId(), quantity, Instant.now().toEpochMilli(),false);
|
||||||
|
requestor.postObject(entry);
|
||||||
|
|
||||||
Toast.makeText(this, String.format("%s created and item added", name), Toast.LENGTH_LONG).show();
|
runOnUiThread(new Runnable() {
|
||||||
} catch (Exception e) {
|
@Override
|
||||||
Toast.makeText(this, "An error occurred", Toast.LENGTH_LONG).show();
|
public void run() {
|
||||||
e.printStackTrace();
|
Toast.makeText(ItemDetails.this, String.format("%s created and item added", name), Toast.LENGTH_LONG).show();
|
||||||
}
|
loadingDialog.cancel();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Toast.makeText(ItemDetails.this, "An error occurred", Toast.LENGTH_LONG).show();
|
||||||
|
loadingDialog.cancel();
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
t.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -28,7 +28,7 @@ import org.json.JSONException;
|
|||||||
|
|
||||||
import static com.example.listify.MainActivity.am;
|
import static com.example.listify.MainActivity.am;
|
||||||
|
|
||||||
public class ListPage extends AppCompatActivity {
|
public class ListPage extends AppCompatActivity implements Requestor.Receiver {
|
||||||
ListView listView;
|
ListView listView;
|
||||||
MyAdapter myAdapter;
|
MyAdapter myAdapter;
|
||||||
Requestor requestor;
|
Requestor requestor;
|
||||||
@ -36,6 +36,7 @@ public class ListPage extends AppCompatActivity {
|
|||||||
Button incrQuan;
|
Button incrQuan;
|
||||||
Button decrQuan;
|
Button decrQuan;
|
||||||
Button removeItem;
|
Button removeItem;
|
||||||
|
ProgressBar loadingListItems;
|
||||||
|
|
||||||
ArrayList<String> pNames = new ArrayList<>();
|
ArrayList<String> pNames = new ArrayList<>();
|
||||||
ArrayList<String> pStores = new ArrayList<>();
|
ArrayList<String> pStores = new ArrayList<>();
|
||||||
@ -50,11 +51,23 @@ public class ListPage extends AppCompatActivity {
|
|||||||
|
|
||||||
DecimalFormat df = new DecimalFormat("0.00");
|
DecimalFormat df = new DecimalFormat("0.00");
|
||||||
|
|
||||||
|
// TODO: Display a message if their list is empty
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
// Read list ID from caller
|
// Read list ID from caller
|
||||||
final int listID = (int) getIntent().getSerializableExtra("listID");
|
final int listID = (int) getIntent().getSerializableExtra("listID");
|
||||||
|
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.activity_list);
|
||||||
|
|
||||||
|
listView = findViewById(R.id.listView);
|
||||||
|
myAdapter = new MyAdapter(this, pNames, pStores, pPrices, pQuantity, pImages);
|
||||||
|
|
||||||
|
listView.setAdapter(myAdapter);
|
||||||
|
|
||||||
|
loadingListItems = findViewById(R.id.progress_loading_list_items);
|
||||||
|
loadingListItems.setVisibility(View.VISIBLE);
|
||||||
|
|
||||||
pNames.add("Total Price");
|
pNames.add("Total Price");
|
||||||
pStores.add("");
|
pStores.add("");
|
||||||
pPrices.add("0.00");
|
pPrices.add("0.00");
|
||||||
@ -62,6 +75,7 @@ public class ListPage extends AppCompatActivity {
|
|||||||
pImages.add("-1");
|
pImages.add("-1");
|
||||||
pListItemPair.add(null);
|
pListItemPair.add(null);
|
||||||
|
|
||||||
|
|
||||||
Properties configs = new Properties();
|
Properties configs = new Properties();
|
||||||
try {
|
try {
|
||||||
configs = AuthManager.loadProperties(this, "android.resource://" + getPackageName() + "/raw/auths.json");
|
configs = AuthManager.loadProperties(this, "android.resource://" + getPackageName() + "/raw/auths.json");
|
||||||
@ -69,17 +83,31 @@ public class ListPage extends AppCompatActivity {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
requestor = new Requestor(am, configs.getProperty("apiKey"));
|
requestor = new Requestor(am, configs.getProperty("apiKey"));
|
||||||
SynchronousReceiver<List> lr = new SynchronousReceiver<>();
|
|
||||||
requestor.getObject(Integer.toString(listID), List.class, lr);
|
requestor.getObject(Integer.toString(listID), List.class, this);
|
||||||
|
|
||||||
List list;
|
/*pNames.add("Half-gallon organic whole milk");
|
||||||
|
pStores.add("Kroger");
|
||||||
|
pPrices.add("$5.00");
|
||||||
|
pQuantity.add("1");
|
||||||
|
pImages.add(R.drawable.milk);
|
||||||
|
|
||||||
try {
|
pNames.add("5-bunch medium bananas");
|
||||||
list = lr.await();
|
pStores.add("Kroger");
|
||||||
}
|
pPrices.add("$3.00");
|
||||||
catch (Exception e) {
|
pQuantity.add("1");
|
||||||
list = null;
|
pImages.add(R.drawable.bananas);
|
||||||
}
|
|
||||||
|
pNames.add("JIF 40-oz creamy peanut butter");
|
||||||
|
pStores.add("Kroger");
|
||||||
|
pPrices.add("$7.00");
|
||||||
|
pQuantity.add("1");
|
||||||
|
pImages.add(R.drawable.peanutbutter);*/
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void acceptDelivery(Object delivered) {
|
||||||
|
List list = (List) delivered;
|
||||||
|
|
||||||
if(list != null) {
|
if(list != null) {
|
||||||
for (ListEntry entry : list.getEntries()) {
|
for (ListEntry entry : list.getEntries()) {
|
||||||
@ -123,7 +151,6 @@ public class ListPage extends AppCompatActivity {
|
|||||||
|
|
||||||
double newTotal = Double.parseDouble(pPrices.get(0)) + (item.getPrice().doubleValue() * entry.getQuantity());
|
double newTotal = Double.parseDouble(pPrices.get(0)) + (item.getPrice().doubleValue() * entry.getQuantity());
|
||||||
pPrices.set(0, df.format(newTotal));
|
pPrices.set(0, df.format(newTotal));
|
||||||
|
|
||||||
index++;
|
index++;
|
||||||
|
|
||||||
pNames.add(index, item.getDescription());
|
pNames.add(index, item.getDescription());
|
||||||
@ -141,15 +168,15 @@ public class ListPage extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
loadingListItems.setVisibility(View.GONE);
|
||||||
|
myAdapter.notifyDataSetChanged();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
setContentView(R.layout.activity_list);
|
|
||||||
|
|
||||||
listView = findViewById(R.id.listView);
|
|
||||||
myAdapter = new MyAdapter(this, pNames, pStores, pPrices, pQuantity, pImages);
|
|
||||||
|
|
||||||
listView.setAdapter(myAdapter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyAdapter extends ArrayAdapter<String> {
|
class MyAdapter extends ArrayAdapter<String> {
|
||||||
|
|||||||
@ -0,0 +1,36 @@
|
|||||||
|
package com.example.listify;
|
||||||
|
|
||||||
|
import android.app.Dialog;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.graphics.drawable.ColorDrawable;
|
||||||
|
import android.view.Window;
|
||||||
|
import android.view.WindowManager;
|
||||||
|
|
||||||
|
public class LoadingCircleDialog {
|
||||||
|
Dialog loadingDialog;
|
||||||
|
|
||||||
|
public LoadingCircleDialog(Context context) {
|
||||||
|
loadingDialog = new Dialog(context);
|
||||||
|
|
||||||
|
// Create and show a loading dialog
|
||||||
|
loadingDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
|
||||||
|
loadingDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||||
|
|
||||||
|
// layout to display
|
||||||
|
loadingDialog.setContentView(R.layout.dialog_loading);
|
||||||
|
|
||||||
|
// set color transpartent
|
||||||
|
loadingDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
|
||||||
|
loadingDialog.setCancelable(false);
|
||||||
|
loadingDialog.setCanceledOnTouchOutside(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void show() {
|
||||||
|
loadingDialog.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cancel() {
|
||||||
|
loadingDialog.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -9,6 +9,7 @@ import android.widget.EditText;
|
|||||||
import android.widget.ImageButton;
|
import android.widget.ImageButton;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
|
import android.widget.ProgressBar;
|
||||||
import android.widget.SearchView;
|
import android.widget.SearchView;
|
||||||
import com.example.listify.adapter.SearchResultsListAdapter;
|
import com.example.listify.adapter.SearchResultsListAdapter;
|
||||||
import com.example.listify.data.ItemSearch;
|
import com.example.listify.data.ItemSearch;
|
||||||
@ -22,8 +23,8 @@ import java.util.Properties;
|
|||||||
|
|
||||||
import static com.example.listify.MainActivity.am;
|
import static com.example.listify.MainActivity.am;
|
||||||
|
|
||||||
public class SearchResults extends AppCompatActivity implements SortDialogFragment.OnSortingListener {
|
public class SearchResults extends AppCompatActivity implements SortDialogFragment.OnSortingListener, Requestor.Receiver {
|
||||||
private ListView listView;
|
private ProgressBar loadingSearch;
|
||||||
private SearchResultsListAdapter searchResultsListAdapter;
|
private SearchResultsListAdapter searchResultsListAdapter;
|
||||||
private List<Product> resultsProductList = new ArrayList<>();
|
private List<Product> resultsProductList = new ArrayList<>();
|
||||||
private List<Product> resultsProductListSorted = new ArrayList<>();
|
private List<Product> resultsProductListSorted = new ArrayList<>();
|
||||||
@ -47,6 +48,8 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
|
|||||||
Toolbar toolbar = findViewById(R.id.toolbar);
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||||
setSupportActionBar(toolbar);
|
setSupportActionBar(toolbar);
|
||||||
|
|
||||||
|
loadingSearch = (ProgressBar) findViewById(R.id.progress_loading_search);
|
||||||
|
|
||||||
// Back button closes this activity and returns to previous activity (MainActivity)
|
// Back button closes this activity and returns to previous activity (MainActivity)
|
||||||
ImageButton backButton = (ImageButton) findViewById(R.id.backToHomeButton);
|
ImageButton backButton = (ImageButton) findViewById(R.id.backToHomeButton);
|
||||||
backButton.setOnClickListener(new View.OnClickListener() {
|
backButton.setOnClickListener(new View.OnClickListener() {
|
||||||
@ -77,7 +80,7 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
listView = (ListView) findViewById(R.id.search_results_list);
|
ListView listView = (ListView) findViewById(R.id.search_results_list);
|
||||||
searchResultsListAdapter = new SearchResultsListAdapter(this, resultsProductListSorted);
|
searchResultsListAdapter = new SearchResultsListAdapter(this, resultsProductListSorted);
|
||||||
listView.setAdapter(searchResultsListAdapter);
|
listView.setAdapter(searchResultsListAdapter);
|
||||||
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||||
@ -96,6 +99,15 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
|
|||||||
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
|
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onQueryTextSubmit(String query) {
|
public boolean onQueryTextSubmit(String query) {
|
||||||
|
// Show progress bar
|
||||||
|
loadingSearch.setVisibility(View.VISIBLE);
|
||||||
|
|
||||||
|
// Clear the old search results
|
||||||
|
resultsProductList.clear();
|
||||||
|
|
||||||
|
// Clear old search results from the view
|
||||||
|
resultsProductListSorted.clear();
|
||||||
|
searchResultsListAdapter.notifyDataSetChanged();
|
||||||
doSearch(query);
|
doSearch(query);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -122,12 +134,8 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
|
|||||||
sortDialog.show(getSupportFragmentManager(), "Sort");
|
sortDialog.show(getSupportFragmentManager(), "Sort");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Override default phone back button to add animation
|
// Override default phone back button to add animation
|
||||||
@Override
|
@Override
|
||||||
public void onBackPressed() {
|
public void onBackPressed() {
|
||||||
@ -136,10 +144,6 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void doSearch(String query) {
|
private void doSearch(String query) {
|
||||||
|
|
||||||
// Clear the old search results
|
|
||||||
resultsProductList.clear();
|
|
||||||
|
|
||||||
Properties configs = new Properties();
|
Properties configs = new Properties();
|
||||||
try {
|
try {
|
||||||
configs = AuthManager.loadProperties(this, "android.resource://" + getPackageName() + "/raw/auths.json");
|
configs = AuthManager.loadProperties(this, "android.resource://" + getPackageName() + "/raw/auths.json");
|
||||||
@ -148,32 +152,7 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
|
|||||||
}
|
}
|
||||||
|
|
||||||
Requestor requestor = new Requestor(am, configs.getProperty("apiKey"));
|
Requestor requestor = new Requestor(am, configs.getProperty("apiKey"));
|
||||||
|
requestor.getObject(query, ItemSearch.class, this);
|
||||||
SynchronousReceiver<ItemSearch> itemReceiver = new SynchronousReceiver<>();
|
|
||||||
requestor.getObject(query, ItemSearch.class, itemReceiver, itemReceiver);
|
|
||||||
ItemSearch results;
|
|
||||||
try {
|
|
||||||
results = itemReceiver.await();
|
|
||||||
for (int i = 0; i < results.getResults().size(); i++) {
|
|
||||||
// TODO: Change to dynamically grab chain name by id
|
|
||||||
resultsProductList.add(new Product(results.getResults().get(i).getDescription(), results.getResults().get(i).getProductID(), "Kroger", results.getResults().get(i).getChainID(), results.getResults().get(i).getUpc(), results.getResults().get(i).getDescription(), results.getResults().get(i).getPrice(), results.getResults().get(i).getImageURL(), results.getResults().get(i).getDepartment()));
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a list of all stores in the results so the user can filter by store name
|
|
||||||
for (int i = 0; i < resultsProductList.size(); i++) {
|
|
||||||
if (!stores.contains(resultsProductList.get(i).getChainName())) {
|
|
||||||
stores.add(resultsProductList.get(i).getChainName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add all results to the sorted list
|
|
||||||
resultsProductListSorted.addAll(resultsProductList);
|
|
||||||
|
|
||||||
// Apply selected sorting to the list
|
|
||||||
sortResults();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sorts the search results
|
// Sorts the search results
|
||||||
@ -203,7 +182,6 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
|
|||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// TODO: May need to change this depending on if price is stored as a string or a double
|
|
||||||
case 2:
|
case 2:
|
||||||
resultsProductListSorted.sort(new Comparator<Product>() {
|
resultsProductListSorted.sort(new Comparator<Product>() {
|
||||||
@Override
|
@Override
|
||||||
@ -257,7 +235,57 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
|
|||||||
resultsProductListSorted.clear();
|
resultsProductListSorted.clear();
|
||||||
resultsProductListSorted.addAll(temp);
|
resultsProductListSorted.addAll(temp);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
searchResultsListAdapter.notifyDataSetChanged();
|
// This is called after the search results come back from the server
|
||||||
|
// TODO: Display a "no results" message if nothing is found when searching
|
||||||
|
@Override
|
||||||
|
public void acceptDelivery(Object delivered) {
|
||||||
|
ItemSearch results = (ItemSearch) delivered;
|
||||||
|
|
||||||
|
try {
|
||||||
|
for (int i = 0; i < results.getResults().size(); i++) {
|
||||||
|
// TODO: Change to dynamically grab chain name by id
|
||||||
|
resultsProductList.add(new Product(
|
||||||
|
results.getResults().get(i).getDescription(),
|
||||||
|
results.getResults().get(i).getProductID(),
|
||||||
|
"Kroger",
|
||||||
|
results.getResults().get(i).getChainID(),
|
||||||
|
results.getResults().get(i).getUpc(),
|
||||||
|
results.getResults().get(i).getDescription(),
|
||||||
|
results.getResults().get(i).getPrice(),
|
||||||
|
results.getResults().get(i).getImageURL(),
|
||||||
|
results.getResults().get(i).getDepartment()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a list of all stores in the results so the user can filter by store name
|
||||||
|
for (int i = 0; i < resultsProductList.size(); i++) {
|
||||||
|
if (!stores.contains(resultsProductList.get(i).getChainName())) {
|
||||||
|
stores.add(resultsProductList.get(i).getChainName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add all results to the sorted list
|
||||||
|
resultsProductListSorted.addAll(resultsProductList);
|
||||||
|
|
||||||
|
// Apply selected sorting to the list
|
||||||
|
sortResults();
|
||||||
|
|
||||||
|
// Updates the list of search results. Runs on the main UI thread since other threads are
|
||||||
|
// not allowed to change UI elements
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
searchResultsListAdapter.notifyDataSetChanged();
|
||||||
|
|
||||||
|
// Hide progress bar
|
||||||
|
loadingSearch.setVisibility(View.GONE);
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,15 +1,22 @@
|
|||||||
package com.example.listify.ui.lists;
|
package com.example.listify.ui.lists;
|
||||||
|
|
||||||
|
import android.app.Dialog;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.graphics.drawable.ColorDrawable;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.Window;
|
||||||
|
import android.view.WindowManager;
|
||||||
import android.widget.AdapterView;
|
import android.widget.AdapterView;
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
|
import android.widget.ProgressBar;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
|
import androidx.fragment.app.ListFragment;
|
||||||
|
|
||||||
import com.amplifyframework.auth.AuthException;
|
import com.amplifyframework.auth.AuthException;
|
||||||
import com.example.listify.AuthManager;
|
import com.example.listify.AuthManager;
|
||||||
@ -17,6 +24,7 @@ import com.example.listify.CreateListAddDialogFragment;
|
|||||||
import com.example.listify.CreateListDialogFragment;
|
import com.example.listify.CreateListDialogFragment;
|
||||||
import com.example.listify.ItemDetails;
|
import com.example.listify.ItemDetails;
|
||||||
import com.example.listify.ListPage;
|
import com.example.listify.ListPage;
|
||||||
|
import com.example.listify.LoadingCircleDialog;
|
||||||
import com.example.listify.R;
|
import com.example.listify.R;
|
||||||
import com.example.listify.Requestor;
|
import com.example.listify.Requestor;
|
||||||
import com.example.listify.SearchResults;
|
import com.example.listify.SearchResults;
|
||||||
@ -34,16 +42,20 @@ import java.util.Properties;
|
|||||||
|
|
||||||
import static com.example.listify.MainActivity.am;
|
import static com.example.listify.MainActivity.am;
|
||||||
|
|
||||||
public class ListsFragment extends Fragment implements CreateListDialogFragment.OnNewListListener {
|
public class ListsFragment extends Fragment implements CreateListDialogFragment.OnNewListListener, Requestor.Receiver {
|
||||||
ArrayList<List> shoppingLists = new ArrayList<>();
|
ArrayList<List> shoppingLists = new ArrayList<>();
|
||||||
DisplayShoppingListsAdapter displayShoppingListsAdapter;
|
DisplayShoppingListsAdapter displayShoppingListsAdapter;
|
||||||
|
Requestor requestor;
|
||||||
ListView shoppingListsView;
|
ListView shoppingListsView;
|
||||||
|
ProgressBar loadingLists;
|
||||||
|
int resultsIndex;
|
||||||
|
|
||||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||||
View root = inflater.inflate(R.layout.fragment_lists, container, false);
|
View root = inflater.inflate(R.layout.fragment_lists, container, false);
|
||||||
shoppingListsView = root.findViewById(R.id.shopping_lists);
|
shoppingListsView = root.findViewById(R.id.shopping_lists);
|
||||||
|
loadingLists = (ProgressBar) root.findViewById(R.id.progress_loading_lists);
|
||||||
|
loadingLists.setVisibility(View.VISIBLE);
|
||||||
|
|
||||||
// TODO: Switch this to async
|
|
||||||
Properties configs = new Properties();
|
Properties configs = new Properties();
|
||||||
try {
|
try {
|
||||||
configs = AuthManager.loadProperties(getContext(), "android.resource://" + getActivity().getPackageName() + "/raw/auths.json");
|
configs = AuthManager.loadProperties(getContext(), "android.resource://" + getActivity().getPackageName() + "/raw/auths.json");
|
||||||
@ -51,36 +63,13 @@ public class ListsFragment extends Fragment implements CreateListDialogFragment.
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
Requestor requestor = new Requestor(am, configs.getProperty("apiKey"));
|
requestor = new Requestor(am, configs.getProperty("apiKey"));
|
||||||
SynchronousReceiver<Integer[]> listIdsReceiver = new SynchronousReceiver<>();
|
SynchronousReceiver<Integer[]> listIdsReceiver = new SynchronousReceiver<>();
|
||||||
SynchronousReceiver<List> listReceiver = new SynchronousReceiver<>();
|
|
||||||
|
|
||||||
requestor.getListOfIds(List.class, listIdsReceiver, listIdsReceiver);
|
final Requestor.Receiver<Integer[]> recv = this;
|
||||||
try {
|
requestor.getListOfIds(List.class, recv, null);
|
||||||
Integer[] listIds = listIdsReceiver.await();
|
|
||||||
for (int i = 0; i < listIds.length; i++) {
|
|
||||||
requestor.getObject(Integer.toString(listIds[i]), List.class, listReceiver, listReceiver);
|
|
||||||
shoppingLists.add(listReceiver.await());
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Set adapter and display this users lists
|
|
||||||
displayShoppingListsAdapter = new DisplayShoppingListsAdapter(getActivity(), shoppingLists);
|
|
||||||
shoppingListsView.setAdapter(displayShoppingListsAdapter);
|
|
||||||
shoppingListsView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
|
||||||
Intent listPage = new Intent(getContext(), ListPage.class);
|
|
||||||
|
|
||||||
// Send the list ID
|
|
||||||
listPage.putExtra("listID", shoppingLists.get(position).getItemID());
|
|
||||||
startActivity(listPage);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
FloatingActionButton fab = (FloatingActionButton) root.findViewById(R.id.new_list_fab);
|
FloatingActionButton fab = (FloatingActionButton) root.findViewById(R.id.new_list_fab);
|
||||||
Fragment thisFragment = this;
|
Fragment thisFragment = this;
|
||||||
fab.setOnClickListener(new View.OnClickListener() {
|
fab.setOnClickListener(new View.OnClickListener() {
|
||||||
@ -97,6 +86,8 @@ public class ListsFragment extends Fragment implements CreateListDialogFragment.
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendNewListName(String name) {
|
public void sendNewListName(String name) {
|
||||||
|
LoadingCircleDialog loadingDialog = new LoadingCircleDialog(getActivity());
|
||||||
|
loadingDialog.show();
|
||||||
|
|
||||||
Properties configs = new Properties();
|
Properties configs = new Properties();
|
||||||
try {
|
try {
|
||||||
@ -111,13 +102,95 @@ public class ListsFragment extends Fragment implements CreateListDialogFragment.
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
requestor.postObject(newList, idReceiver, idReceiver);
|
requestor.postObject(newList, idReceiver, idReceiver);
|
||||||
newList.setItemID(idReceiver.await());
|
|
||||||
shoppingLists.add(newList);
|
|
||||||
displayShoppingListsAdapter.notifyDataSetChanged();
|
|
||||||
Toast.makeText(getContext(), String.format("%s created", name), Toast.LENGTH_LONG).show();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Toast.makeText(getContext(), "An error occurred", Toast.LENGTH_LONG).show();
|
Toast.makeText(getContext(), "An error occurred", Toast.LENGTH_LONG).show();
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
Thread t = new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
newList.setItemID(idReceiver.await());
|
||||||
|
} catch (Exception e) {
|
||||||
|
getActivity().runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Toast.makeText(getContext(), "An error occurred", Toast.LENGTH_LONG).show();
|
||||||
|
loadingDialog.cancel();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
}
|
||||||
|
shoppingLists.add(newList);
|
||||||
|
|
||||||
|
getActivity().runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
displayShoppingListsAdapter.notifyDataSetChanged();
|
||||||
|
loadingDialog.cancel();
|
||||||
|
Toast.makeText(getContext(), String.format("%s created", name), Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
t.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void acceptDelivery(Object delivered) {
|
||||||
|
Integer[] listIds = (Integer[]) delivered;
|
||||||
|
// Create threads and add them to a list
|
||||||
|
Thread[] threads = new Thread[listIds.length];
|
||||||
|
List[] results = new List[listIds.length];
|
||||||
|
for (int i = 0; i < listIds.length; i++) {
|
||||||
|
SynchronousReceiver<List> listReceiver = new SynchronousReceiver<>();
|
||||||
|
requestor.getObject(Integer.toString(listIds[i]), List.class, listReceiver, listReceiver);
|
||||||
|
final int finalI = i;
|
||||||
|
Thread t = new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
results[finalI] = listReceiver.await();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
threads[i] = t;
|
||||||
|
t.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for each thread to finish and add results to shoppingLists
|
||||||
|
for (int i = 0; i < threads.length; i++) {
|
||||||
|
try {
|
||||||
|
threads[i].join();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
shoppingLists.add(results[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set adapter and display this users lists
|
||||||
|
displayShoppingListsAdapter = new DisplayShoppingListsAdapter(getActivity(), shoppingLists);
|
||||||
|
|
||||||
|
getActivity().runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
shoppingListsView.setAdapter(displayShoppingListsAdapter);
|
||||||
|
shoppingListsView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||||
|
Intent listPage = new Intent(getContext(), ListPage.class);
|
||||||
|
|
||||||
|
// Send the list ID
|
||||||
|
listPage.putExtra("listID", shoppingLists.get(position).getItemID());
|
||||||
|
startActivity(listPage);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
loadingLists.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,9 +1,19 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<androidx.constraintlayout.widget.ConstraintLayout
|
<RelativeLayout
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
|
<ProgressBar
|
||||||
|
android:id="@+id/progress_loading_list_items"
|
||||||
|
style="?android:attr/progressBarStyleLarge"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_centerHorizontal="true"
|
||||||
|
android:layout_centerVertical="true"
|
||||||
|
android:indeterminate="true"
|
||||||
|
android:visibility="gone"/>
|
||||||
|
|
||||||
<ListView
|
<ListView
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
@ -11,4 +21,4 @@
|
|||||||
|
|
||||||
</ListView>
|
</ListView>
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</RelativeLayout>
|
||||||
@ -12,6 +12,16 @@
|
|||||||
tools:context=".SearchResults"
|
tools:context=".SearchResults"
|
||||||
tools:showIn="@layout/activity_search_results">
|
tools:showIn="@layout/activity_search_results">
|
||||||
|
|
||||||
|
<ProgressBar
|
||||||
|
android:id="@+id/progress_loading_search"
|
||||||
|
style="?android:attr/progressBarStyleLarge"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_centerHorizontal="true"
|
||||||
|
android:layout_centerVertical="true"
|
||||||
|
android:indeterminate="true"
|
||||||
|
android:visibility="gone"/>
|
||||||
|
|
||||||
<ListView
|
<ListView
|
||||||
android:id="@+id/search_results_list"
|
android:id="@+id/search_results_list"
|
||||||
android:layout_width="fill_parent"
|
android:layout_width="fill_parent"
|
||||||
|
|||||||
16
Listify/app/src/main/res/layout/dialog_loading.xml
Normal file
16
Listify/app/src/main/res/layout/dialog_loading.xml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<RelativeLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:background="@null">
|
||||||
|
|
||||||
|
<ProgressBar
|
||||||
|
android:id="@+id/progress_loading_lists"
|
||||||
|
style="?android:attr/progressBarStyleLarge"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_centerHorizontal="true"
|
||||||
|
android:layout_centerVertical="true"
|
||||||
|
android:indeterminate="true"
|
||||||
|
android:visibility="visible"/>
|
||||||
|
</RelativeLayout>
|
||||||
@ -12,6 +12,16 @@
|
|||||||
tools:context=".ui.lists.ListsFragment"
|
tools:context=".ui.lists.ListsFragment"
|
||||||
tools:showIn="@layout/fragment_lists">
|
tools:showIn="@layout/fragment_lists">
|
||||||
|
|
||||||
|
<ProgressBar
|
||||||
|
android:id="@+id/progress_loading_lists"
|
||||||
|
style="?android:attr/progressBarStyleLarge"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_centerHorizontal="true"
|
||||||
|
android:layout_centerVertical="true"
|
||||||
|
android:indeterminate="true"
|
||||||
|
android:visibility="gone"/>
|
||||||
|
|
||||||
<ListView
|
<ListView
|
||||||
android:id="@+id/shopping_lists"
|
android:id="@+id/shopping_lists"
|
||||||
android:layout_width="fill_parent"
|
android:layout_width="fill_parent"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user