refactor alerts code

Should be no change here, but the goal's to make it easier to mix
functionality, e.g. adding notagain checkbox to other types.
This commit is contained in:
Eric House 2019-03-08 13:29:30 -08:00
parent a40c6f494f
commit dfcf76f5cd
8 changed files with 62 additions and 56 deletions

View file

@ -40,20 +40,15 @@ public class ConfirmThenAlert extends DlgDelegateAlert {
public ConfirmThenAlert() {}
@Override
public Dialog onCreateDialog( Bundle sis )
public void populateBuilder( Context context, DlgState state,
AlertDialog.Builder builder,
NotAgainView naView )
{
final Context context = getActivity();
final DlgState state = getState( sis );
NotAgainView naView = (NotAgainView)
LocUtils.inflate( context, R.layout.not_again_view );
naView.setMessage( state.m_msg );
naView.setShowNACheckbox( null != state.m_onNAChecked );
OnClickListener lstnr = mkCallbackClickListener( naView );
AlertDialog.Builder builder = LocUtils.makeAlertBuilder( context )
.setTitle( state.m_titleId == 0 ? R.string.query_title : state.m_titleId )
.setView( naView )
builder.setTitle( state.m_titleId == 0 ? R.string.query_title : state.m_titleId )
.setPositiveButton( state.m_posButton, lstnr )
.setNegativeButton( state.m_negButton, lstnr );
@ -62,6 +57,5 @@ public class ConfirmThenAlert extends DlgDelegateAlert {
builder.setNeutralButton( pair.buttonStr,
mkCallbackClickListener( pair, naView ) );
}
return builder.create();
}
}

View file

