Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialog / RubikDialogSetName.java @ 1bdf3dc2

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.dialog;
21

    
22
import android.app.Dialog;
23
import android.content.DialogInterface;
24
import android.os.Bundle;
25
import android.support.annotation.NonNull;
26
import android.support.v4.app.FragmentActivity;
27
import android.support.v7.app.AlertDialog;
28
import android.support.v7.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.magic.R;
40
import org.distorted.magic.RubikActivity;
41
import org.distorted.object.RubikObjectList;
42
import org.distorted.scores.RubikScores;
43
import org.distorted.uistate.RubikState;
44
import org.distorted.uistate.RubikStatePlay;
45

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

    
48
public class RubikDialogSetName extends AppCompatDialogFragment
49
  {
50
  @Override
51
  public void onStart()
52
    {
53
    super.onStart();
54

    
55
    Window window = getDialog().getWindow();
56
    window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
57
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
58
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
59

    
60
    AlertDialog dialog = (AlertDialog) getDialog();
61
    if (dialog != null)
62
      {
63
      Button positiveButton = dialog.getButton(Dialog.BUTTON_POSITIVE);
64
      positiveButton.setEnabled(false);
65
      }
66
    }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
  @NonNull
71
  @Override
72
  public Dialog onCreateDialog(Bundle savedInstanceState)
73
    {
74
    FragmentActivity act = getActivity();
75
    LayoutInflater inflater = act.getLayoutInflater();
76
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
77

    
78
    Bundle args = getArguments();
79
    String name;
80

    
81
    try
82
      {
83
      name = args.getString("name");
84
      }
85
    catch(Exception e)
86
      {
87
      name = "";
88
      }
89

    
90
    boolean first = name.length()==0;
91

    
92
    TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
93
    tv.setText( first ? R.string.choose_name : R.string.name_taken);
94
    builder.setCustomTitle(tv);
95

    
96
    final View view = inflater.inflate(R.layout.dialog_set_name, null);
97
    TextView text = view.findViewById(R.id.set_name_message);
98

    
99
    if( first )
100
      {
101
      text.setText(R.string.new_name);
102
      }
103
    else
104
      {
105
      text.setText(act.getString(R.string.new_name_try_again, name));
106
      }
107

    
108
    builder.setCancelable(true);
109

    
110
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
111
      {
112
      @Override
113
      public void onClick(DialogInterface dialog, int which)
114
        {
115
        RubikActivity act = (RubikActivity)getActivity();
116
        RubikState.switchState(act,RubikState.PLAY);
117

    
118
        EditText edit = view.findViewById(R.id.set_name);
119
        String name = edit.getText().toString();
120
        RubikScores.getInstance().setName(name);
121

    
122
        RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
123
        int object = play.getObject();
124
        int size   = play.getSize();
125

    
126
        Bundle bundle = new Bundle();
127
        bundle.putInt("tab", RubikObjectList.pack(object,size) );
128
        bundle.putBoolean("submitting", true);
129

    
130
        RubikDialogScores scores = new RubikDialogScores();
131
        scores.setArguments(bundle);
132
        scores.show(act.getSupportFragmentManager(), null);
133
        }
134
      });
135

    
136
    builder.setView(view);
137
    final Dialog dialog = builder.create();
138
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
139

    
140
    EditText edit = view.findViewById(R.id.set_name);
141
    edit.requestFocus();
142

    
143
    edit.addTextChangedListener(new TextWatcher()
144
      {
145
      @Override
146
      public void afterTextChanged(Editable s) {}
147

    
148
      @Override
149
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
150

    
151
      @Override
152
      public void onTextChanged(CharSequence s, int start, int before, int count)
153
        {
154
        ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(count>0);
155
        }
156
      });
157

    
158
    return dialog;
159
    }
160
  }
(7-7/9)