1 |
cc5ec229
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2020 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
|
|
// This file is part of Magic Cube. //
|
5 |
|
|
// //
|
6 |
|
|
// Magic Cube is free software: you can redistribute it and/or modify //
|
7 |
|
|
// it under the terms of the GNU General Public License as published by //
|
8 |
|
|
// the Free Software Foundation, either version 2 of the License, or //
|
9 |
|
|
// (at your option) any later version. //
|
10 |
|
|
// //
|
11 |
|
|
// Magic Cube is distributed in the hope that it will be useful, //
|
12 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14 |
|
|
// GNU General Public License for more details. //
|
15 |
|
|
// //
|
16 |
|
|
// You should have received a copy of the GNU General Public License //
|
17 |
|
|
// along with Magic Cube. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
1f9772f3
|
Leszek Koltunski
|
package org.distorted.dialogs;
|
21 |
cc5ec229
|
Leszek Koltunski
|
|
22 |
f895e77a
|
Leszek Koltunski
|
import android.os.Bundle;
|
23 |
66e777b0
|
Leszek Koltunski
|
import androidx.annotation.NonNull;
|
24 |
|
|
import androidx.fragment.app.FragmentActivity;
|
25 |
|
|
import androidx.viewpager.widget.PagerAdapter;
|
26 |
|
|
import androidx.viewpager.widget.ViewPager;
|
27 |
362807f0
|
Leszek Koltunski
|
|
28 |
|
|
import android.util.DisplayMetrics;
|
29 |
cc5ec229
|
Leszek Koltunski
|
import android.view.View;
|
30 |
|
|
import android.view.ViewGroup;
|
31 |
d5fb0e7e
|
Leszek Koltunski
|
import android.widget.LinearLayout;
|
32 |
cc5ec229
|
Leszek Koltunski
|
|
33 |
318c0a7d
|
Leszek Koltunski
|
import org.distorted.objectlib.main.ObjectType;
|
34 |
3f7a4363
|
Leszek Koltunski
|
|
35 |
00af5060
|
Leszek Koltunski
|
import org.distorted.main.R;
|
36 |
6a083c6a
|
Leszek Koltunski
|
import org.distorted.network.RubikScores;
|
37 |
|
|
import org.distorted.network.RubikNetwork;
|
38 |
fcd5b990
|
Leszek Koltunski
|
import org.distorted.screens.RubikScreenPlay;
|
39 |
4888e97c
|
Leszek Koltunski
|
|
40 |
cc5ec229
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
41 |
|
|
|
42 |
6a083c6a
|
Leszek Koltunski
|
class RubikDialogScoresPagerAdapter extends PagerAdapter implements RubikNetwork.Receiver
|
43 |
cc5ec229
|
Leszek Koltunski
|
{
|
44 |
00af5060
|
Leszek Koltunski
|
private final FragmentActivity mAct;
|
45 |
|
|
private final RubikDialogScores mDialog;
|
46 |
|
|
private final RubikDialogScoresView[] mViews;
|
47 |
|
|
private final ViewPager mViewPager;
|
48 |
|
|
private final int mNumTabs;
|
49 |
|
|
private final boolean mIsSubmitting;
|
50 |
3e5ad461
|
Leszek Koltunski
|
private int mToDoTab, mToDoLvl;
|
51 |
cc5ec229
|
Leszek Koltunski
|
|
52 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53 |
|
|
|
54 |
00af5060
|
Leszek Koltunski
|
private void addSection(int tab, int level, int lastLevel, final RubikDialogScoresView view, final String[] country, final String[] name, final float[] time)
|
55 |
028f3e0d
|
Leszek Koltunski
|
{
|
56 |
00af5060
|
Leszek Koltunski
|
String title = level==lastLevel-1 ?
|
57 |
|
|
mAct.getString(R.string.level_full) :
|
58 |
|
|
mAct.getString(R.string.lv_placeholder,level+1);
|
59 |
|
|
|
60 |
|
|
final LinearLayout section = view.createSection(mAct, tab, title, level, country, name, time);
|
61 |
028f3e0d
|
Leszek Koltunski
|
|
62 |
3e5ad461
|
Leszek Koltunski
|
mAct.runOnUiThread(new Runnable()
|
63 |
|
|
{
|
64 |
|
|
@Override
|
65 |
|
|
public void run()
|
66 |
|
|
{
|
67 |
|
|
view.addSection(section);
|
68 |
|
|
}
|
69 |
|
|
});
|
70 |
028f3e0d
|
Leszek Koltunski
|
|
71 |
3e5ad461
|
Leszek Koltunski
|
try
|
72 |
028f3e0d
|
Leszek Koltunski
|
{
|
73 |
3e5ad461
|
Leszek Koltunski
|
Thread.sleep(50);
|
74 |
|
|
}
|
75 |
|
|
catch( InterruptedException ignored)
|
76 |
|
|
{
|
77 |
|
|
|
78 |
|
|
}
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
82 |
13ed61b5
|
Leszek Koltunski
|
|
83 |
00af5060
|
Leszek Koltunski
|
private void getNext(int currentTab, int[] toDoTab, int[] maxTab, int[] lastTab)
|
84 |
3e5ad461
|
Leszek Koltunski
|
{
|
85 |
|
|
if( toDoTab[currentTab]<maxTab[currentTab] )
|
86 |
|
|
{
|
87 |
|
|
mToDoTab = currentTab;
|
88 |
00af5060
|
Leszek Koltunski
|
mToDoLvl = toDoTab[currentTab]<maxTab[currentTab]-1 ? toDoTab[currentTab] : lastTab[currentTab]-1;
|
89 |
3e5ad461
|
Leszek Koltunski
|
toDoTab[currentTab]++;
|
90 |
|
|
}
|
91 |
|
|
else
|
92 |
|
|
{
|
93 |
|
|
for(int tab=0; tab<mNumTabs; tab++)
|
94 |
13ed61b5
|
Leszek Koltunski
|
{
|
95 |
3e5ad461
|
Leszek Koltunski
|
if( toDoTab[tab]<maxTab[tab] )
|
96 |
13ed61b5
|
Leszek Koltunski
|
{
|
97 |
3e5ad461
|
Leszek Koltunski
|
mToDoTab = tab;
|
98 |
00af5060
|
Leszek Koltunski
|
mToDoLvl = toDoTab[tab]<maxTab[tab]-1 ? toDoTab[tab] : lastTab[tab]-1;
|
99 |
3e5ad461
|
Leszek Koltunski
|
toDoTab[tab]++;
|
100 |
|
|
break;
|
101 |
13ed61b5
|
Leszek Koltunski
|
}
|
102 |
|
|
}
|
103 |
3e5ad461
|
Leszek Koltunski
|
}
|
104 |
|
|
}
|
105 |
13ed61b5
|
Leszek Koltunski
|
|
106 |
3e5ad461
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
107 |
028f3e0d
|
Leszek Koltunski
|
|
108 |
3e5ad461
|
Leszek Koltunski
|
public void receive(final String[][][] country, final String[][][] name, final float[][][] time)
|
109 |
|
|
{
|
110 |
|
|
prepareView();
|
111 |
|
|
|
112 |
8ab435b9
|
Leszek Koltunski
|
ObjectType[] types = ObjectType.values();
|
113 |
fcd5b990
|
Leszek Koltunski
|
int MAX = RubikScreenPlay.LEVELS_SHOWN;
|
114 |
3e5ad461
|
Leszek Koltunski
|
int toDo=0;
|
115 |
|
|
int[] toDoTab = new int[mNumTabs];
|
116 |
|
|
int[] maxTab = new int[mNumTabs];
|
117 |
00af5060
|
Leszek Koltunski
|
int[] lastTab = new int[mNumTabs];
|
118 |
3e5ad461
|
Leszek Koltunski
|
|
119 |
|
|
for(int i=0; i<mNumTabs; i++)
|
120 |
|
|
{
|
121 |
8ab435b9
|
Leszek Koltunski
|
lastTab[i]= RubikScreenPlay.getDBLevel(types[i]);
|
122 |
7ac0ee88
|
Leszek Koltunski
|
maxTab[i] = Math.min(lastTab[i],MAX);
|
123 |
|
|
toDoTab[i]= 0;
|
124 |
3e5ad461
|
Leszek Koltunski
|
|
125 |
|
|
toDo += maxTab[i];
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
while( toDo>0 )
|
129 |
|
|
{
|
130 |
|
|
toDo--;
|
131 |
00af5060
|
Leszek Koltunski
|
getNext(mViewPager.getCurrentItem(),toDoTab,maxTab,lastTab);
|
132 |
|
|
addSection( mToDoTab,mToDoLvl, lastTab[mToDoTab], mViews[mToDoTab],
|
133 |
3e5ad461
|
Leszek Koltunski
|
country[mToDoTab][mToDoLvl],name[mToDoTab][mToDoLvl],time[mToDoTab][mToDoLvl]);
|
134 |
028f3e0d
|
Leszek Koltunski
|
}
|
135 |
|
|
}
|
136 |
|
|
|
137 |
17f9a695
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
138 |
|
|
|
139 |
|
|
public void message(final String mess)
|
140 |
|
|
{
|
141 |
|
|
mAct.runOnUiThread(new Runnable()
|
142 |
|
|
{
|
143 |
|
|
@Override
|
144 |
|
|
public void run()
|
145 |
|
|
{
|
146 |
|
|
for(int i=0; i<mNumTabs; i++)
|
147 |
|
|
{
|
148 |
|
|
mViews[i].message(mess);
|
149 |
|
|
}
|
150 |
|
|
}
|
151 |
|
|
});
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
155 |
|
|
|
156 |
4c0cd600
|
Leszek Koltunski
|
public void error(final String error)
|
157 |
17f9a695
|
Leszek Koltunski
|
{
|
158 |
|
|
char errorNumber = error.charAt(0);
|
159 |
|
|
|
160 |
|
|
switch(errorNumber)
|
161 |
|
|
{
|
162 |
90fd47b0
|
Leszek Koltunski
|
case '1': message("Network error");
|
163 |
4c0cd600
|
Leszek Koltunski
|
break;
|
164 |
f895e77a
|
Leszek Koltunski
|
case '2': RubikScores scores = RubikScores.getInstance();
|
165 |
|
|
Bundle bundle = new Bundle();
|
166 |
|
|
bundle.putString("name", scores.getName() );
|
167 |
|
|
|
168 |
|
|
RubikDialogSetName nameDiag = new RubikDialogSetName();
|
169 |
|
|
nameDiag.setArguments(bundle);
|
170 |
|
|
nameDiag.show(mAct.getSupportFragmentManager(), null);
|
171 |
|
|
|
172 |
|
|
mDialog.dismiss();
|
173 |
|
|
|
174 |
17f9a695
|
Leszek Koltunski
|
break;
|
175 |
|
|
case '3': message("Server error");
|
176 |
|
|
break;
|
177 |
4c0cd600
|
Leszek Koltunski
|
default : message("Unexpected error");
|
178 |
17f9a695
|
Leszek Koltunski
|
}
|
179 |
|
|
}
|
180 |
|
|
|
181 |
028f3e0d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
182 |
|
|
|
183 |
|
|
private void prepareView()
|
184 |
946d9762
|
Leszek Koltunski
|
{
|
185 |
|
|
mAct.runOnUiThread(new Runnable()
|
186 |
|
|
{
|
187 |
|
|
@Override
|
188 |
|
|
public void run()
|
189 |
|
|
{
|
190 |
4888e97c
|
Leszek Koltunski
|
for(int i=0; i<mNumTabs; i++)
|
191 |
946d9762
|
Leszek Koltunski
|
{
|
192 |
|
|
mViews[i].prepareView(mAct);
|
193 |
|
|
}
|
194 |
|
|
}
|
195 |
|
|
});
|
196 |
|
|
try
|
197 |
|
|
{
|
198 |
7aff6aa7
|
Leszek Koltunski
|
Thread.sleep(150);
|
199 |
946d9762
|
Leszek Koltunski
|
}
|
200 |
d5d503d0
|
Leszek Koltunski
|
catch( InterruptedException ignored)
|
201 |
946d9762
|
Leszek Koltunski
|
{
|
202 |
|
|
|
203 |
|
|
}
|
204 |
028f3e0d
|
Leszek Koltunski
|
}
|
205 |
946d9762
|
Leszek Koltunski
|
|
206 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
207 |
|
|
|
208 |
f895e77a
|
Leszek Koltunski
|
RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager, boolean isSubmitting, RubikDialogScores diag)
|
209 |
cc5ec229
|
Leszek Koltunski
|
{
|
210 |
|
|
mAct = act;
|
211 |
f895e77a
|
Leszek Koltunski
|
mDialog = diag;
|
212 |
318c0a7d
|
Leszek Koltunski
|
mNumTabs = ObjectType.NUM_OBJECTS;
|
213 |
4888e97c
|
Leszek Koltunski
|
mViews = new RubikDialogScoresView[mNumTabs];
|
214 |
028f3e0d
|
Leszek Koltunski
|
mViewPager = viewPager;
|
215 |
4895fff6
|
Leszek Koltunski
|
mIsSubmitting = isSubmitting;
|
216 |
cc5ec229
|
Leszek Koltunski
|
|
217 |
|
|
viewPager.setAdapter(this);
|
218 |
4888e97c
|
Leszek Koltunski
|
viewPager.setOffscreenPageLimit(mNumTabs-1);
|
219 |
cc5ec229
|
Leszek Koltunski
|
}
|
220 |
|
|
|
221 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
222 |
|
|
|
223 |
|
|
@Override
|
224 |
|
|
@NonNull
|
225 |
|
|
public Object instantiateItem(@NonNull ViewGroup collection, int position)
|
226 |
|
|
{
|
227 |
362807f0
|
Leszek Koltunski
|
DisplayMetrics metrics = mAct.getResources().getDisplayMetrics();
|
228 |
|
|
|
229 |
|
|
mViews[position] = new RubikDialogScoresView(mAct, metrics.heightPixels, mIsSubmitting);
|
230 |
946d9762
|
Leszek Koltunski
|
collection.addView(mViews[position]);
|
231 |
|
|
|
232 |
|
|
boolean allCreated = true;
|
233 |
|
|
|
234 |
4888e97c
|
Leszek Koltunski
|
for(int i=0; i<mNumTabs; i++)
|
235 |
946d9762
|
Leszek Koltunski
|
{
|
236 |
|
|
if( mViews[i]==null )
|
237 |
|
|
{
|
238 |
|
|
allCreated = false;
|
239 |
|
|
break;
|
240 |
|
|
}
|
241 |
|
|
}
|
242 |
|
|
|
243 |
|
|
if( allCreated )
|
244 |
|
|
{
|
245 |
6a083c6a
|
Leszek Koltunski
|
RubikNetwork network = RubikNetwork.getInstance();
|
246 |
4895fff6
|
Leszek Koltunski
|
|
247 |
6a083c6a
|
Leszek Koltunski
|
if( mIsSubmitting ) network.submit ( this, mAct );
|
248 |
|
|
else network.download( this, mAct );
|
249 |
946d9762
|
Leszek Koltunski
|
}
|
250 |
|
|
|
251 |
|
|
return mViews[position];
|
252 |
cc5ec229
|
Leszek Koltunski
|
}
|
253 |
|
|
|
254 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
255 |
|
|
|
256 |
|
|
@Override
|
257 |
|
|
public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
|
258 |
|
|
{
|
259 |
|
|
collection.removeView((View) view);
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
263 |
|
|
|
264 |
|
|
@Override
|
265 |
|
|
public int getCount()
|
266 |
|
|
{
|
267 |
4888e97c
|
Leszek Koltunski
|
return mNumTabs;
|
268 |
cc5ec229
|
Leszek Koltunski
|
}
|
269 |
|
|
|
270 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
271 |
|
|
|
272 |
|
|
@Override
|
273 |
|
|
public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
|
274 |
|
|
{
|
275 |
|
|
return view == object;
|
276 |
|
|
}
|
277 |
|
|
}
|