diff --git a/photoviewslider/src/main/java/br/com/jeancsanchez/photoviewslider/Photo.java b/photoviewslider/src/main/java/br/com/jeancsanchez/photoviewslider/Photo.java index ef8554e..c0f9bbc 100644 --- a/photoviewslider/src/main/java/br/com/jeancsanchez/photoviewslider/Photo.java +++ b/photoviewslider/src/main/java/br/com/jeancsanchez/photoviewslider/Photo.java @@ -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; } } diff --git a/photoviewslider/src/main/java/br/com/jeancsanchez/photoviewslider/PhotoListIsEmptyException.java b/photoviewslider/src/main/java/br/com/jeancsanchez/photoviewslider/PhotoListIsEmptyException.java new file mode 100644 index 0000000..b2c9ec7 --- /dev/null +++ b/photoviewslider/src/main/java/br/com/jeancsanchez/photoviewslider/PhotoListIsEmptyException.java @@ -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!"); + } +} diff --git a/photoviewslider/src/main/java/br/com/jeancsanchez/photoviewslider/PhotosViewSlider.java b/photoviewslider/src/main/java/br/com/jeancsanchez/photoviewslider/PhotosViewSlider.java index dab7233..81775c5 100644 --- a/photoviewslider/src/main/java/br/com/jeancsanchez/photoviewslider/PhotosViewSlider.java +++ b/photoviewslider/src/main/java/br/com/jeancsanchez/photoviewslider/PhotosViewSlider.java @@ -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 { @@ -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(); } @@ -77,21 +92,31 @@ private void init(){ * @param photos List of photos that will to be show on grid. */ public void initializePhotos(List 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 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. @@ -99,21 +124,74 @@ public void initializePhotos(List photos, int gridColumns) { * @param gridColumns Number of columns that photos grid will have. */ public void initializePhotos(List 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 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); @@ -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); } @@ -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]; @@ -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(); @@ -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; } @@ -247,7 +312,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; } } @@ -255,6 +320,15 @@ public boolean onTouch(View v, MotionEvent event) { } + @Override + public void onClick(View view) { + int viewId = view.getId(); + + if (viewId == R.id.btn_share) + preparePhotoForShare(); + } + + private class PhotosViewAdapter extends RecyclerView.Adapter implements View.OnClickListener { private Context context; private List photoList; @@ -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);