Project

General

Profile

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

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

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.fragment.app.FragmentActivity;
16
import androidx.appcompat.app.AlertDialog;
17
import android.text.Editable;
18
import android.text.TextWatcher;
19
import android.util.TypedValue;
20
import android.view.View;
21
import android.view.WindowManager;
22
import android.widget.Button;
23
import android.widget.EditText;
24
import android.widget.TextView;
25

    
26
import org.distorted.main.R;
27
import org.distorted.main.RubikActivity;
28
import org.distorted.external.RubikScores;
29
import org.distorted.screens.ScreenList;
30

    
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

    
33
public class RubikDialogSetName extends RubikDialogAbstract
34
  {
35
  private static final int MAX_NAME_LEN = 15;
36
  private EditText mEdit;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
  public int getResource()
62
    {
63
    return R.layout.dialog_set_name;
64
    }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
  public int getTitleResource()
69
    {
70
    return mArgument.length()==0 ? R.string.choose_name : R.string.name_taken;
71
    }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

    
75
  public boolean hasArgument()
76
    {
77
    return true;
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  public void setPositive(AlertDialog.Builder builder)
83
    {
84
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
85
      {
86
      @Override
87
      public void onClick(DialogInterface dialog, int which)
88
        {
89
        String name = mEdit.getText().toString();
90
        int len = name.length();
91

    
92
        if( len>0 )
93
          {
94
          if( len>MAX_NAME_LEN )
95
            {
96
            name = name.substring(0,MAX_NAME_LEN);
97
            }
98

    
99
          name = name.replace(' ', '_');
100

    
101
          try
102
            {
103
            RubikActivity act = (RubikActivity)getActivity();
104
            ScreenList.switchScreen(act, ScreenList.PLAY);
105
            RubikScores.getInstance().setName(name);
106

    
107
            Bundle bundle = new Bundle();
108
            bundle.putString("argument", "true");
109
            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
          }
118
        }
119
      });
120
    }
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

    
156
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
157

    
158
    mEdit.requestFocus();
159

    
160
    mEdit.addTextChangedListener(new TextWatcher()
161
      {
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
    }
175
  }
(16-16/25)