Skip to content

Commit

Permalink
small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
MFlisar committed Mar 4, 2017
1 parent 5a4965d commit 82a3145
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ public HashSet<Integer> getSelection() {
}

@Override
public void updateSelection(int start, int end, boolean isSelected) {
public boolean isSelected(int index) {
return mAdapter.getSelection().contains(index);
}

@Override
public void updateSelection(int start, int end, boolean isSelected, boolean calledFromOnStart) {
mAdapter.selectRange(start, end, isSelected);
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public enum Mode
private ISelectionStartFinishedListener mStartFinishedListener;
private HashSet<Integer> mOriginalSelection;
private boolean mFirstWasSelected;
private boolean mCheckSelectionState = false;

/**
* @param selectionHandler the handler that takes care to handle the selection events
Expand All @@ -57,7 +58,7 @@ public DragSelectionProcessor withMode(Mode mode)
mMode = mode;
return this;
}

/**
* @param startFinishedListener a listener that get's notified when the drag selection is started or finished
* @return this
Expand All @@ -68,6 +69,17 @@ public DragSelectionProcessor withStartFinishedListener(ISelectionStartFinishedL
return this;
}

/**
* If this is enabled, the processor will check if an items selection state is toggled before notifying the {@link ISelectionHandler}
* @param check true, if this check should be enabled
* @return this
*/
public DragSelectionProcessor withCheckSelectionState(boolean check)
{
mCheckSelectionState = check;
return this;
}

@Override
public void onSelectionStarted(int start)
{
Expand All @@ -81,22 +93,22 @@ public void onSelectionStarted(int start)
{
case Simple:
{
mSelectionHandler.updateSelection(start, start, true);
mSelectionHandler.updateSelection(start, start, true, true);
break;
}
case ToggleAndUndo:
{
mSelectionHandler.updateSelection(start, start, !mOriginalSelection.contains(start));
mSelectionHandler.updateSelection(start, start, !mOriginalSelection.contains(start), true);
break;
}
case FirstItemDependent:
{
mSelectionHandler.updateSelection(start, start, !mFirstWasSelected);
mSelectionHandler.updateSelection(start, start, !mFirstWasSelected, true);
break;
}
case FirstItemDependentToggleAndUndo:
{
mSelectionHandler.updateSelection(start, start, !mFirstWasSelected);
mSelectionHandler.updateSelection(start, start, !mFirstWasSelected, true);
break;
}
}
Expand All @@ -120,45 +132,69 @@ public void onSelectChange(int start, int end, boolean isSelected)
{
case Simple:
{
mSelectionHandler.updateSelection(start, end, isSelected);
if (mCheckSelectionState)
checkedUpdateSelection(start, end, isSelected);
else
mSelectionHandler.updateSelection(start, end, isSelected, false);
break;
}
case ToggleAndUndo:
{
for (int i = start; i <= end; i++)
mSelectionHandler.updateSelection(i, i, isSelected ? !mOriginalSelection.contains(i) : mOriginalSelection.contains(i));
checkedUpdateSelection(i, i, isSelected ? !mOriginalSelection.contains(i) : mOriginalSelection.contains(i));
break;
}
case FirstItemDependent:
{
for (int i = start; i <= end; i++)
mSelectionHandler.updateSelection(i, i, isSelected ? !mFirstWasSelected : mFirstWasSelected);
checkedUpdateSelection(start, end, isSelected ? !mFirstWasSelected : mFirstWasSelected);
break;
}
case FirstItemDependentToggleAndUndo:
{
for (int i = start; i <= end; i++)
mSelectionHandler.updateSelection(i, i, isSelected ? !mFirstWasSelected : mOriginalSelection.contains(i));
checkedUpdateSelection(i, i, isSelected ? !mFirstWasSelected : mOriginalSelection.contains(i));
break;
}
}
}

private void checkedUpdateSelection(int start, int end, boolean newSelectionState)
{
if (mCheckSelectionState)
{
for (int i = start; i <= end; i++)
{
if (mSelectionHandler.isSelected(i) != newSelectionState)
mSelectionHandler.updateSelection(i, i, newSelectionState, false);
}
}
else
mSelectionHandler.updateSelection(start, end, newSelectionState, false);
}

public interface ISelectionHandler
{
/**
* Return the currently selected items => can be ignored for {@link Mode#Simple} and {@link Mode#FirstItemDependent}
* @return the currently selected items => can be ignored for {@link Mode#Simple} and {@link Mode#FirstItemDependent}
*/
Set<Integer> getSelection();

/**
* update your adapter and select select/unselect the passed index range, you be get a single for all modes but {@link Mode#Simple}
* only used, if {@link DragSelectionProcessor#withCheckSelectionState(boolean)} was enabled
* @param index the index which selection state wants to be known
* @return the current selection state of the passed in index
*/
boolean isSelected(int index);

/**
* update your adapter and select select/unselect the passed index range, you be get a single for all modes but {@link Mode#Simple} and {@link Mode#FirstItemDependent}
*
* @param start the first item of the range who's selection state changed
* @param end the last item of the range who's selection state changed
* @param isSelected true, if the range should be selected, false otherwise
* @param calledFromOnStart true, if it was called from the {@link DragSelectionProcessor#onSelectionStarted(int)} event
*/
void updateSelection(int start, int end, boolean isSelected);
void updateSelection(int start, int end, boolean isSelected, boolean calledFromOnStart);
}

public interface ISelectionStartFinishedListener
Expand Down

0 comments on commit 82a3145

Please sign in to comment.