Skip to content

Commit

Permalink
[android] #WIP work on ConfirmListDialog (checkboxes, radiobuttons)
Browse files Browse the repository at this point in the history
  • Loading branch information
votaguz authored and gubatron committed Mar 10, 2016
1 parent 783a284 commit 53cdf6a
Show file tree
Hide file tree
Showing 8 changed files with 406 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2014 Angel Leon, Alden Torres
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal" >

<CheckBox
android:id="@+id/view_selectable_list_item_checkbox"
android:layout_width="wrap_content"
android:layout_height="match_parent" />

<ImageView
android:id="@+id/confirmation_dialog_multiple_selection_list_item_art"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center|center_vertical"
android:layout_margin="2dp"
android:src="@drawable/app_icon" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1.0">

<TextView
android:id="@+id/confirmation_dialog_multiple_selection_list_item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:layout_gravity="center_vertical|left" />

<TextView
android:id="@+id/confirmation_dialog_multiple_selection_list_item_size"
android:layout_gravity="center_vertical|right"
android:layout_width="50dp"
android:layout_height="wrap_content" />

</LinearLayout>


</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,36 @@
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal" >

<!-- TODO: Checkboxes for selection support -->

<ImageView
android:id="@+id/list_item_track_confirmation_dialog_art"
android:id="@+id/confirmation_dialog_no_selection_list_item_art"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center|center_vertical"
android:layout_margin="2dp"
android:src="@drawable/app_icon" />

<TextView
android:id="@+id/list_item_track_confirmation_dialog_track_title"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:layout_gravity="center_vertical|left"
android:layout_weight="1.0" />
android:orientation="vertical"
android:layout_weight="1.0">

<TextView
android:id="@+id/confirmation_dialog_no_selection_list_item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:layout_gravity="center_vertical|left" />

<TextView
android:id="@+id/confirmation_dialog_no_selection_list_item_size"
android:layout_gravity="center_vertical|right"
android:layout_width="50dp"
android:layout_height="wrap_content" />

</LinearLayout>

<TextView
android:id="@+id/list_item_track_confirmation_dialog_file_size_in_human"
android:layout_gravity="center_vertical|right"
android:layout_width="50dp"
android:layout_height="wrap_content" />

</LinearLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2014 Angel Leon, Alden Torres
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal" >

<RadioButton
android:id="@+id/view_selectable_list_item_radiobutton"
android:layout_width="wrap_content"
android:layout_height="match_parent" />

<ImageView
android:id="@+id/confirmation_dialog_single_selection_list_item_art"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center|center_vertical"
android:layout_margin="2dp"
android:src="@drawable/app_icon" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1.0">

<TextView
android:id="@+id/confirmation_dialog_single_selection_list_item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:layout_gravity="center_vertical|left" />

<TextView
android:id="@+id/confirmation_dialog_single_selection_list_item_size"
android:layout_gravity="center_vertical|right"
android:layout_width="50dp"
android:layout_height="wrap_content" />

</LinearLayout>


</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

package com.frostwire.android.gui.dialogs;

import java.util.List;

import android.app.Dialog;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
Expand All @@ -31,7 +29,13 @@

