Commit 52d0e56d authored by Alexander Ivanov's avatar Alexander Ivanov

Implement group add using DialogFragment.

parent de3f566b
......@@ -205,7 +205,6 @@ public class ContactAdd extends GroupListActivity implements
@Override
public void onAccept(DialogBuilder dialogBuilder) {
super.onAccept(dialogBuilder);
switch (dialogBuilder.getDialogId()) {
case DIALOG_SUBSCRIPTION_REQUEST_ID:
try {
......@@ -222,7 +221,6 @@ public class ContactAdd extends GroupListActivity implements
@Override
public void onDecline(DialogBuilder dialogBuilder) {
super.onDecline(dialogBuilder);
switch (dialogBuilder.getDialogId()) {
case DIALOG_SUBSCRIPTION_REQUEST_ID:
try {
......@@ -239,7 +237,6 @@ public class ContactAdd extends GroupListActivity implements
@Override
public void onCancel(DialogBuilder dialogBuilder) {
super.onCancel(dialogBuilder);
switch (dialogBuilder.getDialogId()) {
case DIALOG_SUBSCRIPTION_REQUEST_ID:
finish();
......
......@@ -18,7 +18,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
......@@ -30,9 +29,8 @@ import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.xabber.android.ui.dialog.ConfirmDialogListener;
import com.xabber.android.ui.dialog.DialogBuilder;
import com.xabber.android.ui.dialog.GroupAddDialogBuilder;
import com.xabber.android.ui.dialog.GroupAddDialogFragment;
import com.xabber.android.ui.dialog.GroupAddDialogFragment.OnGroupAddConfirmed;
import com.xabber.android.ui.helper.ManagedListActivity;
import com.xabber.androiddev.R;
......@@ -43,15 +41,13 @@ import com.xabber.androiddev.R;
*
*/
public abstract class GroupListActivity extends ManagedListActivity implements
ConfirmDialogListener, OnItemClickListener {
OnItemClickListener, OnGroupAddConfirmed {
private static final String SAVED_GROUPS = "com.xabber.android.ui.ContactList.SAVED_GROUPS";
private static final String SAVED_SELECTED = "com.xabber.android.ui.ContactList.SAVED_SELECTED";
static final int OPTION_MENU_ADD_GROUP_ID = 1;
static final int DIALOG_ADD_GROUP_ID = 0x10;
private ArrayAdapter<String> arrayAdapter;
private ListView listView;
......@@ -148,29 +144,22 @@ public abstract class GroupListActivity extends ManagedListActivity implements
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case OPTION_MENU_ADD_GROUP_ID:
showDialog(DIALOG_ADD_GROUP_ID);
showGroupAddDialog();
return true;
}
return false;
}
@Override
protected Dialog onCreateDialog(int id) {
super.onCreateDialog(id);
switch (id) {
case DIALOG_ADD_GROUP_ID:
return new GroupAddDialogBuilder(this, DIALOG_ADD_GROUP_ID, this,
getGroups()).create();
default:
return null;
}
private void showGroupAddDialog() {
GroupAddDialogFragment.newInstance(getGroups()).show(
getSupportFragmentManager(), "GROUP-ADD");
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (listView.getItemAtPosition(position) == null) // Footer
showDialog(DIALOG_ADD_GROUP_ID);
showGroupAddDialog();
}
/**
......@@ -192,24 +181,12 @@ public abstract class GroupListActivity extends ManagedListActivity implements
}
@Override
public void onAccept(DialogBuilder dialog) {
switch (dialog.getDialogId()) {
case DIALOG_ADD_GROUP_ID:
String group = ((GroupAddDialogBuilder) dialog).getName();
ArrayList<String> groups = getGroups();
groups.add(group);
ArrayList<String> selected = getSelected();
selected.add(group);
setGroups(groups, selected);
}
}
@Override
public void onDecline(DialogBuilder dialog) {
}
@Override
public void onCancel(DialogBuilder dialog) {
public void onGroupAddConfirmed(String group) {
ArrayList<String> groups = getGroups();
groups.add(group);
ArrayList<String> selected = getSelected();
selected.add(group);
setGroups(groups, selected);
}
}
......@@ -14,6 +14,8 @@
*/
package com.xabber.android.ui.dialog;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
......@@ -45,6 +47,12 @@ public abstract class AbstractDialogFragment extends DialogFragment {
return this;
}
protected AbstractDialogFragment putAgrument(String key,
ArrayList<String> value) {
initArguments().putStringArrayList(key, value);
return this;
}
protected AbstractDialogFragment putAgrument(String key, boolean value) {
initArguments().putBoolean(key, value);
return this;
......
......@@ -14,51 +14,68 @@
*/
package com.xabber.android.ui.dialog;
import java.util.Collection;
import java.util.ArrayList;
import android.app.Activity;
import android.content.DialogInterface;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.xabber.androiddev.R;
public class GroupAddDialogBuilder extends ConfirmDialogBuilder {
public class GroupAddDialogFragment extends ConfirmDialogFragment {
private final Collection<String> groups;
private final EditText nameView;
private static final String GROUPS = "GROUPS";
public GroupAddDialogBuilder(Activity activity, int dialogId,
ConfirmDialogListener listener, Collection<String> groups) {
super(activity, dialogId, listener);
setTitle(R.string.group_add);
this.groups = groups;
View layout = activity.getLayoutInflater().inflate(R.layout.group_name,
null);
/**
* @param account
* can be <code>null</code> to be used for all accounts.
* @param group
* can be <code>null</code> to be used for "no group".
* @return
*/
public static DialogFragment newInstance(ArrayList<String> groups) {
return new GroupAddDialogFragment().putAgrument(GROUPS, groups);
}
private ArrayList<String> groups;
private EditText nameView;
@Override
protected Builder getBuilder() {
groups = getArguments().getStringArrayList(GROUPS);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.group_add);
View layout = getActivity().getLayoutInflater().inflate(
R.layout.group_name, null);
nameView = (EditText) layout.findViewById(R.id.group_name);
setView(layout);
builder.setView(layout);
return builder;
}
@Override
public void onAccept(DialogInterface dialog) {
String name = nameView.getText().toString();
if ("".equals(name)) {
Toast.makeText(activity,
activity.getString(R.string.group_is_empty),
protected boolean onPositiveClick() {
String group = nameView.getText().toString();
if ("".equals(group)) {
Toast.makeText(getActivity(), getString(R.string.group_is_empty),
Toast.LENGTH_LONG).show();
return;
return false;
}
if (groups.contains(name)) {
Toast.makeText(activity, activity.getString(R.string.group_exists),
if (groups.contains(group)) {
Toast.makeText(getActivity(), getString(R.string.group_exists),
Toast.LENGTH_LONG).show();
return;
return false;
}
super.onAccept(dialog);
((OnGroupAddConfirmed) getActivity()).onGroupAddConfirmed(group);
return true;
}
public String getName() {
return nameView.getText().toString();
public interface OnGroupAddConfirmed {
void onGroupAddConfirmed(String group);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment