Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapped excluded images list into a singleton #168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.esafirm.imagepicker.features.common.BaseConfig;
import com.esafirm.imagepicker.features.imageloader.ImageLoader;
import com.esafirm.imagepicker.helper.ExcludedMediaSingleton;
import com.esafirm.imagepicker.model.Image;

import java.io.File;
Expand All @@ -16,7 +17,7 @@ public class ImagePickerConfig extends BaseConfig implements Parcelable {
static final int NO_COLOR = -1;

private ArrayList<Image> selectedImages;
private ArrayList<File> excludedImages;


private String folderTitle;
private String imageTitle;
Expand All @@ -36,6 +37,7 @@ public class ImagePickerConfig extends BaseConfig implements Parcelable {
private transient String language;

public ImagePickerConfig() {
ExcludedMediaSingleton.getInstance().resetExclusions();
}

public int getArrowColor() {
Expand Down Expand Up @@ -111,22 +113,15 @@ public void setSelectedImages(ArrayList<Image> selectedImages) {
}

public ArrayList<File> getExcludedImages() {
return excludedImages;
return ExcludedMediaSingleton.getInstance().getExcludedImages();
}

public void setExcludedImages(ArrayList<Image> excludedImages) {
if (excludedImages != null && !excludedImages.isEmpty()) {
this.excludedImages = new ArrayList<>();
for (Image image : excludedImages) {
this.excludedImages.add(new File(image.getPath()));
}
} else {
this.excludedImages = null;
}
ExcludedMediaSingleton.getInstance().setExcludedImages(excludedImages);
}

public void setExcludedImageFiles(ArrayList<File> excludedImages) {
this.excludedImages = excludedImages;
ExcludedMediaSingleton.getInstance().setExcludedImageFiles(excludedImages);
}

public boolean isFolderMode() {
Expand Down Expand Up @@ -175,10 +170,8 @@ public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeTypedList(this.selectedImages);

dest.writeByte((byte) (excludedImages != null ? 1 : 0));
if (excludedImages != null) {
dest.writeList(this.excludedImages);
}
dest.writeByte((byte) ((ExcludedMediaSingleton.getInstance().getExcludedImages() == null ||
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is not needed anymore.

ExcludedMediaSingleton.getInstance().getExcludedImages().size() == 0)? 0 : 1));

dest.writeString(this.folderTitle);
dest.writeString(this.imageTitle);
Expand All @@ -198,10 +191,10 @@ protected ImagePickerConfig(Parcel in) {
this.selectedImages = in.createTypedArrayList(Image.CREATOR);

boolean isPresent = in.readByte() != 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove isPresent

if (isPresent) {
this.excludedImages = new ArrayList<>();
in.readList(this.excludedImages, File.class.getClassLoader());
}
// if (isPresent) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove all this comments

// this.excludedImages = new ArrayList<>();
// in.readList(this.excludedImages, File.class.getClassLoader());
// }

this.folderTitle = in.readString();
this.imageTitle = in.readString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.esafirm.imagepicker.helper;

import com.esafirm.imagepicker.model.Image;

import java.io.File;
import java.util.ArrayList;

/**
* Code written by Qandeel Abbassi on 9/20/2018 at 7:03 PM.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, no signatures or this kind of comment in the repo

*/
public class ExcludedMediaSingleton {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be generate singleton for transferring large data between others activities/fragment to ImagePicker.
Can you add ImagePickerConfig.selectedImages to this as well?

The name could be renamed to something more general as well, and please omit "Singleton" from it.

private static ExcludedMediaSingleton ourInstance;
private ArrayList<File> excludedImages;

private ExcludedMediaSingleton() {
}

public static ExcludedMediaSingleton getInstance() {
if (ourInstance == null) { //if there is no instance available... create new one
ourInstance = new ExcludedMediaSingleton();
}
return ourInstance;
}

public void resetExclusions() {
if (excludedImages != null) {
excludedImages.clear();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could lead to some unexpected behavior since excludedImages is shared between ImagePicker and others screen on the app.

If you want to do this, please copy the list instead of just assigning it in setExcludedImageFiles()

}
excludedImages = null;
}

public void setExcludedImages(ArrayList<Image> excludedImages) {
if (excludedImages != null && !excludedImages.isEmpty()) {
this.excludedImages = new ArrayList<>();
for (Image image : excludedImages) {
this.excludedImages.add(new File(image.getPath()));
}
} else {
this.excludedImages = null;
}
}

public void setExcludedImageFiles(ArrayList<File> excludedImages) {
this.excludedImages = excludedImages;
}

public ArrayList<File> getExcludedImages() {
return excludedImages;
}
}