import com.frostwire.android.R;
import com.frostwire.android.gui.views.AbstractDialog;
import com.frostwire.android.gui.views.AbstractListAdapter;
import com.frostwire.logging.Logger;
import com.frostwire.search.SearchResult;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
* This dialog should evolve to allow us for reuse on a number of situations in which you
Expand All @@ -46,44 +50,68 @@
*
* @author aldenml
* @author gubatron
*
* @param <Adapter>
* @param <I>
* @author votaguz
*/
public abstract class ConfirmListDialog<Adapter extends AbstractListAdapter<?>,T> extends AbstractDialog {
public abstract class AbstractConfirmListDialog<T extends SearchResult> extends AbstractDialog {

/**
* TODOS: 1. Add an optional text filter control that will be connected to the adapter.
* 2. Add list selection-mode flags: SINGLE_SELECTION, MULTIPLE_SELECTION, NO_SELECTION (current)
*/

Logger LOGGER = Logger.getLogger(AbstractConfirmListDialog.class);

public enum SelectionMode {
NO_SELECTION,
SINGLE_SELECTION,
MULTIPLE_SELECTION,
}

private final static String TAG = "confirm_list_dialog";
private String title;
private String dialogText;
private final SelectionMode selectionMode;
private OnCancelListener onCancelListener;
private OnClickListener onYesListener;

public ConfirmListDialog() {
private ConfirmListDialogDefaultAdapter<T> customAdapter;
private ConfirmListDialogDefaultAdapter<T> adapter;

abstract protected OnClickListener createOnYesListener(AbstractConfirmListDialog dlg);

/** rebuilds list of objects from json and does listView.setAdapter(YourAdapter(theObjectList)) */
abstract public List<T> deserializeData(String listDataInJSON);

public AbstractConfirmListDialog(SelectionMode selectionMode,
ConfirmListDialogDefaultAdapter customAdapter) {
super(TAG, R.layout.dialog_confirm_list);
this.selectionMode = selectionMode;
this.customAdapter = customAdapter;
this.adapter = customAdapter;
}
public OnClickListener getOnYesListener() {
return onYesListener;

public AbstractConfirmListDialog(SelectionMode selectionMode) {
this(selectionMode, null);
}

abstract protected OnClickListener createOnYesListener(List<T> listData);

/** rebuilds list of objects from json and does listView.setAdapter(YourAdapter(theObjectList)) */
abstract protected List<T> initListAdapter(ListView listView, String listDataInJSON);

protected void prepareArguments(String dialogTitle, String dialogText, String listDataInJSON) {

public SelectionMode getSelectionMode() {
return selectionMode;
}

protected void prepareArguments(String dialogTitle,
String dialogText,
String listDataInJSON,
SelectionMode selectionMode) {
Bundle bundle = new Bundle();
bundle.putString("title", dialogTitle);
bundle.putString("dialogText", dialogText);
bundle.putString("listData", listDataInJSON);
bundle.putInt("selectionMode", selectionMode.ordinal());
setArguments(bundle);
}


protected void prepareArguments(String dialogTitle, String dialogText, String listDataInJSON){
prepareArguments(dialogTitle, dialogText, listDataInJSON, selectionMode);
}

@Override
protected void initComponents(Dialog dlg, Bundle savedInstanceState) {
//TODO: Add checkbox on list item and make sure the adapter
Expand All @@ -97,10 +125,22 @@ protected void initComponents(Dialog dlg, Bundle savedInstanceState) {
dialogText = bundle.getString("dialogText");
TextView textView = findView(dlg, R.id.dialog_confirm_list_text);
textView.setText(dialogText);



ListView listView = findView(dlg, R.id.dialog_confirm_list_list);
String listDataString = bundle.getString("listData");
List<T> listData = initListAdapter(listView, listDataString);
List<T> listData = deserializeData(listDataString);

if (customAdapter != null) {
customAdapter.addList(listData);
adapter = customAdapter;
} else {
adapter = new ConfirmListDialogDefaultAdapter<>(getActivity(),
listData,
selectionMode);
}

listView.setAdapter(adapter);

final Dialog dialog = dlg;
Button noButton = findView(dialog, R.id.dialog_confirm_list_button_no);
Expand All @@ -118,16 +158,65 @@ public void onClick(View v) {
dialog.setOnCancelListener(onCancelListener);
}

onYesListener = createOnYesListener(listData);
onYesListener = createOnYesListener(this);

if (onYesListener != null) {
Button yesButton = findView(dialog, R.id.dialog_confirm_list_button_yes);
yesButton.setOnClickListener(onYesListener);
}
}

protected void setOnYesListener(OnClickListener listener) {
public void setOnYesListener(OnClickListener listener) {
onYesListener = listener;
}

public OnClickListener getOnYesListener() {
return onYesListener;
}

public Set<T> getChecked() {
Set<T> result = Collections.EMPTY_SET;
if (adapter != null) {
result = adapter.getChecked();
}
return result;
}

public List<T> getList() {
List<T> result = Collections.EMPTY_LIST;
if (adapter != null) {
result = adapter.getList();
}
return result;
}

public boolean[] getSelected() {
boolean[] result = new boolean[0];
if (adapter != null) {
List<T> checked = (List<T>) adapter.getChecked();

if (checked == null || checked.isEmpty()) {
return result;
}

result = new boolean[checked.size()];

List<T> all = adapter.getList();

Iterator<T> iterator = checked.iterator();

while (iterator.hasNext()) {
T item = iterator.next();
int i = all.indexOf(item);
if (i >= 0 && i < result.length) {
result[i]=true;
} else {
LOGGER.warn("getSelected() is not finding the checked items on the list. Verify your classes implement equals() and hashCode()");
}
}

}
return result;
}

}

0 comments on commit 53cdf6a

Please sign in to comment.