Skip to content

Instantly share code, notes, and snippets.

@dhiegoabrantes
Created January 23, 2019 06:11
Show Gist options
  • Select an option

  • Save dhiegoabrantes/8afb27eb7ecb8c03b50a2abe111f04da to your computer and use it in GitHub Desktop.

Select an option

Save dhiegoabrantes/8afb27eb7ecb8c03b50a2abe111f04da to your computer and use it in GitHub Desktop.
Retrieving recipe step index
package com.example.android.bakingapp;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.gson.Gson;
public class Activity_RecipeDetail extends AppCompatActivity implements OnStepClickListener{
Recipe recipe;
private boolean isTwoPane;
static String SHARED_PREFERENCES_NAME="com.example.android.bakingapp";
static String RECIPE_NAME_KEY="recipeName";
static String INGREDIENTS_KEY="recipeIngredients";
private int mClickedItemIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_detail);
recipe = (Recipe) getIntent().getParcelableExtra("recipe");
setTitle(recipe.getName());
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
FragmentManager fm = getSupportFragmentManager();
if(savedInstanceState != null){
mClickedItemIndex = savedInstanceState.getInt("mClickedItemIndex");
}
if(findViewById(R.id.separatorView) !=null){
isTwoPane=true;
Bundle bundle1 = new Bundle();
bundle1.putInt("index",mClickedItemIndex);
bundle1.putString("step_title",recipe.getSteps().get(mClickedItemIndex).getShortDescription());
bundle1.putBoolean("isTwoPane",isTwoPane);
bundle1.putParcelableArrayList("steps",recipe.getSteps());
Fragment_StepDescription stepDescriptionFrag = new Fragment_StepDescription();
stepDescriptionFrag.setArguments(bundle1);
fm.beginTransaction()
.add(R.id.recipe_step_frame, stepDescriptionFrag)
.commit();
}
else{
isTwoPane=false;
}
Bundle bundle2 = new Bundle();
bundle2.putParcelable("recipe", getIntent().getParcelableExtra("recipe"));
bundle2.putBoolean("isTwoPane",isTwoPane);
bundle2.putInt("mClickedItemIndex", mClickedItemIndex);
Fragment_RecipeDetail recipeDetail = new Fragment_RecipeDetail();
recipeDetail.setArguments(bundle2);
fm.beginTransaction()
.add(R.id.recipe_detail_frame, recipeDetail)
.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_recipe_detail, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
finish();
return true;
case R.id.addToWidget:
SharedPreferences preferences = this.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = gson.toJson(recipe.getIngredients());
preferences.edit().putString(RECIPE_NAME_KEY, recipe.getName()).apply();
preferences.edit().putString(INGREDIENTS_KEY, json).apply();
BakingAppWidgetProvider.sendRefreshBroadcast(this);
Toast.makeText(this, "Added to Widget", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(int index) {
mClickedItemIndex = index;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("mClickedItemIndex", mClickedItemIndex);
}
}
package com.example.android.bakingapp;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class Adapter_RecipeStep extends RecyclerView.Adapter<Adapter_RecipeStep.MyViewHolder> {
private List<RecipeStep> steps;
final private ListItemClickListener mOnClickListener;
int mClickedItemIndex=0;
boolean isTwoPane;
public interface ListItemClickListener{
void onListItemClick(int clickedItemIndex);
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
@BindView(R.id.recipeSteps_layout)
ConstraintLayout layout;
@BindView(R.id.step_name_lvrow_tv)
TextView name_tv;
public MyViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
view.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int clickedPosition=getAdapterPosition();
mClickedItemIndex=clickedPosition;
notifyDataSetChanged();
mOnClickListener.onListItemClick(clickedPosition);
}
}
public Adapter_RecipeStep(List<RecipeStep> steps,boolean isTwoPane,ListItemClickListener listener, int clickedItemIndex) {
this.steps = steps;
this.isTwoPane=isTwoPane;
mOnClickListener=listener;
mClickedItemIndex = clickedItemIndex;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recipe_steps_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
RecipeStep step = steps.get(position);
if(position==0){
holder.name_tv.setGravity(View.TEXT_ALIGNMENT_CENTER);
holder.name_tv.setText(step.getShortDescription());
}
else{
holder.name_tv.setText("Step #"+Integer.toString(position)+" "+step.getShortDescription());
}
if(isTwoPane) {
if (mClickedItemIndex == position) {
holder.layout.setBackgroundResource(R.color.listRowSelected);
} else {
holder.layout.setBackgroundResource(R.color.listRow);
}
}
}
@Override
public int getItemCount() {
return steps.size();
}
}
package com.example.android.bakingapp;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class Fragment_RecipeDetail extends Fragment implements Adapter_RecipeStep.ListItemClickListener {
@BindView(R.id.ingredients_rv)
RecyclerView ingredients_rv;
@BindView(R.id.steps_rv)
RecyclerView steps_rv;
List<Ingredient> ingredients;
Adapter_Ingredient ingredientAdapter;
ArrayList<RecipeStep> steps;
Adapter_RecipeStep stepAdapter;
boolean isTwoPane;
int mClickedItemIndex = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.fragment_recipe_detail, container, false);
ButterKnife.bind(this,rootView);
Recipe recipe = getArguments().getParcelable("recipe");
isTwoPane = getArguments().getBoolean("isTwoPane");
mClickedItemIndex = getArguments().getInt("mClickedItemIndex");
AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(getContext());
ingredients=recipe.getIngredients();
ingredientAdapter = new Adapter_Ingredient(ingredients);
ingredientAdapter.notifyDataSetChanged();
RecyclerView.LayoutManager mLayoutManager1 = new LinearLayoutManager(getActivity());
ingredients_rv.setLayoutManager(mLayoutManager1);
ingredients_rv.setAdapter(ingredientAdapter);
steps=recipe.getSteps();
stepAdapter = new Adapter_RecipeStep(steps,isTwoPane,this, mClickedItemIndex);
stepAdapter.notifyDataSetChanged();
RecyclerView.LayoutManager mLayoutManager2 = new LinearLayoutManager(getActivity());
steps_rv.setLayoutManager(mLayoutManager2);
steps_rv.setAdapter(stepAdapter);
//when the index > 0, we force the load of the recipe detail
if(mClickedItemIndex > 0){
init(mClickedItemIndex);
}
return rootView;
}
private void init(int clickedItemIndex){
if(isTwoPane){
Bundle bundle = new Bundle();
bundle.putInt("index",clickedItemIndex);
if(clickedItemIndex==0){
bundle.putString("step_title",steps.get(clickedItemIndex).getShortDescription());
}
else{
bundle.putString("step_title","Step #"+Integer.toString(clickedItemIndex));
}
bundle.putBoolean("isTwoPane",isTwoPane);
bundle.putParcelableArrayList("steps",steps);
Fragment_StepDescription stepDescriptionFrag = new Fragment_StepDescription();
stepDescriptionFrag.setArguments(bundle);
getFragmentManager().beginTransaction()
.replace(R.id.recipe_step_frame, stepDescriptionFrag)
.commit();
}
else{
Intent intent = new Intent(getActivity(), Activity_StepDescription.class);
intent.putExtra("index",clickedItemIndex);
if(clickedItemIndex==0){
intent.putExtra("step_title",steps.get(clickedItemIndex).getShortDescription());
}
else{
intent.putExtra("step_title","Step #"+Integer.toString(clickedItemIndex));
}
intent.putExtra("isTwoPane",isTwoPane);
intent.putParcelableArrayListExtra("steps",steps);
startActivity(intent );
}
}
@Override
public void onListItemClick(int clickedItemIndex){
mClickedItemIndex = clickedItemIndex;
init(mClickedItemIndex);
((OnStepClickListener) getActivity()).onClick(mClickedItemIndex);
}
}
package com.example.android.bakingapp;
public interface OnStepClickListener {
void onClick(int index);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment