1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2020 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.dialogs;
|
11
|
|
12
|
import android.app.Dialog;
|
13
|
import android.content.DialogInterface;
|
14
|
import android.os.Bundle;
|
15
|
import androidx.annotation.NonNull;
|
16
|
import androidx.fragment.app.FragmentActivity;
|
17
|
import androidx.appcompat.app.AlertDialog;
|
18
|
import androidx.appcompat.app.AppCompatDialogFragment;
|
19
|
import android.text.Editable;
|
20
|
import android.text.TextWatcher;
|
21
|
import android.util.DisplayMetrics;
|
22
|
import android.util.TypedValue;
|
23
|
import android.view.LayoutInflater;
|
24
|
import android.view.View;
|
25
|
import android.view.Window;
|
26
|
import android.view.WindowManager;
|
27
|
import android.widget.Button;
|
28
|
import android.widget.EditText;
|
29
|
import android.widget.TextView;
|
30
|
|
31
|
import org.distorted.main.R;
|
32
|
import org.distorted.main.RubikActivity;
|
33
|
import org.distorted.external.RubikScores;
|
34
|
import org.distorted.objects.RubikObjectList;
|
35
|
import org.distorted.screens.ScreenList;
|
36
|
|
37
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38
|
|
39
|
public class RubikDialogSetName extends AppCompatDialogFragment
|
40
|
{
|
41
|
private static final int MAX_NAME_LEN = 15;
|
42
|
private EditText mEdit;
|
43
|
|
44
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
45
|
|
46
|
@Override
|
47
|
public void onStart()
|
48
|
{
|
49
|
super.onStart();
|
50
|
|
51
|
AlertDialog dialog = (AlertDialog)getDialog();
|
52
|
|
53
|
if( dialog!=null )
|
54
|
{
|
55
|
Button positiveButton = dialog.getButton(Dialog.BUTTON_POSITIVE);
|
56
|
|
57
|
if( positiveButton!=null )
|
58
|
{
|
59
|
String editName = mEdit.getText().toString();
|
60
|
positiveButton.setEnabled(editName.length()>0);
|
61
|
}
|
62
|
}
|
63
|
}
|
64
|
|
65
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
66
|
|
67
|
@NonNull
|
68
|
@Override
|
69
|
public Dialog onCreateDialog(Bundle savedInstanceState)
|
70
|
{
|
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
|
final float titleSize= displaymetrics.widthPixels * RubikActivity.MENU_BIG_TEXT_SIZE;
|
78
|
final float okSize = displaymetrics.widthPixels * RubikActivity.DIALOG_BUTTON_SIZE;
|
79
|
final float textSize = displaymetrics.widthPixels * RubikActivity.MENU_SMALL_TEXT_SIZE;
|
80
|
|
81
|
Bundle args = getArguments();
|
82
|
String name;
|
83
|
|
84
|
try
|
85
|
{
|
86
|
name = args.getString("name");
|
87
|
}
|
88
|
catch(Exception e)
|
89
|
{
|
90
|
name = "";
|
91
|
}
|
92
|
|
93
|
boolean first = name.length()==0;
|
94
|
|
95
|
TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
|
96
|
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
|
97
|
tv.setText( first ? R.string.choose_name : R.string.name_taken);
|
98
|
builder.setCustomTitle(tv);
|
99
|
|
100
|
final View view = inflater.inflate(R.layout.dialog_set_name, null);
|
101
|
TextView text = view.findViewById(R.id.set_name_message);
|
102
|
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
|
103
|
mEdit = view.findViewById(R.id.set_name);
|
104
|
mEdit.setHeight( (int)(2*titleSize) );
|
105
|
mEdit.setTextSize(TypedValue.COMPLEX_UNIT_PX, 1.5f*titleSize);
|
106
|
|
107
|
if( first )
|
108
|
{
|
109
|
text.setText(R.string.new_name);
|
110
|
}
|
111
|
else
|
112
|
{
|
113
|
text.setText(act.getString(R.string.new_name_try_again, name));
|
114
|
}
|
115
|
|
116
|
builder.setCancelable(true);
|
117
|
|
118
|
builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
|
119
|
{
|
120
|
@Override
|
121
|
public void onClick(DialogInterface dialog, int which)
|
122
|
{
|
123
|
String name = mEdit.getText().toString();
|
124
|
int len = name.length();
|
125
|
|
126
|
if( len>0 )
|
127
|
{
|
128
|
if( len>MAX_NAME_LEN )
|
129
|
{
|
130
|
name = name.substring(0,MAX_NAME_LEN);
|
131
|
}
|
132
|
|
133
|
name = name.replace(' ', '_');
|
134
|
|
135
|
RubikActivity act = (RubikActivity)getActivity();
|
136
|
ScreenList.switchScreen(act, ScreenList.PLAY);
|
137
|
RubikScores.getInstance().setName(name);
|
138
|
|
139
|
Bundle bundle = new Bundle();
|
140
|
bundle.putInt("tab", RubikObjectList.getCurrObject() );
|
141
|
bundle.putBoolean("submitting", true);
|
142
|
|
143
|
RubikDialogScores scores = new RubikDialogScores();
|
144
|
scores.setArguments(bundle);
|
145
|
scores.show(act.getSupportFragmentManager(), null);
|
146
|
}
|
147
|
}
|
148
|
});
|
149
|
|
150
|
builder.setView(view);
|
151
|
final Dialog dialog = builder.create();
|
152
|
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
|
153
|
|
154
|
mEdit.requestFocus();
|
155
|
|
156
|
mEdit.addTextChangedListener(new TextWatcher()
|
157
|
{
|
158
|
@Override
|
159
|
public void afterTextChanged(Editable s) {}
|
160
|
|
161
|
@Override
|
162
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
163
|
|
164
|
@Override
|
165
|
public void onTextChanged(CharSequence s, int start, int before, int count)
|
166
|
{
|
167
|
((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(count>0);
|
168
|
}
|
169
|
});
|
170
|
|
171
|
dialog.setCanceledOnTouchOutside(false);
|
172
|
Window window = dialog.getWindow();
|
173
|
|
174
|
if( window!=null )
|
175
|
{
|
176
|
window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS);
|
177
|
}
|
178
|
|
179
|
dialog.setOnShowListener(new DialogInterface.OnShowListener()
|
180
|
{
|
181
|
@Override
|
182
|
public void onShow(DialogInterface dialog)
|
183
|
{
|
184
|
Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
|
185
|
btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, okSize);
|
186
|
}
|
187
|
});
|
188
|
|
189
|
return dialog;
|
190
|
}
|
191
|
}
|