Revision c02fa107
Added by Leszek Koltunski almost 3 years ago
| src/main/java/org/distorted/dialogs/RubikDialogSetName.java | ||
|---|---|---|
| 12 | 12 | import android.app.Dialog; | 
| 13 | 13 | import android.content.DialogInterface; | 
| 14 | 14 | import android.os.Bundle; | 
| 15 | import androidx.annotation.NonNull; | |
| 16 | 15 | import androidx.fragment.app.FragmentActivity; | 
| 17 | 16 | import androidx.appcompat.app.AlertDialog; | 
| 18 | import androidx.appcompat.app.AppCompatDialogFragment; | |
| 19 | 17 | import android.text.Editable; | 
| 20 | 18 | import android.text.TextWatcher; | 
| 21 | import android.util.DisplayMetrics; | |
| 22 | 19 | import android.util.TypedValue; | 
| 23 | import android.view.LayoutInflater; | |
| 24 | 20 | import android.view.View; | 
| 25 | import android.view.Window; | |
| 26 | 21 | import android.view.WindowManager; | 
| 27 | 22 | import android.widget.Button; | 
| 28 | 23 | import android.widget.EditText; | 
| ... | ... | |
| 31 | 26 | import org.distorted.main.R; | 
| 32 | 27 | import org.distorted.main.RubikActivity; | 
| 33 | 28 | import org.distorted.external.RubikScores; | 
| 34 | import org.distorted.objects.RubikObjectList; | |
| 35 | 29 | import org.distorted.screens.ScreenList; | 
| 36 | 30 |  | 
| 37 | 31 | /////////////////////////////////////////////////////////////////////////////////////////////////// | 
| 38 | 32 |  | 
| 39 | public class RubikDialogSetName extends AppCompatDialogFragment
 | |
| 33 | public class RubikDialogSetName extends RubikDialogAbstract
 | |
| 40 | 34 |   {
 | 
| 41 | 35 | private static final int MAX_NAME_LEN = 15; | 
| 42 | 36 | private EditText mEdit; | 
| ... | ... | |
| 64 | 58 |  | 
| 65 | 59 | /////////////////////////////////////////////////////////////////////////////////////////////////// | 
| 66 | 60 |  | 
| 67 | @NonNull | |
| 68 | @Override | |
| 69 | public Dialog onCreateDialog(Bundle savedInstanceState) | |
| 61 | public int getResource() | |
| 70 | 62 |     {
 | 
| 71 | FragmentActivity act = getActivity(); | |
| 72 | LayoutInflater inflater = act.getLayoutInflater(); | |
| 73 | AlertDialog.Builder builder = new AlertDialog.Builder(act); | |
| 74 |  | |
| 75 | DisplayMetrics displaymetrics = new DisplayMetrics(); | |
| 76 | act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); | |
| 77 | int h = displaymetrics.heightPixels; | |
| 78 | final float titleSize= h*0.032f; | |
| 79 | final float okSize = h*0.040f; | |
| 80 | final float textSize = h*0.022f; | |
| 81 |  | |
| 82 | Bundle args = getArguments(); | |
| 83 | String name; | |
| 84 |  | |
| 85 | try | |
| 86 |       {
 | |
| 87 |       name = args.getString("name");
 | |
| 88 | } | |
| 89 | catch(Exception e) | |
| 90 |       {
 | |
| 91 | name = ""; | |
| 92 | } | |
| 63 | return R.layout.dialog_set_name; | |
| 64 | } | |
| 93 | 65 |  | 
| 94 |     boolean first = name.length()==0;
 | |
| 66 | ///////////////////////////////////////////////////////////////////////////////////////////////////
 | |
| 95 | 67 |  | 
| 96 |     TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
 | |
| 97 |     tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
 | |
| 98 |     tv.setText( first ? R.string.choose_name : R.string.name_taken);
 | |
| 99 |     builder.setCustomTitle(tv);
 | |
| 68 |   public int getTitleResource()
 | |
| 69 |     {
 | |
| 70 |     return mArgument.length()==0 ? R.string.choose_name : R.string.name_taken;
 | |
| 71 |     }
 | |
