Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogNewRecord.java @ 8ab435b9

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.network.RubikScores;
41
import org.distorted.objectlib.main.ObjectType;
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.switchScreen(act, ScreenList.PLAY);
79

    
80
        if( name.length()>0 )
81
          {
82
          RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
83
          ObjectType object = play.getObject();
84

    
85
          bundle.putInt("tab", object.ordinal() );
86
          bundle.putBoolean("submitting", true);
87

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

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

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

    
113
    Bundle args = getArguments();
114
    long time;
115

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

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

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

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

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

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

    
155
    return dialog;
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
  public static String getDialogTag()
161
    {
162
    return "DialogNewRecord";
163
    }
164
  }
(5-5/19)