add data binding and check in back stack for dialogs

This commit is contained in:
iamharsshit 2018-03-14 11:50:44 +05:30
parent b33725b435
commit 2af12afb64
9 changed files with 179 additions and 143 deletions

View file

@ -1,6 +1,8 @@
package eu.siacs.conversations.ui; package eu.siacs.conversations.ui;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable; import android.text.Editable;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@ -62,6 +64,12 @@ public class BlocklistActivity extends AbstractSearchableListItemActivity implem
} }
protected void showEnterJidDialog() { protected void showEnterJidDialog() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
EnterJidDialog dialog = EnterJidDialog.newInstance( EnterJidDialog dialog = EnterJidDialog.newInstance(
mKnownHosts, null, mKnownHosts, null,
getString(R.string.block_jabber_id), getString(R.string.block), getString(R.string.block_jabber_id), getString(R.string.block),
@ -76,7 +84,7 @@ public class BlocklistActivity extends AbstractSearchableListItemActivity implem
return true; return true;
}); });
dialog.show(getSupportFragmentManager(), "block_contact_dialog"); dialog.show(ft, "dialog");
} }
protected void refreshUiReal() { protected void refreshUiReal() {

View file

@ -6,6 +6,8 @@ import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import android.view.ActionMode; import android.view.ActionMode;
import android.view.Menu; import android.view.Menu;
@ -230,6 +232,12 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity {
} }
protected void showEnterJidDialog(XmppUri uri) { protected void showEnterJidDialog(XmppUri uri) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
Jid jid = uri == null ? null : uri.getJid(); Jid jid = uri == null ? null : uri.getJid();
EnterJidDialog dialog = EnterJidDialog.newInstance( EnterJidDialog dialog = EnterJidDialog.newInstance(
mKnownHosts, mKnownHosts,
@ -256,7 +264,7 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity {
return true; return true;
}); });
dialog.show(getSupportFragmentManager(), "enter_contact_dialog"); dialog.show(ft, "dialog");
} }
@Override @Override

View file

@ -1,6 +1,7 @@
package eu.siacs.conversations.ui; package eu.siacs.conversations.ui;
import android.app.Dialog; import android.app.Dialog;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.content.Context; import android.content.Context;
@ -15,6 +16,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.databinding.CreateConferenceDialogBinding;
public class CreateConferenceDialog extends DialogFragment { public class CreateConferenceDialog extends DialogFragment {
@ -40,16 +42,14 @@ public class CreateConferenceDialog extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog_title_create_conference); builder.setTitle(R.string.dialog_title_create_conference);
final View dialogView = getActivity().getLayoutInflater().inflate(R.layout.create_conference_dialog, null); CreateConferenceDialogBinding binding = DataBindingUtil.inflate(getActivity().getLayoutInflater(), R.layout.create_conference_dialog, null, false);
final Spinner spinner = dialogView.findViewById(R.id.account);
final EditText subject = dialogView.findViewById(R.id.subject);
ArrayList<String> mActivatedAccounts = getArguments().getStringArrayList(ACCOUNTS_LIST_KEY); ArrayList<String> mActivatedAccounts = getArguments().getStringArrayList(ACCOUNTS_LIST_KEY);
StartConversationActivity.populateAccountSpinner(getActivity(), mActivatedAccounts, spinner); StartConversationActivity.populateAccountSpinner(getActivity(), mActivatedAccounts, binding.account);
builder.setView(dialogView); builder.setView(binding.getRoot());
builder.setPositiveButton(R.string.choose_participants, new DialogInterface.OnClickListener() { builder.setPositiveButton(R.string.choose_participants, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
mListener.onCreateDialogPositiveClick(spinner, subject.getText().toString()); mListener.onCreateDialogPositiveClick(binding.account, binding.subject.getText().toString());
} }
}); });
builder.setNegativeButton(R.string.cancel, null); builder.setNegativeButton(R.string.cancel, null);

View file