| 100 | 72 |  | 
| 101 | final View view = inflater.inflate(R.layout.dialog_set_name, null); | |
| 102 | TextView text = view.findViewById(R.id.set_name_message); | |
| 103 | text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); | |
| 104 | mEdit = view.findViewById(R.id.set_name); | |
| 105 | mEdit.setHeight( (int)(2*titleSize) ); | |
| 106 | mEdit.setTextSize(TypedValue.COMPLEX_UNIT_PX, 1.5f*titleSize); | |
| 73 | /////////////////////////////////////////////////////////////////////////////////////////////////// | |
| 107 | 74 |  | 
| 108 | if( first ) | |
| 109 |       {
 | |
| 110 | text.setText(R.string.new_name); | |
| 111 | } | |
| 112 | else | |
| 113 |       {
 | |
| 114 | text.setText(act.getString(R.string.new_name_try_again, name)); | |
| 115 | } | |
| 75 | public boolean hasArgument() | |
| 76 |     {
 | |
| 77 | return true; | |
| 78 | } | |
| 116 | 79 |  | 
| 117 |     builder.setCancelable(true);
 | |
| 80 | ///////////////////////////////////////////////////////////////////////////////////////////////////
 | |
| 118 | 81 |  | 
| 82 | public void setPositive(AlertDialog.Builder builder) | |
| 83 |     {
 | |
| 119 | 84 | builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener() | 
| 120 | 85 |       {
 | 
| 121 | 86 | @Override | 
| ... | ... | |
| 140 | 105 | RubikScores.getInstance().setName(name); | 
| 141 | 106 |  | 
| 142 | 107 | Bundle bundle = new Bundle(); | 
| 143 |             bundle.putInt("tab", RubikObjectList.getCurrObject() );
 | |
| 144 |             bundle.putBoolean("submitting", true);
 | |
| 145 |  | |
| 108 |             bundle.putString("argument", "true");
 | |
| 146 | 109 | RubikDialogScores scores = new RubikDialogScores(); | 
| 147 | 110 | scores.setArguments(bundle); | 
| 148 | 111 | scores.show(act.getSupportFragmentManager(), null); | 
| ... | ... | |
| 154 | 117 | } | 
| 155 | 118 | } | 
| 156 | 119 | }); | 
| 120 | } | |
| 121 |  | |
| 122 | /////////////////////////////////////////////////////////////////////////////////////////////////// | |
| 123 |  | |
| 124 | public void setNegative(AlertDialog.Builder builder) | |
| 125 |     {
 | |
| 126 |  | |
| 127 | } | |
| 128 |  | |
| 129 | /////////////////////////////////////////////////////////////////////////////////////////////////// | |
| 130 |  | |
| 131 | public void onShowDialog(DialogInterface dialog, float size) | |
| 132 |     {
 | |
| 133 | Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE); | |
| 134 | btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); | |
| 135 | } | |
| 136 |  | |
| 137 | /////////////////////////////////////////////////////////////////////////////////////////////////// | |
| 138 |  | |
| 139 | public void prepareBody(Dialog dialog, View view, FragmentActivity act, float size) | |
| 140 |     {
 | |
| 141 | TextView text = view.findViewById(R.id.set_name_message); | |
| 142 | text.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); | |
| 143 | mEdit = view.findViewById(R.id.set_name); | |
| 144 | mEdit.setHeight( (int)(2*mTitleSize) ); | |
| 145 | mEdit.setTextSize(TypedValue.COMPLEX_UNIT_PX, 1.5f*mTitleSize); | |
| 146 |  | |
| 147 | if( mArgument.length()==0 ) | |
| 148 |       {
 | |
| 149 | text.setText(R.string.new_name); | |
| 150 | } | |
| 151 | else | |
| 152 |       {
 | |
| 153 | text.setText(act.getString(R.string.new_name_try_again, mArgument)); | |
| 154 | } | |
| 157 | 155 |  | 
| 158 | builder.setView(view); | |
| 159 | final Dialog dialog = builder.create(); | |
| 160 | 156 | dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); | 
| 161 | 157 |  | 
| 162 | 158 | mEdit.requestFocus(); | 
| ... | ... | |
| 175 | 171 | ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(count>0); | 
| 176 | 172 | } | 
| 177 | 173 | }); | 
| 178 |  | |
| 179 | dialog.setCanceledOnTouchOutside(false); | |
| 180 | Window window = dialog.getWindow(); | |
| 181 |  | |
| 182 | if( window!=null ) | |
| 183 |       {
 | |
| 184 | window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS); | |
| 185 | } | |
| 186 |  | |
| 187 | dialog.setOnShowListener(new DialogInterface.OnShowListener() | |
| 188 |       {
 | |
| 189 | @Override | |
| 190 | public void onShow(DialogInterface dialog) | |
| 191 |         {
 | |
| 192 | Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE); | |
| 193 | btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, okSize); | |
| 194 | } | |
| 195 | }); | |
| 196 |  | |
| 197 | return dialog; | |
| 198 | 174 | } | 
| 199 | 175 | } | 
Also available in: Unified diff
Continue unifying all the dialogs under one RubikDialogAbstract.