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 |
f895e77a
|
Leszek Koltunski
|
import android.view.LayoutInflater;
|
32 |
|
|
import android.view.View;
|
33 |
|
|
import android.view.Window;
|
34 |
|
|
import android.view.WindowManager;
|
35 |
1bdf3dc2
|
Leszek Koltunski
|
import android.widget.Button;
|
36 |
f895e77a
|
Leszek Koltunski
|
import android.widget.EditText;
|
37 |
|
|
import android.widget.TextView;
|
38 |
|
|
|
39 |
1f9772f3
|
Leszek Koltunski
|
import org.distorted.main.R;
|
40 |
|
|
import org.distorted.main.RubikActivity;
|
41 |
|
|
import org.distorted.objects.RubikObjectList;
|
42 |
f895e77a
|
Leszek Koltunski
|
import org.distorted.scores.RubikScores;
|
43 |
1f9772f3
|
Leszek Koltunski
|
import org.distorted.states.RubikState;
|
44 |
|
|
import org.distorted.states.RubikStatePlay;
|
45 |
f895e77a
|
Leszek Koltunski
|
|
46 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
47 |
|
|
|
48 |
|
|
public class RubikDialogSetName extends AppCompatDialogFragment
|
49 |
|
|
{
|
50 |
f4ee4d70
|
Leszek Koltunski
|
private static final int MAX_NAME_LEN = 15;
|
51 |
90fd47b0
|
Leszek Koltunski
|
private EditText mEdit;
|
52 |
|
|
|
53 |
2d942f35
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54 |
|
|
|
55 |
|
|
@Override
|
56 |
|
|
public void onStart()
|
57 |
|
|
{
|
58 |
|
|
super.onStart();
|
59 |
|
|
|
60 |
|
|
AlertDialog dialog = (AlertDialog)getDialog();
|
61 |
|
|
|
62 |
|
|
if( dialog!=null )
|
63 |
|
|
{
|
64 |
|
|
Button positiveButton = dialog.getButton(Dialog.BUTTON_POSITIVE);
|
65 |
|
|
|
66 |
|
|
if( positiveButton!=null )
|
67 |
|
|
{
|
68 |
|
|
String editName = mEdit.getText().toString();
|
69 |
|
|
positiveButton.setEnabled(editName.length()>0);
|
70 |
|
|
}
|
71 |
|
|
}
|
72 |
|
|
}
|
73 |
|
|
|
74 |
f895e77a
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
75 |
|
|
|
76 |
|
|
@NonNull
|
77 |
|
|
@Override
|
78 |
|
|
public Dialog onCreateDialog(Bundle savedInstanceState)
|
79 |
|
|
{
|
80 |
|
|
FragmentActivity act = getActivity();
|
81 |
|
|
LayoutInflater inflater = act.getLayoutInflater();
|
82 |
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(act);
|
83 |
|
|
|
84 |
|
|
Bundle args = getArguments();
|
85 |
|
|
String name;
|
86 |
|
|
|
87 |
|
|
try
|
88 |
|
|
{
|
89 |
|
|
name = args.getString("name");
|
90 |
|
|
}
|
91 |
|
|
catch(Exception e)
|
92 |
|
|
{
|
93 |
|
|
name = "";
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
boolean first = name.length()==0;
|
97 |
|
|
|
98 |
|
|
TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
|
99 |
|
|
tv.setText( first ? R.string.choose_name : R.string.name_taken);
|
100 |
|
|
builder.setCustomTitle(tv);
|
101 |
|
|
|
102 |
|
|
final View view = inflater.inflate(R.layout.dialog_set_name, null);
|
103 |
|
|
TextView text = view.findViewById(R.id.set_name_message);
|
104 |
90fd47b0
|
Leszek Koltunski
|
mEdit = view.findViewById(R.id.set_name);
|
105 |
f895e77a
|
Leszek Koltunski
|
|
106 |
|
|
if( first )
|
107 |
|
|
{
|
108 |
|
|
text.setText(R.string.new_name);
|
109 |
|
|
}
|
110 |
|
|
else
|
111 |
|
|
{
|
112 |
|
|
text.setText(act.getString(R.string.new_name_try_again, name));
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
builder.setCancelable(true);
|
116 |
db266f25
|
Leszek Koltunski
|
|
117 |
f895e77a
|
Leszek Koltunski
|
builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
|
118 |
|
|
{
|
119 |
|
|
@Override
|
120 |
|
|
public void onClick(DialogInterface dialog, int which)
|
121 |
|
|
{
|
122 |
90fd47b0
|
Leszek Koltunski
|
String name = mEdit.getText().toString();
|
123 |
f4ee4d70
|
Leszek Koltunski
|
int len = name.length();
|
124 |
90fd47b0
|
Leszek Koltunski
|
|
125 |
f4ee4d70
|
Leszek Koltunski
|
if( len>0 )
|
126 |
90fd47b0
|
Leszek Koltunski
|
{
|
127 |
f4ee4d70
|
Leszek Koltunski
|
if( len>MAX_NAME_LEN )
|
128 |
|
|
{
|
129 |
|
|
name = name.substring(0,MAX_NAME_LEN);
|
130 |
|
|
}
|
131 |
|
|
|
132 |
90fd47b0
|
Leszek Koltunski
|
RubikActivity act = (RubikActivity)getActivity();
|
133 |
|
|
RubikState.switchState(act,RubikState.PLAY);
|
134 |
|
|
RubikScores.getInstance().setName(name);
|
135 |
|
|
RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
|
136 |
|
|
|
137 |
|
|
int object = play.getObject();
|
138 |
|
|
int size = play.getSize();
|
139 |
|
|
int sizeIndex = RubikObjectList.getSizeIndex(object,size);
|
140 |
|
|
|
141 |
|
|
Bundle bundle = new Bundle();
|
142 |
|
|
bundle.putInt("tab", RubikObjectList.pack(object,sizeIndex) );
|
143 |
|
|
bundle.putBoolean("submitting", true);
|
144 |
|
|
|
145 |
|
|
RubikDialogScores scores = new RubikDialogScores();
|
146 |
|
|
scores.setArguments(bundle);
|
147 |
|
|
scores.show(act.getSupportFragmentManager(), null);
|
148 |
|
|
}
|
149 |
f895e77a
|
Leszek Koltunski
|
}
|
150 |
|
|
});
|
151 |
|
|
|
152 |
|
|
builder.setView(view);
|
153 |
1bdf3dc2
|
Leszek Koltunski
|
final Dialog dialog = builder.create();
|
154 |
db266f25
|
Leszek Koltunski
|
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
|
155 |
f895e77a
|
Leszek Koltunski
|
|
156 |
90fd47b0
|
Leszek Koltunski
|
mEdit.requestFocus();
|
157 |
1bdf3dc2
|
Leszek Koltunski
|
|
158 |
90fd47b0
|
Leszek Koltunski
|
mEdit.addTextChangedListener(new TextWatcher()
|
159 |
1bdf3dc2
|
Leszek Koltunski
|
{
|
160 |
|
|
@Override
|
161 |
|
|
public void afterTextChanged(Editable s) {}
|
162 |
|
|
|
163 |
|
|
@Override
|
164 |
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
165 |
|
|
|
166 |
|
|
@Override
|
167 |
|
|
public void onTextChanged(CharSequence s, int start, int before, int count)
|
168 |
|
|
{
|
169 |
|
|
((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(count>0);
|
170 |
|
|
}
|
171 |
|
|
});
|
172 |
|
|
|
173 |
85248b04
|
Leszek Koltunski
|
dialog.setCanceledOnTouchOutside(false);
|
174 |
|
|
|
175 |
|
|
Window window = dialog.getWindow();
|
176 |
|
|
|
177 |
|
|
if( window!=null )
|
178 |
|
|
{
|
179 |
|
|
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
|
180 |
|
|
}
|
181 |
|
|
|
182 |
db266f25
|
Leszek Koltunski
|
return dialog;
|
183 |
f895e77a
|
Leszek Koltunski
|
}
|
184 |
|
|
}
|