1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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
|
package org.distorted.dialogs;
|
21
|
|
22
|
import android.os.Bundle;
|
23
|
import androidx.annotation.NonNull;
|
24
|
import androidx.fragment.app.FragmentActivity;
|
25
|
import androidx.viewpager.widget.PagerAdapter;
|
26
|
import androidx.viewpager.widget.ViewPager;
|
27
|
|
28
|
import android.util.DisplayMetrics;
|
29
|
import android.view.View;
|
30
|
import android.view.ViewGroup;
|
31
|
import android.widget.LinearLayout;
|
32
|
|
33
|
import org.distorted.scores.RubikScores;
|
34
|
import org.distorted.scores.RubikScoresDownloader;
|
35
|
import org.distorted.objects.RubikObjectList;
|
36
|
|
37
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38
|
|
39
|
class RubikDialogScoresPagerAdapter extends PagerAdapter implements RubikScoresDownloader.Receiver
|
40
|
{
|
41
|
private FragmentActivity mAct;
|
42
|
private RubikDialogScores mDialog;
|
43
|
private RubikDialogScoresView[] mViews;
|
44
|
private ViewPager mViewPager;
|
45
|
private int mNumTabs;
|
46
|
private boolean mIsSubmitting;
|
47
|
private int mToDoTab, mToDoLvl;
|
48
|
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
private void addSection(int tab, int level, final RubikDialogScoresView view, final String[] country, final String[] name, final float[] time)
|
52
|
{
|
53
|
final LinearLayout section = view.createSection(mAct, tab, level, country, name, time);
|
54
|
|
55
|
mAct.runOnUiThread(new Runnable()
|
56
|
{
|
57
|
@Override
|
58
|
public void run()
|
59
|
{
|
60
|
view.addSection(section);
|
61
|
}
|
62
|
});
|
63
|
|
64
|
try
|
65
|
{
|
66
|
Thread.sleep(50);
|
67
|
}
|
68
|
catch( InterruptedException ignored)
|
69
|
{
|
70
|
|
71
|
}
|
72
|
}
|
73
|
|
74
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
75
|
|
76
|
private void getNext(int currentTab, int[] toDoTab, int[] maxTab)
|
77
|
{
|
78
|
if( toDoTab[currentTab]<maxTab[currentTab] )
|
79
|
{
|
80
|
mToDoTab = currentTab;
|
81
|
mToDoLvl = toDoTab[currentTab];
|
82
|
toDoTab[currentTab]++;
|
83
|
}
|
84
|
else
|
85
|
{
|
86
|
for(int tab=0; tab<mNumTabs; tab++)
|
87
|
{
|
88
|
if( toDoTab[tab]<maxTab[tab] )
|
89
|
{
|
90
|
mToDoTab = tab;
|
91
|
mToDoLvl = toDoTab[tab];
|
92
|
toDoTab[tab]++;
|
93
|
break;
|
94
|
}
|
95
|
}
|
96
|
}
|
97
|
}
|
98
|
|
99
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
100
|
|
101
|
public void receive(final String[][][] country, final String[][][] name, final float[][][] time)
|
102
|
{
|
103
|
prepareView();
|
104
|
|
105
|
int toDo=0;
|
106
|
int[] toDoTab = new int[mNumTabs];
|
107
|
int[] maxTab = new int[mNumTabs];
|
108
|
|
109
|
for(int i=0; i<mNumTabs; i++)
|
110
|
{
|
111
|
int object = RubikObjectList.unpackObject(i);
|
112
|
int sizeIndex= RubikObjectList.unpackSizeIndex(i);
|
113
|
maxTab[i] = RubikObjectList.getMaxLevel(object, sizeIndex);
|
114
|
toDoTab[i] = 0;
|
115
|
|
116
|
toDo += maxTab[i];
|
117
|
}
|
118
|
|
119
|
while( toDo>0 )
|
120
|
{
|
121
|
toDo--;
|
122
|
getNext(mViewPager.getCurrentItem(),toDoTab,maxTab);
|
123
|
addSection( mToDoTab,mToDoLvl, mViews[mToDoTab],
|
124
|
country[mToDoTab][mToDoLvl],name[mToDoTab][mToDoLvl],time[mToDoTab][mToDoLvl]);
|
125
|
}
|
126
|
}
|
127
|
|
128
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
129
|
|
130
|
public void message(final String mess)
|
131
|
{
|
132
|
mAct.runOnUiThread(new Runnable()
|
133
|
{
|
134
|
@Override
|
135
|
public void run()
|
136
|
{
|
137
|
for(int i=0; i<mNumTabs; i++)
|
138
|
{
|
139
|
mViews[i].message(mess);
|
140
|
}
|
141
|
}
|
142
|
});
|
143
|
}
|
144
|
|
145
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
146
|
|
147
|
public void error(final String error)
|
148
|
{
|
149
|
char errorNumber = error.charAt(0);
|
150
|
|
151
|
switch(errorNumber)
|
152
|
{
|
153
|
case '1': message("Network error");
|
154
|
break;
|
155
|
case '2': RubikScores scores = RubikScores.getInstance();
|
156
|
Bundle bundle = new Bundle();
|
157
|
bundle.putString("name", scores.getName() );
|
158
|
|
159
|
RubikDialogSetName nameDiag = new RubikDialogSetName();
|
160
|
nameDiag.setArguments(bundle);
|
161
|
nameDiag.show(mAct.getSupportFragmentManager(), null);
|
162
|
|
163
|
mDialog.dismiss();
|
164
|
|
165
|
break;
|
166
|
case '3': message("Server error");
|
167
|
break;
|
168
|
default : message("Unexpected error");
|
169
|
}
|
170
|
}
|
171
|
|
172
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
173
|
|
174
|
private void prepareView()
|
175
|
{
|
176
|
mAct.runOnUiThread(new Runnable()
|
177
|
{
|
178
|
@Override
|
179
|
public void run()
|
180
|
{
|
181
|
for(int i=0; i<mNumTabs; i++)
|
182
|
{
|
183
|
mViews[i].prepareView(mAct);
|
184
|
}
|
185
|
}
|
186
|
});
|
187
|
try
|
188
|
{
|
189
|
Thread.sleep(150);
|
190
|
}
|
191
|
catch( InterruptedException ignored)
|
192
|
{
|
193
|
|
194
|
}
|
195
|
}
|
196
|
|
197
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
198
|
|
199
|
RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager, boolean isSubmitting, RubikDialogScores diag)
|
200
|
{
|
201
|
mAct = act;
|
202
|
mDialog = diag;
|
203
|
mNumTabs = RubikObjectList.getTotal();
|
204
|
mViews = new RubikDialogScoresView[mNumTabs];
|
205
|
mViewPager = viewPager;
|
206
|
mIsSubmitting = isSubmitting;
|
207
|
|
208
|
viewPager.setAdapter(this);
|
209
|
viewPager.setOffscreenPageLimit(mNumTabs-1);
|
210
|
}
|
211
|
|
212
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
213
|
|
214
|
@Override
|
215
|
@NonNull
|
216
|
public Object instantiateItem(@NonNull ViewGroup collection, int position)
|
217
|
{
|
218
|
DisplayMetrics metrics = mAct.getResources().getDisplayMetrics();
|
219
|
|
220
|
mViews[position] = new RubikDialogScoresView(mAct, metrics.heightPixels, mIsSubmitting);
|
221
|
collection.addView(mViews[position]);
|
222
|
|
223
|
boolean allCreated = true;
|
224
|
|
225
|
for(int i=0; i<mNumTabs; i++)
|
226
|
{
|
227
|
if( mViews[i]==null )
|
228
|
{
|
229
|
allCreated = false;
|
230
|
break;
|
231
|
}
|
232
|
}
|
233
|
|
234
|
if( allCreated )
|
235
|
{
|
236
|
RubikScoresDownloader downloader = RubikScoresDownloader.getInstance();
|
237
|
|
238
|
if( mIsSubmitting ) downloader.submit ( this, mAct );
|
239
|
else downloader.download( this, mAct );
|
240
|
}
|
241
|
|
242
|
return mViews[position];
|
243
|
}
|
244
|
|
245
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
246
|
|
247
|
@Override
|
248
|
public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
|
249
|
{
|
250
|
collection.removeView((View) view);
|
251
|
}
|
252
|
|
253
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
254
|
|
255
|
@Override
|
256
|
public int getCount()
|
257
|
{
|
258
|
return mNumTabs;
|
259
|
}
|
260
|
|
261
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
262
|
|
263
|
@Override
|
264
|
public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
|
265
|
{
|
266
|
return view == object;
|
267
|
}
|
268
|
}
|