Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresPagerAdapter.java @ 46be3ddf

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