Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogNewRecord.java @ fcd5b990

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

    
30
import android.util.DisplayMetrics;
31
import android.util.TypedValue;
32
import android.view.LayoutInflater;
33
import android.view.View;
34
import android.view.Window;
35
import android.widget.Button;
36
import android.widget.TextView;
37

    
38
import org.distorted.main.R;
39
import org.distorted.main.RubikActivity;
40
import org.distorted.objects.ObjectList;
41
import org.distorted.network.RubikScores;
42
import org.distorted.screens.ScreenList;
43
import org.distorted.screens.RubikScreenPlay;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
public class RubikDialogNewRecord extends AppCompatDialogFragment
48
  {
49
  @NonNull
50
  @Override
51
  public Dialog onCreateDialog(Bundle savedInstanceState)
52
    {
53
    FragmentActivity act = getActivity();
54
    LayoutInflater inflater = act.getLayoutInflater();
55
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
56

    
57
    DisplayMetrics displaymetrics = new DisplayMetrics();
58
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
59
    final float titleSize= displaymetrics.widthPixels * RubikActivity.MENU_BIG_TEXT_SIZE;
60
    final float okSize   = displaymetrics.widthPixels * RubikActivity.DIALOG_BUTTON_SIZE;
61
    final float textSize = displaymetrics.widthPixels * RubikActivity.MENU_BIG_TEXT_SIZE;
62

    
63
    TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
64
    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
65
    tv.setText(R.string.new_record);
66
    builder.setCustomTitle(tv);
67
    builder.setCancelable(true);
68

    
69
    builder.setPositiveButton( R.string.yes, new DialogInterface.OnClickListener()
70
      {
71
      @Override
72
      public void onClick(DialogInterface dialog, int which)
73
        {
74
        RubikActivity act = (RubikActivity)getActivity();
75
        RubikScores scores = RubikScores.getInstance();
76
        String name = scores.getName();
77
        Bundle bundle = new Bundle();
78
        ScreenList.switchState(act, ScreenList.PLAY);
79

    
80
        if( name.length()>0 )
81
          {
82
          RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getStateClass();
83
          int object = play.getObject();
84
          int size   = play.getSize();
85
          int sizeIndex = ObjectList.getSizeIndex(object,size);
86

    
87
          bundle.putInt("tab", ObjectList.pack(object,sizeIndex) );
88
          bundle.putBoolean("submitting", true);
89

    
90
          RubikDialogScores scoresDiag = new RubikDialogScores();
91
          scoresDiag.setArguments(bundle);
92
          scoresDiag.show(act.getSupportFragmentManager(), null);
93
          }
94
        else
95
          {
96
          bundle.putString("name", name );
97

    
98
          RubikDialogSetName nameDiag = new RubikDialogSetName();
99
          nameDiag.setArguments(bundle);
100
          nameDiag.show(act.getSupportFragmentManager(), null);
101
          }
102
        }
103
      });
104

    
105
    builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener()
106
      {
107
      @Override
108
      public void onClick(DialogInterface dialog, int which)
109
        {
110
        RubikActivity act = (RubikActivity)getActivity();
111
        ScreenList.switchState(act, ScreenList.PLAY);
112
        }
113
      });
114

    
115
    Bundle args = getArguments();
116
    long time;
117

    
118
    try
119
      {
120
      time = args.getLong("time");
121
      }
122
    catch(Exception e)
123
      {
124
      time = 0;
125
      }
126

    
127
    final View view = inflater.inflate(R.layout.dialog_new_record, null);
128
    TextView text = view.findViewById(R.id.new_record_time);
129
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
130
    text.setText(getString(R.string.ti_placeholder, (time/10)/100.0f));
131
    builder.setView(view);
132

    
133
    TextView submit = view.findViewById(R.id.new_record_submit);
134
    submit.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
135

    
136
    Dialog dialog = builder.create();
137
    dialog.setCanceledOnTouchOutside(false);
138
    Window window = dialog.getWindow();
139

    
140
    if( window!=null )
141
      {
142
      window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS);
143
      }
144

    
145
    dialog.setOnShowListener(new DialogInterface.OnShowListener()
146
      {
147
      @Override
148
      public void onShow(DialogInterface dialog)
149
        {
150
        Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
151
        btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, okSize);
152
        Button btnNegative = ((AlertDialog)dialog).getButton(Dialog.BUTTON_NEGATIVE);
153
        btnNegative.setTextSize(TypedValue.COMPLEX_UNIT_PX, okSize);
154
        }
155
      });
156

    
157
    return dialog;
158
    }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
  public static String getDialogTag()
163
    {
164
    return "DialogNewRecord";
165
    }
166
  }
(4-4/18)