From 58e5145296994f04f0a3e38d676997eb500e2fa8 Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 24 Mar 2017 19:58:28 -0700 Subject: [PATCH] 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. --- .../eehouse/android/xw4/BoardDelegate.java | 58 ++++-------- .../eehouse/android/xw4/XWDialogFragment.java | 92 ++++++++++++------- 2 files changed, 79 insertions(+), 71 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardDelegate.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardDelegate.java index f54a4da9b..f8ecd42ec 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardDelegate.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardDelegate.java @@ -80,7 +80,6 @@ public class BoardDelegate extends DelegateBase implements TransportProcs.TPMsgHandler, View.OnClickListener, DwnldDelegate.DownloadFinishedListener, ConnStatusHandler.ConnStatusCBacks, - XWDialogFragment.OnLastGoneListener, NFCUtils.NFCActor { 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 ); } - // Button button = ad.getButton( AlertDialog.BUTTON_POSITIVE ); - // button.setVisibility( nukeInviteButton ? View.GONE : View.VISIBLE ); - // if ( !nukeInviteButton ) { - // button.setText( buttonTxt ); - // } - // button = ad.getButton( AlertDialog.BUTTON_NEUTRAL ); - // button.setVisibility( nukeNeutButton ? View.GONE : View.VISIBLE ); + AlertDialog.Builder ab = makeAlertBuilder() + .setTitle( titleID ) + .setMessage( message ); OnClickListener lstnr = new OnClickListener() { public void onClick( DialogInterface dialog, int item ){ @@ -510,14 +505,10 @@ public class BoardDelegate extends DelegateBase } } }; - - AlertDialog.Builder ab = makeAlertBuilder() - .setTitle( titleID ) - .setMessage( message ) - .setPositiveButton( buttonTxt, lstnr ); + alert.setNoDismissListenerPos( ab, buttonTxt, lstnr ); if ( showNeutButton ) { - OnClickListener lstnrMore = new OnClickListener() { + lstnr = new OnClickListener() { public void onClick( DialogInterface dialog, int item ) { String msg = sentInfo[0].getAsText( activity ); @@ -526,25 +517,26 @@ public class BoardDelegate extends DelegateBase .show(); } }; - ab.setNeutralButton( R.string.newgame_invite_more, lstnrMore ); + alert.setNoDismissListenerNeut( ab, R.string.newgame_invite_more, + lstnr ); } if ( showInviteButton ) { - ab.setNegativeButton( R.string.button_wait, - new OnClickListener() { - @Override - public void onClick( DialogInterface di, - int item ) { - finish(); - } - } ); + lstnr = new OnClickListener() { + @Override + public void onClick( DialogInterface di, int item ) { + alert.dismiss(); + finish(); + } + }; + alert.setNoDismissListenerNeg( ab, R.string.button_wait, lstnr ); } dialog = ab.create(); // Hack: I can't prevent screen rotation from duplicating this alert - // if ( null != m_inviteAlert ) { - // m_inviteAlert.dismiss(); - // } + if ( null != m_inviteAlert ) { + m_inviteAlert.dismiss(); + } m_inviteAlert = alert; alert.setOnCancelListener( new DBAlert.OnCancelListener() { @Override @@ -558,7 +550,6 @@ public class BoardDelegate extends DelegateBase m_inviteAlert = null; } } ); - } return dialog; } // makeInviteDialog @@ -621,14 +612,12 @@ public class BoardDelegate extends DelegateBase @Override protected void onResume() { - XWDialogFragment.setOnLastGoneListener(this); super.onResume(); doResume( false ); } protected void onPause() { - XWDialogFragment.setOnLastGoneListener(null); closeIfFinishing( false ); m_handler = null; ConnStatusHandler.setHandler( null ); @@ -1449,14 +1438,6 @@ public class BoardDelegate extends DelegateBase return data; } - ////////////////////////////////////////////////// - // XWDialogFragment.OnLastGoneListener - ////////////////////////////////////////////////// - public void onLastGone() - { - showInviteAlertIf(); - } - ////////////////////////////////////////////////// // ConnStatusHandler.ConnStatusCBacks ////////////////////////////////////////////////// @@ -2310,8 +2291,7 @@ public class BoardDelegate extends DelegateBase private void showInviteAlertIf() { - if ( null == m_inviteAlert && m_mySIS.nMissing > 0 - && XWDialogFragment.inviteAlertCount() == 0 ) { + if ( null == m_inviteAlert && m_mySIS.nMissing > 0 && !isFinishing() ) { InviteAlertState ias = new InviteAlertState(); ias.summary = m_summary; ias.gi = m_gi; diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWDialogFragment.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWDialogFragment.java index ca0d03c1e..a40792070 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWDialogFragment.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWDialogFragment.java @@ -19,19 +19,25 @@ package org.eehouse.android.xw4; +import android.app.AlertDialog; +import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; 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; class XWDialogFragment extends DialogFragment { private static final String TAG = XWDialogFragment.class.getSimpleName(); - private static int s_count = 0; private OnDismissListener m_onDismiss; private OnCancelListener m_onCancel; - private static OnLastGoneListener s_onLast; + private Map m_buttonMap; public interface OnDismissListener { void onDismissed( XWDialogFragment frag ); @@ -39,8 +45,25 @@ class XWDialogFragment extends DialogFragment { public interface OnCancelListener { 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 @@ -61,28 +84,6 @@ class XWDialogFragment extends DialogFragment { 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 ) { Assert.assertTrue( null == lstnr || null == m_onDismiss || !BuildConfig.DEBUG ); @@ -95,15 +96,42 @@ class XWDialogFragment extends DialogFragment { 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 ); - s_onLast = lstnr; + ab.setPositiveButton( buttonID, null ); + 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 ); - return s_count; + ab.setNeutralButton( buttonID, null ); + 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 getButtonMap() + { + if ( null == m_buttonMap ) { + m_buttonMap = new HashMap(); + } + 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 ); + } } }