Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialog / RubikDialogSetName.java @ f895e77a

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.view.LayoutInflater;
30
import android.view.View;
31
import android.view.Window;
32
import android.view.WindowManager;
33
import android.widget.EditText;
34
import android.widget.TextView;
35

    
36
import org.distorted.magic.R;
37
import org.distorted.magic.RubikActivity;
38
import org.distorted.object.RubikObjectList;
39
import org.distorted.scores.RubikScores;
40
import org.distorted.uistate.RubikState;
41
import org.distorted.uistate.RubikStatePlay;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
public class RubikDialogSetName extends AppCompatDialogFragment
46
  {
47
  @Override
48
  public void onStart()
49
    {
50
    super.onStart();
51

    
52
    Window window = getDialog().getWindow();
53
    window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
54
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
55
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
56
    }
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  @NonNull
61
  @Override
62
  public Dialog onCreateDialog(Bundle savedInstanceState)
63
    {
64
    FragmentActivity act = getActivity();
65
    LayoutInflater inflater = act.getLayoutInflater();
66
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
67

    
68
    Bundle args = getArguments();
69
    String name;
70

    
71
    try
72
      {
73
      name = args.getString("name");
74
      }
75
    catch(Exception e)
76
      {
77
      name = "";
78
      }
79

    
80
    boolean first = name.length()==0;
81

    
82
    TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
83
    tv.setText( first ? R.string.choose_name : R.string.name_taken);
84
    builder.setCustomTitle(tv);
85

    
86
    final View view = inflater.inflate(R.layout.dialog_set_name, null);
87
    TextView text = view.findViewById(R.id.set_name_message);
88

    
89
    if( first )
90
      {
91
      text.setText(R.string.new_name);
92
      }
93
    else
94
      {
95
      text.setText(act.getString(R.string.new_name_try_again, name));
96
      }
97

    
98
    builder.setCancelable(true);
99
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
100
      {
101
      @Override
102
      public void onClick(DialogInterface dialog, int which)
103
        {
104
        RubikActivity act = (RubikActivity)getActivity();
105
        RubikState.switchState(act,RubikState.PLAY);
106

    
107
        EditText edit = view.findViewById(R.id.set_name);
108
        String name = edit.getText().toString();
109
        RubikScores.getInstance().setName(name);
110

    
111
        RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
112
        int object = play.getObject();
113
        int size   = play.getSize();
114

    
115
        Bundle bundle = new Bundle();
116
        bundle.putInt("tab", RubikObjectList.pack(object,size) );
117
        bundle.putBoolean("submitting", true);
118

    
119
        RubikDialogScores scores = new RubikDialogScores();
120
        scores.setArguments(bundle);
121
        scores.show(act.getSupportFragmentManager(), null);
122
        }
123
      });
124

    
125
    builder.setView(view);
126

    
127
    return builder.create();
128
    }
129
  }
(7-7/9)