12 changed files with 304 additions and 3 deletions
			
			
		| @ -0,0 +1,134 @@ | |||
| package gr.auth.databases.flavours.activities; | |||
| 
 | |||
| import android.annotation.SuppressLint; | |||
| import android.os.Bundle; | |||
| import android.util.Log; | |||
| import android.view.KeyEvent; | |||
| import android.view.MenuItem; | |||
| import android.view.View; | |||
| import android.view.inputmethod.EditorInfo; | |||
| import android.widget.EditText; | |||
| import android.widget.LinearLayout; | |||
| import android.widget.TextView; | |||
| 
 | |||
| import java.util.ArrayList; | |||
| 
 | |||
| import androidx.appcompat.app.ActionBar; | |||
| import androidx.appcompat.widget.AppCompatButton; | |||
| import androidx.appcompat.widget.AppCompatImageButton; | |||
| import androidx.appcompat.widget.Toolbar; | |||
| import gr.auth.databases.flavours.R; | |||
| import gr.auth.databases.flavours.base.BaseActivity; | |||
| import gr.auth.databases.flavours.model.IngredientSummary; | |||
| import gr.auth.databases.flavours.model.ItemSummary; | |||
| 
 | |||
| public class AddItemActivity extends BaseActivity { | |||
|     public static final String ITEM_TYPE = "ITEM_TYPE"; | |||
| 
 | |||
|     private ArrayList<IngredientSummary> ingredients = new ArrayList<>(); | |||
|     private ItemSummary.ItemType itemType; | |||
|     private EditText itemNameInput, itemDescriptionInput; | |||
| 
 | |||
|     @SuppressLint("ClickableViewAccessibility") | |||
|     @Override | |||
|     protected void onCreate(Bundle savedInstanceState) { | |||
|         super.onCreate(savedInstanceState); | |||
|         setContentView(R.layout.activity_add_item); | |||
| 
 | |||
|         Bundle extras = getIntent().getExtras(); | |||
|         if (extras != null) { | |||
|             itemType = (ItemSummary.ItemType) extras.getSerializable(ITEM_TYPE); | |||
|         } | |||
| 
 | |||
|         Toolbar toolbar = findViewById(R.id.add_item_toolbar); | |||
|         if (itemType == ItemSummary.ItemType.FOOD) { | |||
|             toolbar.setTitle(getString(R.string.add_item_toolbar_title_food)); | |||
|         } else if (itemType == ItemSummary.ItemType.DRINK) { | |||
|             toolbar.setTitle(getString(R.string.add_item_toolbar_title_drink)); | |||
|         } | |||
| 
 | |||
|         setSupportActionBar(toolbar); | |||
|         ActionBar actionbar = getSupportActionBar(); | |||
|         if (actionbar != null) { | |||
|             actionbar.setDisplayHomeAsUpEnabled(true); | |||
|             actionbar.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp); | |||
|         } | |||
| 
 | |||
|         itemNameInput = findViewById(R.id.add_item_name); | |||
|         AppCompatButton addItemButton = findViewById(R.id.add_item_add_btn); | |||
|         final TextView ingredientsTitle = findViewById(R.id.add_item_add_ingredient_title); | |||
|         final EditText addIngredientInput = findViewById(R.id.add_item_add_ingredient); | |||
|         final LinearLayout ingredientsList = findViewById(R.id.add_item_ingredients_list); | |||
| 
 | |||
|         if (itemType == ItemSummary.ItemType.FOOD) { | |||
|             itemNameInput.setHint(R.string.add_food_hint_name); | |||
|             addItemButton.setText(R.string.add_food_add_btn); | |||
|         } else if (itemType == ItemSummary.ItemType.DRINK) { | |||
|             itemNameInput.setHint(R.string.add_drink_hint_name); | |||
|             addItemButton.setText(R.string.add_drink_add_btn); | |||
|         } | |||
| 
 | |||
|         addIngredientInput.setOnEditorActionListener(new TextView.OnEditorActionListener() { | |||
|             @Override | |||
|             public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) { | |||
|                 if (actionId == EditorInfo.IME_ACTION_DONE) { | |||
|                     ingredients.add(new IngredientSummary( | |||
|                             addIngredientInput.getText().toString(), false)); | |||
| 
 | |||
|                     View ingredientRow = getLayoutInflater().inflate(R.layout.add_item_ingredient_row, ingredientsList, false); | |||
|                     TextView ingredientName = ingredientRow.findViewById(R.id.add_item_ingredient_name); | |||
|                     ingredientName.setText(ingredients.get(ingredients.size() - 1).getName()); | |||
| 
 | |||
|                     AppCompatImageButton removeIngredientBtn = ingredientRow.findViewById(R.id.add_item_remove_ingredient); | |||
|                     removeIngredientBtn.setOnClickListener(new View.OnClickListener() { | |||
|                         @Override | |||
|                         public void onClick(View view) { | |||
|                             int index = ingredientsList.indexOfChild((View) view.getParent()); | |||
|                             ingredients.remove(index); | |||
|                             ingredientsList.removeViewAt(index); | |||
|                         } | |||
|                     }); | |||
| 
 | |||
|                     ingredientsList.addView(ingredientRow); | |||
|                     ingredientsTitle.setVisibility(View.VISIBLE); | |||
|                     ingredientsList.setVisibility(View.VISIBLE); | |||
| 
 | |||
|                     addIngredientInput.setText(""); | |||
|                     return true; | |||
|                 } | |||
|                 return false; | |||
|             } | |||
|         }); | |||
| 
 | |||
