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