Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogAbstract.java @ 0a9adc31

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.content.res.ColorStateList;
15
import android.graphics.Color;
16
import android.graphics.drawable.ColorDrawable;
17
import android.os.Bundle;
18
import android.util.DisplayMetrics;
19
import android.util.TypedValue;
20
import android.view.LayoutInflater;
21
import android.view.View;
22
import android.view.Window;
23
import android.widget.Button;
24
import android.widget.TextView;
25

    
26
import androidx.annotation.NonNull;
27
import androidx.appcompat.app.AlertDialog;
28
import androidx.appcompat.app.AppCompatDialogFragment;
29
import androidx.fragment.app.FragmentActivity;
30

    
31
import org.distorted.helpers.BaseActivity;
32
import org.distorted.main.MainActivity;
33
import org.distorted.main.R;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
abstract public class RubikDialogAbstract extends AppCompatDialogFragment
38
  {
39
  static final int PARAMETRIC_TITLE = -10000;
40

    
41
  protected float mTitleSize, mButSize, mTextSize;
42
  protected int mWidth, mHeight;
43
  protected String mArgument;
44

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

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

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
  String getTitleString(FragmentActivity act)
59
    {
60
    return "";
61
    }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

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

    
81
    if( hasArgument() )
82
      {
83
      Bundle args = getArguments();
84

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

    
96
    final View view = inflater.inflate(getResource(), null);
97
    builder.setView(view);
98
    builder.setCancelable(true);
99

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

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

    
116
      builder.setCustomTitle(tv);
117
      }
118

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

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

    
145
    Dialog dialog = builder.create();
146
    dialog.setCanceledOnTouchOutside(false);
147

    
148
    prepareBody(dialog,view,act,mTextSize);
149

    
150
    Window window = dialog.getWindow();
151

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

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

    
178
    return dialog;
179
    }
180
  }
(3-3/25)