Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 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 java.util.ArrayList;
13

    
14
import android.app.Dialog;
15
import android.graphics.Bitmap;
16
import android.util.TypedValue;
17
import android.view.View;
18
import android.view.Window;
19
import android.view.WindowManager;
20
import android.widget.LinearLayout;
21
import android.widget.TextView;
22

    
23
import androidx.fragment.app.FragmentActivity;
24

    
25
import org.distorted.main.R;
26
import org.distorted.external.RubikNetwork;
27
import org.distorted.external.RubikUpdates;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
public class RubikDialogUpdates extends RubikDialogAbstract implements RubikNetwork.IconReceiver, RubikNetwork.Updatee
32
  {
33
  private TextView mText;
34
  private LinearLayout mLayout;
35
  private int mMargin, mSize, mFontSize, mPadding;
36
  private ArrayList<RubikDialogUpdateView> mPanes;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
  @Override
41
  public void onResume()
42
    {
43
    super.onResume();
44

    
45
    Window window = getDialog().getWindow();
46

    
47
    if( window!=null )
48
      {
49
      WindowManager.LayoutParams params = window.getAttributes();
50
      params.width  = (int)Math.min( mHeight*0.65f,mWidth*0.98f );
51
      window.setAttributes(params);
52
      }
53
    }
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  public int getResource()      { return R.layout.dialog_scrollable_panes; }
58
  public int getTitleResource() { return R.string.updates; }
59
  public boolean hasArgument()  { return false; }
60
  public int getPositive()      { return R.string.ok; }
61
  public int getNegative()      { return -1; }
62

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

    
65
  public void positiveAction()
66
    {
67

    
68
    }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  public void negativeAction()
73
    {
74

    
75
    }
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
  public void prepareBody(Dialog dialog, View view, FragmentActivity act, float size)
80
    {
81
    if( mPanes==null ) mPanes = new ArrayList<>();
82

    
83
    int minH  = (int)(mHeight*0.25f);
84
    mMargin   = (int)(mHeight*0.01f);
85
    mSize     = (int)(mHeight*0.14f);
86
    mFontSize = (int)(mHeight*0.02f);
87
    mPadding  = (int)(mHeight*0.01f);
88

    
89
    mLayout= view.findViewById(R.id.dialog_scrollable_main_layout);
90
    mText  = view.findViewById(R.id.dialog_scrollable_message);
91
    mText.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
92
    mText.setText( act.getString(R.string.downloading) );
93

    
94
    mLayout.setMinimumHeight(minH);
95
    mText.setMinimumHeight(minH);
96
    view.setMinimumHeight(minH);
97

    
98
    RubikNetwork network = RubikNetwork.getInstance();
99
    network.signUpForUpdates(this);
100
    }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
  private void receiveUpdate(RubikUpdates updates, FragmentActivity act)
105
    {
106
    int numC = updates.getCompletedNumber();
107
    int numS = updates.getStartedNumber();
108

    
109
    if( numC+numS<=0 )
110
      {
111
      mText.setText(act.getString(R.string.no_updates));
112
      }
113
    else
114
      {
115
      int textH = (int)(mSize*0.27f);
116
      int buttH = (int)(mSize*0.35f);
117

    
118
      LinearLayout.LayoutParams pV = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, mSize );
119
      pV.setMargins(mMargin, mMargin, mMargin, 0);
120
      LinearLayout.LayoutParams pL = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, mSize );
121
      pL.setMargins(mMargin, mMargin, mMargin, mMargin);
122
      LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, textH );
123
      LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, buttH );
124

    
125
      mText.setVisibility(View.GONE);
126

    
127
      if( mLayout!=null )
128
        {
129
        for(int i=0; i<numC; i++)
130
          {
131
          RubikUpdates.UpdateInfo info = updates.getCompletedUpdate(i);
132
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
133
          View pane = rubikView.createView(act,info,mFontSize,mPadding,pV,pT,pB);
134
          mLayout.addView(pane);
135
          mPanes.add(rubikView);
136
          }
137

    
138
        for(int i=0; i<numS; i++)
139
          {
140
          RubikUpdates.UpdateInfo info = updates.getStartedUpdate(i);
141
          RubikDialogUpdateView rubikView = new RubikDialogUpdateView();
142
          View pane = rubikView.createView(act,info,mFontSize,mPadding,(i==numS-1?pL:pV),pT,pB);
143
          mLayout.addView(pane);
144
          mPanes.add(rubikView);
145
          }
146

    
147
        RubikNetwork network = RubikNetwork.getInstance();
148
        network.downloadIcons(act,this);
149
        }
150
      else
151
        {
152
        android.util.Log.e("D", "mainLayout NULL");
153
        }
154
      }
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  public void receiveUpdate(RubikUpdates updates)
160
    {
161
    FragmentActivity act = getActivity();
162

    
163
    if( act!=null )
164
      {
165
      act.runOnUiThread(new Runnable()
166
        {
167
        @Override
168
        public void run()
169
          {
170
          receiveUpdate(updates,act);
171
          }
172
        });
173
      }
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  public void errorUpdate()
179
    {
180
    FragmentActivity act = getActivity();
181
    if( act!=null ) mText.setText(act.getString(R.string.networkError));
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  public void iconDownloaded(int ordinal, Bitmap icon, boolean downloaded)
187
    {
188
    FragmentActivity act = getActivity();
189

    
190
    if( act!=null )
191
      {
192
      act.runOnUiThread(new Runnable()
193
        {
194
        @Override
195
        public void run()
196
          {
197
          RubikDialogUpdateView view = mPanes.get(ordinal);
198
          if( view!=null ) view.setIcon(icon,downloaded);
199
          }
200
        });
201
      }
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  public static String getDialogTag()
207
    {
208
    return "DialogUpdates";
209
    }
210
  }
(26-26/26)