Skip to content

Commit

Permalink
add API: SelectionCreator.maxSelectablePerMediaType()
Browse files Browse the repository at this point in the history
  • Loading branch information
gejiaheng committed Nov 7, 2017
1 parent a7e1a31 commit 07d63a7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
19 changes: 19 additions & 0 deletions matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,29 @@ public SelectionCreator countable(boolean countable) {
public SelectionCreator maxSelectable(int maxSelectable) {
if (maxSelectable < 1)
throw new IllegalArgumentException("maxSelectable must be greater than or equal to one");
if (mSelectionSpec.maxImageSelectable > 0 || mSelectionSpec.maxVideoSelectable > 0)
throw new IllegalStateException("already set maxImageSelectable and maxVideoSelectable");
mSelectionSpec.maxSelectable = maxSelectable;
return this;
}

/**
* Only useful when {@link SelectionSpec#mediaTypeExclusive} set true and you want to set different maximum
* selectable files for image and video media types.
*
* @param maxImageSelectable Maximum selectable count for image.
* @param maxVideoSelectable Maximum selectable count for video.
* @return
*/
public SelectionCreator maxSelectablePerMediaType(int maxImageSelectable, int maxVideoSelectable) {
if (maxImageSelectable < 1 || maxVideoSelectable < 1)
throw new IllegalArgumentException(("max selectable must be greater than or equal to one"));
mSelectionSpec.maxSelectable = -1;
mSelectionSpec.maxImageSelectable = maxImageSelectable;
mSelectionSpec.maxVideoSelectable = maxVideoSelectable;
return this;
}

/**
* Add filter to filter each selecting item.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public final class SelectionSpec {
public int orientation;
public boolean countable;
public int maxSelectable;
public int maxImageSelectable;
public int maxVideoSelectable;
public List<Filter> filters;
public boolean capture;
public CaptureStrategy captureStrategy;
Expand Down Expand Up @@ -67,6 +69,8 @@ private void reset() {
orientation = 0;
countable = false;
maxSelectable = 1;
maxImageSelectable = 0;
maxVideoSelectable = 0;
filters = null;
capture = false;
captureStrategy = null;
Expand All @@ -77,7 +81,7 @@ private void reset() {
}

public boolean singleSelectionModeEnabled() {
return !countable && maxSelectable == 1;
return !countable && (maxSelectable == 1 || (maxImageSelectable == 1 && maxVideoSelectable == 1));
}

public boolean needOrientationRestriction() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public boolean isSelected(Item item) {

public IncapableCause isAcceptable(Item item) {
if (maxSelectableReached()) {
int maxSelectable = SelectionSpec.getInstance().maxSelectable;
int maxSelectable = currentMaxSelectable();
String cause;

try {
Expand All @@ -193,7 +193,21 @@ public IncapableCause isAcceptable(Item item) {
}

public boolean maxSelectableReached() {
return mItems.size() == SelectionSpec.getInstance().maxSelectable;
return mItems.size() == currentMaxSelectable();
}

// depends
private int currentMaxSelectable() {
SelectionSpec spec = SelectionSpec.getInstance();
if (spec.maxSelectable > 0) {
return spec.maxSelectable;
} else if (mCollectionType == COLLECTION_IMAGE) {
return spec.maxImageSelectable;
} else if (mCollectionType == COLLECTION_VIDEO) {
return spec.maxVideoSelectable;
} else {
return spec.maxSelectable;
}
}

public int getCollectionType() {
Expand Down

0 comments on commit 07d63a7

Please sign in to comment.