Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogSetName.java @ 05f6d7bd

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 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
42
import org.distorted.main.RubikActivity;
43 acabdd83 Leszek Koltunski
import org.distorted.external.RubikScores;
44 400ff34d Leszek Koltunski
import org.distorted.objects.RubikObjectList;
45 fcd5b990 Leszek Koltunski
import org.distorted.screens.ScreenList;
46 f895e77a Leszek Koltunski
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49
public class RubikDialogSetName extends AppCompatDialogFragment
50
  {
51 f4ee4d70 Leszek Koltunski
  private static final int MAX_NAME_LEN = 15;
52 90fd47b0 Leszek Koltunski
  private EditText mEdit;
53
54 2d942f35 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 f895e77a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 fb37b424 Leszek Koltunski
    DisplayMetrics displaymetrics = new DisplayMetrics();
86
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
87
    final float titleSize= displaymetrics.widthPixels * RubikActivity.MENU_BIG_TEXT_SIZE;
88 eb376d3a Leszek Koltunski
    final float okSize   = displaymetrics.widthPixels * RubikActivity.DIALOG_BUTTON_SIZE;
89 fb37b424 Leszek Koltunski
    final float textSize = displaymetrics.widthPixels * RubikActivity.MENU_SMALL_TEXT_SIZE;
90
91 f895e77a Leszek Koltunski
    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 fb37b424 Leszek Koltunski
    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
107 f895e77a Leszek Koltunski
    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 fb37b424 Leszek Koltunski
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
113 90fd47b0 Leszek Koltunski
    mEdit = view.findViewById(R.id.set_name);
114 fb37b424 Leszek Koltunski
    mEdit.setHeight( (int)(2*titleSize) );
115
    mEdit.setTextSize(TypedValue.COMPLEX_UNIT_PX, 1.5f*titleSize);
116 f895e77a Leszek Koltunski
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 db266f25 Leszek Koltunski
128 f895e77a Leszek Koltunski
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
129
      {
130
      @Override
131
      public void onClick(DialogInterface dialog, int which)
132
        {
133 90fd47b0 Leszek Koltunski
        String name = mEdit.getText().toString();
134 f4ee4d70 Leszek Koltunski
        int len = name.length();
135 90fd47b0 Leszek Koltunski
136 f4ee4d70 Leszek Koltunski
        if( len>0 )
137 90fd47b0 Leszek Koltunski
          {
138 f4ee4d70 Leszek Koltunski
          if( len>MAX_NAME_LEN )
139
            {
140
            name = name.substring(0,MAX_NAME_LEN);
141
            }
142
143 c0125648 Leszek Koltunski
          name = name.replace(' ', '_');
144
145 90fd47b0 Leszek Koltunski
          RubikActivity act = (RubikActivity)getActivity();
146 f5da732a Leszek Koltunski
          ScreenList.switchScreen(act, ScreenList.PLAY);
147 90fd47b0 Leszek Koltunski
          RubikScores.getInstance().setName(name);
148
149
          Bundle bundle = new Bundle();
150 400ff34d Leszek Koltunski
          bundle.putInt("tab", RubikObjectList.getCurrObject() );
151 90fd47b0 Leszek Koltunski
          bundle.putBoolean("submitting", true);
152
153
          RubikDialogScores scores = new RubikDialogScores();
154
          scores.setArguments(bundle);
155
          scores.show(act.getSupportFragmentManager(), null);
156
          }
157 f895e77a Leszek Koltunski
        }
158
      });
159
160
    builder.setView(view);
161 1bdf3dc2 Leszek Koltunski
    final Dialog dialog = builder.create();
162 db266f25 Leszek Koltunski
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
163 f895e77a Leszek Koltunski
164 90fd47b0 Leszek Koltunski
    mEdit.requestFocus();
165 1bdf3dc2 Leszek Koltunski
166 90fd47b0 Leszek Koltunski
    mEdit.addTextChangedListener(new TextWatcher()
167 1bdf3dc2 Leszek Koltunski
      {
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 85248b04 Leszek Koltunski
    dialog.setCanceledOnTouchOutside(false);
182
    Window window = dialog.getWindow();
183
184
    if( window!=null )
185
      {
186 61a7b812 Leszek Koltunski
      window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS);
187 85248b04 Leszek Koltunski
      }
188
189 fb37b424 Leszek Koltunski
    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 db266f25 Leszek Koltunski
    return dialog;
200 f895e77a Leszek Koltunski
    }
201
  }