From c0a93e3684cbba0ff737819b66436cf3efdf3a9c Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Fri, 27 Nov 2020 18:13:37 -0500 Subject: [PATCH] Rename list functionality --- .../java/com/example/listify/ListPage.java | 55 ++++++++++---- .../listify/RenameListDialogFragment.java | 71 +++++++++++++++++++ .../ShoppingListsSwipeableAdapter.java | 11 +-- .../java/com/example/listify/data/List.java | 3 +- .../com/example/listify/data/ListEntry.java | 4 +- .../main/res/layout/dialog_rename_list.xml | 22 ++++++ 6 files changed, 146 insertions(+), 20 deletions(-) create mode 100644 Listify/app/src/main/java/com/example/listify/RenameListDialogFragment.java create mode 100644 Listify/app/src/main/res/layout/dialog_rename_list.xml diff --git a/Listify/app/src/main/java/com/example/listify/ListPage.java b/Listify/app/src/main/java/com/example/listify/ListPage.java index b695e23..86bbdee 100644 --- a/Listify/app/src/main/java/com/example/listify/ListPage.java +++ b/Listify/app/src/main/java/com/example/listify/ListPage.java @@ -1,15 +1,16 @@ package com.example.listify; -import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.graphics.Color; import android.os.Bundle; +import android.text.Editable; import android.util.Log; import android.view.*; import android.widget.*; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; @@ -27,7 +28,7 @@ import java.util.Properties; import static com.example.listify.MainActivity.am; -public class ListPage extends AppCompatActivity implements Requestor.Receiver { +public class ListPage extends AppCompatActivity implements Requestor.Receiver, RenameListDialogFragment.OnRenameListListener { ListView listView; MyAdapter myAdapter; Requestor requestor; @@ -59,17 +60,15 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver { Map storeHeaderIndex = new HashMap<>(); DecimalFormat df = new DecimalFormat("0.00"); - int LIST_ID; - String LIST_NAME; + List selectedList; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); - LIST_ID = (int) getIntent().getSerializableExtra("listID"); - LIST_NAME = (String) getIntent().getSerializableExtra("listName"); - setTitle(LIST_NAME); + selectedList = (List) getIntent().getSerializableExtra("selectedList"); + setTitle(selectedList.getName()); Properties configs = new Properties(); try { @@ -78,7 +77,7 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver { e.printStackTrace(); } requestor = new Requestor(am, configs.getProperty("apiKey")); - requestor.getObject(Integer.toString(LIST_ID), List.class, this); + requestor.getObject(Integer.toString(selectedList.getListID()), List.class, this); listView = findViewById(R.id.listView); myAdapter = new MyAdapter(this, pNames, pStores, pPrices, pQuantity, pImages); @@ -160,7 +159,7 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver { } requestor = new Requestor(am, configs.getProperty("apiKey")); - requestor.getObject(Integer.toString(LIST_ID), List.class, ListPage.this); + requestor.getObject(Integer.toString(selectedList.getListID()), List.class, ListPage.this); } }); } @@ -168,16 +167,16 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver { @Override public boolean onCreateOptionsMenu(Menu menu) { //Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.list, menu); - - //return super.onCreateOptionsMenu(menu); + + MenuItem renameItem = menu.findItem(R.id.action_rename_list); renameItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { - Toast.makeText(ListPage.this, "Rename List", Toast.LENGTH_SHORT).show(); + RenameListDialogFragment renameListDialog = new RenameListDialogFragment(); + renameListDialog.show(getSupportFragmentManager(), "Rename List"); return false; } }); @@ -196,7 +195,7 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver { @Override public boolean onMenuItemClick(MenuItem item) { - ListDuplicate duplicate = new ListDuplicate(LIST_ID, String.format("%s copy", LIST_NAME)); + ListDuplicate duplicate = new ListDuplicate(selectedList.getListID(), String.format("%s copy", selectedList.getName())); Properties configs = new Properties(); try { @@ -348,6 +347,34 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver { refreshList.setRefreshing(false); } + @Override + public void sendRenameListName(String name) { + selectedList.setName(name); + + Properties configs = new Properties(); + try { + configs = AuthManager.loadProperties(ListPage.this, "android.resource://" + getPackageName() + "/raw/auths.json"); + } catch (IOException | JSONException e) { + e.printStackTrace(); + } + + requestor = new Requestor(am, configs.getProperty("apiKey")); + try { + requestor.putObject(selectedList); + } catch (Exception e) { + e.printStackTrace(); + } + + + runOnUiThread(new Runnable() { + @Override + public void run() { + setTitle(name); + Toast.makeText(ListPage.this, "List Renamed", Toast.LENGTH_SHORT).show(); + } + }); + } + class MyAdapter extends ArrayAdapter { Context context; ArrayList pNames; diff --git a/Listify/app/src/main/java/com/example/listify/RenameListDialogFragment.java b/Listify/app/src/main/java/com/example/listify/RenameListDialogFragment.java new file mode 100644 index 0000000..1b82b6d --- /dev/null +++ b/Listify/app/src/main/java/com/example/listify/RenameListDialogFragment.java @@ -0,0 +1,71 @@ +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.EditText; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; +import androidx.fragment.app.DialogFragment; + + +public class RenameListDialogFragment extends DialogFragment { + + public interface OnRenameListListener { + void sendRenameListName(String name); + } + + public OnRenameListListener onRenameListListener; + + EditText etRenameListName; + + public RenameListDialogFragment() {} + + + @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_rename_list, null); + builder.setView(root) + // Add action buttons + .setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + onRenameListListener.sendRenameListName(etRenameListName.getText().toString()); + } + }) + .setNegativeButton("cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + RenameListDialogFragment.this.getDialog().cancel(); + } + }); + + etRenameListName = (EditText) root.findViewById(R.id.et_renamed_list_name); + + return builder.create(); + } + + // Required to extend DialogFragment + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + try { + onRenameListListener = (OnRenameListListener) getTargetFragment(); + if (onRenameListListener == null) { + onRenameListListener = (OnRenameListListener) getActivity(); + } + } catch (ClassCastException e) { + Log.e("CreateListDialogFragment", "onAttach: ClassCastException: " + e.getMessage()); + } + } +} diff --git a/Listify/app/src/main/java/com/example/listify/adapter/ShoppingListsSwipeableAdapter.java b/Listify/app/src/main/java/com/example/listify/adapter/ShoppingListsSwipeableAdapter.java index fbdf493..dc2bd72 100644 --- a/Listify/app/src/main/java/com/example/listify/adapter/ShoppingListsSwipeableAdapter.java +++ b/Listify/app/src/main/java/com/example/listify/adapter/ShoppingListsSwipeableAdapter.java @@ -100,7 +100,11 @@ public class ShoppingListsSwipeableAdapter extends BaseAdapter { holder.listName.setText(curList.getName()); } - holder.itemCount.setText(String.format("%d items", curList.getEntries().length)); + if (curList.getEntries() != null) { + holder.itemCount.setText(String.format("%d items", curList.getEntries().length)); + } else { + holder.itemCount.setText("0 items"); + } holder.deleteList.setOnClickListener(new View.OnClickListener() { @Override @@ -161,9 +165,8 @@ public class ShoppingListsSwipeableAdapter extends BaseAdapter { public void onClick(View v) { Intent listPage = new Intent(activity, ListPage.class); - // Send the list ID and list name - listPage.putExtra("listID", curList.getListID()); - listPage.putExtra("listName", curList.getName()); + // Send the selected list + listPage.putExtra("selectedList", curList); activity.startActivity(listPage); } diff --git a/Listify/app/src/main/java/com/example/listify/data/List.java b/Listify/app/src/main/java/com/example/listify/data/List.java index 47f3971..79fc8d1 100644 --- a/Listify/app/src/main/java/com/example/listify/data/List.java +++ b/Listify/app/src/main/java/com/example/listify/data/List.java @@ -1,8 +1,9 @@ package com.example.listify.data; +import java.io.Serializable; import java.util.Arrays; -public class List { +public class List implements Serializable { Integer listID; String name; String owner; diff --git a/Listify/app/src/main/java/com/example/listify/data/ListEntry.java b/Listify/app/src/main/java/com/example/listify/data/ListEntry.java index e482079..66720e8 100644 --- a/Listify/app/src/main/java/com/example/listify/data/ListEntry.java +++ b/Listify/app/src/main/java/com/example/listify/data/ListEntry.java @@ -1,6 +1,8 @@ package com.example.listify.data; -public class ListEntry { +import java.io.Serializable; + +public class ListEntry implements Serializable { Integer listID; Integer productID; Integer quantity; diff --git a/Listify/app/src/main/res/layout/dialog_rename_list.xml b/Listify/app/src/main/res/layout/dialog_rename_list.xml new file mode 100644 index 0000000..55ca2e1 --- /dev/null +++ b/Listify/app/src/main/res/layout/dialog_rename_list.xml @@ -0,0 +1,22 @@ + + + + + + + \ No newline at end of file