mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2025-12-15 18:28:47 +00:00
Rename list functionality
This commit is contained in:
parent
0550ae4faf
commit
c0a93e3684
@ -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<String, Integer> 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<String> {
|
||||
Context context;
|
||||
ArrayList<String> pNames;
|
||||
|
||||
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
22
Listify/app/src/main/res/layout/dialog_rename_list.xml
Normal file
22
Listify/app/src/main/res/layout/dialog_rename_list.xml
Normal file
@ -0,0 +1,22 @@
|
||||
<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="Rename List"
|
||||
android:layout_gravity="center"
|
||||
android:textSize="20sp"
|
||||
android:layout_marginTop="5dp"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/et_renamed_list_name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="50dp"
|
||||
android:layout_marginHorizontal="15dp"
|
||||
android:singleLine="true"
|
||||
android:hint="@string/new_list_name"/>
|
||||
|
||||
</LinearLayout>
|
||||
Loading…
Reference in New Issue
Block a user