Project

General

Profile

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

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

1 f895e77a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 68191e7d Leszek Koltunski
// 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 f895e77a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10 1f9772f3 Leszek Koltunski
package org.distorted.dialogs;
11 f895e77a Leszek Koltunski
12
import android.app.Dialog;
13
import android.content.DialogInterface;
14
import android.os.Bundle;
15 66e777b0 Leszek Koltunski
import androidx.fragment.app.FragmentActivity;
16
import androidx.appcompat.app.AlertDialog;
17 1bdf3dc2 Leszek Koltunski
import android.text.Editable;
18
import android.text.TextWatcher;
19 fb37b424 Leszek Koltunski
import android.util.TypedValue;
20 f895e77a Leszek Koltunski
import android.view.View;
21
import android.view.WindowManager;
22 1bdf3dc2 Leszek Koltunski
import android.widget.Button;
23 f895e77a Leszek Koltunski
import android.widget.EditText;
24
import android.widget.TextView;
25
26 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
27
import org.distorted.main.RubikActivity;
28 acabdd83 Leszek Koltunski
import org.distorted.external.RubikScores;
29 fcd5b990 Leszek Koltunski
import org.distorted.screens.ScreenList;
30 f895e77a Leszek Koltunski
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32
33 c02fa107 Leszek Koltunski
public class RubikDialogSetName extends RubikDialogAbstract
34 f895e77a Leszek Koltunski
  {
35 f4ee4d70 Leszek Koltunski
  private static final int MAX_NAME_LEN = 15;
36 90fd47b0 Leszek Koltunski
  private EditText mEdit;
37
38 2d942f35 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
39
40
  @Override
41
  public void onStart()
42
    {
43
    super.onStart();
44
45
    AlertDialog dialog = (AlertDialog)getDialog();
46
47
    if( dialog!=null )
48
      {
49
      Button positiveButton = dialog.getButton(Dialog.BUTTON_POSITIVE);
50
51
      if( positiveButton!=null )
52
        {
53
        String editName = mEdit.getText().toString();
54
        positiveButton.setEnabled(editName.length()>0);
55
        }
56
      }
57
    }
58
59 f895e77a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61 c02fa107 Leszek Koltunski
  public int getResource()
62 f895e77a Leszek Koltunski
    {
63 c02fa107 Leszek Koltunski
    return R.layout.dialog_set_name;
64
    }
65 f895e77a Leszek Koltunski
66 c02fa107 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
67 f895e77a Leszek Koltunski
68 c02fa107 Leszek Koltunski
  public int getTitleResource()
69
    {
70
    return mArgument.length()==0 ? R.string.choose_name : R.string.name_taken;
71
    }
72 f895e77a Leszek Koltunski
73 c02fa107 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
74 f895e77a Leszek Koltunski
75 c02fa107 Leszek Koltunski
  public boolean hasArgument()
76
    {
77
    return true;
78
    }
79 f895e77a Leszek Koltunski
80 c02fa107 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
81 db266f25 Leszek Koltunski
82 c02fa107 Leszek Koltunski
  public void setPositive(AlertDialog.Builder builder)
83
    {
84 f895e77a Leszek Koltunski
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
85
      {
86
      @Override
87
      public void onClick(DialogInterface dialog, int which)
88
        {
89 90fd47b0 Leszek Koltunski
        String name = mEdit.getText().toString();
90 f4ee4d70 Leszek Koltunski
        int len = name.length();
91 90fd47b0 Leszek Koltunski
92 f4ee4d70 Leszek Koltunski
        if( len>0 )
93 90fd47b0 Leszek Koltunski
          {
94 f4ee4d70 Leszek Koltunski
          if( len>MAX_NAME_LEN )
95
            {
96
            name = name.substring(0,MAX_NAME_LEN);
97
            }
98
99 c0125648 Leszek Koltunski
          name = name.replace(' ', '_');
100
101 67179b40 Leszek Koltunski
          try
102
            {
103
            RubikActivity act = (RubikActivity)getActivity();
104
            ScreenList.switchScreen(act, ScreenList.PLAY);
105
            RubikScores.getInstance().setName(name);
106 90fd47b0 Leszek Koltunski
107 67179b40 Leszek Koltunski
            Bundle bundle = new Bundle();
108 c02fa107 Leszek Koltunski
            bundle.putString("argument", "true");
109 67179b40 Leszek Koltunski
            RubikDialogScores scores = new RubikDialogScores();
110
            scores.setArguments(bundle);
111
            scores.show(act.getSupportFragmentManager(), null);
112
            }
113
          catch(IllegalStateException ex)
114
            {
115
            android.util.Log.e("D", "IllegalStateException trying to display SetName");
116
            }
117 90fd47b0 Leszek Koltunski
          }
118 f895e77a Leszek Koltunski
        }
119
      });
120 c02fa107 Leszek Koltunski
    }
121
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
124
  public void setNegative(AlertDialog.Builder builder)
125
    {
126
127
    }
128
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
131
  public void onShowDialog(DialogInterface dialog, float size)
132
    {
133
    Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
134
    btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
135
    }
136
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
139
  public void prepareBody(Dialog dialog, View view, FragmentActivity act, float size)
140
    {
141
    TextView text = view.findViewById(R.id.set_name_message);
142
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
143
    mEdit = view.findViewById(R.id.set_name);
144
    mEdit.setHeight( (int)(2*mTitleSize) );
145
    mEdit.setTextSize(TypedValue.COMPLEX_UNIT_PX, 1.5f*mTitleSize);
146
147
    if( mArgument.length()==0 )
148
      {
149
      text.setText(R.string.new_name);
150
      }
151
    else
152
      {
153
      text.setText(act.getString(R.string.new_name_try_again, mArgument));
154
      }
155 f895e77a Leszek Koltunski
156 db266f25 Leszek Koltunski
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
157 f895e77a Leszek Koltunski
158 90fd47b0 Leszek Koltunski
    mEdit.requestFocus();
159 1bdf3dc2 Leszek Koltunski
160 90fd47b0 Leszek Koltunski
    mEdit.addTextChangedListener(new TextWatcher()
161 1bdf3dc2 Leszek Koltunski
      {
162
      @Override
163
      public void afterTextChanged(Editable s) {}
164
165
      @Override
166
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
167
168
      @Override
169
      public void onTextChanged(CharSequence s, int start, int before, int count)
170
        {
171
        ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(count>0);
172
        }
173
      });
174 f895e77a Leszek Koltunski
    }
175
  }