-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
372 additions
and
172 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
96 changes: 96 additions & 0 deletions
96
app/src/main/java/edu/sc/purplelimited/ui/suggestions/SuggestionsAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package edu.sc.purplelimited.ui.suggestions; | ||
|
||
import android.content.Context; | ||
import android.graphics.Bitmap; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.Button; | ||
import android.widget.ImageView; | ||
import android.widget.TextView; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.viewpager.widget.PagerAdapter; | ||
|
||
import com.android.volley.Response; | ||
import com.android.volley.VolleyError; | ||
import com.android.volley.toolbox.ImageRequest; | ||
|
||
import java.util.ArrayList; | ||
|
||
import edu.sc.purplelimited.R; | ||
import edu.sc.purplelimited.classes.ImageQueue; | ||
import edu.sc.purplelimited.classes.Recipe; | ||
import edu.sc.purplelimited.ui.saved_recipes.SavedRecipesFragment; | ||
|
||
public class SuggestionsAdapter extends PagerAdapter { | ||
|
||
private final Context context; | ||
private final ArrayList<Recipe> suggestions; | ||
|
||
public SuggestionsAdapter(Context context, ArrayList<Recipe> suggestions) { | ||
this.context = context; | ||
this.suggestions = suggestions; | ||
} | ||
|
||
@Override | ||
public int getCount() { | ||
return suggestions.size(); | ||
} | ||
|
||
@Override | ||
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { | ||
return view.equals(object); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Object instantiateItem(@NonNull ViewGroup container, int position) { | ||
View view = LayoutInflater.from(context).inflate(R.layout.search_result_card_images, container, false); | ||
|
||
// Instantiate the Views | ||
ImageView cardImage = view.findViewById(R.id.searchCardImage); | ||
TextView cardTitle = view.findViewById(R.id.searchCardTitle); | ||
TextView cardDescription = view.findViewById(R.id.searchCardDescription); | ||
Button saveButton = view.findViewById(R.id.saveFromSearch); | ||
saveButton.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
SavedRecipesFragment.addRecipe(suggestions.get(position), context); | ||
} | ||
}); | ||
|
||
// Get current recipe information | ||
Recipe recipe = suggestions.get(position); | ||
String title = recipe.getName(); | ||
if(title.length() > 56) { | ||
title = title.substring(0,56) + "..."; | ||
} | ||
String description = recipe.getDescription(); | ||
String url = recipe.getThumbnailURL(); | ||
|
||
// Image request for the thumbnail | ||
ImageRequest imageRequest = new ImageRequest(url, | ||
new Response.Listener<Bitmap>() { | ||
@Override | ||
public void onResponse(Bitmap response) { | ||
cardImage.setImageBitmap(response); | ||
} | ||
}, 0, 0, ImageView.ScaleType.CENTER_CROP, null, new Response.ErrorListener() { | ||
@Override | ||
public void onErrorResponse(VolleyError error) {/*empty*/} | ||
}); | ||
ImageQueue.getInstance(context).addToQueue(imageRequest); | ||
|
||
cardTitle.setText(title); | ||
cardDescription.setText(description); | ||
container.addView(view, 0); | ||
|
||
return view; | ||
} | ||
|
||
@Override | ||
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { | ||
container.removeView((View)object); | ||
} | ||
} |
Oops, something went wrong.