Project

General

Profile

Download (7.27 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / dialogs / RubikDialogSetName.java @ 7ac0ee88

1 f895e77a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 1f9772f3 Leszek Koltunski
package org.distorted.dialogs;
21 f895e77a Leszek Koltunski
22
import android.app.Dialog;
23
import android.content.DialogInterface;
24
import android.os.Bundle;
25 66e777b0 Leszek Koltunski
import androidx.annotation.NonNull;
26
import androidx.fragment.app.FragmentActivity;
27
import androidx.appcompat.app.AlertDialog;
28
import androidx.appcompat.app.AppCompatDialogFragment;
29 1bdf3dc2 Leszek Koltunski
import android.text.Editable;
30
import android.text.TextWatcher;
31 fb37b424 Leszek Koltunski
import android.util.DisplayMetrics;
32
import android.util.TypedValue;
33 f895e77a Leszek Koltunski
import android.view.LayoutInflater;
34
import android.view.View;
35
import android.view.Window;
36
import android.view.WindowManager;
37 1bdf3dc2 Leszek Koltunski
import android.widget.Button;
38 f895e77a Leszek Koltunski
import android.widget.EditText;
39
import android.widget.TextView;
40
41 3f7a4363 Leszek Koltunski
import org.distorted.objectlib.main.ObjectList;
42
43 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
44
import org.distorted.main.RubikActivity;
45 6a083c6a Leszek Koltunski
import org.distorted.network.RubikScores;
46 fcd5b990 Leszek Koltunski
import org.distorted.screens.ScreenList;
47
import org.distorted.screens.RubikScreenPlay;
48 f895e77a Leszek Koltunski
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51
public class RubikDialogSetName extends AppCompatDialogFragment
52
  {
53 f4ee4d70 Leszek Koltunski
  private static final int MAX_NAME_LEN = 15;
54 90fd47b0 Leszek Koltunski
  private EditText mEdit;
55
56 2d942f35 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58
  @Override
59
  public void onStart()
60
    {
61
    super.onStart();
62
63
    AlertDialog dialog = (AlertDialog)getDialog();
64
65
    if( dialog!=null )
66
      {
67
      Button positiveButton = dialog.getButton(Dialog.BUTTON_POSITIVE);
68
69
      if( positiveButton!=null )
70
        {
71
        String editName = mEdit.getText().toString();
72
        positiveButton.setEnabled(editName.length()>0);
73
        }
74
      }
75
    }
76
77 f895e77a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
78
79
  @NonNull
80
  @Override
81
  public Dialog onCreateDialog(Bundle savedInstanceState)
82
    {
83
    FragmentActivity act = getActivity();
84
    LayoutInflater inflater = act.getLayoutInflater();
85
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
86
87 fb37b424 Leszek Koltunski
    DisplayMetrics displaymetrics = new DisplayMetrics();
88
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
89
    final float titleSize= displaymetrics.widthPixels * RubikActivity.MENU_BIG_TEXT_SIZE;
90 eb376d3a Leszek Koltunski
    final float okSize   = displaymetrics.widthPixels * RubikActivity.DIALOG_BUTTON_SIZE;
91 fb37b424 Leszek Koltunski
    final float textSize = displaymetrics.widthPixels * RubikActivity.MENU_SMALL_TEXT_SIZE;
92
93 f895e77a Leszek Koltunski
    Bundle args = getArguments();
94
    String name;
95
96
    try
97
      {
98
      name = args.getString("name");
99
      }
100
    catch(Exception e)
101
      {
102
      name = "";
103
      }
104
105
    boolean first = name.length()==0;
106
107
    TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
108 fb37b424 Leszek Koltunski
    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
109 f895e77a Leszek Koltunski
    tv.setText( first ? R.string.choose_name : R.string.name_taken);
110
    builder.setCustomTitle(tv);
111
112
    final View view = inflater.inflate(R.layout.dialog_set_name, null);
113
    TextView text = view.findViewById(R.id.set_name_message);
114 fb37b424 Leszek Koltunski
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
115 90fd47b0 Leszek Koltunski
    mEdit = view.findViewById(R.id.set_name);
116 fb37b424 Leszek Koltunski
    mEdit.setHeight( (int)(2*titleSize) );
117
    mEdit.setTextSize(TypedValue.COMPLEX_UNIT_PX, 1.5f*titleSize);
118 f895e77a Leszek Koltunski
119
    if( first )
120
      {
121
      text.setText(R.string.new_name);
122
      }
123
    else
124
      {
125
      text.setText(act.getString(R.string.new_name_try_again, name));
126
      }
127
128
    builder.setCancelable(true);
129 db266f25 Leszek Koltunski
130 f895e77a Leszek Koltunski
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
131
      {
132
      @Override
133
      public void onClick(DialogInterface dialog, int which)
134
        {
135 90fd47b0 Leszek Koltunski
        String name = mEdit.getText().toString();
136 f4ee4d70 Leszek Koltunski
        int len = name.length();
137 90fd47b0 Leszek Koltunski
138 f4ee4d70 Leszek Koltunski
        if( len>0 )
139 90fd47b0 Leszek Koltunski
          {
140 f4ee4d70 Leszek Koltunski
          if( len>MAX_NAME_LEN )
141
            {
142
            name = name.substring(0,MAX_NAME_LEN);
143
            }
144
145 c0125648 Leszek Koltunski
          name = name.replace(' ', '_');
146
147 90fd47b0 Leszek Koltunski
          RubikActivity act = (RubikActivity)getActivity();
148 f5da732a Leszek Koltunski
          ScreenList.switchScreen(act, ScreenList.PLAY);
149 90fd47b0 Leszek Koltunski
          RubikScores.getInstance().setName(name);
150 f5da732a Leszek Koltunski
          RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
151 90fd47b0 Leszek Koltunski
152
          Bundle bundle = new Bundle();
153 7ac0ee88 Leszek Koltunski
          bundle.putInt("tab", play.getObject() );
154 90fd47b0 Leszek Koltunski
          bundle.putBoolean("submitting", true);
155
156
          RubikDialogScores scores = new RubikDialogScores();
157
          scores.setArguments(bundle);
158
          scores.show(act.getSupportFragmentManager(), null);
159
          }
160 f895e77a Leszek Koltunski
        }
161
      });
162
163
    builder.setView(view);
164 1bdf3dc2 Leszek Koltunski
    final Dialog dialog = builder.create();
165 db266f25 Leszek Koltunski
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
166 f895e77a Leszek Koltunski
167 90fd47b0 Leszek Koltunski
    mEdit.requestFocus();
168 1bdf3dc2 Leszek Koltunski
169 90fd47b0 Leszek Koltunski
    mEdit.addTextChangedListener(new TextWatcher()
170 1bdf3dc2 Leszek Koltunski
      {
171
      @Override
172
      public void afterTextChanged(Editable s) {}
173
174
      @Override
175
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
176
177
      @Override
178
      public void onTextChanged(CharSequence s, int start, int before, int count)
179
        {
180
        ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(count>0);
181
        }
182
      });
183
184 85248b04 Leszek Koltunski
    dialog.setCanceledOnTouchOutside(false);
185
    Window window = dialog.getWindow();
186
187
    if( window!=null )
188
      {
189 e365b6c2 Leszek Koltunski
      window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS2);
190 85248b04 Leszek Koltunski
      }
191
192 fb37b424 Leszek Koltunski
    dialog.setOnShowListener(new DialogInterface.OnShowListener()
193
      {
194
      @Override
195
      public void onShow(DialogInterface dialog)
196
        {
197
        Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
198
        btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, okSize);
199
        }
200
      });
201
202 db266f25 Leszek Koltunski
    return dialog;
203 f895e77a Leszek Koltunski
    }
204
  }