Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialog / RubikDialogSetName.java @ 53f23b64

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
        int sizeIndex = RubikObjectList.getSizeIndex(object,size);
126

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

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

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

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

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

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

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

    
159
    return dialog;
160
    }
161
  }
(8-8/9)