From ddd5f7b978708b1d777f5337d61a0b48c3a6bf8d Mon Sep 17 00:00:00 2001 From: Eric House Date: Tue, 19 Jan 2021 13:40:08 -0800 Subject: [PATCH] remember last-used radio and start with it next time Saves a step if you generally create games the same way most of the time. Required reordering alert init. --- .../android/xw4/GamesListDelegate.java | 30 +++++++++++-------- .../eehouse/android/xw4/NewWithKnowns.java | 26 ++++++++++++++-- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GamesListDelegate.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GamesListDelegate.java index cef778d82..83ec6388b 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GamesListDelegate.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GamesListDelegate.java @@ -919,7 +919,6 @@ public class GamesListDelegate extends ListDelegateBase // String[] names = XwJNI.kplr_getPlayers(); final NewWithKnowns view = (NewWithKnowns) LocUtils.inflate( m_activity, R.layout.new_game_with_knowns ); - view.configure( standalone, GameUtils.makeDefaultName( m_activity ) ); AlertDialog.Builder ab = makeAlertBuilder() .setView( view ) .setTitle( standalone ? R.string.new_game : R.string.new_game_networked ) @@ -964,17 +963,24 @@ public class GamesListDelegate extends ListDelegateBase } final AlertDialog dialog = ab.create(); - view.setCallback( new NewWithKnowns.ButtonChangeListener() { - @Override - public void onNewButtonText( String txt ) { - Button button = dialog.getButton( DialogInterface.BUTTON_POSITIVE ); - if ( null != button ) { - button.setText( txt ); - } else { - Log.e( TAG, "Button still null" ); - } - } - } ); + + dialog.setOnShowListener(new DialogInterface.OnShowListener() { + @Override + public void onShow( DialogInterface diface ) { + view.setCallback( new NewWithKnowns.ButtonChangeListener() { + @Override + public void onNewButtonText( String txt ) { + Button button = dialog.getButton( DialogInterface.BUTTON_POSITIVE ); + if ( null != button ) { + button.setText( txt ); + } else { + Assert.failDbg(); + } + } + } ); + view.configure( standalone, GameUtils.makeDefaultName( m_activity ) ); + } + }); return dialog; } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/NewWithKnowns.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/NewWithKnowns.java index 5a15ad66a..fdf5334eb 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/NewWithKnowns.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/NewWithKnowns.java @@ -27,6 +27,7 @@ import android.widget.AdapterView.OnItemSelectedListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.LinearLayout; +import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Spinner; import android.widget.TextView; @@ -39,6 +40,8 @@ public class NewWithKnowns extends LinearLayout { private static final String TAG = NewWithKnowns.class.getSimpleName(); private static final String KP_NAME_KEY = TAG + "/kp_last_name"; + private static final String KP_PREVSOLO_KEY = TAG + "/kp_prev_solo"; + private static final String KP_PREVNET_KEY = TAG + "/kp_prev_net"; public interface ButtonChangeListener { void onNewButtonText( String txt ); @@ -68,15 +71,16 @@ public class NewWithKnowns extends LinearLayout void configure( boolean standalone, String gameName ) { + Context context = getContext(); mStandalone = standalone; boolean hasKnowns = !standalone && XwJNI.hasKnownPlayers(); int[] toHide; if ( hasKnowns ) { String[] knowns = XwJNI.kplr_getPlayers(); - mCurKnown = DBUtils.getStringFor( getContext(), KP_NAME_KEY, + mCurKnown = DBUtils.getStringFor( context, KP_NAME_KEY, knowns[0] ); ArrayAdapter adapter = new - ArrayAdapter( getContext(), + ArrayAdapter( context, android.R.layout.simple_spinner_item, knowns ); adapter.setDropDownViewResource( android.R.layout @@ -108,14 +112,27 @@ public class NewWithKnowns extends LinearLayout RadioGroup group = (RadioGroup)findViewById( R.id.group ); group.setOnCheckedChangeListener( this ); + + String key = standalone ? KP_PREVSOLO_KEY : KP_PREVNET_KEY; + int lastSet = DBUtils.getIntFor( context, key, 0 ); + if ( lastSet != 0 ) { + // Let's made sure it's still a RadioButton. Ids are generated by + // the build system and can change. Passing a non-radiobutton id + // to check() still calls onCheckedChanged() etc. + View view = findViewById( lastSet ); + if ( null != view && view instanceof RadioButton ) { + group.check( lastSet ); + } + } } void onButtonPressed( ButtonCallbacks procs ) { + Context context = getContext(); String gameName = gameName(); switch ( mCurRadio ) { case R.id.radio_known: - DBUtils.setStringFor( getContext(), KP_NAME_KEY, mCurKnown ); + DBUtils.setStringFor( context, KP_NAME_KEY, mCurKnown ); procs.onUseKnown( mCurKnown, gameName ); break; case R.id.radio_unknown: @@ -129,6 +146,9 @@ public class NewWithKnowns extends LinearLayout Assert.failDbg(); break; } + + String key = mStandalone ? KP_PREVSOLO_KEY : KP_PREVNET_KEY; + DBUtils.setIntFor( context, key, mCurRadio ); } private String gameName()