@ -365,7 +365,8 @@ public class DlgDelegate {
final Action action, ActionPair more,
final Object[] params )
{
if ( XWPrefs.getPrefsBoolean( m_activity, prefsKey, false ) ) {
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 ) {
@ -393,7 +394,8 @@ public class DlgDelegate {
{
if ( 0 == nakey ||
! XWPrefs.getPrefsBoolean( m_activity, nakey, false ) ) {
DlgState state = new DlgState( DlgID.CONFIRM_THEN ).setOnNA(onNA)
DlgState state = new DlgState( DlgID.CONFIRM_THEN )
.setOnNA(onNA)
.setMsg( msg )
.setPosButton( posButton )
.setNegButton( negButton )

View file

@ -63,6 +63,32 @@ abstract class DlgDelegateAlert extends XWDialogFragment {
setArguments( state.toBundle() );
}
abstract void populateBuilder( Context context, DlgState state,
AlertDialog.Builder builder,
NotAgainView naView );
Dialog create( AlertDialog.Builder builder ) { return builder.create(); }
@Override
public final Dialog onCreateDialog( Bundle sis )
{
Context context = getActivity();
DlgState state = getState( sis );
NotAgainView naView =
((NotAgainView)LocUtils.inflate( context, R.layout.not_again_view ))
.setMessage( state.m_msg )
.setShowNACheckbox( 0 != state.m_prefsKey );
AlertDialog.Builder builder = LocUtils.makeAlertBuilder( context )
.setView( naView )
;
populateBuilder( context, state, builder, naView );
return create( builder );
}
@Override
public void onSaveInstanceState( Bundle bundle )
{

View file

@ -46,11 +46,10 @@ public class EnableSMSAlert extends DlgDelegateAlert {
public EnableSMSAlert() {}
@Override
public Dialog onCreateDialog( Bundle sis )
public void populateBuilder( Context context, final DlgState state,
AlertDialog.Builder builder,
NotAgainView naView )
{
Context context = getActivity();
final DlgState state = getState( sis );
View layout = LocUtils.inflate( context, R.layout.confirm_sms );
mSpinner = (Spinner)layout.findViewById( R.id.confirm_sms_reasons );
@ -74,16 +73,20 @@ public class EnableSMSAlert extends DlgDelegateAlert {
}
};
AlertDialog dialog = LocUtils.makeAlertBuilder( context )
.setTitle( R.string.confirm_sms_title )
builder.setTitle( R.string.confirm_sms_title )
.setView( layout )
.setPositiveButton( R.string.button_enable, lstnr )
.setNegativeButton( android.R.string.cancel, null )
.create();
;
}
@Override
Dialog create( AlertDialog.Builder builder )
{
Dialog dialog = super.create( builder );
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
public void onShow( DialogInterface dialog ) {
checkEnableButton( (Dialog)dialog );
}
});

View file

@ -53,12 +53,10 @@ public class InviteChoicesAlert extends DlgDelegateAlert {
public InviteChoicesAlert() {}
@Override
public Dialog onCreateDialog( Bundle sis )
public void populateBuilder( final Context context, final DlgState state,
AlertDialog.Builder builder,
NotAgainView naView )
{
final Context context = getActivity();
final DlgState state = getState( sis );
final ArrayList<InviteMeans> means =
new ArrayList<InviteMeans>();
ArrayList<String> items = new ArrayList<String>();
@ -156,8 +154,7 @@ public class InviteChoicesAlert extends DlgDelegateAlert {
}
};
AlertDialog.Builder builder = LocUtils.makeAlertBuilder( context )
.setTitle( R.string.invite_choice_title )
builder.setTitle( R.string.invite_choice_title )
.setSingleChoiceItems( items.toArray( new String[items.size()] ),
sel[0], selChanged )
.setPositiveButton( android.R.string.ok, okClicked )
@ -176,8 +173,6 @@ public class InviteChoicesAlert extends DlgDelegateAlert {
};
builder.setNeutralButton( R.string.ok_with_robots, ocl );
}
return builder.create();
}
private void add( List<String> items, List<InviteMeans> means,

View file

@ -46,18 +46,11 @@ public class NotAgainAlert extends DlgDelegateAlert {
public NotAgainAlert() {}
@Override
public Dialog onCreateDialog( Bundle sis )
public void populateBuilder( Context context, DlgState state,
AlertDialog.Builder builder,
NotAgainView naView )
{
Context context = getActivity();
DlgState state = getState( sis );
NotAgainView naView = (NotAgainView)
LocUtils.inflate( context, R.layout.not_again_view );
naView.setMessage( state.m_msg );
AlertDialog.Builder builder = LocUtils.makeAlertBuilder( context )
.setTitle( R.string.newbie_title )
.setView( naView )
builder.setTitle( R.string.newbie_title )
.setPositiveButton( android.R.string.ok,
mkCallbackClickListener( naView ) );
@ -66,7 +59,5 @@ public class NotAgainAlert extends DlgDelegateAlert {
builder.setNegativeButton( pair.buttonStr,
mkCallbackClickListener( pair, naView ) );
}
return builder.create();
}
}

View file

@ -28,14 +28,16 @@ import android.widget.ScrollView;
import android.widget.TextView;
public class NotAgainView extends ScrollView {
private static final String TAG = NotAgainView.class.getSimpleName();
public NotAgainView( Context cx, AttributeSet as ) {
super( cx, as );
}
public void setMessage( String msg )
public NotAgainView setMessage( String msg )
{
((TextView)findViewById( R.id.msg )).setText( msg );
return this;
}
public boolean getChecked()
@ -44,9 +46,10 @@ public class NotAgainView extends ScrollView {
return cbx.isChecked();
}
public void setShowNACheckbox( boolean show )
public NotAgainView setShowNACheckbox( boolean show )
{
findViewById( R.id.not_again_check )
.setVisibility( show ? View.VISIBLE : View.GONE );
return this;
}
}

View file

@ -46,15 +46,11 @@ public class OkOnlyAlert extends DlgDelegateAlert {
public OkOnlyAlert() {}
@Override
public Dialog onCreateDialog( Bundle sis )
public void populateBuilder( Context context, DlgState state,
AlertDialog.Builder builder,
NotAgainView naView )
{
final Context context = getActivity();
final DlgState state = getState( sis );
AlertDialog.Builder builder = LocUtils.makeAlertBuilder( getActivity() )
.setTitle( state.m_titleId == 0 ? R.string.info_title : state.m_titleId )
.setMessage( state.m_msg )
builder.setTitle( state.m_titleId == 0 ? R.string.info_title : state.m_titleId )
.setPositiveButton( android.R.string.ok, null )
;
@ -62,9 +58,5 @@ public class OkOnlyAlert extends DlgDelegateAlert {
if ( null != pair ) {
builder.setNeutralButton( pair.buttonStr, mkCallbackClickListener( pair ) );
}
Dialog dialog = builder.create();
// dialog = setCallbackDismissListener( dialog, state, dlgID );
return dialog;
}
}