Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogNewRecord.java @ 3f7a4363

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.objectlib.main.ObjectList;
39

    
40
import org.distorted.main.R;
41
import org.distorted.main.RubikActivity;
42
import org.distorted.network.RubikScores;
43
import org.distorted.screens.ScreenList;
44
import org.distorted.screens.RubikScreenPlay;
45

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
158
    return dialog;
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  public static String getDialogTag()
164
    {
165
    return "DialogNewRecord";
166
    }
167
  }
(5-5/19)