Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / DialogAbstract.java @ b3278db6

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.helpers.BaseActivity;
29
import org.distorted.main.MainActivity;
30
import org.distorted.main.R;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

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

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

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

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

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

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

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

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

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

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

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

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

    
113
      builder.setCustomTitle(tv);
114
      }
115

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

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

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

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

    
147
    Window window = dialog.getWindow();
148

    
149
    if( window!=null )
150
      {
151
      BaseActivity bact = (BaseActivity) act;
152
      int m = bact.getMediumColor();
153
      window.setBackgroundDrawableResource(m);
154
      window.getDecorView().setSystemUiVisibility(MainActivity.FLAGS);
155
      }
156

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

    
175
    return dialog;
176
    }
177
  }
(3-3/26)