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.os.Bundle;
|
14
|
import androidx.fragment.app.FragmentActivity;
|
15
|
import androidx.appcompat.app.AlertDialog;
|
16
|
import android.text.Editable;
|
17
|
import android.text.TextWatcher;
|
18
|
import android.util.TypedValue;
|
19
|
import android.view.View;
|
20
|
import android.view.WindowManager;
|
21
|
import android.widget.Button;
|
22
|
import android.widget.EditText;
|
23
|
import android.widget.TextView;
|
24
|
|
25
|
import org.distorted.main.R;
|
26
|
import org.distorted.external.RubikScores;
|
27
|
import org.distorted.playui.PlayActivity;
|
28
|
|
29
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
30
|
|
31
|
public class RubikDialogSetName extends RubikDialogAbstract
|
32
|
{
|
33
|
private static final int MAX_NAME_LEN = 15;
|
34
|
private EditText mEdit;
|
35
|
|
36
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
37
|
|
38
|
@Override
|
39
|
public void onStart()
|
40
|
{
|
41
|
super.onStart();
|
42
|
|
43
|
AlertDialog dialog = (AlertDialog)getDialog();
|
44
|
|
45
|
if( dialog!=null )
|
46
|
{
|
47
|
Button positiveButton = dialog.getButton(Dialog.BUTTON_POSITIVE);
|
48
|
|
49
|
if( positiveButton!=null )
|
50
|
{
|
51
|
String editName = mEdit.getText().toString();
|
52
|
positiveButton.setEnabled(editName.length()>0);
|
53
|
}
|
54
|
}
|
55
|
}
|
56
|
|
57
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
58
|
|
59
|
public int getResource() { return R.layout.dialog_set_name; }
|
60
|
public int getTitleResource() { return mArgument.length()==0 ? R.string.choose_name : R.string.name_taken; }
|
61
|
public boolean hasArgument() { return true; }
|
62
|
public int getPositive() { return R.string.ok; }
|
63
|
public int getNegative() { return -1; }
|
64
|
|
65
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
66
|
|
67
|
public void positiveAction()
|
68
|
{
|
69
|
String name = mEdit.getText().toString();
|
70
|
int len = name.length();
|
71
|
|
72
|
if( len>0 )
|
73
|
{
|
74
|
if( len>MAX_NAME_LEN )
|
75
|
{
|
76
|
name = name.substring(0,MAX_NAME_LEN);
|
77
|
}
|
78
|
|
79
|
name = name.replace(' ', '_');
|
80
|
|
81
|
try
|
82
|
{
|
83
|
PlayActivity act = (PlayActivity)getActivity();
|
84
|
RubikScores.getInstance().setName(name);
|
85
|
Bundle bundle = new Bundle();
|
86
|
bundle.putString("argument", "true");
|
87
|
RubikDialogScores scores = new RubikDialogScores();
|
88
|
scores.setArguments(bundle);
|
89
|
scores.show(act.getSupportFragmentManager(), null);
|
90
|
}
|
91
|
catch(IllegalStateException ex)
|
92
|
{
|
93
|
android.util.Log.e("D", "IllegalStateException trying to display SetName");
|
94
|
}
|
95
|
}
|
96
|
}
|
97
|
|
98
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
99
|
|
100
|
public void negativeAction()
|
101
|
{
|
102
|
|
103
|
}
|
104
|
|
105
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106
|
|
107
|
public void prepareBody(Dialog dialog, View view, FragmentActivity act, float size)
|
108
|
{
|
109
|
TextView text = view.findViewById(R.id.set_name_message);
|
110
|
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
|
111
|
mEdit = view.findViewById(R.id.set_name);
|
112
|
mEdit.setHeight( (int)(2*mTitleSize) );
|
113
|
mEdit.setTextSize(TypedValue.COMPLEX_UNIT_PX, 1.5f*mTitleSize);
|
114
|
|
115
|
if( mArgument.length()==0 )
|
116
|
{
|
117
|
text.setText(R.string.new_name);
|
118
|
}
|
119
|
else
|
120
|
{
|
121
|
text.setText(act.getString(R.string.new_name_try_again, mArgument));
|
122
|
}
|
123
|
|
124
|
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
|
125
|
|
126
|
mEdit.requestFocus();
|
127
|
|
128
|
mEdit.addTextChangedListener(new TextWatcher()
|
129
|
{
|
130
|
@Override
|
131
|
public void afterTextChanged(Editable s) {}
|
132
|
|
133
|
@Override
|
134
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
135
|
|
136
|
@Override
|
137
|
public void onTextChanged(CharSequence s, int start, int before, int count)
|
138
|
{
|
139
|
((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(count>0);
|
140
|
}
|
141
|
});
|
142
|
}
|
143
|
}
|