db connections finished hardcoded

This commit is contained in:
Clayton Wilson 2020-10-08 19:08:20 -04:00
parent 762e7907b8
commit d3571cec3d
7 changed files with 192 additions and 31 deletions

View File

@ -0,0 +1,97 @@
package com.example.listify;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
public class CreateListAddDialogFragment extends DialogFragment {
public interface OnNewListAddListener {
void sendNewListName(String name, int quantity);
}
public OnNewListAddListener onNewListListener;
EditText etNewListName;
EditText etQuantity;
Button btnMinus;
Button btnPlus;
public CreateListAddDialogFragment() {}
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = requireActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
View root = inflater.inflate(R.layout.dialog_create_list_add, null);
builder.setView(root)
// Add action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
onNewListListener.sendNewListName(etNewListName.getText().toString(), Integer.parseInt(etQuantity.getText().toString()));
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
CreateListAddDialogFragment.this.getDialog().cancel();
}
});
etNewListName = (EditText) root.findViewById(R.id.et_new_list_name);
// Set up quantity selection
etQuantity = (EditText) root.findViewById(R.id.et_quantity);
btnMinus = (Button) root.findViewById(R.id.btn_minus);
btnMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int curQauntity = Integer.parseInt(etQuantity.getText().toString());
if (curQauntity > 0) {
curQauntity--;
etQuantity.setText(String.format("%d", curQauntity));
}
}
});
btnPlus = (Button) root.findViewById(R.id.btn_plus);
btnPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int curQauntity = Integer.parseInt(etQuantity.getText().toString());
curQauntity++;
etQuantity.setText(String.format("%d", curQauntity));
}
});
return builder.create();
}
// Required to extend DialogFragment
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
onNewListListener = (OnNewListAddListener) getActivity();
} catch (ClassCastException e) {
Log.e("CreateListAddDialogFragment", "onAttach: ClassCastException: " + e.getMessage());
}
}
}

View File

@ -51,7 +51,6 @@ public class CreateListDialogFragment extends DialogFragment {
etNewListName = (EditText) root.findViewById(R.id.et_new_list_name);
return builder.create();
}

View File

