place enter jid dialog in dialog fragment

This commit is contained in:
iamharsshit 2018-03-12 16:42:51 +05:30
parent b48e37f72f
commit b33725b435
4 changed files with 89 additions and 52 deletions

View File

@ -62,8 +62,8 @@ public class BlocklistActivity extends AbstractSearchableListItemActivity implem
}
protected void showEnterJidDialog() {
EnterJidDialog dialog = new EnterJidDialog(
this, mKnownHosts, null,
EnterJidDialog dialog = EnterJidDialog.newInstance(
mKnownHosts, null,
getString(R.string.block_jabber_id), getString(R.string.block),
null, account.getJid().asBareJid().toString(), true
);
@ -76,7 +76,7 @@ public class BlocklistActivity extends AbstractSearchableListItemActivity implem
return true;
});
dialog.show();
dialog.show(getSupportFragmentManager(), "block_contact_dialog");
}
protected void refreshUiReal() {

View File

@ -231,8 +231,7 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity {
protected void showEnterJidDialog(XmppUri uri) {
Jid jid = uri == null ? null : uri.getJid();
EnterJidDialog dialog = new EnterJidDialog(
this,
EnterJidDialog dialog = EnterJidDialog.newInstance(
mKnownHosts,
mActivatedAccounts,
getString(R.string.enter_contact),
@ -257,7 +256,7 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity {
return true;
});
dialog.show();
dialog.show(getSupportFragmentManager(), "enter_contact_dialog");
}
@Override

View File

@ -1,16 +1,18 @@
package eu.siacs.conversations.ui;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import eu.siacs.conversations.Config;
@ -19,41 +21,54 @@ import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
import eu.siacs.conversations.ui.util.DelayedHintHelper;
import rocks.xmpp.addr.Jid;
public class EnterJidDialog {
public interface OnEnterJidDialogPositiveListener {
boolean onEnterJidDialogPositive(Jid account, Jid contact) throws EnterJidDialog.JidError;
}
public class EnterJidDialog extends DialogFragment{
public static class JidError extends Exception {
final String msg;
private OnEnterJidDialogPositiveListener mListener = null;
public JidError(final String msg) {
this.msg = msg;
}
private static final String TITLE_KEY = "title";
private static final String POSITIVE_BUTTON_KEY = "positive_button";
private static final String PREFILLED_JID_KEY = "prefilled_jid";
private static final String ACCOUNT_KEY = "account";
private static final String ALLOW_EDIT_JID_KEY = "allow_edit_jid";
private static final String ACCOUNTS_LIST_KEY = "activated_accounts_list";
private static final String CONFERENCE_HOSTS_KEY = "known_conference_hosts";
public String toString() {
return msg;
}
}
protected final AlertDialog dialog;
protected View.OnClickListener dialogOnClick;
protected OnEnterJidDialogPositiveListener listener = null;
public EnterJidDialog(
final Context context, Collection<String> knownHosts, final List<String> activatedAccounts,
public static EnterJidDialog newInstance(
Collection<String> knownHosts, final List<String> activatedAccounts,
final String title, final String positiveButton,
final String prefilledJid, final String account, boolean allowEditJid
) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
View dialogView = LayoutInflater.from(context).inflate(R.layout.enter_jid_dialog, null);
final String prefilledJid, final String account, boolean allowEditJid) {
EnterJidDialog dialog = new EnterJidDialog();
Bundle bundle = new Bundle();
bundle.putString(TITLE_KEY, title);
bundle.putString(POSITIVE_BUTTON_KEY, positiveButton);
bundle.putString(PREFILLED_JID_KEY, prefilledJid);
bundle.putString(ACCOUNT_KEY, account);
bundle.putBoolean(ALLOW_EDIT_JID_KEY, allowEditJid);
bundle.putStringArrayList(ACCOUNTS_LIST_KEY, (ArrayList<String>) activatedAccounts);
bundle.putSerializable(CONFERENCE_HOSTS_KEY, (HashSet) knownHosts);
dialog.setArguments(bundle);
return dialog;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(getArguments().getString(TITLE_KEY));
View dialogView = getActivity().getLayoutInflater().inflate(R.layout.enter_jid_dialog, null);
final Spinner spinner = dialogView.findViewById(R.id.account);
final AutoCompleteTextView jid = dialogView.findViewById(R.id.jid);
jid.setAdapter(new KnownHostsAdapter(context, R.layout.simple_list_item, knownHosts));
jid.setAdapter(new KnownHostsAdapter(getActivity(), R.layout.simple_list_item, (Collection<String>) getArguments().getSerializable(CONFERENCE_HOSTS_KEY)));
String prefilledJid = getArguments().getString(PREFILLED_JID_KEY);
if (prefilledJid != null) {
jid.append(prefilledJid);
if (!allowEditJid) {
if (!getArguments().getBoolean(ALLOW_EDIT_JID_KEY)) {
jid.setFocusable(false);
jid.setFocusableInTouchMode(false);
jid.setClickable(false);
@ -63,10 +78,11 @@ public class EnterJidDialog {
DelayedHintHelper.setHint(R.string.account_settings_example_jabber_id,jid);
String account = getArguments().getString(ACCOUNT_KEY);
if (account == null) {
StartConversationActivity.populateAccountSpinner(context, activatedAccounts, spinner);
StartConversationActivity.populateAccountSpinner(getActivity(), getArguments().getStringArrayList(ACCOUNTS_LIST_KEY), spinner);
} else {
ArrayAdapter<String> adapter = new ArrayAdapter<>(context,
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
R.layout.simple_list_item,
new String[] { account });
spinner.setEnabled(false);
@ -76,10 +92,10 @@ public class EnterJidDialog {
builder.setView(dialogView);
builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(positiveButton, null);
this.dialog = builder.create();
builder.setPositiveButton(getArguments().getString(POSITIVE_BUTTON_KEY), null);
AlertDialog dialog = builder.create();
this.dialogOnClick = v -> {
View.OnClickListener dialogOnClick = v -> {
final Jid accountJid;
if (!spinner.isEnabled() && account == null) {
return;
@ -97,13 +113,13 @@ public class EnterJidDialog {
try {
contactJid = Jid.of(jid.getText().toString());
} catch (final IllegalArgumentException e) {
jid.setError(context.getString(R.string.invalid_jid));
jid.setError(getActivity().getString(R.string.invalid_jid));
return;
}
if(listener != null) {
if(mListener != null) {
try {
if(listener.onEnterJidDialogPositive(accountJid, contactJid)) {
if(mListener.onEnterJidDialogPositive(accountJid, contactJid)) {
dialog.dismiss();
}
} catch(JidError error) {
@ -111,15 +127,37 @@ public class EnterJidDialog {
}
}
};
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(dialogOnClick);
return dialog;
}
public void setOnEnterJidDialogPositiveListener(OnEnterJidDialogPositiveListener listener) {
this.listener = listener;
this.mListener = listener;
}
public Dialog show() {
this.dialog.show();
this.dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(this.dialogOnClick);
return this.dialog;
public interface OnEnterJidDialogPositiveListener {
boolean onEnterJidDialogPositive(Jid account, Jid contact) throws EnterJidDialog.JidError;
}
public static class JidError extends Exception {
final String msg;
public JidError(final String msg) {
this.msg = msg;
}
public String toString() {
return msg;
}
}
@Override
public void onDestroyView() {
Dialog dialog = getDialog();
if (dialog != null && getRetainInstance()) {
dialog.setDismissMessage(null);
}
super.onDestroyView();
}
}

View File

@ -440,8 +440,8 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
@SuppressLint("InflateParams")
protected void showCreateContactDialog(final String prefilledJid, final Invite invite) {
EnterJidDialog dialog = new EnterJidDialog(
this, mKnownHosts, mActivatedAccounts,
EnterJidDialog dialog = EnterJidDialog.newInstance(
mKnownHosts, mActivatedAccounts,
getString(R.string.dialog_title_create_contact), getString(R.string.create),
prefilledJid, null, invite == null || !invite.hasFingerprints()
);
@ -474,7 +474,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
return true;
}
});
dialog.show();
dialog.show(getSupportFragmentManager(), "create_contact_dialog");
}
@SuppressLint("InflateParams")