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.dialog;
|
21
|
|
22
|
import android.os.Bundle;
|
23
|
import android.support.annotation.NonNull;
|
24
|
import android.support.v4.app.FragmentActivity;
|
25
|
import android.support.v4.view.PagerAdapter;
|
26
|
import android.support.v4.view.ViewPager;
|
27
|
import android.view.View;
|
28
|
import android.view.ViewGroup;
|
29
|
import android.widget.LinearLayout;
|
30
|
|
31
|
import org.distorted.scores.RubikScores;
|
32
|
import org.distorted.scores.RubikScoresDownloader;
|
33
|
import org.distorted.object.RubikObjectList;
|
34
|
|
35
|
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
|
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
|
int c = mViewPager.getCurrentItem();
|
55
|
|
56
|
addPage(c, mViews[c],country[c],name[c],time[c]);
|
57
|
|
58
|
for(int i=0; i<mNumTabs; i++)
|
59
|
{
|
60
|
if( i==c ) continue;
|
61
|
|
62
|
addPage(i, mViews[i],country[i],name[i],time[i]);
|
63
|
}
|
64
|
}
|
65
|
|
66
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
67
|
|
68
|
public void message(final String mess)
|
69
|
{
|
70
|
mAct.runOnUiThread(new Runnable()
|
71
|
{
|
72
|
@Override
|
73
|
public void run()
|
74
|
{
|
75
|
for(int i=0; i<mNumTabs; i++)
|
76
|
{
|
77
|
mViews[i].message(mess);
|
78
|
}
|
79
|
}
|
80
|
});
|
81
|
}
|
82
|
|
83
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
84
|
|
85
|
public void error(final String error)
|
86
|
{
|
87
|
char errorNumber = error.charAt(0);
|
88
|
|
89
|
switch(errorNumber)
|
90
|
{
|
91
|
case '1': message("Client error");
|
92
|
break;
|
93
|
case '2': RubikScores scores = RubikScores.getInstance();
|
94
|
Bundle bundle = new Bundle();
|
95
|
bundle.putString("name", scores.getName() );
|
96
|
|
97
|
RubikDialogSetName nameDiag = new RubikDialogSetName();
|
98
|
nameDiag.setArguments(bundle);
|
99
|
nameDiag.show(mAct.getSupportFragmentManager(), null);
|
100
|
|
101
|
mDialog.dismiss();
|
102
|
|
103
|
break;
|
104
|
case '3': message("Server error");
|
105
|
break;
|
106
|
default : message("Unexpected error");
|
107
|
}
|
108
|
}
|
109
|
|
110
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
|
112
|
private void prepareView()
|
113
|
{
|
114
|
mAct.runOnUiThread(new Runnable()
|
115
|
{
|
116
|
@Override
|
117
|
public void run()
|
118
|
{
|
119
|
for(int i=0; i<mNumTabs; i++)
|
120
|
{
|
121
|
mViews[i].prepareView(mAct);
|
122
|
}
|
123
|
}
|
124
|
});
|
125
|
try
|
126
|
{
|
127
|
Thread.sleep(150);
|
128
|
}
|
129
|
catch( InterruptedException ie)
|
130
|
{
|
131
|
|
132
|
}
|
133
|
}
|
134
|
|
135
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
136
|
|
137
|
private void addPage(int tab, final RubikDialogScoresView view, final String[][] country, final String[][] name, final float[][] time)
|
138
|
{
|
139
|
for(int i=0; i<MAX_SCRAMBLE; i++)
|
140
|
{
|
141
|
final LinearLayout section = view.createSection(mAct, tab, i, country[i], name[i], time[i]);
|
142
|
|
143
|
mAct.runOnUiThread(new Runnable()
|
144
|
{
|
145
|
@Override
|
146
|
public void run()
|
147
|
{
|
148
|
view.addSection(section);
|
149
|
}
|
150
|
});
|
151
|
|
152
|
try
|
153
|
{
|
154
|
Thread.sleep(50);
|
155
|
}
|
156
|
catch( InterruptedException ie)
|
157
|
{
|
158
|
|
159
|
}
|
160
|
}
|
161
|
}
|
162
|
|
163
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
164
|
|
165
|
RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager, boolean isSubmitting, RubikDialogScores diag)
|
166
|
{
|
167
|
mAct = act;
|
168
|
mDialog = diag;
|
169
|
mNumTabs = RubikObjectList.getTotal();
|
170
|
mViews = new RubikDialogScoresView[mNumTabs];
|
171
|
mViewPager = viewPager;
|
172
|
mIsSubmitting = isSubmitting;
|
173
|
|
174
|
viewPager.setAdapter(this);
|
175
|
viewPager.setOffscreenPageLimit(mNumTabs-1);
|
176
|
}
|
177
|
|
178
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
179
|
|
180
|
@Override
|
181
|
@NonNull
|
182
|
public Object instantiateItem(@NonNull ViewGroup collection, int position)
|
183
|
{
|
184
|
mViews[position] = new RubikDialogScoresView(mAct, mIsSubmitting);
|
185
|
collection.addView(mViews[position]);
|
186
|
|
187
|
boolean allCreated = true;
|
188
|
|
189
|
for(int i=0; i<mNumTabs; i++)
|
190
|
{
|
191
|
if( mViews[i]==null )
|
192
|
{
|
193
|
allCreated = false;
|
194
|
break;
|
195
|
}
|
196
|
}
|
197
|
|
198
|
if( allCreated )
|
199
|
{
|
200
|
RubikScoresDownloader downloader = RubikScoresDownloader.getInstance();
|
201
|
|
202
|
if( mIsSubmitting ) downloader.submit ( this, mAct );
|
203
|
else downloader.download( this, mAct );
|
204
|
}
|
205
|
|
206
|
return mViews[position];
|
207
|
}
|
208
|
|
209
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
210
|
|
211
|
@Override
|
212
|
public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
|
213
|
{
|
214
|
collection.removeView((View) view);
|
215
|
}
|
216
|
|
217
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
218
|
|
219
|
@Override
|
220
|
public int getCount()
|
221
|
{
|
222
|
return mNumTabs;
|
223
|
}
|
224
|
|
225
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
226
|
|
227
|
@Override
|
228
|
public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
|
229
|
{
|
230
|
return view == object;
|
231
|
}
|
232
|
}
|