another try: make AlertDialog not exit from buttons

Rewrote Wait/Invite alert to use new functionality that allows it to set
its AlertDialog buttons to not exit when tapped. Turns out things are
much simpler when I don't have to hook into all the places my dialog
might need to be put up again.
This commit is contained in:
Eric House 2017-03-24 19:58:28 -07:00
parent 265818725c
commit 58e5145296
2 changed files with 79 additions and 71 deletions

View file

@ -80,7 +80,6 @@ public class BoardDelegate extends DelegateBase
implements TransportProcs.TPMsgHandler, View.OnClickListener, implements TransportProcs.TPMsgHandler, View.OnClickListener,
DwnldDelegate.DownloadFinishedListener, DwnldDelegate.DownloadFinishedListener,
ConnStatusHandler.ConnStatusCBacks, ConnStatusHandler.ConnStatusCBacks,
XWDialogFragment.OnLastGoneListener,
NFCUtils.NFCActor { NFCUtils.NFCActor {
private static final String TAG = BoardDelegate.class.getSimpleName(); private static final String TAG = BoardDelegate.class.getSimpleName();
@ -487,13 +486,9 @@ public class BoardDelegate extends DelegateBase
message += "\n\n" + LocUtils.getString( activity, R.string.invite_stays ); message += "\n\n" + LocUtils.getString( activity, R.string.invite_stays );
} }
// Button button = ad.getButton( AlertDialog.BUTTON_POSITIVE ); AlertDialog.Builder ab = makeAlertBuilder()
// button.setVisibility( nukeInviteButton ? View.GONE : View.VISIBLE ); .setTitle( titleID )
// if ( !nukeInviteButton ) { .setMessage( message );
// button.setText( buttonTxt );
// }
// button = ad.getButton( AlertDialog.BUTTON_NEUTRAL );
// button.setVisibility( nukeNeutButton ? View.GONE : View.VISIBLE );
OnClickListener lstnr = new OnClickListener() { OnClickListener lstnr = new OnClickListener() {
public void onClick( DialogInterface dialog, int item ){ public void onClick( DialogInterface dialog, int item ){
@ -510,14 +505,10 @@ public class BoardDelegate extends DelegateBase
} }
} }
}; };
alert.setNoDismissListenerPos( ab, buttonTxt, lstnr );
AlertDialog.Builder ab = makeAlertBuilder()
.setTitle( titleID )
.setMessage( message )
.setPositiveButton( buttonTxt, lstnr );
if ( showNeutButton ) { if ( showNeutButton ) {
OnClickListener lstnrMore = new OnClickListener() { lstnr = new OnClickListener() {
public void onClick( DialogInterface dialog, public void onClick( DialogInterface dialog,
int item ) { int item ) {
String msg = sentInfo[0].getAsText( activity ); String msg = sentInfo[0].getAsText( activity );
@ -526,25 +517,26 @@ public class BoardDelegate extends DelegateBase
.show(); .show();
} }
}; };
ab.setNeutralButton( R.string.newgame_invite_more, lstnrMore ); alert.setNoDismissListenerNeut( ab, R.string.newgame_invite_more,
lstnr );
} }
if ( showInviteButton ) { if ( showInviteButton ) {
ab.setNegativeButton( R.string.button_wait, lstnr = new OnClickListener() {
new OnClickListener() { @Override
@Override public void onClick( DialogInterface di, int item ) {
public void onClick( DialogInterface di, alert.dismiss();
int item ) { finish();
finish(); }
} };
} ); alert.setNoDismissListenerNeg( ab, R.string.button_wait, lstnr );
} }
dialog = ab.create(); dialog = ab.create();
// Hack: I can't prevent screen rotation from duplicating this alert // Hack: I can't prevent screen rotation from duplicating this alert
// if ( null != m_inviteAlert ) { if ( null != m_inviteAlert ) {
// m_inviteAlert.dismiss(); m_inviteAlert.dismiss();
// } }
m_inviteAlert = alert; m_inviteAlert = alert;
alert.setOnCancelListener( new DBAlert.OnCancelListener() { alert.setOnCancelListener( new DBAlert.OnCancelListener() {
@Override @Override
@ -558,7 +550,6 @@ public class BoardDelegate extends DelegateBase
m_inviteAlert = null; m_inviteAlert = null;
} }
} ); } );
} }
return dialog; return dialog;
} // makeInviteDialog } // makeInviteDialog
@ -621,14 +612,12 @@ public class BoardDelegate extends DelegateBase
@Override @Override
protected void onResume() protected void onResume()
{ {
XWDialogFragment.setOnLastGoneListener(this);
super.onResume(); super.onResume();
doResume( false ); doResume( false );
} }
protected void onPause() protected void onPause()
{ {
XWDialogFragment.setOnLastGoneListener(null);
closeIfFinishing( false ); closeIfFinishing( false );
m_handler = null; m_handler = null;
ConnStatusHandler.setHandler( null ); ConnStatusHandler.setHandler( null );
@ -1449,14 +1438,6 @@ public class BoardDelegate extends DelegateBase
return data; return data;
} }
//////////////////////////////////////////////////
// XWDialogFragment.OnLastGoneListener
//////////////////////////////////////////////////
public void onLastGone()
{
showInviteAlertIf();
}
////////////////////////////////////////////////// //////////////////////////////////////////////////
// ConnStatusHandler.ConnStatusCBacks // ConnStatusHandler.ConnStatusCBacks
////////////////////////////////////////////////// //////////////////////////////////////////////////
@ -2310,8 +2291,7 @@ public class BoardDelegate extends DelegateBase
private void showInviteAlertIf() private void showInviteAlertIf()
{ {
if ( null == m_inviteAlert && m_mySIS.nMissing > 0 if ( null == m_inviteAlert && m_mySIS.nMissing > 0 && !isFinishing() ) {
&& XWDialogFragment.inviteAlertCount() == 0 ) {
InviteAlertState ias = new InviteAlertState(); InviteAlertState ias = new InviteAlertState();
ias.summary = m_summary; ias.summary = m_summary;
ias.gi = m_gi; ias.gi = m_gi;

View file

@ -19,19 +19,25 @@
package org.eehouse.android.xw4; package org.eehouse.android.xw4;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.view.View.OnClickListener;
import android.view.View;
import java.util.HashMap;
import java.util.Map;
import junit.framework.Assert; import junit.framework.Assert;
class XWDialogFragment extends DialogFragment { class XWDialogFragment extends DialogFragment {
private static final String TAG = XWDialogFragment.class.getSimpleName(); private static final String TAG = XWDialogFragment.class.getSimpleName();
private static int s_count = 0;
private OnDismissListener m_onDismiss; private OnDismissListener m_onDismiss;
private OnCancelListener m_onCancel; private OnCancelListener m_onCancel;
private static OnLastGoneListener s_onLast; private Map<Integer, DialogInterface.OnClickListener> m_buttonMap;
public interface OnDismissListener { public interface OnDismissListener {
void onDismissed( XWDialogFragment frag ); void onDismissed( XWDialogFragment frag );
@ -39,8 +45,25 @@ class XWDialogFragment extends DialogFragment {
public interface OnCancelListener { public interface OnCancelListener {
void onCancelled( XWDialogFragment frag ); void onCancelled( XWDialogFragment frag );
} }
public interface OnLastGoneListener {
void onLastGone(); @Override
public void onStart()
{
super.onStart();
if ( null != m_buttonMap ) {
AlertDialog dialog = (AlertDialog)getDialog();
for ( final int but : m_buttonMap.keySet() ) {
// final int fbut = but;
dialog.getButton( but )
.setOnClickListener( new OnClickListener() {
@Override
public void onClick( View view ) {
dialogButtonClicked( view, but );
}
} );
}
}
} }
@Override @Override
@ -61,28 +84,6 @@ class XWDialogFragment extends DialogFragment {
super.onDismiss( dif ); super.onDismiss( dif );
} }
@Override
public void onAttach( Context context )
{
++s_count;
super.onAttach( context );
// DbgUtils.logd(TAG, "%s added to %s; now %d", toString(),
// context.getClass().getSimpleName(), s_count );
}
@Override
public void onDetach()
{
--s_count;
Assert.assertTrue( s_count >= 0 || !BuildConfig.DEBUG );
super.onDetach();
// DbgUtils.logd(TAG, "%s removed from %s; now %d", toString(),
// getActivity().getClass().getSimpleName(), s_count );
if ( 0 == s_count && null != s_onLast ) {
s_onLast.onLastGone();
}
}
protected void setOnDismissListener( OnDismissListener lstnr ) protected void setOnDismissListener( OnDismissListener lstnr )
{ {
Assert.assertTrue( null == lstnr || null == m_onDismiss || !BuildConfig.DEBUG ); Assert.assertTrue( null == lstnr || null == m_onDismiss || !BuildConfig.DEBUG );
@ -95,15 +96,42 @@ class XWDialogFragment extends DialogFragment {
m_onCancel = lstnr; m_onCancel = lstnr;
} }
protected static void setOnLastGoneListener( OnLastGoneListener lstnr ) protected void setNoDismissListenerPos( AlertDialog.Builder ab, int buttonID,
DialogInterface.OnClickListener lstnr )
{ {
Assert.assertTrue( null == lstnr || null == s_onLast || !BuildConfig.DEBUG ); ab.setPositiveButton( buttonID, null );
s_onLast = lstnr; getButtonMap().put( AlertDialog.BUTTON_POSITIVE, lstnr );
} }
protected static int inviteAlertCount() protected void setNoDismissListenerNeut( AlertDialog.Builder ab, int buttonID,
DialogInterface.OnClickListener lstnr )
{ {
DbgUtils.logd( TAG, "inviteAlertCount() => %d", s_count ); ab.setNeutralButton( buttonID, null );
return s_count; getButtonMap().put( AlertDialog.BUTTON_NEUTRAL, lstnr );
}
protected void setNoDismissListenerNeg( AlertDialog.Builder ab, int buttonID,
DialogInterface.OnClickListener lstnr )
{
ab.setNegativeButton( buttonID, null );
getButtonMap().put( AlertDialog.BUTTON_NEGATIVE, lstnr );
}
private Map<Integer, DialogInterface.OnClickListener> getButtonMap()
{
if ( null == m_buttonMap ) {
m_buttonMap = new HashMap<Integer, DialogInterface.OnClickListener>();
}
return m_buttonMap;
}
private void dialogButtonClicked( View view, int button )
{
DialogInterface.OnClickListener listener = m_buttonMap.get( button );
if ( null != listener ) {
listener.onClick( getDialog(), button );
} else {
Assert.assertFalse( BuildConfig.DEBUG );
}
} }
} }