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

View file

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

View file

@ -1,16 +1,18 @@
package eu.siacs.conversations.ui; 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.support.v7.app.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView; import android.widget.AutoCompleteTextView;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.List; import java.util.List;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
@ -19,41 +21,54 @@ import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
import eu.siacs.conversations.ui.util.DelayedHintHelper; import eu.siacs.conversations.ui.util.DelayedHintHelper;
import rocks.xmpp.addr.Jid; import rocks.xmpp.addr.Jid;
public class EnterJidDialog { public class EnterJidDialog extends DialogFragment{
public interface OnEnterJidDialogPositiveListener {
boolean onEnterJidDialogPositive(Jid account, Jid contact) throws EnterJidDialog.JidError;
}
public static class JidError extends Exception { private OnEnterJidDialogPositiveListener mListener = null;
final String msg;
public JidError(final String msg) { private static final String TITLE_KEY = "title";
this.msg = msg; 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() { public static EnterJidDialog newInstance(
return msg; Collection<String> knownHosts, final List<String> activatedAccounts,
}
}
protected final AlertDialog dialog;
protected View.OnClickListener dialogOnClick;
protected OnEnterJidDialogPositiveListener listener = null;
public EnterJidDialog(
final Context context, Collection<String> knownHosts, final List<String> activatedAccounts,
final String title, final String positiveButton, final String title, final String positiveButton,
final String prefilledJid, final String account, boolean allowEditJid final String prefilledJid, final String account, boolean allowEditJid) {
) { EnterJidDialog dialog = new EnterJidDialog();
AlertDialog.Builder builder = new AlertDialog.Builder(context); Bundle bundle = new Bundle();
builder.setTitle(title); bundle.putString(TITLE_KEY, title);
View dialogView = LayoutInflater.from(context).inflate(R.layout.enter_jid_dialog, null); 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 Spinner spinner = dialogView.findViewById(R.id.account);
final AutoCompleteTextView jid = dialogView.findViewById(R.id.jid); 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) { if (prefilledJid != null) {
jid.append(prefilledJid); jid.append(prefilledJid);
if (!allowEditJid) { if (!getArguments().getBoolean(ALLOW_EDIT_JID_KEY)) {
jid.setFocusable(false); jid.setFocusable(false);
jid.setFocusableInTouchMode(false); jid.setFocusableInTouchMode(false);
jid.setClickable(false); jid.setClickable(false);
@ -63,10 +78,11 @@ public class EnterJidDialog {
DelayedHintHelper.setHint(R.string.account_settings_example_jabber_id,jid); DelayedHintHelper.setHint(R.string.account_settings_example_jabber_id,jid);
String account = getArguments().getString(ACCOUNT_KEY);
if (account == null) { if (account == null) {
StartConversationActivity.populateAccountSpinner(context, activatedAccounts, spinner); StartConversationActivity.populateAccountSpinner(getActivity(), getArguments().getStringArrayList(ACCOUNTS_LIST_KEY), spinner);
} else { } else {
ArrayAdapter<String> adapter = new ArrayAdapter<>(context, ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
R.layout.simple_list_item, R.layout.simple_list_item,
new String[] { account }); new String[] { account });
spinner.setEnabled(false); spinner.setEnabled(false);
@ -76,10 +92,10 @@ public class EnterJidDialog {
builder.setView(dialogView); builder.setView(dialogView);
builder.setNegativeButton(R.string.cancel, null); builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(positiveButton, null); builder.setPositiveButton(getArguments().getString(POSITIVE_BUTTON_KEY), null);
this.dialog = builder.create(); AlertDialog dialog = builder.create();
this.dialogOnClick = v -> { View.OnClickListener dialogOnClick = v -> {
final Jid accountJid; final Jid accountJid;
if (!spinner.isEnabled() && account == null) { if (!spinner.isEnabled() && account == null) {
return; return;
@ -97,13 +113,13 @@ public class EnterJidDialog {
try { try {
contactJid = Jid.of(jid.getText().toString()); contactJid = Jid.of(jid.getText().toString());
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
jid.setError(context.getString(R.string.invalid_jid)); jid.setError(getActivity().getString(R.string.invalid_jid));
return; return;
} }
if(listener != null) { if(mListener != null) {
try { try {
if(listener.onEnterJidDialogPositive(accountJid, contactJid)) { if(mListener.onEnterJidDialogPositive(accountJid, contactJid)) {
dialog.dismiss(); dialog.dismiss();
} }
} catch(JidError error) { } 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) { public void setOnEnterJidDialogPositiveListener(OnEnterJidDialogPositiveListener listener) {
this.listener = listener; this.mListener = listener;
} }
public Dialog show() { public interface OnEnterJidDialogPositiveListener {
this.dialog.show(); boolean onEnterJidDialogPositive(Jid account, Jid contact) throws EnterJidDialog.JidError;
this.dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(this.dialogOnClick); }
return this.dialog;
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") @SuppressLint("InflateParams")
protected void showCreateContactDialog(final String prefilledJid, final Invite invite) { protected void showCreateContactDialog(final String prefilledJid, final Invite invite) {
EnterJidDialog dialog = new EnterJidDialog( EnterJidDialog dialog = EnterJidDialog.newInstance(
this, mKnownHosts, mActivatedAccounts, mKnownHosts, mActivatedAccounts,
getString(R.string.dialog_title_create_contact), getString(R.string.create), getString(R.string.dialog_title_create_contact), getString(R.string.create),
prefilledJid, null, invite == null || !invite.hasFingerprints() prefilledJid, null, invite == null || !invite.hasFingerprints()
); );
@ -474,7 +474,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
return true; return true;
} }
}); });
dialog.show(); dialog.show(getSupportFragmentManager(), "create_contact_dialog");
} }
@SuppressLint("InflateParams") @SuppressLint("InflateParams")