simplify dialogs

Get rid of subclasses, since there's not the clear inheritance structure
I imagined. Any dialog type might want a not-again checkbox, or to have a
third button offer an unexpected option. This is a big change that needs
some bake time.
This commit is contained in:
Eric House 2020-06-23 08:51:29 -07:00
parent 9ae39ffe28
commit c8b587c29b
11 changed files with 187 additions and 222 deletions

View file

@ -2488,7 +2488,7 @@ public class BoardDelegate extends DelegateBase
boolean banned = Perm.SEND_SMS.isBanned(m_activity);
int explID = banned
? R.string.banned_nbs_perms : R.string.missing_sms_perms;
DlgDelegate.ConfirmThenBuilder builder =
DlgDelegate.Builder builder =
makeConfirmThenBuilder( explID, Action.DROP_SMS_ACTION );
if ( banned ) {
builder.setActionPair( Action.PERMS_BANNED_INFO,

View file

@ -1,6 +1,7 @@
/* -*- compile-command: "find-and-gradle.sh inXw4dDebug"; -*- */
/*
* Copyright 2017 by Eric House (xwords@eehouse.org). All rights reserved.
* Copyright 2017 - 2020 by Eric House (xwords@eehouse.org). All rights
* reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@ -29,6 +30,7 @@ import org.eehouse.android.xw4.DlgDelegate.ActionPair;
import org.eehouse.android.xw4.loc.LocUtils;
public class ConfirmThenAlert extends DlgDelegateAlert {
private static final String TAG = ConfirmThenAlert.class.getSimpleName();
public static ConfirmThenAlert newInstance( DlgState state )
{
@ -46,8 +48,11 @@ public class ConfirmThenAlert extends DlgDelegateAlert {
NotAgainView naView = addNAView( state, builder );
OnClickListener lstnr = mkCallbackClickListener( naView );
builder.setTitle( state.m_titleId == 0 ? R.string.query_title : state.m_titleId )
.setPositiveButton( state.m_posButton, lstnr )
if ( 0 != state.m_titleId ) {
builder.setTitle( state.m_titleId );
}
builder.setPositiveButton( state.m_posButton, lstnr )
.setNegativeButton( state.m_negButton, lstnr );
if ( null != state.m_pair ) {

View file

@ -181,7 +181,7 @@ public class ConnViaViewLayout extends LinearLayout {
break;
}
DlgDelegate.DlgDelegateBuilder builder = 0 != keyID
DlgDelegate.Builder builder = 0 != keyID
? m_dlgDlgt.makeNotAgainBuilder( msgID, keyID )
: m_dlgDlgt.makeOkOnlyBuilder( msgID )
.setActionPair( Action.PERMS_BANNED_INFO,

View file

@ -41,10 +41,8 @@ import android.widget.EditText;
import android.widget.TextView;
import org.eehouse.android.xw4.DlgDelegate.Action;
import org.eehouse.android.xw4.DlgDelegate.ConfirmThenBuilder;
import org.eehouse.android.xw4.DlgDelegate.Builder;
import org.eehouse.android.xw4.DlgDelegate.DlgClickNotify;
import org.eehouse.android.xw4.DlgDelegate.NotAgainBuilder;
import org.eehouse.android.xw4.DlgDelegate.OkOnlyBuilder;
import org.eehouse.android.xw4.MultiService.MultiEvent;
import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnType;
import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnTypeSet;
@ -521,33 +519,33 @@ public class DelegateBase implements DlgClickNotify,
return LocUtils.makeAlertBuilder( m_activity );
}
public NotAgainBuilder
public Builder
makeNotAgainBuilder( String msg, int key, Action action )
{
return m_dlgDelegate.makeNotAgainBuilder( msg, key, action );
}
public NotAgainBuilder
public Builder
makeNotAgainBuilder( int msgId, int key, Action action )
{
return m_dlgDelegate.makeNotAgainBuilder( msgId, key, action );
}
public NotAgainBuilder makeNotAgainBuilder( String msg, int key )
public Builder makeNotAgainBuilder( String msg, int key )
{
return m_dlgDelegate.makeNotAgainBuilder( msg, key );
}
public NotAgainBuilder makeNotAgainBuilder( int msgId, int key )
public Builder makeNotAgainBuilder( int msgID, int key )
{
return m_dlgDelegate.makeNotAgainBuilder( msgId, key );
return m_dlgDelegate.makeNotAgainBuilder( msgID, key );
}
public ConfirmThenBuilder makeConfirmThenBuilder( String msg, Action action ) {
public Builder makeConfirmThenBuilder( String msg, Action action ) {
return m_dlgDelegate.makeConfirmThenBuilder( msg, action );
}
public ConfirmThenBuilder makeConfirmThenBuilder( int msgId, Action action ) {
public Builder makeConfirmThenBuilder( int msgId, Action action ) {
return m_dlgDelegate.makeConfirmThenBuilder( msgId, action );
}
@ -578,12 +576,12 @@ public class DelegateBase implements DlgClickNotify,
m_dlgDelegate.showInviteChoicesThen( action, info );
}
public OkOnlyBuilder makeOkOnlyBuilder( int msgId )
public Builder makeOkOnlyBuilder( int msgID )
{
return m_dlgDelegate.makeOkOnlyBuilder( msgId );
return m_dlgDelegate.makeOkOnlyBuilder( msgID );
}
public OkOnlyBuilder makeOkOnlyBuilder( String msg )
public Builder makeOkOnlyBuilder( String msg )
{
return m_dlgDelegate.makeOkOnlyBuilder( msg );
}
@ -722,12 +720,10 @@ public class DelegateBase implements DlgClickNotify,
final int key = notAgainKey;
runOnUiThread( new Runnable() {
public void run() {
DlgDelegate.DlgDelegateBuilder builder;
if ( 0 == key ) {
builder = makeOkOnlyBuilder( msg );
} else {
builder = makeNotAgainBuilder( msg, key );
}
Builder builder = 0 == key
? makeOkOnlyBuilder( msg )
: makeNotAgainBuilder( msg, key )
;
builder.show();
}
});

View file

@ -154,139 +154,173 @@ public class DlgDelegate {
}
}
public abstract class DlgDelegateBuilder {
protected String m_msgString;
protected int m_prefsNAKey;
protected Action m_onNA;
protected int m_posButton = android.R.string.ok;
protected int m_negButton = android.R.string.cancel;
protected Action m_action;
protected Object[] m_params;
protected int m_titleId = 0;
protected ActionPair m_actionPair;
public class Builder {
private final DlgState mState;
public DlgDelegateBuilder( String msg, Action action )
{ m_msgString = msg; m_action = action; }
Builder( DlgID dlgID ) {
mState = new DlgState( dlgID )
.setPosButton( android.R.string.ok ) // default
;
}
public DlgDelegateBuilder( int msgId, Action action )
{ this( getString(msgId), action );}
Builder setMessage( String msg )
{
mState.setMsg( msg );
return this;
}
public DlgDelegateBuilder setAction( Action action )
{ m_action = action; return this; }
Builder setMessageID( int msgID )
{
mState.setMsg( getString( msgID ) );
return this;
}
public DlgDelegateBuilder setNAKey( int keyId )
{ m_prefsNAKey = keyId; return this; }
Builder setActionPair( Action action, int strID )
{
mState.setActionPair( new ActionPair( action, strID ) );
return this;
}
public DlgDelegateBuilder setOnNA( Action onNA )
{ m_onNA = onNA; return this; }
Builder setAction( Action action )
{
mState.setAction( action );
return this;
}
public DlgDelegateBuilder setPosButton( int id )
{ m_posButton = id; return this; }
Builder setPosButton( int strID )
{
mState.setPosButton( strID );
return this;
}
public DlgDelegateBuilder setNegButton( int id )
{ m_negButton = id; return this; }
Builder setNegButton( int strID )
{
mState.setNegButton( strID );
return this;
}
public DlgDelegateBuilder setParams( Object... params )
{ m_params = params; return this; }
Builder setTitle( int strID )
{
mState.setTitle( strID );
return this;
}
public DlgDelegateBuilder setTitle( int titleId )
{ m_titleId = titleId; return this; }
Builder setParams( Object... params )
{
mState.setParams( params );
return this;
}
public DlgDelegateBuilder setActionPair( ActionPair pr )
{ m_actionPair = pr; return this; }
Builder setNAKey( int keyID )
{
mState.setPrefsKey( keyID );
return this;
}
public DlgDelegateBuilder setActionPair( Action actn, int id )
{ return setActionPair( new ActionPair( actn, id ) ); }
Builder setOnNA( Action onNAAction )
{
mState.setOnNA( onNAAction );
return this;
}
abstract void show();
}
public class OkOnlyBuilder extends DlgDelegateBuilder {
public OkOnlyBuilder(String msg) { super( msg, Action.SKIP_CALLBACK ); }
public OkOnlyBuilder(int msgId) { super( msgId, Action.SKIP_CALLBACK ); }
@Override
public void show()
{
showOKOnlyDialogThen( m_msgString, m_action, m_actionPair,
m_params, m_titleId );
int naKey = mState.m_prefsNAKey;
final Action action = mState.m_action;
// Log.d( TAG, "show(): key: %d; action: %s", naKey, action );
if ( 0 == naKey || ! XWPrefs.getPrefsBoolean( m_activity, naKey, false ) ) {
m_dlgt.show( mState );
} else if ( Action.SKIP_CALLBACK != action ) {
post( new Runnable() {
@Override
public void run() {
Log.d( TAG, "calling onPosButton()" );
XWActivity xwact = (XWActivity)m_activity;
xwact.onPosButton( action, mState.m_params );
}
});
}
}
}
public class ConfirmThenBuilder extends DlgDelegateBuilder {
public ConfirmThenBuilder(String msg, Action action) {super(msg, action);}
public ConfirmThenBuilder(int msgId, Action action) {super(msgId, action);}
@Override
public void show()
{
showConfirmThen( m_prefsNAKey, m_onNA, m_msgString, m_posButton,
m_negButton, m_action, m_titleId, m_actionPair,
m_params );
}
}
public class NotAgainBuilder extends DlgDelegateBuilder {
public NotAgainBuilder(String msg, int key, Action action)
{ super(msg, action); setNAKey( key ); }
public NotAgainBuilder(int msgId, int key, Action action)
{ super(msgId, action); m_prefsNAKey = key; }
public NotAgainBuilder( String msg, int key )
{ super( msg, Action.SKIP_CALLBACK ); m_prefsNAKey = key; }
public NotAgainBuilder( int msgId, int key )
{ super( msgId, Action.SKIP_CALLBACK ); m_prefsNAKey = key; }
@Override
public void show()
{
showNotAgainDlgThen( m_msgString, m_prefsNAKey,
m_action, m_actionPair,
m_params );
}
}
public OkOnlyBuilder makeOkOnlyBuilder( String msg )
public Builder makeOkOnlyBuilder( String msg )
{
return new OkOnlyBuilder( msg );
}
public OkOnlyBuilder makeOkOnlyBuilder( int msgId )
{
return new OkOnlyBuilder( msgId );
// return new OkOnlyBuilder( msg );
Builder builder = new Builder( DlgID.DIALOG_OKONLY )
.setMessage( msg )
;
return builder;
}
public ConfirmThenBuilder makeConfirmThenBuilder( String msg, Action action )
public Builder makeOkOnlyBuilder( int msgID )
{
return new ConfirmThenBuilder( msg, action );
Builder builder = new Builder( DlgID.DIALOG_OKONLY )
.setMessageID( msgID )
;
return builder;
}
public ConfirmThenBuilder makeConfirmThenBuilder(int msgId, Action action)
private Builder makeConfirmThenBuilder( Action action )
{
return new ConfirmThenBuilder( msgId, action );
return new Builder( DlgID.CONFIRM_THEN )
.setAction( action )
.setNegButton( android.R.string.cancel )
;
}
public Builder makeConfirmThenBuilder( String msg, Action action )
{
return makeConfirmThenBuilder( action )
.setMessage( msg )
;
}
public NotAgainBuilder makeNotAgainBuilder( int msgId, int key,
Action action )
public Builder makeConfirmThenBuilder( int msgID, Action action )
{
return new NotAgainBuilder( msgId, key, action );
return makeConfirmThenBuilder( action )
.setMessageID( msgID )
;
}
public NotAgainBuilder makeNotAgainBuilder( String msg, int key,
Action action )
private Builder makeNotAgainBuilder( int key )
{
return new NotAgainBuilder( msg, key, action );
return new Builder( DlgID.DIALOG_NOTAGAIN )
.setNAKey( key )
.setAction( Action.SKIP_CALLBACK )
;
}
public NotAgainBuilder makeNotAgainBuilder( String msg, int key )
public Builder makeNotAgainBuilder( String msg, int key, Action action )
{
return new NotAgainBuilder( msg, key );
return makeNotAgainBuilder( key )
.setMessage( msg )
.setAction( action )
;
}
public NotAgainBuilder makeNotAgainBuilder( int msgId, int key ) {
return new NotAgainBuilder( msgId, key );
public Builder makeNotAgainBuilder( int msgID, int key, Action action )
{
return makeNotAgainBuilder( key )
.setMessageID( msgID )
.setAction( action )
;
}
public Builder makeNotAgainBuilder( String msg, int key )
{
return makeNotAgainBuilder( key )
.setMessage( msg )
;
}
public Builder makeNotAgainBuilder( int msgID, int key )
{
return makeNotAgainBuilder( key )
.setMessageID( msgID )
;
}
public static final int SMS_BTN = AlertDialog.BUTTON_POSITIVE;
@ -309,11 +343,10 @@ public class DlgDelegate {
void inviteChoiceMade( Action action, InviteMeans means, Object... params );
}
public interface HasDlgDelegate {
OkOnlyBuilder makeOkOnlyBuilder( int msgID );
OkOnlyBuilder makeOkOnlyBuilder( String msg );
NotAgainBuilder makeNotAgainBuilder( int msgID, int prefsKey,
Action action );
NotAgainBuilder makeNotAgainBuilder( int msgID, int prefsKey );
Builder makeOkOnlyBuilder( int msgID );
Builder makeOkOnlyBuilder( String msg );
Builder makeNotAgainBuilder( int msgID, int prefsKey, Action action );
Builder makeNotAgainBuilder( int msgID, int prefsKey );
}
private Activity m_activity;
@ -337,19 +370,6 @@ public class DlgDelegate {
stopProgress();
}
private void showOKOnlyDialogThen( String msg, Action action,
ActionPair more, Object[] params,
int titleId )
{
DlgState state = new DlgState( DlgID.DIALOG_OKONLY )
.setMsg( msg )
.setParams( params )
.setTitle( titleId )
.setActionPair( more )
.setAction(action);
m_dlgt.show( state );
}
// Puts up alert asking to choose a reason to enable SMS, and on dismiss
// calls onPosButton/onNegButton with the action and in params a Boolean
// indicating whether enabling is now ok.
@ -361,62 +381,6 @@ public class DlgDelegate {
m_dlgt.show( state );
}
private void showNotAgainDlgThen( String msg, int prefsKey,
final Action action, ActionPair more,
final Object[] params )
{
if ( 0 != prefsKey
&& XWPrefs.getPrefsBoolean( m_activity, prefsKey, false ) ) {
// If it's set, do the action without bothering with the
// dialog
if ( Action.SKIP_CALLBACK != action ) {
post( new Runnable() {
public void run() {
XWActivity xwact = (XWActivity)m_activity;
xwact.onPosButton( action, params );
}
});
}
} else {
DlgState state = new DlgState( DlgID.DIALOG_NOTAGAIN )
.setMsg( msg)
.setPrefsKey( prefsKey )
.setAction( action )
.setActionPair( more )
.setParams( params );
m_dlgt.show( state );
}
}
private void showConfirmThen( int nakey, Action onNA, String msg,
int posButton, int negButton,
final Action action, int titleId,
ActionPair more, final Object[] params )
{
if ( 0 == nakey ||
! XWPrefs.getPrefsBoolean( m_activity, nakey, false ) ) {
DlgState state = new DlgState( DlgID.CONFIRM_THEN )
.setOnNA( onNA )
.setMsg( msg )
.setPosButton( posButton )
.setNegButton( negButton )
.setAction( action )
.setTitle( titleId )
.setActionPair( more )
.setParams( params )
.setPrefsKey( nakey )
;
m_dlgt.show( state );
} else if ( Action.SKIP_CALLBACK != action ) {
post( new Runnable() {
public void run() {
XWActivity xwact = (XWActivity)m_activity;
xwact.onDismissed( action, params );
}
});
}
}
public void showInviteChoicesThen( final Action action,
SentInvitesInfo info )
{

View file

@ -89,6 +89,10 @@ abstract class DlgDelegateAlert extends XWDialogFragment {
AlertDialog.Builder builder = LocUtils.makeAlertBuilder( context );
if ( 0 != state.m_titleId ) {
builder.setTitle( state.m_titleId );
}
populateBuilder( context, state, builder );
return create( builder );

View file

@ -168,7 +168,7 @@ public class DlgState implements Parcelable {
private void testCanParcelize()
{
if (BuildConfig.DEBUG) {
if ( BuildConfig.DEBUG ) {
Parcel parcel = Parcel.obtain();
writeToParcel(parcel, 0);

View file

@ -2438,7 +2438,7 @@ public class GamesListDelegate extends ListDelegateBase
boolean handled = false;
String msg = intent.getStringExtra( ALERT_MSG );
if ( null != msg ) {
DlgDelegate.DlgDelegateBuilder builder =
DlgDelegate.Builder builder =
makeOkOnlyBuilder( msg );
if ( intent.getBooleanExtra( WITH_EMAIL, false ) ) {
builder.setActionPair( Action.SEND_EMAIL,

View file

@ -1,6 +1,7 @@
/* -*- compile-command: "find-and-gradle.sh inXw4dDebug"; -*- */
/* -*- compile-command: "find-and-gradle.sh inXw4dDeb"; -*- */
/*
* Copyright 2017 by Eric House (xwords@eehouse.org). All rights reserved.
* Copyright 2017 - 2020 by Eric House (xwords@eehouse.org). All rights
* reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@ -40,9 +41,8 @@ public class OkOnlyAlert extends DlgDelegateAlert {
public void populateBuilder( Context context, DlgState state,
AlertDialog.Builder builder )
{
builder.setTitle( state.m_titleId == 0 ? R.string.info_title : state.m_titleId )
.setMessage( state.m_msg )
.setPositiveButton( android.R.string.ok, null )
builder.setMessage( state.m_msg )
.setPositiveButton( state.m_posButton, null )
;
ActionPair pair = state.m_pair;

View file

@ -26,11 +26,8 @@ import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import org.eehouse.android.xw4.DlgDelegate.Action;
import org.eehouse.android.xw4.DlgDelegate.ConfirmThenBuilder;
import org.eehouse.android.xw4.DlgDelegate.NotAgainBuilder;
import org.eehouse.android.xw4.DlgDelegate.OkOnlyBuilder;
import org.eehouse.android.xw4.DlgDelegate.Builder;
import org.eehouse.android.xw4.loc.LocUtils;
public class PrefsActivity extends PreferenceActivity
@ -102,32 +99,32 @@ public class PrefsActivity extends PreferenceActivity
m_dlgt.onActivityResult( rc, resultCode, data );
}
public OkOnlyBuilder makeOkOnlyBuilder( int msgId )
public Builder makeOkOnlyBuilder( int msgID )
{
return m_dlgt.makeOkOnlyBuilder( msgId );
return m_dlgt.makeOkOnlyBuilder( msgID );
}
public OkOnlyBuilder makeOkOnlyBuilder( String msg )
public Builder makeOkOnlyBuilder( String msg )
{
return m_dlgt.makeOkOnlyBuilder( msg );
}
public NotAgainBuilder makeNotAgainBuilder(int msgId, int key, Action action)
public Builder makeNotAgainBuilder(int msgID, int key, Action action)
{
return m_dlgt.makeNotAgainBuilder( msgId, key, action );
return m_dlgt.makeNotAgainBuilder( msgID, key, action );
}
public NotAgainBuilder makeNotAgainBuilder( int msgId, int key )
public Builder makeNotAgainBuilder( int msgID, int key )
{
return m_dlgt.makeNotAgainBuilder( msgId, key );
return m_dlgt.makeNotAgainBuilder( msgID, key );
}
public ConfirmThenBuilder makeConfirmThenBuilder(String msg, Action action)
public Builder makeConfirmThenBuilder( String msg, Action action )
{
return m_dlgt.makeConfirmThenBuilder( msg, action );
}
public ConfirmThenBuilder makeConfirmThenBuilder(int msgID, Action action)
public Builder makeConfirmThenBuilder( int msgID, Action action )
{
return m_dlgt.makeConfirmThenBuilder( msgID, action );
}

View file

@ -204,23 +204,22 @@ public class XWActivity extends FragmentActivity
// This are a hack! I need some way to build fragment-based alerts from
// inside fragment-based alerts.
public DlgDelegate.NotAgainBuilder makeNotAgainBuilder( String msg, int keyId )
public DlgDelegate.Builder makeNotAgainBuilder( String msg, int keyID )
{
return m_dlgt.makeNotAgainBuilder( msg, keyId );
return m_dlgt.makeNotAgainBuilder( msg, keyID );
}
public DlgDelegate.NotAgainBuilder makeNotAgainBuilder( int msgID, int keyId )
public DlgDelegate.Builder makeNotAgainBuilder( int msgID, int keyID )
{
return m_dlgt.makeNotAgainBuilder( msgID, keyId );
return m_dlgt.makeNotAgainBuilder( msgID, keyID );
}
public DlgDelegate.ConfirmThenBuilder makeConfirmThenBuilder( int msgID,
Action action )
public DlgDelegate.Builder makeConfirmThenBuilder( int msgID, Action action )
{
return m_dlgt.makeConfirmThenBuilder( msgID, action );
}
public DlgDelegate.OkOnlyBuilder makeOkOnlyBuilder( int msgID )
public DlgDelegate.Builder makeOkOnlyBuilder( int msgID )
{
return m_dlgt.makeOkOnlyBuilder( msgID );
}