Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresPagerAdapter.java @ b61c66ad

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