Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogUpdates.java @ c02fa107

1 5f3801d3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 68191e7d Leszek Koltunski
// 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 5f3801d3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.dialogs;
11
12 b88cdd91 Leszek Koltunski
import java.util.ArrayList;
13
14 5f3801d3 Leszek Koltunski
import android.app.Dialog;
15
import android.content.DialogInterface;
16 b88cdd91 Leszek Koltunski
import android.graphics.Bitmap;
17 5f3801d3 Leszek Koltunski
import android.util.TypedValue;
18 2a2ca758 Leszek Koltunski
import android.view.View;
19 5f3801d3 Leszek Koltunski
import android.widget.Button;
20 9c39179e Leszek Koltunski
import android.widget.LinearLayout;
21 5f3801d3 Leszek Koltunski
import android.widget.TextView;
22
23
import androidx.appcompat.app.AlertDialog;
24
import androidx.fragment.app.FragmentActivity;
25
26
import org.distorted.main.R;
27 acabdd83 Leszek Koltunski
import org.distorted.external.RubikNetwork;
28
import org.distorted.external.RubikUpdates;
29 5f3801d3 Leszek Koltunski
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31
32 c02fa107 Leszek Koltunski
public class RubikDialogUpdates extends RubikDialogAbstract implements RubikNetwork.IconReceiver, RubikNetwork.Updatee
33 5f3801d3 Leszek Koltunski
  {
34 9c39179e Leszek Koltunski
  private TextView mText;
35
  private LinearLayout mLayout;
36 99b8a069 Leszek Koltunski
  private int mMargin, mSize, mFontSize, mPadding;
37 b88cdd91 Leszek Koltunski
  private ArrayList<RubikDialogUpdateView> mPanes;
38
39 9c39179e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41 c02fa107 Leszek Koltunski
  public int getResource()
42 2a2ca758 Leszek Koltunski
    {
43 c02fa107 Leszek Koltunski
    return R.layout.dialog_updates;
44
    }
45 2a2ca758 Leszek Koltunski
46 c02fa107 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
47 179f7189 Leszek Koltunski
48 c02fa107 Leszek Koltunski
  public int getTitleResource()
49
    {
50
    return R.string.updates;
51 2a2ca758 Leszek Koltunski
    }
52
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55 c02fa107 Leszek Koltunski
  public boolean hasArgument()
56 5f3801d3 Leszek Koltunski
    {
57 c02fa107 Leszek Koltunski
    return false;
58
    }
59 b88cdd91 Leszek Koltunski
60 c02fa107 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62
  public void setPositive(AlertDialog.Builder builder)
63
    {
64 5f3801d3 Leszek Koltunski
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
65
      {
66
      @Override
67
      public void onClick(DialogInterface dialog, int which)
68
        {
69
70
        }
71
      });
72 c02fa107 Leszek Koltunski
    }
73 5f3801d3 Leszek Koltunski
74 c02fa107 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
75 2a2ca758 Leszek Koltunski
76 c02fa107 Leszek Koltunski
  public void setNegative(AlertDialog.Builder builder)
77
    {
78 5f3801d3 Leszek Koltunski
79 c02fa107 Leszek Koltunski
    }
80 5f3801d3 Leszek Koltunski
81 c02fa107 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
82
83
  public void onShowDialog(DialogInterface dialog, float size)
84
    {
85
    Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
86
    btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
87
    }
88
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
91
  public void prepareBody(Dialog dialog, View view, FragmentActivity act, float size)
92
    {
93
    if( mPanes==null ) mPanes = new ArrayList<>();
94
95
    int minH  = (int)(mHeight*0.25f);
96
    mMargin   = (int)(mHeight*0.01f);
97
    mSize     = (int)(mHeight*0.14f);
98
    mFontSize = (int)(mHeight*0.02f);
99
    mPadding  = (int)(mHeight*0.01f);
100
101
    mLayout= view.findViewById(R.id.updates_main_layout);
102
    mText  = view.findViewById(R.id.updates_message);
103
    mText.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
104
    mText.setText( act.getString(R.string.downloading) );
105
106
    mLayout.setMinimumHeight(minH);
107
    mText.setMinimumHeight(minH);
108
    view.setMinimumHeight(minH);
109 5f3801d3 Leszek Koltunski
110 c651024f Leszek Koltunski
    RubikNetwork network = RubikNetwork.getInstance();
111
    network.signUpForUpdates(this);
112 9c39179e Leszek Koltunski
    }
113
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
116 179f7189 Leszek Koltunski
  private void receiveUpdate(RubikUpdates updates, FragmentActivity act)