@ -5,8 +5,8 @@ import android.os.Bundle;
import com.amplifyframework.auth.AuthException;
import com.bumptech.glide.Glide;
import com.example.listify.data.List;
import com.example.listify.data.ListEntry;
import com.example.listify.model.Product;
import com.example.listify.model.ShoppingList;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
@ -24,7 +24,7 @@ import java.time.Instant;
import java.util.ArrayList;
import java.util.Properties;
public class ItemDetails extends AppCompatActivity implements ListPickerDialogFragment.OnListPickListener, CreateListDialogFragment.OnNewListListener {
public class ItemDetails extends AppCompatActivity implements ListPickerDialogFragment.OnListPickListener, CreateListAddDialogFragment.OnNewListAddListener {
private Product curProduct;
private LinearLayout linAddItem;
private LinearLayout linCreateList;
@ -109,8 +109,8 @@ public class ItemDetails extends AppCompatActivity implements ListPickerDialogFr
public void onClick(View v) {
closeFABMenu();
CreateListDialogFragment createListDialogFragment = new CreateListDialogFragment();
createListDialogFragment.show(getSupportFragmentManager(), "Create New List");
CreateListAddDialogFragment createListAddDialogFragment = new CreateListAddDialogFragment();
createListAddDialogFragment.show(getSupportFragmentManager(), "Create New List");
}
});
@ -162,13 +162,41 @@ public class ItemDetails extends AppCompatActivity implements ListPickerDialogFr
}
// Add the viewed item to the selected list
@Override
public void sendListSelection(int selectedListIndex, int quantity) {
Toast.makeText(this, String.format("%d of Item added to %s", quantity, shoppingLists.get(selectedListIndex).getName()), Toast.LENGTH_LONG).show();
AuthManager authManager = new AuthManager();
try {
authManager.signIn("merzn@purdue.edu", "Password123");
} catch (AuthException e) {
e.printStackTrace();
}
Properties configs = new Properties();
try {
configs = AuthManager.loadProperties(this, "android.resource://" + getPackageName() + "/raw/auths.json");
} catch (IOException | JSONException e) {
e.printStackTrace();
}
Requestor requestor = new Requestor(authManager, configs.getProperty("apiKey"));
SynchronousReceiver<Integer> idReceiver = new SynchronousReceiver<>();
try {
ListEntry entry = new ListEntry(shoppingLists.get(selectedListIndex).getItemID(), curProduct.getItemId(), quantity, Instant.now().toEpochMilli(),false);
requestor.postObject(entry);
Toast.makeText(this, String.format("%d of Item added to %s", quantity, shoppingLists.get(selectedListIndex).getName()), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, "An error occurred", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
// Create a new list and add the item to it
@Override
public void sendNewListName(String name) {AuthManager authManager = new AuthManager();
public void sendNewListName(String name, int quantity) {
AuthManager authManager = new AuthManager();
try {
authManager.signIn("merzn@purdue.edu", "Password123");
} catch (AuthException e) {
@ -187,10 +215,11 @@ public class ItemDetails extends AppCompatActivity implements ListPickerDialogFr
try {
requestor.postObject(newList, idReceiver, idReceiver);
System.out.println(idReceiver.await());
// TODO: add item to new list
newList.getItemID();
Toast.makeText(this, String.format("%s created", name), Toast.LENGTH_LONG).show();
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();
} catch (Exception e) {
Toast.makeText(this, "An error occurred", Toast.LENGTH_LONG).show();
e.printStackTrace();

View File

@ -27,7 +27,7 @@ import java.util.Arrays;
import java.util.Properties;
import java.util.Random;
public class MainActivity extends AppCompatActivity implements CreateListDialogFragment.OnNewListListener {
public class MainActivity extends AppCompatActivity implements CreateListAddDialogFragment.OnNewListAddListener {
private AppBarConfiguration mAppBarConfiguration;
@Override
@ -176,7 +176,7 @@ public class MainActivity extends AppCompatActivity implements CreateListDialogF
}
@Override
public void sendNewListName(String name) {
public void sendNewListName(String name, int quantity) {
AuthManager authManager = new AuthManager();
try {
authManager.signIn("merzn@purdue.edu", "Password123");

View File

@ -5,31 +5,21 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;
import com.amplifyframework.auth.AuthException;
import com.example.listify.adapter.SearchResultsListAdapter;
import com.example.listify.data.Item;
import com.example.listify.data.ItemSearch;
import com.example.listify.data.ListEntry;
import com.example.listify.model.Product;
import org.json.JSONException;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Properties;
import java.util.Random;
public class SearchResults extends AppCompatActivity implements SortDialogFragment.OnSortingListener {
private ListView listView;
@ -170,7 +160,8 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
try {
results = itemReceiver.await();
for (int i = 0; i < results.getResults().size(); i++) {
resultsProductList.add(new Product(results.getResults().get(i).getDescription(), results.getResults().get(i).getProductID(), Integer.toString(results.getResults().get(i).getChainID()), 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()));
// 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();

View File

@ -8,20 +8,17 @@ import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import com.amplifyframework.auth.AuthException;
import com.example.listify.AuthManager;
import com.example.listify.CreateListAddDialogFragment;
import com.example.listify.CreateListDialogFragment;
import com.example.listify.MainActivity;
import com.example.listify.R;
import com.example.listify.Requestor;
import com.example.listify.SynchronousReceiver;
import com.example.listify.adapter.DisplayShoppingListsAdapter;
import com.example.listify.data.List;
import com.example.listify.model.ShoppingList;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import org.json.JSONException;
@ -29,10 +26,9 @@ import org.json.JSONException;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Properties;
public class ListsFragment extends Fragment implements CreateListDialogFragment.OnNewListListener {
public class ListsFragment extends Fragment implements CreateListAddDialogFragment.OnNewListAddListener {
ArrayList<List> shoppingLists = new ArrayList<>();
ListView shoppingListsView;
@ -93,7 +89,7 @@ public class ListsFragment extends Fragment implements CreateListDialogFragment.
}
@Override
public void sendNewListName(String name) {
public void sendNewListName(String name, int quantity) {
AuthManager authManager = new AuthManager();
try {
authManager.signIn("merzn@purdue.edu", "Password123");

View File

@ -0,0 +1,49 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/create_a_new_list"
android:layout_gravity="center"
android:textSize="20sp"
android:layout_marginTop="5dp"/>
<EditText
android:id="@+id/et_new_list_name"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginHorizontal="15dp"
android:maxLines="1"
android:hint="@string/new_list_name"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/btn_minus"
android:layout_width="60dp"
android:layout_height="50dp"
android:text="@string/minus"/>
<EditText
android:id="@+id/et_quantity"
android:layout_width="60dp"
android:layout_height="50dp"
android:text="@string/_1"
android:inputType="number"/>
<Button
android:id="@+id/btn_plus"
android:layout_width="60dp"
android:layout_height="50dp"
android:text="@string/plus"/>
</LinearLayout>
</LinearLayout>