Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogSetName.java @ acabdd83

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.dialogs;
21

    
22
import android.app.Dialog;
23
import android.content.DialogInterface;
24
import android.os.Bundle;
25
import androidx.annotation.NonNull;
26
import androidx.fragment.app.FragmentActivity;
27
import androidx.appcompat.app.AlertDialog;
28
import androidx.appcompat.app.AppCompatDialogFragment;
29
import android.text.Editable;
30
import android.text.TextWatcher;
31
import android.util.DisplayMetrics;
32
import android.util.TypedValue;
33
import android.view.LayoutInflater;
34
import android.view.View;
35
import android.view.Window;
36
import android.view.WindowManager;
37
import android.widget.Button;
38
import android.widget.EditText;
39
import android.widget.TextView;
40

    
41
import org.distorted.main.R;
42
import org.distorted.main.RubikActivity;
43
import org.distorted.external.RubikScores;
44
import org.distorted.objects.RubikObjectList;
45
import org.distorted.screens.ScreenList;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
public class RubikDialogSetName extends AppCompatDialogFragment
50
  {
51
  private static final int MAX_NAME_LEN = 15;
52
  private EditText mEdit;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
  @Override
57
  public void onStart()
58
    {
59
    super.onStart();
60

    
61
    AlertDialog dialog = (AlertDialog)getDialog();
62

    
63
    if( dialog!=null )
64
      {
65
      Button positiveButton = dialog.getButton(Dialog.BUTTON_POSITIVE);
66

    
67
      if( positiveButton!=null )
68
        {
69
        String editName = mEdit.getText().toString();
70
        positiveButton.setEnabled(editName.length()>0);
71
        }
72
      }
73
    }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
  @NonNull
78
  @Override
79
  public Dialog onCreateDialog(Bundle savedInstanceState)
80
    {
81
    FragmentActivity act = getActivity();
82
    LayoutInflater inflater = act.getLayoutInflater();
83
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
84

    
85
    DisplayMetrics displaymetrics = new DisplayMetrics();
86
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
87
    final float titleSize= displaymetrics.widthPixels * RubikActivity.MENU_BIG_TEXT_SIZE;
88
    final float okSize   = displaymetrics.widthPixels * RubikActivity.DIALOG_BUTTON_SIZE;
89
    final float textSize = displaymetrics.widthPixels * RubikActivity.MENU_SMALL_TEXT_SIZE;
90

    
91
    Bundle args = getArguments();
92
    String name;
93

    
94
    try
95
      {
96
      name = args.getString("name");
97
      }
98
    catch(Exception e)
99
      {
100
      name = "";
101
      }
102

    
103
    boolean first = name.length()==0;
104

    
105
    TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
106
    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
107
    tv.setText( first ? R.string.choose_name : R.string.name_taken);
108
    builder.setCustomTitle(tv);
109

    
110
    final View view = inflater.inflate(R.layout.dialog_set_name, null);
111
    TextView text = view.findViewById(R.id.set_name_message);
112
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
113
    mEdit = view.findViewById(R.id.set_name);
114
    mEdit.setHeight( (int)(2*titleSize) );
115
    mEdit.setTextSize(TypedValue.COMPLEX_UNIT_PX, 1.5f*titleSize);
116

    
117
    if( first )
118
      {
119
      text.setText(R.string.new_name);
120
      }
121
    else
122
      {
123
      text.setText(act.getString(R.string.new_name_try_again, name));
124
      }
125

    
126
    builder.setCancelable(true);
127

    
128
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
129
      {
130
      @Override
131
      public void onClick(DialogInterface dialog, int which)
132
        {
133
        String name = mEdit.getText().toString();
134
        int len = name.length();
135

    
136
        if( len>0 )
137
          {
138
          if( len>MAX_NAME_LEN )
139
            {
140
            name = name.substring(0,MAX_NAME_LEN);
141
            }
142

    
143
          name = name.replace(' ', '_');
144

    
145
          RubikActivity act = (RubikActivity)getActivity();
146
          ScreenList.switchScreen(act, ScreenList.PLAY);
147
          RubikScores.getInstance().setName(name);
148

    
149
          Bundle bundle = new Bundle();
150
          bundle.putInt("tab", RubikObjectList.getCurrObject() );
151
          bundle.putBoolean("submitting", true);
152

    
153
          RubikDialogScores scores = new RubikDialogScores();
154
          scores.setArguments(bundle);
155
          scores.show(act.getSupportFragmentManager(), null);
156
          }
157
        }
158
      });
159

    
160
    builder.setView(view);
161
    final Dialog dialog = builder.create();
162
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
163

    
164
    mEdit.requestFocus();
165

    
166
    mEdit.addTextChangedListener(new TextWatcher()
167
      {
168
      @Override
169
      public void afterTextChanged(Editable s) {}
170

    
171
      @Override
172
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
173

    
174
      @Override
175
      public void onTextChanged(CharSequence s, int start, int before, int count)
176
        {
177
        ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(count>0);
178
        }
179
      });
180

    
181
    dialog.setCanceledOnTouchOutside(false);
182
    Window window = dialog.getWindow();
183

    
184
    if( window!=null )
185
      {
186
      window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS2);
187
      }
188

    
189
    dialog.setOnShowListener(new DialogInterface.OnShowListener()
190
      {
191
      @Override
192
      public void onShow(DialogInterface dialog)
193
        {
194
        Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
195
        btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, okSize);
196
        }
197
      });
198

    
199
    return dialog;
200
    }
201
  }
(14-14/21)