Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 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.content.DialogInterface;
14
import android.os.Bundle;
15
import android.util.DisplayMetrics;
16
import android.util.TypedValue;
17
import android.view.LayoutInflater;
18
import android.view.View;
19
import android.view.Window;
20
import android.widget.Button;
21
import android.widget.TextView;
22

    
23
import androidx.annotation.NonNull;
24
import androidx.appcompat.app.AlertDialog;
25
import androidx.appcompat.app.AppCompatDialogFragment;
26
import androidx.fragment.app.FragmentActivity;
27

    
28
import org.distorted.main.R;
29
import org.distorted.main.RubikActivity;
30

    
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

    
33
abstract public class RubikDialogAbstract extends AppCompatDialogFragment
34
  {
35
  static final int PARAMETRIC_TITLE = -10000;
36

    
37
  protected float mTitleSize, mButSize, mTextSize;
38
  protected int mWidth, mHeight;
39
  protected String mArgument;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
  abstract int getResource();
44
  abstract int getTitleResource();
45
  abstract int getPositive();
46
  abstract int getNegative();
47
  abstract boolean hasArgument();
48
  abstract void positiveAction();
49
  abstract void negativeAction();
50
  abstract void prepareBody(Dialog dialog, View view, FragmentActivity act, float size);
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  String getTitleString(FragmentActivity act)
55
    {
56
    return "";
57
    }
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

    
69
    DisplayMetrics displaymetrics = new DisplayMetrics();
70
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
71
    mWidth    = displaymetrics.widthPixels;
72
    mHeight   = displaymetrics.heightPixels;
73
    mTitleSize= mHeight*0.032f;
74
    mButSize  = mHeight*0.040f;
75
    mTextSize = mHeight*0.025f;
76

    
77
    if( hasArgument() )
78
      {
79
      Bundle args = getArguments();
80

    
81
      try
82
        {
83
        mArgument = args!=null ? args.getString("argument") : "";
84
        }
85
      catch(Exception e)
86
        {
87
        mArgument = "";
88
        }
89
      }
90
    else mArgument = "";
91

    
92
    final View view = inflater.inflate(getResource(), null);
93
    builder.setView(view);
94
    builder.setCancelable(true);
95

    
96
    int title = getTitleResource();
97
    if( title>=0 || title==PARAMETRIC_TITLE )
98
      {
99
      TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
100
      tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleSize);
101

    
102
      if( title>=0 )
103
        {
104
        tv.setText(title);
105
        }
106
      else
107
        {
108
        String titleString = getTitleString(act);
109
        tv.setText(titleString);
110
        }
111

    
112
      builder.setCustomTitle(tv);
113
      }
114

    
115
    int positive = getPositive();
116
    if( positive>=0 )
117
      {
118
      builder.setPositiveButton( positive, new DialogInterface.OnClickListener()
119
        {
120
        @Override
121
        public void onClick(DialogInterface dialog, int which)
122
          {
123
          positiveAction();
124
          }
125
        });
126
      }
127

    
128
    int negative = getNegative();
129
    if( negative>=0 )
130
      {
131
      builder.setNegativeButton( negative, new DialogInterface.OnClickListener()
132
        {
133
        @Override
134
        public void onClick(DialogInterface dialog, int which)
135
          {
136
          negativeAction();
137
          }
138
        });
139
      }
140

    
141
    Dialog dialog = builder.create();
142
    dialog.setCanceledOnTouchOutside(false);
143

    
144
    prepareBody(dialog,view,act,mTextSize);
145

    
146
    Window window = dialog.getWindow();
147

    
148
    if( window!=null )
149
      {
150
      window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS);
151
      }
152

    
153
    dialog.setOnShowListener(new DialogInterface.OnShowListener()
154
      {
155
      @Override
156
      public void onShow(DialogInterface dialog)
157
        {
158
        if( positive>=0 )
159
          {
160
          Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
161
          btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButSize);
162
          }
163
        if( negative>=0 )
164
          {
165
          Button btnNegative = ((AlertDialog)dialog).getButton(Dialog.BUTTON_NEGATIVE);
166
          btnNegative.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButSize);
167
          }
168
        }
169
      });
170

    
171
    return dialog;
172
    }
173
  }
(3-3/26)