Skip to content

Commit

Permalink
added feature that clearing current selection if max selectable is 1 z…
Browse files Browse the repository at this point in the history
  • Loading branch information
luowenyue committed Feb 8, 2022
1 parent 47023f1 commit 8d742fb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
11 changes: 11 additions & 0 deletions matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,17 @@ public SelectionCreator setOnCheckedListener(@Nullable OnCheckedListener listene
return this;
}

/**
* Set if selection should change on case where maxSelectable is 1
*
* @param changeSelectionOnSelectingOne
* @return {@link SelectionCreator} for fluent API.
*/
public SelectionCreator setChangeSelectionOnSelectingOne(boolean changeSelectionOnSelectingOne) {
mSelectionSpec.changeSelectionOnSelectingOne = changeSelectionOnSelectingOne;
return this;
}

/**
* Start to select media and wait for result.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public final class SelectionSpec {
public boolean showPreview;
public Drawable audioThumbnail;
public Drawable audioPlaceholder;
public boolean changeSelectionOnSelectingOne;

private SelectionSpec() {
}
Expand Down Expand Up @@ -100,6 +101,7 @@ private void reset() {
showPreview = true;
audioThumbnail = null;
audioPlaceholder = null;
changeSelectionOnSelectingOne = false;
}

public boolean singleSelectionModeEnabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ public boolean isSelected(Item item) {
}

public IncapableCause isAcceptable(Item item) {
if (maxSelectableReached()) {
if (shouldRemoveSelection()) {
mItems.clear();
}
else if (maxSelectableReached()) {
int maxSelectable = currentMaxSelectable();
String cause;

Expand Down Expand Up @@ -211,6 +214,11 @@ public boolean maxSelectableReached() {
return mItems.size() == currentMaxSelectable();
}

public boolean shouldRemoveSelection() {
SelectionSpec spec = SelectionSpec.getInstance();
return mItems.size() == 1 && currentMaxSelectable() == 1 && spec.changeSelectionOnSelectingOne;
}

// depends
private int currentMaxSelectable() {
SelectionSpec spec = SelectionSpec.getInstance();
Expand Down

0 comments on commit 8d742fb

Please sign in to comment.