Apostolos Fanakis
6 years ago
24 changed files with 613 additions and 35 deletions
@ -0,0 +1,92 @@ |
|||
package gr.auth.databases.flavours.activities; |
|||
|
|||
import android.app.Activity; |
|||
import android.content.Intent; |
|||
import android.os.Bundle; |
|||
import android.view.MenuItem; |
|||
import android.view.View; |
|||
import android.widget.EditText; |
|||
import android.widget.RadioGroup; |
|||
import android.widget.Toast; |
|||
|
|||
import androidx.appcompat.app.ActionBar; |
|||
import androidx.appcompat.widget.AppCompatButton; |
|||
import androidx.appcompat.widget.Toolbar; |
|||
import gr.auth.databases.flavours.R; |
|||
import gr.auth.databases.flavours.base.BaseActivity; |
|||
import gr.auth.databases.flavours.model.Ingredient; |
|||
|
|||
public class AddIngredientActivity extends BaseActivity { |
|||
public static final String INGREDIENT_ADD_RESULT = "INGREDIENT_ADD_RESULT"; |
|||
|
|||
private EditText ingredientNameInput; |
|||
private RadioGroup ingredientHasAlcoholInput; |
|||
|
|||
@Override |
|||
protected void onCreate(Bundle savedInstanceState) { |
|||
super.onCreate(savedInstanceState); |
|||
setContentView(R.layout.activity_add_ingredient); |
|||
|
|||
Toolbar toolbar = findViewById(R.id.add_ingredient_toolbar); |
|||
setSupportActionBar(toolbar); |
|||
ActionBar actionbar = getSupportActionBar(); |
|||
if (actionbar != null) { |
|||
actionbar.setDisplayHomeAsUpEnabled(true); |
|||
actionbar.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp); |
|||
} |
|||
|
|||
ingredientNameInput = findViewById(R.id.add_ingredient_name); |
|||
ingredientHasAlcoholInput = findViewById(R.id.add_ingredient_contains_alcohol); |
|||
|
|||
AppCompatButton addIngredientButton = findViewById(R.id.add_ingredient_add_btn); |
|||
addIngredientButton.setOnClickListener(new View.OnClickListener() { |
|||
@Override |
|||
public void onClick(View view) { |
|||
int hasAlcoholResult = -1; |
|||
switch (ingredientHasAlcoholInput.getCheckedRadioButtonId()) { |
|||
case R.id.add_ingredient_has_alcohol_yes: |
|||
hasAlcoholResult = 1; |
|||
break; |
|||
case R.id.add_ingredient_has_alcohol_no: |
|||
hasAlcoholResult = 0; |
|||
break; |
|||
} |
|||
|
|||
if (hasAlcoholResult == -1) { |
|||
Toast.makeText(view.getContext(), "Does this ingredient have alcogol?", Toast.LENGTH_SHORT).show(); |
|||
return; |
|||
} |
|||
|
|||
Intent returnIntent = new Intent(); |
|||
returnIntent.putExtra(INGREDIENT_ADD_RESULT, new Ingredient(ingredientNameInput.getText().toString(), |
|||
hasAlcoholResult == 1)); |
|||
setResult(Activity.RESULT_OK, returnIntent); |
|||
finish(); |
|||
} |
|||
}); |
|||
|
|||
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); |
|||
} |
|||
} |
@ -0,0 +1,128 @@ |
|||
package gr.auth.databases.flavours.activities.ingredients; |
|||
|
|||
import android.app.Activity; |
|||
import android.content.Intent; |
|||
import android.os.Bundle; |
|||
import android.view.MenuItem; |
|||
import android.view.View; |
|||
import android.widget.Toast; |
|||
|
|||
import com.google.android.material.floatingactionbutton.FloatingActionButton; |
|||
|
|||
import java.util.ArrayList; |
|||
|
|||
import androidx.appcompat.app.ActionBar; |
|||
import androidx.appcompat.widget.Toolbar; |
|||
import androidx.recyclerview.widget.LinearLayoutManager; |
|||
import androidx.recyclerview.widget.RecyclerView; |
|||
import gr.auth.databases.flavours.R; |
|||
import gr.auth.databases.flavours.activities.AddIngredientActivity; |
|||
import gr.auth.databases.flavours.base.BaseActivity; |
|||
import gr.auth.databases.flavours.model.Ingredient; |
|||
|
|||
import static gr.auth.databases.flavours.activities.AddIngredientActivity.INGREDIENT_ADD_RESULT; |
|||
|
|||
public class IngredientsActivity extends BaseActivity implements IngredientsAdapter.IngredientsAdapterInteractionListener { |
|||
public static final String INGREDIENT_PICK_RESULT = "INGREDIENT_PICK_RESULT"; |
|||
private static final int ADD_NEW_INGREDIENT_REQUEST = 2000; |
|||
|
|||
private boolean isCalledForResult; |
|||
private FloatingActionButton FAB; |
|||
private ArrayList<Ingredient> ingredients = new ArrayList<>(); |
|||
private IngredientsAdapter ingredientsAdapter; |
|||
|
|||
@Override |
|||
protected void onCreate(Bundle savedInstanceState) { |
|||
super.onCreate(savedInstanceState); |
|||
setContentView(R.layout.activity_ingredients); |
|||
|
|||
isCalledForResult = getCallingActivity() != null; |
|||
|
|||
Toolbar toolbar = findViewById(R.id.ingredients_toolbar); |
|||
setSupportActionBar(toolbar); |
|||
ActionBar actionbar = getSupportActionBar(); |
|||
if (actionbar != null) { |
|||
actionbar.setDisplayHomeAsUpEnabled(true); |
|||
actionbar.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp); |
|||
} |
|||
|
|||
FAB = findViewById(R.id.ingredients_fab); |
|||
createDrawer(); |
|||
FAB.setOnClickListener(new View.OnClickListener() { |
|||
@Override |
|||
public void onClick(View view) { |
|||
Intent intent = new Intent(view.getContext(), AddIngredientActivity.class); |
|||
startActivityForResult(intent, ADD_NEW_INGREDIENT_REQUEST); |
|||
} |
|||
}); |
|||
|
|||
RecyclerView recyclerView = findViewById(R.id.ingredients_list); |
|||
recyclerView.setHasFixedSize(true); |
|||
LinearLayoutManager layoutManager = new LinearLayoutManager(this); |
|||
recyclerView.setLayoutManager(layoutManager); |
|||
|
|||
ingredients.add(new Ingredient("Μόνο κρέας", false)); |
|||
ingredients.add(new Ingredient("Σχεδόν μόνο κρέας", true)); |
|||
|
|||
if (isCalledForResult) { |
|||
ingredientsAdapter = new IngredientsAdapter(this, ingredients, this); |
|||
} else { |
|||
ingredientsAdapter = new IngredientsAdapter(this, ingredients, null); |
|||
} |
|||
|
|||
recyclerView.setAdapter(ingredientsAdapter); |
|||
} |
|||
|
|||
@Override |
|||
public void onBackPressed() { |
|||
if (drawer.isDrawerOpen()) { |
|||
drawer.closeDrawer(); |
|||
} else { |
|||
super.onBackPressed(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public boolean onOptionsItemSelected(MenuItem item) { |
|||
int id = item.getItemId(); |
|||
|
|||
if (!isCalledForResult && id == android.R.id.home) { |
|||
drawer.openDrawer(); |
|||
return true; |
|||
} |
|||
|
|||
return super.onOptionsItemSelected(item); |
|||
} |
|||
|
|||
@Override |
|||
public void onDestroy() { |
|||
if (isCalledForResult) { |
|||
Intent returnIntent = new Intent(); |
|||
setResult(Activity.RESULT_CANCELED, returnIntent); |
|||
finish(); |
|||
} |
|||
|
|||
super.onDestroy(); |
|||
} |
|||
|
|||
@Override |
|||
public void onIngredientsAdapterInteraction(Ingredient ingredient) { |
|||
Intent returnIntent = new Intent(); |
|||
returnIntent.putExtra(INGREDIENT_PICK_RESULT, ingredient); |
|||
setResult(Activity.RESULT_OK, returnIntent); |
|||
finish(); |
|||
} |
|||
|
|||
@Override |
|||
protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
|||
if (requestCode == ADD_NEW_INGREDIENT_REQUEST) { |
|||
if (resultCode == Activity.RESULT_OK) { |
|||
Ingredient result = data.getParcelableExtra(INGREDIENT_ADD_RESULT); |
|||
ingredients.add(0, result); |
|||
ingredientsAdapter.notifyDataSetChanged(); |
|||
Toast.makeText(this, "New ingredient added.", Toast.LENGTH_LONG).show(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
@ -0,0 +1,74 @@ |
|||
package gr.auth.databases.flavours.activities.ingredients; |
|||
|
|||
import android.content.Context; |
|||
import android.view.LayoutInflater; |
|||
import android.view.View; |
|||
import android.view.ViewGroup; |
|||
import android.widget.TextView; |
|||
|
|||
import java.util.ArrayList; |
|||
|
|||
import androidx.annotation.NonNull; |
|||
import androidx.cardview.widget.CardView; |
|||
import androidx.recyclerview.widget.RecyclerView; |
|||
import gr.auth.databases.flavours.R; |
|||
import gr.auth.databases.flavours.model.Ingredient; |
|||
|
|||
public class IngredientsAdapter extends RecyclerView.Adapter<IngredientsAdapter.IngredientViewHolder> { |
|||
private Context context; |
|||
private ArrayList<Ingredient> ingredients; |
|||
private IngredientsAdapterInteractionListener ingredientsAdapterInteractionListener; |
|||
|
|||
IngredientsAdapter(Context context, ArrayList<Ingredient> ingredients, |
|||
IngredientsAdapterInteractionListener ingredientsAdapterInteractionListener) { |
|||
this.context = context; |
|||
this.ingredients = ingredients; |
|||
this.ingredientsAdapterInteractionListener = ingredientsAdapterInteractionListener; |
|||
} |
|||
|
|||
@NonNull |
|||
@Override |
|||
public IngredientsAdapter.IngredientViewHolder onCreateViewHolder(@NonNull ViewGroup parent, |
|||
int viewType) { |
|||
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.ingredients_row, parent, false); |
|||
return new IngredientViewHolder(v); |
|||
} |
|||
|
|||
@Override |
|||
public void onBindViewHolder(@NonNull final IngredientViewHolder holder, int position) { |
|||
holder.name.setText(ingredients.get(position).getName()); |
|||
holder.containsAlcohol.setText(context.getString(R.string.ingredients_contains_alcohol_placeholder, |
|||
ingredients.get(position).hasAlcohol() ? "YES" : "NO")); |
|||
|
|||
if (ingredientsAdapterInteractionListener != null) { |
|||
holder.card.setOnClickListener(new View.OnClickListener() { |
|||
@Override |
|||
public void onClick(View view) { |
|||
ingredientsAdapterInteractionListener.onIngredientsAdapterInteraction( |
|||
ingredients.get(holder.getAdapterPosition())); |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public int getItemCount() { |
|||
return ingredients.size(); |
|||
} |
|||
|
|||
static class IngredientViewHolder extends RecyclerView.ViewHolder { |
|||
CardView card; |
|||
TextView name, containsAlcohol; |
|||
|
|||
IngredientViewHolder(View v) { |
|||
super(v); |
|||
card = v.findViewById(R.id.ingredients_row_card); |
|||
name = v.findViewById(R.id.ingredients_ingredient_name); |
|||
containsAlcohol = v.findViewById(R.id.ingredients_ingredient_contains_alcohol); |
|||
} |
|||
} |
|||
|
|||
public interface IngredientsAdapterInteractionListener { |
|||
void onIngredientsAdapterInteraction(Ingredient ingredient); |
|||
} |
|||
} |
@ -0,0 +1,48 @@ |
|||
package gr.auth.databases.flavours.model; |
|||
|
|||
import android.os.Parcel; |
|||
import android.os.Parcelable; |
|||
|
|||
public class Ingredient implements Parcelable { |
|||
private String name; |
|||
private boolean hasAlcohol; |
|||
|
|||
public Ingredient(String name, boolean hasAlcohol) { |
|||
this.name = name; |
|||
this.hasAlcohol = hasAlcohol; |
|||
} |
|||
|
|||
public String getName() { |
|||
return name; |
|||
} |
|||
|
|||
public boolean hasAlcohol() { |
|||
return hasAlcohol; |
|||
} |
|||
|
|||
@Override |
|||
public int describeContents() { |
|||
return 0; |
|||
} |
|||
|
|||
@Override |
|||
public void writeToParcel(Parcel out, int flags) { |
|||
out.writeString(name); |
|||
out.writeByte((byte) (hasAlcohol ? 1 : 0)); |
|||
} |
|||
|
|||
public static final Parcelable.Creator<Ingredient> CREATOR = new Parcelable.Creator<Ingredient>() { |
|||
public Ingredient createFromParcel(Parcel in) { |
|||
return new Ingredient(in); |
|||
} |
|||
|
|||
public Ingredient[] newArray(int size) { |
|||
return new Ingredient[size]; |
|||
} |
|||
}; |
|||
|
|||
private Ingredient(Parcel in) { |
|||
name = in.readString(); |
|||
hasAlcohol = in.readByte() != 0; |
|||
} |
|||
} |
After Width: | Height: | Size: 303 B |
After Width: | Height: | Size: 210 B |
After Width: | Height: | Size: 393 B |
After Width: | Height: | Size: 570 B |
After Width: | Height: | Size: 752 B |
@ -0,0 +1,89 @@ |
|||
<?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_ingredient_appbar" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:theme="@style/ToolbarTheme"> |
|||
|
|||
<androidx.appcompat.widget.Toolbar |
|||
android:id="@+id/add_ingredient_toolbar" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="?attr/actionBarSize" |
|||
android:background="?attr/colorPrimary" |
|||
app:popupTheme="@style/ToolbarTheme" |
|||
app:title="@string/add_ingredient_toolbar_title" /> |
|||
</com.google.android.material.appbar.AppBarLayout> |
|||
|
|||
<androidx.core.widget.NestedScrollView |
|||
android:layout_width="match_parent" |
|||
android:layout_height="match_parent" |
|||
android:focusableInTouchMode="true" |
|||
android:paddingTop="4dp" |
|||
android:paddingBottom="4dp" |
|||
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: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_ingredient_name" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:hint="@string/add_ingredient_hint_name" |
|||
android:importantForAutofill="no" |
|||
android:inputType="text" |
|||
tools:targetApi="o" /> |
|||
</com.google.android.material.textfield.TextInputLayout> |
|||
|
|||
<TextView |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:text="@string/add_ingredient_has_alcohol_title" |
|||
android:textStyle="bold" /> |
|||
|
|||
<RadioGroup |
|||
android:id="@+id/add_ingredient_contains_alcohol" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:orientation="horizontal"> |
|||
|
|||
<androidx.appcompat.widget.AppCompatRadioButton |
|||
android:id="@+id/add_ingredient_has_alcohol_yes" |
|||
android:layout_width="wrap_content" |
|||
android:layout_height="wrap_content" |
|||
android:text="@string/add_ingredient_has_alcohol_yes" /> |
|||
|
|||
<androidx.appcompat.widget.AppCompatRadioButton |
|||
android:id="@+id/add_ingredient_has_alcohol_no" |
|||
android:layout_width="wrap_content" |
|||
android:layout_height="wrap_content" |
|||
android:text="@string/add_ingredient_has_alcohol_no" /> |
|||
</RadioGroup> |
|||
|
|||
<androidx.appcompat.widget.AppCompatButton |
|||
android:id="@+id/add_ingredient_add_btn" |
|||
android:layout_width="wrap_content" |
|||
android:layout_height="wrap_content" |
|||
android:layout_gravity="end" |
|||
android:text="@string/add_ingredient_add_btn" /> |
|||
</LinearLayout> |
|||
</androidx.core.widget.NestedScrollView> |
|||
</androidx.coordinatorlayout.widget.CoordinatorLayout> |
@ -0,0 +1,41 @@ |
|||
<?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" |
|||
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/ingredients_appbar" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:theme="@style/ToolbarTheme"> |
|||
|
|||
<androidx.appcompat.widget.Toolbar |
|||
android:id="@+id/ingredients_toolbar" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="?attr/actionBarSize" |
|||
android:background="?attr/colorPrimary" |
|||
app:popupTheme="@style/ToolbarTheme" |
|||
app:title="@string/ingredients_toolbar_title" /> |
|||
</com.google.android.material.appbar.AppBarLayout> |
|||
|
|||
<androidx.recyclerview.widget.RecyclerView |
|||
android:id="@+id/ingredients_list" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="match_parent" |
|||
android:paddingTop="4dp" |
|||
android:paddingBottom="4dp" |
|||
app:layout_behavior="@string/appbar_scrolling_view_behavior" /> |
|||
|
|||
<com.google.android.material.floatingactionbutton.FloatingActionButton |
|||
android:id="@+id/ingredients_fab" |
|||
android:layout_width="wrap_content" |
|||
android:layout_height="wrap_content" |
|||
android:layout_gravity="bottom|end" |
|||
android:layout_margin="16dp" |
|||
android:src="@drawable/ic_add_white_24dp" |
|||
app:fabSize="normal" |
|||
app:layout_behavior="gr.auth.databases.flavours.utils.ScrollAwareFABBehavior" /> |
|||
</androidx.coordinatorlayout.widget.CoordinatorLayout> |
@ -0,0 +1,42 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" |
|||
xmlns:card_view="http://schemas.android.com/apk/res-auto" |
|||
android:id="@+id/ingredients_row_card" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:layout_gravity="center" |
|||
android:layout_marginStart="8dp" |
|||
android:layout_marginEnd="8dp" |
|||
android:foreground="?android:attr/selectableItemBackground" |
|||
card_view:cardCornerRadius="5dp" |
|||
card_view:cardElevation="2dp" |
|||
card_view:cardPreventCornerOverlap="false" |
|||
card_view:cardUseCompatPadding="true"> |
|||
|
|||
<LinearLayout |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:orientation="vertical" |
|||
android:paddingStart="4dp" |
|||
android:paddingTop="6dp" |
|||
android:paddingEnd="4dp" |
|||
android:paddingBottom="6dp"> |
|||
|
|||
<TextView |
|||
android:id="@+id/ingredients_ingredient_name" |
|||
android:layout_width="wrap_content" |
|||
android:layout_height="wrap_content" |
|||
android:layout_gravity="center_vertical" |
|||
android:ellipsize="end" |
|||
android:text="@string/ingredients_name" |
|||
android:textStyle="bold" /> |
|||
|
|||
<TextView |
|||
android:id="@+id/ingredients_ingredient_contains_alcohol" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:layout_gravity="center_vertical" |
|||
android:ellipsize="end" |
|||
android:text="@string/ingredients_contains_alcohol" /> |
|||
</LinearLayout> |
|||
</androidx.cardview.widget.CardView> |
Loading…
Reference in new issue