117 9c39179e Leszek Koltunski
    {
118 2c9ab085 Leszek Koltunski
    int numC = updates.getCompletedNumber();
119 2e48adc9 Leszek Koltunski
    int numS = updates.getStartedNumber();
120 5f3801d3 Leszek Koltunski
121 2e48adc9 Leszek Koltunski
    if( numC+numS<=0 )
122 9c39179e Leszek Koltunski
      {
123
      mText.setText(act.getString(R.string.no_updates));
124
      }
125
    else
126
      {
127 2c9ab085 Leszek Koltunski
      int textH = (int)(mSize*0.27f);
128
      int buttH = (int)(mSize*0.35f);
129 c651024f Leszek Koltunski
130
      LinearLayout.LayoutParams pV = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, mSize );
131
      pV.setMargins(mMargin, mMargin, mMargin, 0);
132
      LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, textH );
133
      LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, buttH );
134
135 9c39179e Leszek Koltunski
      mText.setVisibility(View.GONE);
136
137
      if( mLayout!=null )
138
        {
139 2c9ab085 Leszek Koltunski
        for(int i=0; i<numC; i++)
140 9c39179e Leszek Koltunski
          {
141 2c9ab085 Leszek Koltunski
          RubikUpdates.UpdateInfo info = updates.getCompletedUpdate(i);
142 9c39179e Leszek Koltunski
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
143 06ba394a Leszek Koltunski
          View pane = rubikView.createView(act,info,mFontSize,mPadding,pV,pT,pB);
144 9c39179e Leszek Koltunski
          mLayout.addView(pane);
145 b88cdd91 Leszek Koltunski
          mPanes.add(rubikView);
146 9c39179e Leszek Koltunski
          }
147 2c9ab085 Leszek Koltunski
148
        for(int i=0; i<numS; i++)
149 9c39179e Leszek Koltunski
          {
150 2c9ab085 Leszek Koltunski
          RubikUpdates.UpdateInfo info = updates.getStartedUpdate(i);
151 9c39179e Leszek Koltunski
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
152 06ba394a Leszek Koltunski
          View pane = rubikView.createView(act,info,mFontSize,mPadding,pV,pT,pB);
153 9c39179e Leszek Koltunski
          mLayout.addView(pane);
154 b88cdd91 Leszek Koltunski
          mPanes.add(rubikView);
155 9c39179e Leszek Koltunski
          }
156 ffe7e55d Leszek Koltunski
157
        RubikNetwork network = RubikNetwork.getInstance();
158 7fe62d1f Leszek Koltunski
        network.downloadIcons(act,this);
159 9c39179e Leszek Koltunski
        }
160
      else
161
        {
162
        android.util.Log.e("D", "mainLayout NULL");
163
        }
164
      }
165 5f3801d3 Leszek Koltunski
    }
166
167 179f7189 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
168
169
  public void receiveUpdate(RubikUpdates updates)
170
    {
171
    FragmentActivity act = getActivity();
172
173 c02fa107 Leszek Koltunski
android.util.Log.e("D", "receiveUpdate1");
174
175 179f7189 Leszek Koltunski
    if( act!=null )
176
      {
177
      act.runOnUiThread(new Runnable()
178
        {
179
        @Override
180
        public void run()
181
          {
182
          receiveUpdate(updates,act);
183
          }
184
        });
185
      }
186
    }
187
188 c99db493 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
189
190
  public void errorUpdate()
191
    {
192 46be3ddf Leszek Koltunski
    FragmentActivity act = getActivity();
193
    if( act!=null ) mText.setText(act.getString(R.string.networkError));
194 c99db493 Leszek Koltunski
    }
195
196 ffe7e55d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
197
198 7fe62d1f Leszek Koltunski
  public void iconDownloaded(int ordinal, Bitmap icon, boolean downloaded)
199 ffe7e55d Leszek Koltunski
    {
200 b88cdd91 Leszek Koltunski
    FragmentActivity act = getActivity();
201 ffe7e55d Leszek Koltunski
202 b88cdd91 Leszek Koltunski
    if( act!=null )
203
      {
204
      act.runOnUiThread(new Runnable()
205
        {
206
        @Override
207
        public void run()
208
          {
209
          RubikDialogUpdateView view = mPanes.get(ordinal);
210 7fe62d1f Leszek Koltunski
          if( view!=null ) view.setIcon(icon,downloaded);
211 b88cdd91 Leszek Koltunski
          }
212
        });
213
      }
214 ffe7e55d Leszek Koltunski
    }
215
216 5f3801d3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
217
218
  public static String getDialogTag()
219
    {
220
    return "DialogUpdates";
221
    }
222
  }