From 265818725cdf082540bdffa32bede7ae367cb4d3 Mon Sep 17 00:00:00 2001 From: Eric House Date: Thu, 23 Mar 2017 08:39:15 -0700 Subject: [PATCH] improve (fix?) invite/wait dialog Override onCancel() in order to finish when the alert's closed via the back button. Count the number of alerts and when it drops to 0 (when all the alerts launched from the invite/wait dialog are gone) put the invite/wait up again. --- .../eehouse/android/xw4/BoardDelegate.java | 78 ++++++++++--------- .../eehouse/android/xw4/XWDialogFragment.java | 67 +++++++++++++++- 2 files changed, 106 insertions(+), 39 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 54f100545..f54a4da9b 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,6 +80,7 @@ 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(); @@ -131,7 +132,6 @@ public class BoardDelegate extends DelegateBase private boolean m_overNotShown; private boolean m_dropOnDismiss; private DBAlert m_inviteAlert; - private int m_inviteAlertCount; public class TimerRunnable implements Runnable { private int m_why; @@ -380,27 +380,10 @@ public class BoardDelegate extends DelegateBase .setNegativeButton( R.string.button_no, null ) .create(); break; - case DLG_INVITE: + case DLG_INVITE: { InviteAlertState state = (InviteAlertState)params[0]; dialog = makeInviteDialog( alert, state ); - if ( null != dialog ) { - // Hack: I can't prevent screen rotation from duplicating this alert - if ( null != m_inviteAlert ) { - m_inviteAlert.dismiss(); - } - m_inviteAlert = alert; - alert.setOnDismissListener( new DBAlert.OnDismissListener() { - @Override - public void onDismissed( XWDialogFragment frag ) { - DbgUtils.logd( TAG, "onDismissed(%s)", frag.toString() ); - m_inviteAlert = null; - if ( null != m_mySIS && m_mySIS.nMissing > 0 - && 0 == m_inviteAlertCount ) { - finish(); - } - } - } ); - } + } break; case ENABLE_NFC: @@ -520,7 +503,6 @@ public class BoardDelegate extends DelegateBase if ( state.summary.hasRematchInfo() ) { tryRematchInvites( true ); } else { - ++m_inviteAlertCount; callInviteChoices( sentInfo[0] ); } } else { @@ -539,7 +521,6 @@ public class BoardDelegate extends DelegateBase public void onClick( DialogInterface dialog, int item ) { String msg = sentInfo[0].getAsText( activity ); - ++m_inviteAlertCount; makeOkOnlyBuilder( msg ) .setAction( Action.INVITE_INFO ) .show(); @@ -548,10 +529,36 @@ public class BoardDelegate extends DelegateBase ab.setNeutralButton( R.string.newgame_invite_more, lstnrMore ); } if ( showInviteButton ) { - ab.setNegativeButton( R.string.button_wait, null ); + ab.setNegativeButton( R.string.button_wait, + new OnClickListener() { + @Override + public void onClick( DialogInterface di, + int item ) { + finish(); + } + } ); } dialog = ab.create(); + + // Hack: I can't prevent screen rotation from duplicating this alert + // if ( null != m_inviteAlert ) { + // m_inviteAlert.dismiss(); + // } + m_inviteAlert = alert; + alert.setOnCancelListener( new DBAlert.OnCancelListener() { + @Override + public void onCancelled( XWDialogFragment frag ) { + finish(); + } + } ); + alert.setOnDismissListener( new DBAlert.OnDismissListener() { + @Override + public void onDismissed( XWDialogFragment frag ) { + m_inviteAlert = null; + } + } ); + } return dialog; } // makeInviteDialog @@ -614,12 +621,14 @@ 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 ); @@ -654,7 +663,6 @@ public class BoardDelegate extends DelegateBase protected void onSaveInstanceState( Bundle outState ) { outState.putSerializable( SAVE_MYSIS, m_mySIS ); - m_mySIS = null; // signal that we're exiting } private void getBundledData( Bundle bundle ) @@ -1155,12 +1163,6 @@ public class BoardDelegate extends DelegateBase { boolean handled = true; switch ( action ) { - case INVITE_INFO: - if ( 0 < m_inviteAlertCount ) { - --m_inviteAlertCount; - showInviteAlertIf(); - } - break; case ENABLE_RELAY_DO_OR: if ( m_dropOnDismiss ) { postDelayed( new Runnable() { @@ -1174,12 +1176,6 @@ public class BoardDelegate extends DelegateBase finish(); break; - case LAUNCH_INVITE_ACTION: - Assert.assertTrue( 0 < m_inviteAlertCount || !BuildConfig.DEBUG ); - --m_inviteAlertCount; - showInviteAlertIf(); - break; - default: handled = false; } @@ -1453,6 +1449,14 @@ public class BoardDelegate extends DelegateBase return data; } + ////////////////////////////////////////////////// + // XWDialogFragment.OnLastGoneListener + ////////////////////////////////////////////////// + public void onLastGone() + { + showInviteAlertIf(); + } + ////////////////////////////////////////////////// // ConnStatusHandler.ConnStatusCBacks ////////////////////////////////////////////////// @@ -2307,7 +2311,7 @@ public class BoardDelegate extends DelegateBase private void showInviteAlertIf() { if ( null == m_inviteAlert && m_mySIS.nMissing > 0 - && m_inviteAlertCount == 0 ) { + && XWDialogFragment.inviteAlertCount() == 0 ) { 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 b506c0198..ca0d03c1e 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,15 +19,38 @@ package org.eehouse.android.xw4; -import android.support.v4.app.DialogFragment; +import android.content.Context; import android.content.DialogInterface; +import android.support.v4.app.DialogFragment; + +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; public interface OnDismissListener { void onDismissed( XWDialogFragment frag ); } + public interface OnCancelListener { + void onCancelled( XWDialogFragment frag ); + } + public interface OnLastGoneListener { + void onLastGone(); + } + + @Override + public void onCancel( DialogInterface dialog ) + { + if ( null != m_onCancel ) { + m_onCancel.onCancelled( this ); + } + super.onCancel( dialog ); + } @Override public void onDismiss( DialogInterface dif ) @@ -37,10 +60,50 @@ 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 ); m_onDismiss = lstnr; } - + + protected void setOnCancelListener( OnCancelListener lstnr ) + { + Assert.assertTrue( null == lstnr || null == m_onCancel || !BuildConfig.DEBUG ); + m_onCancel = lstnr; + } + + protected static void setOnLastGoneListener( OnLastGoneListener lstnr ) + { + Assert.assertTrue( null == lstnr || null == s_onLast || !BuildConfig.DEBUG ); + s_onLast = lstnr; + } + + protected static int inviteAlertCount() + { + DbgUtils.logd( TAG, "inviteAlertCount() => %d", s_count ); + return s_count; + } }