@ -1,5 +1,6 @@
package eu.siacs.conversations.ui; package eu.siacs.conversations.ui;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.os.Bundle; import android.os.Bundle;
@ -17,6 +18,7 @@ import java.util.List;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.databinding.EnterJidDialogBinding;
import eu.siacs.conversations.ui.adapter.KnownHostsAdapter; 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;
@ -61,59 +63,57 @@ public class EnterJidDialog extends DialogFragment{
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(getArguments().getString(TITLE_KEY)); builder.setTitle(getArguments().getString(TITLE_KEY));
View dialogView = getActivity().getLayoutInflater().inflate(R.layout.enter_jid_dialog, null); EnterJidDialogBinding binding = DataBindingUtil.inflate(getActivity().getLayoutInflater(), R.layout.enter_jid_dialog, null, false);
final Spinner spinner = dialogView.findViewById(R.id.account); binding.jid.setAdapter(new KnownHostsAdapter(getActivity(), R.layout.simple_list_item, (Collection<String>) getArguments().getSerializable(CONFERENCE_HOSTS_KEY)));
final AutoCompleteTextView jid = dialogView.findViewById(R.id.jid);
jid.setAdapter(new KnownHostsAdapter(getActivity(), R.layout.simple_list_item, (Collection<String>) getArguments().getSerializable(CONFERENCE_HOSTS_KEY)));
String prefilledJid = getArguments().getString(PREFILLED_JID_KEY); String prefilledJid = getArguments().getString(PREFILLED_JID_KEY);
if (prefilledJid != null) { if (prefilledJid != null) {
jid.append(prefilledJid); binding.jid.append(prefilledJid);
if (!getArguments().getBoolean(ALLOW_EDIT_JID_KEY)) { if (!getArguments().getBoolean(ALLOW_EDIT_JID_KEY)) {
jid.setFocusable(false); binding.jid.setFocusable(false);
jid.setFocusableInTouchMode(false); binding.jid.setFocusableInTouchMode(false);
jid.setClickable(false); binding.jid.setClickable(false);
jid.setCursorVisible(false); binding.jid.setCursorVisible(false);
} }
} }
DelayedHintHelper.setHint(R.string.account_settings_example_jabber_id,jid); DelayedHintHelper.setHint(R.string.account_settings_example_jabber_id, binding.jid);
String account = getArguments().getString(ACCOUNT_KEY); String account = getArguments().getString(ACCOUNT_KEY);
if (account == null) { if (account == null) {
StartConversationActivity.populateAccountSpinner(getActivity(), getArguments().getStringArrayList(ACCOUNTS_LIST_KEY), spinner); StartConversationActivity.populateAccountSpinner(getActivity(), getArguments().getStringArrayList(ACCOUNTS_LIST_KEY), binding.account);
} else { } else {
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), 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); binding.account.setEnabled(false);
adapter.setDropDownViewResource(R.layout.simple_list_item); adapter.setDropDownViewResource(R.layout.simple_list_item);
spinner.setAdapter(adapter); binding.account.setAdapter(adapter);
} }
builder.setView(dialogView); builder.setView(binding.getRoot());
builder.setNegativeButton(R.string.cancel, null); builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(getArguments().getString(POSITIVE_BUTTON_KEY), null); builder.setPositiveButton(getArguments().getString(POSITIVE_BUTTON_KEY), null);
AlertDialog dialog = builder.create(); AlertDialog dialog = builder.create();
View.OnClickListener dialogOnClick = v -> { View.OnClickListener dialogOnClick = v -> {
final Jid accountJid; final Jid accountJid;
if (!spinner.isEnabled() && account == null) { if (!binding.account.isEnabled() && account == null) {
return; return;
} }
try { try {
if (Config.DOMAIN_LOCK != null) { if (Config.DOMAIN_LOCK != null) {
accountJid = Jid.of((String) spinner.getSelectedItem(), Config.DOMAIN_LOCK, null); accountJid = Jid.of((String) binding.account.getSelectedItem(), Config.DOMAIN_LOCK, null);
} else { } else {
accountJid = Jid.of((String) spinner.getSelectedItem()); accountJid = Jid.of((String) binding.account.getSelectedItem());
} }
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
return; return;
} }
final Jid contactJid; final Jid contactJid;
try { try {
contactJid = Jid.of(jid.getText().toString()); contactJid = Jid.of(binding.jid.getText().toString());
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
jid.setError(getActivity().getString(R.string.invalid_jid)); binding.jid.setError(getActivity().getString(R.string.invalid_jid));
return; return;
} }
@ -123,7 +123,7 @@ public class EnterJidDialog extends DialogFragment{
dialog.dismiss(); dialog.dismiss();
} }
} catch(JidError error) { } catch(JidError error) {
jid.setError(error.toString()); binding.jid.setError(error.toString());
} }
} }
}; };

