Async loading list products

This commit is contained in:
Clayton Wilson 2020-10-22 18:36:00 -04:00
parent ec28e8babc
commit 07d0cd44cb
2 changed files with 55 additions and 37 deletions

View File

@ -21,13 +21,14 @@ import java.util.Properties;
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;
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<>();
@ -39,11 +40,23 @@ public class ListPage extends AppCompatActivity {
Requestor requestor; Requestor requestor;
// 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);
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");
@ -51,19 +64,32 @@ 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<>();
//ListReceiver<List> lr = new ListReceiver<>(); //ListReceiver<List> lr = new ListReceiver<>();
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()) {
int product = entry.getProductID(); int product = entry.getProductID();
@ -87,31 +113,13 @@ public class ListPage extends AppCompatActivity {
} }
} }
/*pNames.add("Half-gallon organic whole milk"); runOnUiThread(new Runnable() {
pStores.add("Kroger"); @Override
pPrices.add("$5.00"); public void run() {
pQuantity.add("1"); loadingListItems.setVisibility(View.GONE);
pImages.add(R.drawable.milk); myAdapter.notifyDataSetChanged();
}
pNames.add("5-bunch medium bananas"); });
pStores.add("Kroger");
pPrices.add("$3.00");
pQuantity.add("1");
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);*/
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> {

View File

@ -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>