|         addItemButton.setOnClickListener(new View.OnClickListener() { | |||
|             @Override | |||
|             public void onClick(View view) { | |||
|                 //TODO
 | |||
|             } | |||
|         }); | |||
| 
 | |||
|         createDrawer(); | |||
|         drawer.setSelection(-1); | |||
|     } | |||
| 
 | |||
|     @Override | |||
|     public void onBackPressed() { | |||
|         if (drawer.isDrawerOpen()) { | |||
|             drawer.closeDrawer(); | |||
|         } else { | |||
|             super.onBackPressed(); | |||
|         } | |||
|     } | |||
| 
 | |||
|     @Override | |||
|     public boolean onOptionsItemSelected(MenuItem item) { | |||
|         int id = item.getItemId(); | |||
| 
 | |||
|         if (id == android.R.id.home) { | |||
|             drawer.openDrawer(); | |||
|             return true; | |||
|         } | |||
| 
 | |||
|         return super.onOptionsItemSelected(item); | |||
|     } | |||
| } | |||
| After Width: | Height: | Size: 177 B | 
| After Width: | Height: | Size: 139 B | 
| After Width: | Height: | Size: 182 B | 
| After Width: | Height: | Size: 245 B | 
| After Width: | Height: | Size: 239 B | 
| @ -0,0 +1,113 @@ | |||
| <?xml version="1.0" encoding="utf-8"?> | |||
| <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" | |||
|     xmlns:app="http://schemas.android.com/apk/res-auto" | |||
|     xmlns:tools="http://schemas.android.com/tools" | |||
|     android:layout_width="match_parent" | |||
|     android:layout_height="match_parent" | |||
|     android:background="@android:color/background_light" | |||
|     android:fitsSystemWindows="true"> | |||
| 
 | |||
|     <com.google.android.material.appbar.AppBarLayout | |||
|         android:id="@+id/add_item_appbar" | |||
|         android:layout_width="match_parent" | |||
|         android:layout_height="wrap_content" | |||
|         android:theme="@style/Theme.AppCompat.Light"> | |||
| 
 | |||
|         <androidx.appcompat.widget.Toolbar | |||
|             android:id="@+id/add_item_toolbar" | |||
|             android:layout_width="match_parent" | |||
|             android:layout_height="?attr/actionBarSize" | |||
|             android:background="?attr/colorPrimary" | |||
|             app:popupTheme="@style/Theme.AppCompat.Light" /> | |||
|     </com.google.android.material.appbar.AppBarLayout> | |||
| 
 | |||
|     <androidx.core.widget.NestedScrollView | |||
|         android:layout_width="match_parent" | |||
|         android:layout_height="match_parent" | |||
|         app:layout_behavior="@string/appbar_scrolling_view_behavior"> | |||
| 
 | |||
|         <LinearLayout | |||
|             android:layout_width="match_parent" | |||
|             android:layout_height="wrap_content" | |||
|             android:layout_marginStart="4dp" | |||
|             android:layout_marginEnd="4dp" | |||
|             android:focusableInTouchMode="true" | |||
|             android:orientation="vertical"> | |||
| 
 | |||
|             <com.google.android.material.textfield.TextInputLayout | |||
|                 android:layout_width="match_parent" | |||
|                 android:layout_height="wrap_content" | |||
|                 android:layout_marginBottom="5dp" | |||
|                 app:passwordToggleEnabled="true"> | |||
| 
 | |||