View file

@ -1,6 +1,7 @@
package eu.siacs.conversations.ui; package eu.siacs.conversations.ui;
import android.app.Dialog; import android.app.Dialog;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.content.Context; import android.content.Context;
@ -19,6 +20,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.databinding.JoinConferenceDialogBinding;
import eu.siacs.conversations.ui.adapter.KnownHostsAdapter; import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
import eu.siacs.conversations.ui.util.DelayedHintHelper; import eu.siacs.conversations.ui.util.DelayedHintHelper;
@ -50,19 +52,15 @@ public class JoinConferenceDialog extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog_title_join_conference); builder.setTitle(R.string.dialog_title_join_conference);
final View dialogView = getActivity().getLayoutInflater().inflate(R.layout.join_conference_dialog, null); JoinConferenceDialogBinding binding = DataBindingUtil.inflate(getActivity().getLayoutInflater(), R.layout.join_conference_dialog, null, false);
final Spinner spinner = dialogView.findViewById(R.id.account); DelayedHintHelper.setHint(R.string.conference_address_example, binding.jid);
final AutoCompleteTextView jid = dialogView.findViewById(R.id.jid); binding.jid.setAdapter(new KnownHostsAdapter(getActivity(), R.layout.simple_list_item, (Collection<String>) getArguments().getSerializable(CONFERENCE_HOSTS_KEY)));
DelayedHintHelper.setHint(R.string.conference_address_example, jid);
jid.setAdapter(new KnownHostsAdapter(getActivity(), R.layout.simple_list_item, (Collection<String>) getArguments().getSerializable(CONFERENCE_HOSTS_KEY)));
String prefilledJid = getArguments().getString(PREFILLED_JID_KEY); String prefilledJid = getArguments().getString(PREFILLED_JID_KEY);
if (prefilledJid != null) { if (prefilledJid != null) {
jid.append(prefilledJid); binding.jid.append(prefilledJid);
} }
final Checkable bookmarkCheckBox = (CheckBox) dialogView StartConversationActivity.populateAccountSpinner(getActivity(), getArguments().getStringArrayList(ACCOUNTS_LIST_KEY), binding.account);
.findViewById(R.id.bookmark); builder.setView(binding.getRoot());
StartConversationActivity.populateAccountSpinner(getActivity(), getArguments().getStringArrayList(ACCOUNTS_LIST_KEY), spinner);
builder.setView(dialogView);
builder.setPositiveButton(R.string.join, null); builder.setPositiveButton(R.string.join, null);
builder.setNegativeButton(R.string.cancel, null); builder.setNegativeButton(R.string.cancel, null);
AlertDialog dialog = builder.create(); AlertDialog dialog = builder.create();
@ -70,7 +68,7 @@ public class JoinConferenceDialog extends DialogFragment {
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
mListener.onJoinDialogPositiveClick(dialog, spinner, jid, bookmarkCheckBox.isChecked()); mListener.onJoinDialogPositiveClick(dialog, binding.account, binding.jid, binding.bookmark.isChecked());
} }
}); });
return dialog; return dialog;

