Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogAbstract.java @ 76856be3

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
  protected float mTitleSize, mButSize, mTextSize;
36
  protected int mWidth, mHeight;
37
  protected String mArgument;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

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

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
  @NonNull
53
  @Override
54
  public Dialog onCreateDialog(Bundle savedInstanceState)
55
    {
56
    FragmentActivity act = getActivity();
57
    LayoutInflater inflater = act.getLayoutInflater();
58
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
59

    
60
    DisplayMetrics displaymetrics = new DisplayMetrics();
61
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
62
    mWidth    = displaymetrics.widthPixels;
63
    mHeight   = displaymetrics.heightPixels;
64
    mTitleSize= mHeight*0.032f;
65
    mButSize  = mHeight*0.040f;
66
    mTextSize = mHeight*0.025f;
67

    
68
    if( hasArgument() )
69
      {
70
      Bundle args = getArguments();
71

    
72
      try
73
        {
74
        mArgument = args!=null ? args.getString("argument") : "";
75
        }
76
      catch(Exception e)
77
        {
78
        mArgument = "";
79
        }
80
      }
81
    else mArgument = "";
82

    
83
    final View view = inflater.inflate(getResource(), null);
84
    builder.setView(view);
85
    builder.setCancelable(true);
86

    
87
    int title = getTitleResource();
88
    if( title>=0 )
89
      {
90
      TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
91
      tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleSize);
92
      tv.setText(title);
93
      builder.setCustomTitle(tv);
94
      }
95

    
96
    int positive = getPositive();
97
    if( positive>=0 )
98
      {
99
      builder.setPositiveButton( positive, new DialogInterface.OnClickListener()
100
        {
101
        @Override
102
        public void onClick(DialogInterface dialog, int which)
103
          {
104
          positiveAction();
105
          }
106
        });
107
      }
108

    
109
    int negative = getNegative();
110
    if( negative>=0 )
111
      {
112
      builder.setNegativeButton( negative, new DialogInterface.OnClickListener()
113
        {
114
        @Override
115
        public void onClick(DialogInterface dialog, int which)
116
          {
117
          negativeAction();
118
          }
119
        });
120
      }
121

    
122
    Dialog dialog = builder.create();
123
    dialog.setCanceledOnTouchOutside(false);
124

    
125
    prepareBody(dialog,view,act,mTextSize);
126

    
127
    Window window = dialog.getWindow();
128

    
129
    if( window!=null )
130
      {
131
      window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS);
132
      }
133

    
134
    dialog.setOnShowListener(new DialogInterface.OnShowListener()
135
      {
136
      @Override
137
      public void onShow(DialogInterface dialog)
138
        {
139
        if( positive>=0 )
140
          {
141
          Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
142
          btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButSize);
143
          }
144
        if( negative>=0 )
145
          {
146
          Button btnNegative = ((AlertDialog)dialog).getButton(Dialog.BUTTON_NEGATIVE);
147
          btnNegative.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButSize);
148
          }
149
        }
150
      });
151

    
152
    return dialog;
153
    }
154
  }
(2-2/27)