mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2025-12-16 10:48:46 +00:00
Add-to-list dialog and functionality on item details page
This commit is contained in:
parent
b269bc6e78
commit
5fe666fbbb
@ -1,8 +1,12 @@
|
||||
package com.example.listify;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.amazonaws.services.cognitoidentityprovider.model.TooManyFailedAttemptsException;
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.example.listify.adapter.DisplayShoppingListsAdapter;
|
||||
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;
|
||||
@ -11,11 +15,14 @@ import android.widget.ImageButton;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class ItemDetails extends AppCompatActivity {
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ItemDetails extends AppCompatActivity implements ListPickerDialogFragment.OnListPickListener {
|
||||
private Product curProduct;
|
||||
private LinearLayout lin1;
|
||||
private LinearLayout lin2;
|
||||
private LinearLayout linAddItem;
|
||||
private LinearLayout linCreateList;
|
||||
private TextView tvCreateNew;
|
||||
private TextView tvAddItem;
|
||||
private TextView tvItemName;
|
||||
@ -25,6 +32,8 @@ public class ItemDetails extends AppCompatActivity {
|
||||
private TextView tvItemDesc;
|
||||
private ImageButton backToSearchbutton;
|
||||
|
||||
ArrayList<ShoppingList> shoppingLists = new ArrayList<>();
|
||||
|
||||
private boolean isFABOpen = false;
|
||||
|
||||
@Override
|
||||
@ -34,13 +43,13 @@ public class ItemDetails extends AppCompatActivity {
|
||||
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||
setSupportActionBar(toolbar);
|
||||
|
||||
// Load Product object from search results activity
|
||||
curProduct = (Product) getIntent().getSerializableExtra("SelectedProduct");
|
||||
|
||||
// Set up floating action buttons
|
||||
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
|
||||
// fab1 = (FloatingActionButton) findViewById(R.id.fab1);
|
||||
// fab2 = (FloatingActionButton) findViewById(R.id.fab2);
|
||||
lin1 = (LinearLayout) findViewById(R.id.lin1);
|
||||
lin2 = (LinearLayout) findViewById(R.id.lin2);
|
||||
linAddItem = (LinearLayout) findViewById(R.id.lin_add_item);
|
||||
linCreateList = (LinearLayout) findViewById(R.id.lin_create_list);
|
||||
fab.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
@ -52,6 +61,30 @@ public class ItemDetails extends AppCompatActivity {
|
||||
}
|
||||
});
|
||||
|
||||
linAddItem.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
closeFABMenu();
|
||||
|
||||
// Hardcode shopping lists to demonstrate displaying lists
|
||||
for (int i = 0; i < 10; i++) {
|
||||
shoppingLists.add(new ShoppingList(Integer.toString(i)));
|
||||
}
|
||||
|
||||
ListPickerDialogFragment listPickerDialog = new ListPickerDialogFragment(shoppingLists);
|
||||
listPickerDialog.show(getSupportFragmentManager(), "User Lists");
|
||||
}
|
||||
});
|
||||
|
||||
linCreateList.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Toast.makeText(ItemDetails.this, "create", Toast.LENGTH_SHORT).show();
|
||||
closeFABMenu();
|
||||
}
|
||||
});
|
||||
|
||||
// Set data
|
||||
tvItemName = (TextView) findViewById(R.id.item_name);
|
||||
tvItemName.setText(curProduct.getItemName());
|
||||
|
||||
@ -84,19 +117,23 @@ public class ItemDetails extends AppCompatActivity {
|
||||
|
||||
private void showFABMenu(){
|
||||
isFABOpen=true;
|
||||
lin1.animate().translationY(-getResources().getDimension(R.dimen.standard_55));
|
||||
lin2.animate().translationY(-getResources().getDimension(R.dimen.standard_105));
|
||||
linAddItem.animate().translationY(-getResources().getDimension(R.dimen.standard_55));
|
||||
linCreateList.animate().translationY(-getResources().getDimension(R.dimen.standard_105));
|
||||
tvAddItem.setVisibility(View.VISIBLE);
|
||||
tvCreateNew.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
private void closeFABMenu(){
|
||||
isFABOpen=false;
|
||||
lin1.animate().translationY(0);
|
||||
lin2.animate().translationY(0);
|
||||
linAddItem.animate().translationY(0);
|
||||
linCreateList.animate().translationY(0);
|
||||
tvAddItem.setVisibility(View.INVISIBLE);
|
||||
tvCreateNew.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
|
||||
|
||||
@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_SHORT).show();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,121 @@
|
||||
package com.example.listify;
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ListView;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.DialogFragment;
|
||||
import com.example.listify.adapter.DisplayShoppingListsAdapter;
|
||||
import com.example.listify.model.ShoppingList;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
public class ListPickerDialogFragment extends DialogFragment {
|
||||
|
||||
public interface OnListPickListener {
|
||||
void sendListSelection(int selectedListIndex, int quantity);
|
||||
}
|
||||
|
||||
public OnListPickListener onListPickListener;
|
||||
|
||||
ListView userListsView;
|
||||
DisplayShoppingListsAdapter displayShoppingListsAdapter;
|
||||
Button btnMinus;
|
||||
Button btnPlus;
|
||||
EditText etQuantity;
|
||||
private ArrayList<ShoppingList> userLists;
|
||||
private int selectedListIndex;
|
||||
|
||||
public ListPickerDialogFragment(ArrayList<ShoppingList> userLists) {
|
||||
this.userLists = userLists;
|
||||
}
|
||||
|
||||
|
||||
@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_add_to_list, null);
|
||||
builder.setView(root)
|
||||
// Add action buttons
|
||||
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
onListPickListener.sendListSelection(selectedListIndex, Integer.parseInt(etQuantity.getText().toString()));
|
||||
}
|
||||
})
|
||||
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
ListPickerDialogFragment.this.getDialog().cancel();
|
||||
}
|
||||
});
|
||||
|
||||
// Display user's shopping lists
|
||||
userListsView = (ListView) root.findViewById(R.id.user_lists);
|
||||
displayShoppingListsAdapter = new DisplayShoppingListsAdapter(getActivity(), userLists);
|
||||
userListsView.setAdapter(displayShoppingListsAdapter);
|
||||
|
||||
userListsView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
for (int i = 0; i < parent.getChildCount(); i++) {
|
||||
parent.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
|
||||
}
|
||||
|
||||
view.setBackgroundColor(Color.GREEN);
|
||||
selectedListIndex = position;
|
||||
}
|
||||
});
|
||||
|
||||
// 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 {
|
||||
onListPickListener = (OnListPickListener) getActivity();
|
||||
} catch (ClassCastException e) {
|
||||
Log.e("ListPickerDialogFragment", "onAttach: ClassCastException: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,55 @@
|
||||
package com.example.listify.adapter;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.TextView;
|
||||
import com.example.listify.R;
|
||||
import com.example.listify.model.ShoppingList;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class DisplayShoppingListsAdapter extends BaseAdapter {
|
||||
private Activity activity;
|
||||
private ArrayList<ShoppingList> lists;
|
||||
private LayoutInflater inflater;
|
||||
|
||||
public DisplayShoppingListsAdapter(Activity activity, ArrayList<ShoppingList> lists){
|
||||
this.activity = activity;
|
||||
this.lists = lists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return lists.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getItem(int position) {
|
||||
return lists.get(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
if (inflater == null) {
|
||||
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
}
|
||||
if (convertView == null) {
|
||||
convertView = inflater.inflate(R.layout.display_shopping_lists_item, null);
|
||||
}
|
||||
|
||||
ShoppingList curList = lists.get(position);
|
||||
|
||||
TextView tvListName = (TextView) convertView.findViewById(R.id.shopping_list_name);
|
||||
tvListName.setText(curList.getName());
|
||||
|
||||
return convertView;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,74 @@
|
||||
package com.example.listify.model;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
public class ShoppingList extends ArrayList<Product> {
|
||||
private ArrayList<Product> list;
|
||||
private String name;
|
||||
|
||||
public ShoppingList(String name) {
|
||||
list = new ArrayList<Product>();
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Product get(int position) {
|
||||
return this.list.get(position);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return list.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return list.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(@Nullable Object o) {
|
||||
return list.contains(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(@Nullable Object o) {
|
||||
return list.indexOf(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(Product product) {
|
||||
return list.add(product);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Product remove(int index) {
|
||||
return list.remove(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
list.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(@Nullable Object o) {
|
||||
return list.remove(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(@NonNull Collection<? extends Product> c) {
|
||||
return list.addAll(c);
|
||||
}
|
||||
}
|
||||
@ -37,7 +37,7 @@
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/lin2"
|
||||
android:id="@+id/lin_add_item"
|
||||
android:layout_gravity="bottom|end"
|
||||
android:layout_marginBottom="22dp">
|
||||
|
||||
@ -61,7 +61,7 @@
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/lin1"
|
||||
android:id="@+id/lin_create_list"
|
||||
android:layout_gravity="bottom|end"
|
||||
android:layout_marginBottom="22dp">
|
||||
|
||||
|
||||
41
Listify/app/src/main/res/layout/dialog_add_to_list.xml
Normal file
41
Listify/app/src/main/res/layout/dialog_add_to_list.xml
Normal file
@ -0,0 +1,41 @@
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<ListView
|
||||
android:id="@+id/user_lists"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="300dp"
|
||||
android:divider="@color/list_divider"
|
||||
android:dividerHeight="1dp"
|
||||
android:choiceMode="multipleChoiceModal"/>
|
||||
|
||||
<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="-"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/et_quantity"
|
||||
android:layout_width="60dp"
|
||||
android:layout_height="50dp"
|
||||
android:text="1"
|
||||
android:inputType="number"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/btn_plus"
|
||||
android:layout_width="60dp"
|
||||
android:layout_height="50dp"
|
||||
android:text="+"/>
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<TextView
|
||||
android:id="@+id/shopping_list_name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:textSize="20sp"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
</RelativeLayout>
|
||||
Loading…
Reference in New Issue
Block a user