View file

@ -440,6 +440,12 @@ 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) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
EnterJidDialog dialog = EnterJidDialog.newInstance( EnterJidDialog dialog = EnterJidDialog.newInstance(
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),
@ -474,18 +480,30 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
return true; return true;
} }
}); });
dialog.show(getSupportFragmentManager(), "create_contact_dialog"); dialog.show(ft, "dialog");
} }
@SuppressLint("InflateParams") @SuppressLint("InflateParams")
protected void showJoinConferenceDialog(final String prefilledJid) { protected void showJoinConferenceDialog(final String prefilledJid) {
JoinConferenceDialog dialog = JoinConferenceDialog.newInstance(prefilledJid, mActivatedAccounts, mKnownConferenceHosts); FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
dialog.show(getSupportFragmentManager(),"join_conference_dialog"); Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
JoinConferenceDialog joinConferenceFragment = JoinConferenceDialog.newInstance(prefilledJid, mActivatedAccounts, mKnownConferenceHosts);
joinConferenceFragment.show(ft, "dialog");
} }
private void showCreateConferenceDialog() { private void showCreateConferenceDialog() {
CreateConferenceDialog dialog = CreateConferenceDialog.newInstance(mActivatedAccounts); FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
dialog.show(getSupportFragmentManager(),"create_conference_dialog"); Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
CreateConferenceDialog createConferenceFragment = CreateConferenceDialog.newInstance(mActivatedAccounts);
createConferenceFragment.show(ft, "dialog");
} }
private Account getSelectedAccount(Spinner spinner) { private Account getSelectedAccount(Spinner spinner) {

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <layout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical"
@ -7,11 +8,13 @@
android:paddingLeft="?attr/dialog_horizontal_padding" android:paddingLeft="?attr/dialog_horizontal_padding"
android:paddingRight="?attr/dialog_horizontal_padding" android:paddingRight="?attr/dialog_horizontal_padding"
android:paddingTop="?attr/dialog_vertical_padding"> android:paddingTop="?attr/dialog_vertical_padding">
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/your_account" android:text="@string/your_account"
style="@style/InputLabel" /> style="@style/InputLabel" />
<Spinner <Spinner
android:id="@+id/account" android:id="@+id/account"
android:layout_width="fill_parent" android:layout_width="fill_parent"
@ -34,7 +37,6 @@
android:nextFocusUp="@+id/subject" android:nextFocusUp="@+id/subject"
android:nextFocusDown="@+id/subject" android:nextFocusDown="@+id/subject"
android:hint="@string/edit_subject_hint"/> android:hint="@string/edit_subject_hint"/>
</android.support.design.widget.TextInputLayout> </android.support.design.widget.TextInputLayout>
</LinearLayout> </LinearLayout>
</layout>

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <layout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical"
@ -31,5 +32,5 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:inputType="textEmailAddress"/> android:inputType="textEmailAddress"/>
</android.support.design.widget.TextInputLayout> </android.support.design.widget.TextInputLayout>
</LinearLayout> </LinearLayout>
</layout>

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <layout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical"
@ -7,6 +8,7 @@
android:paddingLeft="24dp" android:paddingLeft="24dp"
android:paddingRight="24dp" android:paddingRight="24dp"
android:paddingTop="16dp"> android:paddingTop="16dp">
<TextView <TextView
style="@style/InputLabel" style="@style/InputLabel"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -31,7 +33,6 @@
android:inputType="textEmailAddress"/> android:inputType="textEmailAddress"/>
</android.support.design.widget.TextInputLayout> </android.support.design.widget.TextInputLayout>
<CheckBox <CheckBox
android:id="@+id/bookmark" android:id="@+id/bookmark"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -39,5 +40,5 @@
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:checked="true" android:checked="true"
android:text="@string/save_as_bookmark"/> android:text="@string/save_as_bookmark"/>
</LinearLayout> </LinearLayout>
</layout>