Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogSetName.java @ 58990dfd

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

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

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

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

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
  @Override
40
  public void onStart()
41
    {
42
    super.onStart();
43

    
44
    AlertDialog dialog = (AlertDialog)getDialog();
45

    
46
    if( dialog!=null )
47
      {
48
      Button positiveButton = dialog.getButton(Dialog.BUTTON_POSITIVE);
49

    
50
      if( positiveButton!=null )
51
        {
52
        String editName = mEdit.getText().toString();
53
        positiveButton.setEnabled(editName.length()>0);
54
        }
55
      }
56
    }
57

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

    
60
  public int getResource()      { return R.layout.dialog_set_name; }
61
  public int getTitleResource() { return mArgument.length()==0 ? R.string.choose_name : R.string.name_taken; }
62
  public boolean hasArgument()  { return true; }
63
  public int getPositive()      { return R.string.ok; }
64
  public int getNegative()      { return -1; }
65

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

    
68
  public void positiveAction()
69
    {
70
    String name = mEdit.getText().toString();
71
    int len = name.length();
72

    
73
    if( len>0 )
74
      {
75
      if( len>MAX_NAME_LEN )
76
        {
77
        name = name.substring(0,MAX_NAME_LEN);
78
        }
79

    
80
      name = name.replace(' ', '_');
81

    
82
      try
83
        {
84
        RubikActivity act = (RubikActivity)getActivity();
85
        ScreenList.switchScreen(act, ScreenList.PLAY);
86
        RubikScores.getInstance().setName(name);
87

    
88
        Bundle bundle = new Bundle();
89
        bundle.putString("argument", "true");
90
        RubikDialogScores scores = new RubikDialogScores();
91
        scores.setArguments(bundle);
92
        scores.show(act.getSupportFragmentManager(), null);
93
        }
94
      catch(IllegalStateException ex)
95
        {
96
        android.util.Log.e("D", "IllegalStateException trying to display SetName");
97
        }
98
      }
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
  public void negativeAction()
104
    {
105

    
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
  public void prepareBody(Dialog dialog, View view, FragmentActivity act, float size)
111
    {
112
    TextView text = view.findViewById(R.id.set_name_message);
113
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
114
    mEdit = view.findViewById(R.id.set_name);
115
    mEdit.setHeight( (int)(2*mTitleSize) );
116
    mEdit.setTextSize(TypedValue.COMPLEX_UNIT_PX, 1.5f*mTitleSize);
117

    
118
    if( mArgument.length()==0 )
119
      {
120
      text.setText(R.string.new_name);
121
      }
122
    else
123
      {
124
      text.setText(act.getString(R.string.new_name_try_again, mArgument));
125
      }
126

    
127
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
128

    
129
    mEdit.requestFocus();
130

    
131
    mEdit.addTextChangedListener(new TextWatcher()
132
      {
133
      @Override
134
      public void afterTextChanged(Editable s) {}
135

    
136
      @Override
137
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
138

    
139
      @Override
140
      public void onTextChanged(CharSequence s, int start, int before, int count)
141
        {
142
        ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(count>0);
143
        }
144
      });
145
    }
146
  }
(16-16/26)