Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogSetName.java @ 2d942f35

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.dialogs;
21

    
22
import android.app.Dialog;
23
import android.content.DialogInterface;
24
import android.os.Bundle;
25
import androidx.annotation.NonNull;
26
import androidx.fragment.app.FragmentActivity;
27
import androidx.appcompat.app.AlertDialog;
28
import androidx.appcompat.app.AppCompatDialogFragment;
29
import android.text.Editable;
30
import android.text.TextWatcher;
31
import android.view.LayoutInflater;
32
import android.view.View;
33
import android.view.Window;
34
import android.view.WindowManager;
35
import android.widget.Button;
36
import android.widget.EditText;
37
import android.widget.TextView;
38

    
39
import org.distorted.main.R;
40
import org.distorted.main.RubikActivity;
41
import org.distorted.objects.RubikObjectList;
42
import org.distorted.scores.RubikScores;
43
import org.distorted.states.RubikState;
44
import org.distorted.states.RubikStatePlay;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
public class RubikDialogSetName extends AppCompatDialogFragment
49
  {
50
  private EditText mEdit;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  @Override
55
  public void onStart()
56
    {
57
    super.onStart();
58

    
59
    AlertDialog dialog = (AlertDialog)getDialog();
60

    
61
    if( dialog!=null )
62
      {
63
      Button positiveButton = dialog.getButton(Dialog.BUTTON_POSITIVE);
64

    
65
      if( positiveButton!=null )
66
        {
67
        String editName = mEdit.getText().toString();
68
        positiveButton.setEnabled(editName.length()>0);
69
        }
70
      }
71
    }
72

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

    
75
  @NonNull
76
  @Override
77
  public Dialog onCreateDialog(Bundle savedInstanceState)
78
    {
79
    FragmentActivity act = getActivity();
80
    LayoutInflater inflater = act.getLayoutInflater();
81
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
82

    
83
    Bundle args = getArguments();
84
    String name;
85

    
86
    try
87
      {
88
      name = args.getString("name");
89
      }
90
    catch(Exception e)
91
      {
92
      name = "";
93
      }
94

    
95
    boolean first = name.length()==0;
96

    
97
    TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
98
    tv.setText( first ? R.string.choose_name : R.string.name_taken);
99
    builder.setCustomTitle(tv);
100

    
101
    final View view = inflater.inflate(R.layout.dialog_set_name, null);
102
    TextView text = view.findViewById(R.id.set_name_message);
103
    mEdit = view.findViewById(R.id.set_name);
104

    
105
    if( first )
106
      {
107
      text.setText(R.string.new_name);
108
      }
109
    else
110
      {
111
      text.setText(act.getString(R.string.new_name_try_again, name));
112
      }
113

    
114
    builder.setCancelable(true);
115

    
116
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
117
      {
118
      @Override
119
      public void onClick(DialogInterface dialog, int which)
120
        {
121
        String name = mEdit.getText().toString();
122

    
123
        if( name.length()>0 )
124
          {
125
          RubikActivity act = (RubikActivity)getActivity();
126
          RubikState.switchState(act,RubikState.PLAY);
127
          RubikScores.getInstance().setName(name);
128
          RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
129

    
130
          int object = play.getObject();
131
          int size   = play.getSize();
132
          int sizeIndex = RubikObjectList.getSizeIndex(object,size);
133

    
134
          Bundle bundle = new Bundle();
135
          bundle.putInt("tab", RubikObjectList.pack(object,sizeIndex) );
136
          bundle.putBoolean("submitting", true);
137

    
138
          RubikDialogScores scores = new RubikDialogScores();
139
          scores.setArguments(bundle);
140
          scores.show(act.getSupportFragmentManager(), null);
141
          }
142
        }
143
      });
144

    
145
    builder.setView(view);
146
    final Dialog dialog = builder.create();
147
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
148

    
149
    mEdit.requestFocus();
150

    
151
    mEdit.addTextChangedListener(new TextWatcher()
152
      {
153
      @Override
154
      public void afterTextChanged(Editable s) {}
155

    
156
      @Override
157
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
158

    
159
      @Override
160
      public void onTextChanged(CharSequence s, int start, int before, int count)
161
        {
162
        ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(count>0);
163
        }
164
      });
165

    
166
    dialog.setCanceledOnTouchOutside(false);
167

    
168
    Window window = dialog.getWindow();
169

    
170
    if( window!=null )
171
      {
172
      window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
173
      }
174

    
175
    return dialog;
176
    }
177
  }
(11-11/13)