Skip to content

Commit

Permalink
[android] Adding bases for radiobutton support in confirm dialogs. [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
votaguz authored and gubatron committed Mar 23, 2016
1 parent 39997cd commit 0409298
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 48 deletions.
70 changes: 46 additions & 24 deletions android/res/layout/dialog_confirm_list.xml
Original file line number Diff line number Diff line change
@@ -1,38 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
/*
* Created by Angel Leon (@gubatron), Alden Torres (aldenml)
* Copyright (c) 2011, 2014, FrostWire(TM). All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
* Created by Angel Leon (@gubatron), Alden Torres (aldenml)
* Copyright (c) 2011, 2014, FrostWire(TM). All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="0dp"
android:scrollbars="vertical" >

<TextView
android:id="@+id/dialog_confirm_list_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="this is a parameter"
android:textSize="18sp"/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/dialog_confirm_list_title_icon"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:layout_margin="2dp"
android:src="@drawable/download_icon" />

<TextView
android:id="@+id/dialog_confirm_list_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="this is a parameter"
android:textSize="18sp"/>
</LinearLayout>

<CheckBox
android:id="@+id/dialog_confirm_list_select_all_checkbox"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/select_all"
android:visibility="gone"
/>

<com.frostwire.android.gui.views.ListView
android:id="@+id/dialog_confirm_list_list"
android:layout_weight="1"
Expand Down
1 change: 1 addition & 0 deletions android/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<string name="max_concurrent_uploads">Maximum Simultaneous Uploads</string>
<string name="search_no_keywords">Please enter search keywords</string>
<string name="search_select_filetype">Please select a file type</string>
<string name="select_all">Select All</string>
<string name="searching_for">Searching for</string>
<string name="search_for_type_files">Search for %s files</string>
<string name="update_title">Update time!</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;

Expand Down Expand Up @@ -114,7 +116,7 @@ protected void prepareArguments(String dialogTitle, String dialogText, String li

@Override
protected void initComponents(Dialog dlg, Bundle savedInstanceState) {
//TODO: Add checkbox on list item and make sure the adapter
//TODO: Make sure the adapter
//will know what items have been selected and that we can
//reinit the right checkboxes selected here when orientation change occurs.

Expand All @@ -127,6 +129,21 @@ protected void initComponents(Dialog dlg, Bundle savedInstanceState) {
textView.setText(dialogText);


if (selectionMode == SelectionMode.MULTIPLE_SELECTION){
CheckBox checkBox = findView(dlg, R.id.dialog_confirm_list_select_all_checkbox);
checkBox.setVisibility(View.VISIBLE);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(buttonView.isChecked()){
adapter.checkAll();
} else {
adapter.clearChecked();
}
}
});
}

ListView listView = findView(dlg, R.id.dialog_confirm_list_list);
String listDataString = bundle.getString("listData");
List<T> listData = deserializeData(listDataString);
Expand Down Expand Up @@ -214,9 +231,7 @@ public boolean[] getSelected() {
LOGGER.warn("getSelected() is not finding the checked items on the list. Verify your classes implement equals() and hashCode()");
}
}

}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.frostwire.android.R;
Expand Down Expand Up @@ -54,6 +52,7 @@
*/
public class ConfirmListDialogDefaultAdapter<T extends SearchResult> extends AbstractListAdapter {
private static final Logger LOGGER = Logger.getLogger(ConfirmListDialogDefaultAdapter.class);
private boolean radioButtonVisibility;
private static final int ITEM_TITLE = 0;
private static final int ITEM_SIZE = 1;
private static final int ITEM_ART = 2;
Expand Down Expand Up @@ -95,28 +94,24 @@ public View getView(int position, View view, ViewGroup parent) {
Context ctx = getContext();

if (view == null && ctx != null) {
int layoutId = selectionModeToLayoutId.get(selectionMode).intValue();
// every list view item is wrapped in a generic container which has a hidden checkbox on the left hand side.
int layoutId = selectionModeToLayoutId.get(selectionMode);
view = View.inflate(ctx, layoutId, null);
//LinearLayout container = findView(view, R.id.view_selectable_list_item_container);
//View.inflate(ctx, viewItemId, container);
}

try {

initTouchFeedback(view, item);
setCheckboxesVisibility(selectionMode == SelectionMode.MULTIPLE_SELECTION);
initCheckBox(view, item);
//TODO: setRadioButtonsVisibility(selectionMode == SelectionMode.SINGLE_SELECTION);
//TODO: initRadioButton(view, item);
if(selectionMode == SelectionMode.MULTIPLE_SELECTION) {
initCheckBox(view, item);
setCheckboxesVisibility(selectionMode == SelectionMode.MULTIPLE_SELECTION);
} else if (selectionMode == SelectionMode.SINGLE_SELECTION) {
initRadioButton(view, new RadioButtonTag((T) item, position));
}
populateView(view, item);

} catch (Throwable e) {
LOGGER.error("Fatal error getting view: " + e.getMessage(), e);
}

return view;

}

@Override
Expand All @@ -141,4 +136,5 @@ protected void populateView(View view, Object data) {
LOGGER.warn("populateView(data): data is not a SearchResult instance ("+data.getClass().getSimpleName()+")");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void onClick(View v) {
// TODO: If results is empty, then we should probably trigger an error message in the dialog
// if (results == null || results.isEmpty()) { dlg.displayErrorNotice(ERROR_CODE); return; }
}
startDownloads(ctxRef.get(), results);
startDownloads(ctxRef.get(), results);
dlg.dismiss();
}
}
Expand All @@ -172,6 +172,7 @@ public ConfirmSoundcloudDownloadDialog(SelectionMode selectionMode) {
public static ConfirmSoundcloudDownloadDialog newInstance(String dialogTitle,
String dialogText,
List<SoundcloudSearchResult> listData) {

ConfirmSoundcloudDownloadDialog dlg = new ConfirmSoundcloudDownloadDialog(SelectionMode.MULTIPLE_SELECTION);
SoundcloudSearchResultList srList = new SoundcloudSearchResultList();
srList.listData = listData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,25 @@
import android.view.View.OnKeyListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.*;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.frostwire.android.R;
import com.frostwire.logging.Logger;
import com.frostwire.util.Ref;

import java.util.*;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* We extend from ListAdapter to populate our ListViews.
Expand Down Expand Up @@ -64,6 +77,8 @@ public abstract class AbstractListAdapter<T> extends BaseAdapter implements Filt
protected Set<T> checked;
protected List<T> visualList;

protected int lastSelectedRadioButtonIndex = 0;

public AbstractListAdapter(Context context, int viewItemId, List<T> list, Set<T> checked) {
this.context = context;
this.viewItemId = viewItemId;
Expand Down Expand Up @@ -255,12 +270,12 @@ public List<T> getList() {

/**
* Inflates the view out of the XML.
* <p>
* <p/>
* Sets click and long click listeners in case you need them. (Override onItemClicked and onItemLongClicked)
* <p>
* <p/>
* Let's the adapter know that the view has been created, in case you need to go deeper and create
* more advanced click behavior or even add new Views during runtime.
* <p>
* <p/>
* 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) {
Expand All @@ -279,7 +294,7 @@ public View getView(int position, View view, ViewGroup parent) {

initTouchFeedback(view, item);
initCheckBox(view, item);
//TODO: initRadioButton(view, item);
initRadioButton(view, new RadioButtonTag(item, position));
populateView(view, item);

} catch (Throwable e) {
Expand Down Expand Up @@ -396,7 +411,7 @@ protected Dialog trackDialog(Dialog dialog) {

/**
* Sets up the behavior of a possible checkbox to check this item.
* <p>
* <p/>
* Takes in consideration:
* - Only so many views are created and reused by the ListView
* - Setting the correct checked/unchecked value without triggering the onCheckedChanged event.
Expand Down Expand Up @@ -541,4 +556,61 @@ protected void publishResults(CharSequence constraint, FilterResults results) {
}

}

private final class RadioButtonOnCheckedChangeListener implements OnClickListener {

private final AbstractListAdapter adapter;
private WeakReference<RadioButton> lastRadioButtonChecked = null;

public RadioButtonOnCheckedChangeListener(AbstractListAdapter adapter) {
this.adapter = adapter;
System.out.println("Adding checked listener");
}

@Override
public void onClick(View v) {
System.out.println("On Click Event");
if (v instanceof RadioButton) {
RadioButton radioButton = (RadioButton) v;
RadioButtonTag tag = (RadioButtonTag) radioButton.getTag();

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

lastSelectedRadioButtonIndex = tag.position;
radioButton.setChecked(true);

Ref.free(lastRadioButtonChecked);
lastRadioButtonChecked = new WeakReference<RadioButton>(radioButton);
}
}
}

protected final class RadioButtonTag {
T item;
int position;

public RadioButtonTag(T item, int position) {
this.item = item;
this.position = position;
}
}

protected void initRadioButton(View view, RadioButtonTag tag) {
RadioButton radioButton = (RadioButton) findView(view, R.id.view_selectable_list_item_radiobutton);

if (radioButton != null) {
radioButton.setVisibility(View.VISIBLE);
radioButton.setChecked(false);
radioButton.setTag(tag);

if (!radioButton.hasOnClickListeners()) {
radioButton.setOnClickListener(new RadioButtonOnCheckedChangeListener(this));
}

radioButton.setChecked(tag.position == lastSelectedRadioButtonIndex);
}
}

}

0 comments on commit 0409298

Please sign in to comment.