Skip to content

Commit

Permalink
more ways to initialize photos
Browse files Browse the repository at this point in the history
  • Loading branch information
jeancsanchez committed Aug 24, 2016
1 parent 107d3bd commit 02dde8e
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,34 @@
*/

public class Photo implements Serializable {
private int id;
private String description;
private String image;
private String description = "";
private String imageUrl;


public String getDescription() {
return description;
public Photo(){}

public Photo(String imageUrl){
this.imageUrl = imageUrl;
}

public void setDescription(String description) {
public Photo(String imageUrl, String description){
this.imageUrl = imageUrl;
this.description = description;
}

public String getImage() {
return image;
public String getDescription() {
return description;
}

public void setImage(String image) {
this.image = image;
public void setDescription(String description) {
this.description = description;
}

public int getId() {
return id;
public String getImageUrl() {
return imageUrl;
}

public void setId(int id) {
this.id = id;
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package br.com.jeancsanchez.photoviewslider;

/**
* Created by jean on 24/08/16.
*/

public class PhotoListIsEmptyException extends NullPointerException{
public PhotoListIsEmptyException(){
super("Impossible init the photos without some photo added!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PhotosViewSlider extends LinearLayout implements View.OnClickListener, View.OnTouchListener {
Expand Down Expand Up @@ -69,6 +70,20 @@ public PhotosViewSlider(Context context, AttributeSet attrs, int defStyle) {
private void init(){
inflate(getContext(), R.layout.photos_view, this);
recyclerView = (RecyclerView) findViewById(R.id.photosList);
photoAdapter = new PhotosViewAdapter(getContext());
}


public void initializePhotos(){
try {
if (photos == null)
throw new PhotoListIsEmptyException();

}catch (PhotoListIsEmptyException e){
e.printStackTrace();
}

adapterSetup();
}


Expand All @@ -77,43 +92,106 @@ private void init(){
* @param photos List of photos that will to be show on grid.
*/
public void initializePhotos(List<Photo> photos) {
this.photos = photos;
if(this.photos == null)
this.photos = photos;
else
this.photos.addAll(photos);

adapterSetup();
}


/**
*
* @param photos List of photos that will to be show on grid.
* @param gridColumns Number of columns that photos grid will have.
*/
public void initializePhotos(List<Photo> photos, int gridColumns) {
this.photos = photos;
if(this.photos == null)
this.photos = photos;
else
this.photos.addAll(photos);
this.gridColumns = gridColumns;

adapterSetup();
}


/**
*
* @param photos List of photos that will to be show on grid.
* @param techniqueAnimation Type/Technique of animation on photos detail
* @param gridColumns Number of columns that photos grid will have.
*/
public void initializePhotos(List<Photo> photos, Techniques techniqueAnimation, int gridColumns) {
this.photos = photos;
if(this.photos == null)
this.photos = photos;
else
this.photos.addAll(photos);

this.techniqueAnimation = techniqueAnimation;
this.gridColumns = gridColumns;
adapterSetup();
}


/**
*
* @param photosUrls List of urls's photos that will to be show on grid.
*/
public void initializePhotosUrls(ArrayList<String> photosUrls){
if(photos == null)
photos = new ArrayList<>();

for(String photoUrl : photosUrls)
photos.add(new Photo(photoUrl));

adapterSetup();
}


/**
* Set image url to the list of photos
* @param imageUrl set image url
*/
public void setPhotoUrl(String imageUrl){
if(photos == null)
photos = new ArrayList<>();

photos.add(new Photo(imageUrl));
}


/**
* Set image url to the list of photos
* @param imageUrl set image url
* @param description set description to image
*/
public void setPhotoUrl(String imageUrl, String description){
if(photos == null)
photos = new ArrayList<>();

photos.add(new Photo(imageUrl, description));
}


/**
* Set an effect transition on photos details
* @param techniqueAnimation set image url
*/
public void setTechniqueAnimation(Techniques techniqueAnimation){
this.techniqueAnimation = techniqueAnimation;
}


private void adapterSetup() {
photoAdapter = new PhotosViewAdapter(getContext());
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(gridColumns, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(photoAdapter);
photoAdapter.setupItems(photos);
}


private void generatePhotoDetail() {
viewDialog = inflate(getContext(), R.layout.photo_detail, null);
imgPhoto = (ImageView) viewDialog.findViewById(R.id.img_photo_gallery_detail);
Expand All @@ -135,11 +213,7 @@ private void photoClicked(Photo photo) {
if (photos.get(i) == photo)
currentPosition = i;

showImage(photo.getImage(), photo.getDescription(), currentPosition);
}

public void setTechniqueAnimation(Techniques techniqueAnimation){
this.techniqueAnimation = techniqueAnimation;
showImage(photo.getImageUrl(), photo.getDescription(), currentPosition);
}


Expand All @@ -162,15 +236,6 @@ private void showImage(String url, String description, int currentPosition) {
}


@Override
public void onClick(View view) {
int viewId = view.getId();

if (viewId == R.id.btn_share)
preparePhotoForShare();
}


private void preparePhotoForShare() {
final Bitmap[] image = new Bitmap[1];

Expand All @@ -179,7 +244,7 @@ private void preparePhotoForShare() {
public void run() {
try {
image[0] = Picasso.with(getContext())
.load(photos.get(currentPosition).getImage())
.load(photos.get(currentPosition).getImageUrl())
.get();

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
Expand Down Expand Up @@ -235,7 +300,7 @@ public boolean onTouch(View v, MotionEvent event) {
currentPosition --;

Photo photo = photos.get(currentPosition);
showImage(photo.getImage(), photo.getDescription(), currentPosition);
showImage(photo.getImageUrl(), photo.getDescription(), currentPosition);
return true;
}

Expand All @@ -247,14 +312,23 @@ public boolean onTouch(View v, MotionEvent event) {
currentPosition ++;

Photo photo = photos.get(currentPosition);
showImage(photo.getImage(), photo.getDescription(), currentPosition);
showImage(photo.getImageUrl(), photo.getDescription(), currentPosition);
return true;
}
}
return false;
}


@Override
public void onClick(View view) {
int viewId = view.getId();

if (viewId == R.id.btn_share)
preparePhotoForShare();
}


private class PhotosViewAdapter extends RecyclerView.Adapter<PhotosViewAdapter.PhotosAdapterViewHolder> implements View.OnClickListener {
private Context context;
private List<Photo> photoList;
Expand All @@ -275,7 +349,7 @@ public PhotosAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType
@Override
public void onBindViewHolder(final PhotosAdapterViewHolder holder, int position) {
holder.itemView.setTag(photoList.get(position));
Picasso.with(context).load(photoList.get(position).getImage())
Picasso.with(context).load(photoList.get(position).getImageUrl())
.fit()
.placeholder(R.drawable.photodefault)
.into(holder.photo);
Expand Down

0 comments on commit 02dde8e

Please sign in to comment.