|                 <EditText | |||
|                     android:id="@+id/add_item_name" | |||
|                     android:layout_width="match_parent" | |||
|                     android:layout_height="wrap_content" | |||
|                     android:hint="@string/add_item_hint_name" | |||
|                     android:importantForAutofill="no" | |||
|                     android:inputType="text" | |||
|                     tools:targetApi="o" /> | |||
|             </com.google.android.material.textfield.TextInputLayout> | |||
| 
 | |||
|             <com.google.android.material.textfield.TextInputLayout | |||
|                 android:layout_width="match_parent" | |||
|                 android:layout_height="wrap_content" | |||
|                 android:layout_marginBottom="5dp" | |||
|                 app:passwordToggleEnabled="true"> | |||
| 
 | |||
|                 <EditText | |||
|                     android:id="@+id/add_item_description" | |||
|                     android:layout_width="match_parent" | |||
|                     android:layout_height="wrap_content" | |||
|                     android:hint="@string/add_item_hint_description" | |||
|                     android:importantForAutofill="no" | |||
|                     android:inputType="text" | |||
|                     android:maxLines="1" | |||
|                     tools:targetApi="o" /> | |||
|             </com.google.android.material.textfield.TextInputLayout> | |||
| 
 | |||
|             <TextView | |||
|                 android:id="@+id/add_item_add_ingredient_title" | |||
|                 android:layout_width="match_parent" | |||
|                 android:layout_height="wrap_content" | |||
|                 android:text="@string/add_item_ingredients_title" | |||
|                 android:textSize="16sp" | |||
|                 android:textStyle="bold" | |||
|                 android:visibility="gone" /> | |||
| 
 | |||
|             <LinearLayout | |||
|                 android:id="@+id/add_item_ingredients_list" | |||
|                 android:layout_width="match_parent" | |||
|                 android:layout_height="wrap_content" | |||
|                 android:layout_marginStart="4dp" | |||
|                 android:layout_marginEnd="4dp" | |||
|                 android:orientation="vertical" | |||
|                 android:visibility="gone" /> | |||
| 
 | |||
|             <com.google.android.material.textfield.TextInputLayout | |||
|                 android:layout_width="match_parent" | |||
|                 android:layout_height="wrap_content" | |||
|                 android:layout_marginBottom="5dp"> | |||
| 
 | |||
|                 <EditText | |||
|                     android:id="@+id/add_item_add_ingredient" | |||
|                     android:layout_width="match_parent" | |||
|                     android:layout_height="wrap_content" | |||
|                     android:hint="@string/add_item_add_ingredient" | |||
|                     android:imeOptions="actionDone" | |||
|                     android:importantForAutofill="no" | |||
|                     android:inputType="text" | |||
|                     android:maxLines="1" | |||
|                     tools:targetApi="o" /> | |||
|             </com.google.android.material.textfield.TextInputLayout> | |||
| 
 | |||
|             <androidx.appcompat.widget.AppCompatButton | |||
|                 android:id="@+id/add_item_add_btn" | |||
|                 android:layout_width="wrap_content" | |||
|                 android:layout_height="wrap_content" | |||
|                 android:layout_gravity="end" | |||
|                 android:text="@string/add_restaurant_add_btn" /> | |||
|         </LinearLayout> | |||
|     </androidx.core.widget.NestedScrollView> | |||
| </androidx.coordinatorlayout.widget.CoordinatorLayout> | |||
| @ -0,0 +1,20 @@ | |||
| <?xml version="1.0" encoding="utf-8"?> | |||
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |||
|     android:layout_width="match_parent" | |||
|     android:layout_height="wrap_content" | |||
|     android:orientation="horizontal"> | |||
| 
 | |||
|     <TextView | |||
|         android:id="@+id/add_item_ingredient_name" | |||
|         android:layout_width="0dp" | |||
|         android:layout_height="wrap_content" | |||
|         android:layout_gravity="center_vertical" | |||
|         android:layout_weight="1" | |||
|         android:ellipsize="end" /> | |||
| 
 | |||
|     <androidx.appcompat.widget.AppCompatImageButton | |||
|         android:id="@+id/add_item_remove_ingredient" | |||
|         android:layout_width="wrap_content" | |||
|         android:layout_height="wrap_content" | |||
|         android:src="@drawable/ic_delete_black_18dp" /> | |||
| </LinearLayout> | |||
| @ -0,0 +1,10 @@ | |||
| <?xml version="1.0" encoding="utf-8"?> | |||
| <resources> | |||
|     <string-array name="item_grade"> | |||
|         <item>1</item> | |||
|         <item>2</item> | |||
|         <item>3</item> | |||
|         <item>4</item> | |||
|         <item>5</item> | |||
|     </string-array> | |||
| </resources> | |||
					Loading…
					
					
				
		Reference in new issue