Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresPagerAdapter.java @ 3e5ad461

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 f895e77a Leszek Koltunski
import org.distorted.scores.RubikScores;
34 f0e87514 Leszek Koltunski
import org.distorted.scores.RubikScoresDownloader;
35 1f9772f3 Leszek Koltunski
import org.distorted.objects.RubikObjectList;
36 4888e97c Leszek Koltunski
37 cc5ec229 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
38
39 0fd3ac1f Leszek Koltunski
class RubikDialogScoresPagerAdapter extends PagerAdapter implements RubikScoresDownloader.Receiver
40 cc5ec229 Leszek Koltunski
  {
41
  private FragmentActivity mAct;
42 f895e77a Leszek Koltunski
  private RubikDialogScores mDialog;
43 0fd3ac1f Leszek Koltunski
  private RubikDialogScoresView[] mViews;
44 028f3e0d Leszek Koltunski
  private ViewPager mViewPager;
45 4888e97c Leszek Koltunski
  private int mNumTabs;
46 4895fff6 Leszek Koltunski
  private boolean mIsSubmitting;
47 3e5ad461 Leszek Koltunski
  private int mToDoTab, mToDoLvl;
48 cc5ec229 Leszek Koltunski
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51 3e5ad461 Leszek Koltunski
  private void addSection(int tab, int level, final RubikDialogScoresView view, final String[] country, final String[] name, final float[] time)
52 028f3e0d Leszek Koltunski
    {
53 3e5ad461 Leszek Koltunski
    final LinearLayout section = view.createSection(mAct, tab, level, country, name, time);
54 028f3e0d Leszek Koltunski
55 3e5ad461 Leszek Koltunski
    mAct.runOnUiThread(new Runnable()
56
      {
57
      @Override
58
      public void run()
59
        {
60
        view.addSection(section);
61
        }
62
      });
63 028f3e0d Leszek Koltunski
64 3e5ad461 Leszek Koltunski
    try
65 028f3e0d Leszek Koltunski
      {
66 3e5ad461 Leszek Koltunski
      Thread.sleep(50);
67
      }
68
    catch( InterruptedException ignored)
69
      {
70
71
      }
72
    }
73
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75 13ed61b5 Leszek Koltunski
76 3e5ad461 Leszek Koltunski
  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 13ed61b5 Leszek Koltunski
        {
88 3e5ad461 Leszek Koltunski
        if( toDoTab[tab]<maxTab[tab] )
89 13ed61b5 Leszek Koltunski
          {
90 3e5ad461 Leszek Koltunski
          mToDoTab = tab;
91
          mToDoLvl = toDoTab[tab];
92
          toDoTab[tab]++;
93
          break;
94 13ed61b5 Leszek Koltunski
          }
95
        }
96 3e5ad461 Leszek Koltunski
      }
97
    }
98 13ed61b5 Leszek Koltunski
99 3e5ad461 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
100 028f3e0d Leszek Koltunski
101 3e5ad461 Leszek Koltunski
  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 028f3e0d Leszek Koltunski
      }
126
    }
127
128 17f9a695 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 4c0cd600 Leszek Koltunski
  public void error(final String error)
148 17f9a695 Leszek Koltunski
    {
149
    char errorNumber = error.charAt(0);
150
151
    switch(errorNumber)
152
      {
153 90fd47b0 Leszek Koltunski
      case '1': message("Network error");
154 4c0cd600 Leszek Koltunski
                break;
155 f895e77a Leszek Koltunski
      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 17f9a695 Leszek Koltunski
                break;
166
      case '3': message("Server error");
167
                break;
168 4c0cd600 Leszek Koltunski
      default : message("Unexpected error");
169 17f9a695 Leszek Koltunski
      }
170
    }
171
172 028f3e0d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
173
174
  private void prepareView()
175 946d9762 Leszek Koltunski
    {
176
    mAct.runOnUiThread(new Runnable()
177
      {
178
      @Override
179
      public void run()
180
        {
181 4888e97c Leszek Koltunski
        for(int i=0; i<mNumTabs; i++)
182 946d9762 Leszek Koltunski
          {
183
          mViews[i].prepareView(mAct);
184
          }
185
        }
186
      });
187
    try
188
      {
189 7aff6aa7 Leszek Koltunski
      Thread.sleep(150);
190 946d9762 Leszek Koltunski
      }
191 d5d503d0 Leszek Koltunski
    catch( InterruptedException ignored)
192 946d9762 Leszek Koltunski
      {
193
194
      }
195 028f3e0d Leszek Koltunski
    }
196 946d9762 Leszek Koltunski
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198
199 f895e77a Leszek Koltunski
  RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager, boolean isSubmitting, RubikDialogScores diag)
200 cc5ec229 Leszek Koltunski
    {
201
    mAct = act;
202 f895e77a Leszek Koltunski
    mDialog = diag;
203 4888e97c Leszek Koltunski
    mNumTabs = RubikObjectList.getTotal();
204
    mViews = new RubikDialogScoresView[mNumTabs];
205 028f3e0d Leszek Koltunski
    mViewPager = viewPager;
206 4895fff6 Leszek Koltunski
    mIsSubmitting = isSubmitting;
207 cc5ec229 Leszek Koltunski
208
    viewPager.setAdapter(this);
209 4888e97c Leszek Koltunski
    viewPager.setOffscreenPageLimit(mNumTabs-1);
210 cc5ec229 Leszek Koltunski
    }
211
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213
214
  @Override
215
  @NonNull
216
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
217
    {
218 362807f0 Leszek Koltunski
    DisplayMetrics metrics = mAct.getResources().getDisplayMetrics();
219
220
    mViews[position] = new RubikDialogScoresView(mAct, metrics.heightPixels, mIsSubmitting);
221 946d9762 Leszek Koltunski
    collection.addView(mViews[position]);
222
223
    boolean allCreated = true;
224
225 4888e97c Leszek Koltunski
    for(int i=0; i<mNumTabs; i++)
226 946d9762 Leszek Koltunski
      {
227
      if( mViews[i]==null )
228
        {
229
        allCreated = false;
230
        break;
231
        }
232
      }
233
234
    if( allCreated )
235
      {
236 bc511ea5 Leszek Koltunski
      RubikScoresDownloader downloader = RubikScoresDownloader.getInstance();
237 4895fff6 Leszek Koltunski
238
      if( mIsSubmitting )  downloader.submit  ( this, mAct );
239
      else                 downloader.download( this, mAct );
240 946d9762 Leszek Koltunski
      }
241
242
    return mViews[position];
243 cc5ec229 Leszek Koltunski
    }
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 4888e97c Leszek Koltunski
    return mNumTabs;
259 cc5ec229 Leszek Koltunski
    }
260
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262
263
  @Override
264
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
265
    {
266
    return view == object;
267
    }
268
  }