Enforce synchronous updates for quanitity

Also added a new delete method to requestor to allow for a receiver.
This commit is contained in:
NMerz 2020-10-09 01:16:42 -04:00
parent 702a83ff85
commit a7ac3d6ec9
2 changed files with 34 additions and 24 deletions

View File

@ -1,35 +1,25 @@
package com.example.listify;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.listify.data.Item;
import com.example.listify.data.List;
import com.example.listify.data.ListEntry;
import com.example.listify.model.Product;
import static com.example.listify.MainActivity.am;
import org.json.JSONException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import static com.example.listify.MainActivity.am;
public class ListPage extends AppCompatActivity {
ListView listView;
@ -155,13 +145,21 @@ public class ListPage extends AppCompatActivity {
decrQuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//int q = Integer.parseInt(pQuantity.get(position)) - 1;
//pQuantity.set(position, Integer.toString(q));
int q = Integer.parseInt(pQuantity.get(position)) - 1;
pQuantity.set(position, Integer.toString(q));
ListEntry le = pListItemPair.remove(position);
le.setQuantity(le.getQuantity() - 1);
requestor.deleteObject(le);
pListItemPair.add(position, le);
SynchronousReceiver<Integer> synchronousenforcer = new SynchronousReceiver<>();
requestor.deleteObject(le, synchronousenforcer, synchronousenforcer);
try {
requestor.postObject(le);
synchronousenforcer.await();
} catch (Exception e) {
e.printStackTrace();
}
try {
requestor.postObject(le, synchronousenforcer, synchronousenforcer);
synchronousenforcer.await();
}
catch (Exception e) {
Log.i("Authentication", e.toString());
@ -176,13 +174,21 @@ public class ListPage extends AppCompatActivity {
incrQuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//int q = Integer.parseInt(pQuantity.get(position)) + 1;
//pQuantity.set(position, Integer.toString(q));
int q = Integer.parseInt(pQuantity.get(position)) + 1;
pQuantity.set(position, Integer.toString(q));
ListEntry le = pListItemPair.remove(position);
le.setQuantity(le.getQuantity() + 1);
requestor.deleteObject(le);
pListItemPair.add(position, le);
SynchronousReceiver<Integer> synchronousenforcer = new SynchronousReceiver<>();
requestor.deleteObject(le, synchronousenforcer, synchronousenforcer);
try {
requestor.postObject(le);
synchronousenforcer.await();
} catch (Exception e) {
e.printStackTrace();
}
try {
requestor.postObject(le, synchronousenforcer, synchronousenforcer);
synchronousenforcer.await();
}
catch (Exception e) {
Log.i("Authentication", e.toString());

View File

@ -45,9 +45,13 @@ public class Requestor {
}
public void deleteObject(Object toDelete) {
deleteObject(toDelete, null, null);
}
public void deleteObject(Object toDelete, Receiver receiver, RequestErrorHandler errorHandler) {
String deleteURL = DEV_BASEURL + "/" + toDelete.getClass().getSimpleName();
Request deleteRequest = buildBaseRequest(deleteURL, "DELETE", new Gson().toJson(toDelete));
launchCall(deleteRequest, null, toDelete.getClass(), null);
launchCall(deleteRequest, receiver, toDelete.getClass(), errorHandler);
}
public void deleteObject(String id, Class classType, RequestErrorHandler failureHandler) {