/** * Unpacks the recipes extra from the intent that started this activity and returns an * ObservableArrayList containing the same recipes. * * @return a list of recipes */ private ObservableArrayList<Recipe> unpackRecipes() { final Parcelable[] parcelables = getIntent().getParcelableArrayExtra(EXTRA_RECIPES); Objects.requireNonNull( parcelables, "MealRecipeAddActivity must be started with a recipes extra"); final ObservableArrayList<Recipe> recipes = new ObservableArrayList<>(); recipes.ensureCapacity(parcelables.length); for (Parcelable parcelable : parcelables) { recipes.add((Recipe) parcelable); } return recipes; }
/** * Unpacks the recipes existing in the meal from the intent that started this activity and returns * a List of those recipes * * @return list of recipes */ private List<Recipe> existRecipes() { final Parcelable[] parcelables = getIntent().getParcelableArrayExtra(MealRecipeAddActivity.EXIST_RECIPES); Objects.requireNonNull( parcelables, "MealRecipeAddActivity must be started with a exist recipe list"); final ObservableArrayList<Recipe> exist = new ObservableArrayList<>(); exist.ensureCapacity(parcelables.length); for (Parcelable parcelable : parcelables) { exist.add((Recipe) parcelable); } return exist; }
private void searchRecipes() { final String query = mSearchView.getQuery().toString(); Set<Recipe> recipeSet = new HashSet<>(); recipeSet.addAll(mRecipes); List<Recipe> recipes = null; try { recipes = App.getAccessor().loadRecipes(query); } catch (SQLException e) { e.printStackTrace(); } recipeSet.addAll(recipes); mRecipes.clear(); mRecipes.addAll(recipeSet); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mSelectedRecipes = new ArrayList<>(); setContentView(R.layout.activity_meal_recipe_add); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); setUpActionBar(); // Unpack recipes mVisibleRecipes = new ObservableArrayList<>(); mRecipes = unpackRecipes(); mVisibleRecipes.addAll(mRecipes); mSelectedRecipes.addAll(existRecipes()); // Initialize view final ListView list = (ListView) findViewById(R.id.recipe_list); final RecipeAddListAdapter adapter = new RecipeAddListAdapter(this, mVisibleRecipes, mSelectedRecipes); adapter.setAddListener( new RecipeAddListAdapter.RecipeAddListener() { @Override public void recipeAddRequested(Recipe recipe) { Log.d(TAG, "Added recipe " + recipe); // Add to list of recipes to add if (!mSelectedRecipes.contains(recipe)) { mSelectedRecipes.add(recipe); try { if (!App.getAccessor().containsLocalRecipe(recipe.getObjectId())) { App.getAccessor().storeRecipe(recipe); } } catch (SQLException e) { new AlertDialog.Builder(MealRecipeAddActivity.this) .setTitle("Failed to process recipe") .setMessage(e.getLocalizedMessage()) .show(); } } updateResult(); } }); list.setAdapter(adapter); mSearchView = (SearchView) findViewById(R.id.search); mSearchView.setOnQueryTextListener(new SearchHandler()); // Set initial result updateResult(); }
/** * Update the meals shown on screen. Shows filtered meals if the user types in the search bar or * shows all the meals if nothing is entered */ private void updateVisibleMeals() { final String query = mSearchView.getQuery().toString(); // Make all meals visible mVisibleRecipes.clear(); if (!query.isEmpty()) { final List<Recipe> filteredRecipes = new ArrayList<>(); // Limit mVisibleMeals to the meals whose titles contain the query // Case insensitive final String lowerQuery = query.toLowerCase(Locale.getDefault()); for (Recipe recipe : mRecipes) { final String lowerTitle = recipe.getTitle().toLowerCase(Locale.getDefault()); if (lowerTitle.contains(lowerQuery)) { filteredRecipes.add(recipe); } } mVisibleRecipes.addAll(filteredRecipes); } else { // Empty query mVisibleRecipes.addAll(mRecipes); } }