Skip to content

Commit

Permalink
[android] simplification - ConfirmListDialog.
Browse files Browse the repository at this point in the history
  • Loading branch information
gubatron committed Mar 23, 2016
1 parent 33356fc commit d242bf1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,11 @@ public View getView(int position, View view, ViewGroup parent) {

try {
initTouchFeedback(view, item);
ItemTag itemTag = new ItemTag(item, position);
if(selectionMode == SelectionMode.MULTIPLE_SELECTION) {
initCheckBox(view, itemTag);
initCheckBox(view, item);
setCheckboxesVisibility(true);
} else if (selectionMode == SelectionMode.SINGLE_SELECTION) {
initRadioButton(view, itemTag);
initRadioButton(view, item, position);
}
populateView(view, item);
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,30 @@ public void setDialog(AbstractConfirmListDialog dlg) {
@Override
public void onClick(View v) {
if (Ref.alive(ctxRef) && Ref.alive(dlgRef)) {
AbstractConfirmListDialog dlg = dlgRef.get();
final AbstractConfirmListDialog.SelectionMode selectionMode = dlg.getSelectionMode();
List<SoundcloudSearchResult> results = (selectionMode == AbstractConfirmListDialog.SelectionMode.NO_SELECTION) ?
(List<SoundcloudSearchResult>) dlg.getList() :
new ArrayList<SoundcloudSearchResult>();

if (selectionMode == AbstractConfirmListDialog.SelectionMode.MULTIPLE_SELECTION) {
results.addAll(dlg.getChecked());
} else if (selectionMode == AbstractConfirmListDialog.SelectionMode.SINGLE_SELECTION) {
SoundcloudSearchResult selected = results.get(dlg.getLastSelected());
if (selected == null) {
// MIGHT DO: dlg.displayErrorNotice(ERROR_CODE);
return;
final AbstractConfirmListDialog dlg = dlgRef.get();
try {
final AbstractConfirmListDialog.SelectionMode selectionMode = dlg.getSelectionMode();
List<SoundcloudSearchResult> results = (selectionMode == AbstractConfirmListDialog.SelectionMode.NO_SELECTION) ?
(List<SoundcloudSearchResult>) dlg.getList() :
new ArrayList<SoundcloudSearchResult>();

if (selectionMode == AbstractConfirmListDialog.SelectionMode.MULTIPLE_SELECTION) {
results.addAll(dlg.getChecked());
} else if (selectionMode == AbstractConfirmListDialog.SelectionMode.SINGLE_SELECTION) {
if (results == null || results.isEmpty()) {
return;
}
SoundcloudSearchResult selected = results.get(dlg.getLastSelected());
if (selected == null) {

return;
}
results.add(selected);
}
results.add(selected);
startDownloads(ctxRef.get(), results);
} finally {
dlg.dismiss();
}
startDownloads(ctxRef.get(), results);
dlg.dismiss();
}
}
}
Expand Down Expand Up @@ -196,7 +202,7 @@ public int getItemThumbnailResourceId(SoundcloudSearchResult data) {
}

public static class ConfirmSoundcloudDownloadDialog extends AbstractConfirmListDialog<SoundcloudSearchResult> {
public ConfirmSoundcloudDownloadDialog(Context context, List<SoundcloudSearchResult> listData, SelectionMode selectionMode) {
private ConfirmSoundcloudDownloadDialog(Context context, List<SoundcloudSearchResult> listData, SelectionMode selectionMode) {
super(context, listData, selectionMode, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ public abstract class AbstractListAdapter<T> extends BaseAdapter implements Filt
private final OnClickListener viewOnClickListener;
private final ViewOnLongClickListener viewOnLongClickListener;
private final ViewOnKeyListener viewOnKeyListener;

private final CheckboxOnCheckedChangeListener checkboxOnCheckedChangeListener;
protected int lastSelectedRadioButtonIndex = 0;
private final RadioButtonOnCheckedChangeListener radioButtonCheckedChangeListener;
private OnItemCheckedListener onItemCheckedListener;

private ListAdapterFilter<T> filter;
private boolean checkboxesVisibility;
Expand All @@ -66,10 +70,6 @@ public abstract class AbstractListAdapter<T> extends BaseAdapter implements Filt
protected Set<T> checked;
protected List<T> visualList;

protected int lastSelectedRadioButtonIndex = 0;
private final OnCheckedChangeListener radioButtonCheckedChangeListener;
private OnItemCheckedListener onItemCheckedListener;

public AbstractListAdapter(Context context,
int viewItemId,
List<T> list,
Expand Down Expand Up @@ -275,7 +275,6 @@ public List<T> getList() {
* It will also bind the data to the view, you can refer to it if you need it by doing a .getTag()
*/
public View getView(int position, View view, ViewGroup parent) {

T item = getItem(position);
Context ctx = getContext();

Expand All @@ -287,11 +286,9 @@ public View getView(int position, View view, ViewGroup parent) {
}

try {

initTouchFeedback(view, item);
ItemTag itemTag = new ItemTag(item, position);
initCheckBox(view, itemTag);
initRadioButton(view, itemTag);
initCheckBox(view, item);
initRadioButton(view, item, position);
populateView(view, item);

} catch (Throwable e) {
Expand Down Expand Up @@ -359,6 +356,15 @@ protected boolean onItemKeyMaster(View v) {
}

protected void onItemChecked(CompoundButton v, boolean isChecked) {
T item = (T) v.getTag();
if (item != null) {
if (v.isChecked() && !checked.contains(item)) {
checked.add(item);
} else {
checked.remove(item);
}
}

if (onItemCheckedListener != null) {
onItemCheckedListener.onItemChecked(v, isChecked);
}
Expand Down Expand Up @@ -418,15 +424,14 @@ protected Dialog trackDialog(Dialog dialog) {
*
* @see #getChecked()
*/
protected void initCheckBox(View view, ItemTag itemTag) {
protected void initCheckBox(View view, T item) {
CheckBox checkbox = findView(view, R.id.view_selectable_list_item_checkbox);
if (checkbox != null) {
checkbox.setVisibility((checkboxesVisibility) ? View.VISIBLE : View.GONE);
if (checkbox.getVisibility() == View.VISIBLE) {
printCheckedItems();
checkbox.setOnCheckedChangeListener(null);
checkbox.setChecked(checked.contains(itemTag.item));
checkbox.setTag(itemTag);
checkbox.setChecked(checked.contains(item));
checkbox.setTag(item);
checkbox.setOnCheckedChangeListener(checkboxOnCheckedChangeListener);
}
}
Expand Down Expand Up @@ -516,37 +521,10 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
private final class CheckboxOnCheckedChangeListener implements OnCheckedChangeListener {
@SuppressWarnings("unchecked")
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Object tag = buttonView.getTag();
T item = (T) tag;
if (tag.getClass().equals(ItemTag.class)) {
item = ((ItemTag) tag).item;
}
if (buttonView.isChecked() && !checked.contains(item)) {
checked.add(item);
} else {
checked.remove(item);
}
onItemChecked(buttonView, isChecked);
}
}

private void printCheckedItems() {
if (checked == null || checked.isEmpty()) {
LOG.info("{no checked items}");
return;
}

LOG.info(checked.size() + " checked items:");
for (T c : checked) {
if (c.getClass().equals(ItemTag.class)) {
ItemTag itemTag = (ItemTag) c;
LOG.info(itemTag.position + " -> [" + itemTag.item.toString() + "]");
} else {
LOG.info("[checked] -> [" + c.toString() + "]");
}
}
}

private final class AbstractListAdapterFilter extends Filter {

private final AbstractListAdapter<T> adapter;
Expand Down Expand Up @@ -598,37 +576,41 @@ private final class RadioButtonOnCheckedChangeListener implements OnCheckedChang
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView instanceof RadioButton && isChecked){
RadioButton radioButton = (RadioButton) buttonView;
ItemTag tag = (ItemTag) radioButton.getTag();
T item = (T) radioButton.getTag();
int position = (item == null) ? 0 : getList().indexOf(item);

if(Ref.alive(lastRadioButtonChecked)){
lastRadioButtonChecked.get().setChecked(false);
Ref.free(lastRadioButtonChecked);
}

lastSelectedRadioButtonIndex = tag.position;
lastSelectedRadioButtonIndex = position;
radioButton.setOnCheckedChangeListener(null);
radioButton.setChecked(true);
radioButton.setOnCheckedChangeListener(this);
lastRadioButtonChecked = new WeakReference<>(radioButton);
}
}
}

protected final class ItemTag {
T item;
int position;

public ItemTag(T item, int position) {
this.item = item;
this.position = position;
public void updateLastRadioButtonChecked(RadioButton radioButton) {
if(Ref.alive(lastRadioButtonChecked)){
Ref.free(lastRadioButtonChecked);
}
lastRadioButtonChecked = new WeakReference<>(radioButton);
}
}

protected void initRadioButton(View view, ItemTag tag) {
protected void initRadioButton(View view, T tag, int position) {
RadioButton radioButton = findView(view, R.id.view_selectable_list_item_radiobutton);
if (radioButton != null) {
radioButton.setVisibility(View.VISIBLE);
radioButton.setTag(tag);
radioButton.setOnCheckedChangeListener(radioButtonCheckedChangeListener);
radioButton.setChecked(tag.position == lastSelectedRadioButtonIndex);
radioButton.setChecked(position == lastSelectedRadioButtonIndex);

if (position == lastSelectedRadioButtonIndex) {
radioButtonCheckedChangeListener.updateLastRadioButtonChecked(radioButton);
}
}
}

Expand Down

0 comments on commit d242bf1

Please sign in to comment.