Project

General

Profile

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

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

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.main.R;
34
import org.distorted.network.RubikScores;
35
import org.distorted.network.RubikNetwork;
36
import org.distorted.objects.ObjectList;
37
import org.distorted.screens.RubikScreenPlay;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
class RubikDialogScoresPagerAdapter extends PagerAdapter implements RubikNetwork.Receiver
42
  {
43
  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
  private int mToDoTab, mToDoLvl;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  private void addSection(int tab, int level, int lastLevel, final RubikDialogScoresView view, final String[] country, final String[] name, final float[] time)
54
    {
55
    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

    
61
    mAct.runOnUiThread(new Runnable()
62
      {
63
      @Override
64
      public void run()
65
        {
66
        view.addSection(section);
67
        }
68
      });
69

    
70
    try
71
      {
72
      Thread.sleep(50);
73
      }
74
    catch( InterruptedException ignored)
75
      {
76

    
77
      }
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  private void getNext(int currentTab, int[] toDoTab, int[] maxTab, int[] lastTab)
83
    {
84
    if( toDoTab[currentTab]<maxTab[currentTab] )
85
      {
86
      mToDoTab = currentTab;
87
      mToDoLvl = toDoTab[currentTab]<maxTab[currentTab]-1 ? toDoTab[currentTab] : lastTab[currentTab]-1;
88
      toDoTab[currentTab]++;
89
      }
90
    else
91
      {
92
      for(int tab=0; tab<mNumTabs; tab++)
93
        {
94
        if( toDoTab[tab]<maxTab[tab] )
95
          {
96
          mToDoTab = tab;
97
          mToDoLvl = toDoTab[tab]<maxTab[tab]-1 ? toDoTab[tab] : lastTab[tab]-1;
98
          toDoTab[tab]++;
99
          break;
100
          }
101
        }
102
      }
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
  public void receive(final String[][][] country, final String[][][] name, final float[][][] time)
108
    {
109
    prepareView();
110

    
111
    int MAX = RubikScreenPlay.LEVELS_SHOWN;
112
    int toDo=0;
113
    int[] toDoTab = new int[mNumTabs];
114
    int[] maxTab  = new int[mNumTabs];
115
    int[] lastTab = new int[mNumTabs];
116

    
117
    for(int i=0; i<mNumTabs; i++)
118
      {
119
      int object   = ObjectList.unpackObject(i);
120
      int sizeIndex= ObjectList.unpackSizeIndex(i);
121
      lastTab[i]   = ObjectList.getMaxLevel(object, sizeIndex);
122
      maxTab[i]    = Math.min(lastTab[i],MAX);
123
      toDoTab[i]   = 0;
124

    
125
      toDo += maxTab[i];
126
      }
127

    
128
    while( toDo>0 )
129
      {
130
      toDo--;
131
      getNext(mViewPager.getCurrentItem(),toDoTab,maxTab,lastTab);
132
      addSection( mToDoTab,mToDoLvl, lastTab[mToDoTab], mViews[mToDoTab],
133
                  country[mToDoTab][mToDoLvl],name[mToDoTab][mToDoLvl],time[mToDoTab][mToDoLvl]);
134
      }
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  public void message(final String mess)
140
    {
141
    mAct.runOnUiThread(new Runnable()
142
      {
143
      @Override
144
      public void run()
145
        {
146
        for(int i=0; i<mNumTabs; i++)
147
          {
148
          mViews[i].message(mess);
149
          }
150
        }
151
      });
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
  public void error(final String error)
157
    {
158
    char errorNumber = error.charAt(0);
159

    
160
    switch(errorNumber)
161
      {
162
      case '1': message("Network error");
163
                break;
164
      case '2': RubikScores scores = RubikScores.getInstance();
165
                Bundle bundle = new Bundle();
166
                bundle.putString("name", scores.getName() );
167

    
168
                RubikDialogSetName nameDiag = new RubikDialogSetName();
169
                nameDiag.setArguments(bundle);
170
                nameDiag.show(mAct.getSupportFragmentManager(), null);
171

    
172
                mDialog.dismiss();
173

    
174
                break;
175
      case '3': message("Server error");
176
                break;
177
      default : message("Unexpected error");
178
      }
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  private void prepareView()
184
    {
185
    mAct.runOnUiThread(new Runnable()
186
      {
187
      @Override
188
      public void run()
189
        {
190
        for(int i=0; i<mNumTabs; i++)
191
          {
192
          mViews[i].prepareView(mAct);
193
          }
194
        }
195
      });
196
    try
197
      {
198
      Thread.sleep(150);
199
      }
200
    catch( InterruptedException ignored)
201
      {
202

    
203
      }
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager, boolean isSubmitting, RubikDialogScores diag)
209
    {
210
    mAct = act;
211
    mDialog = diag;
212
    mNumTabs = ObjectList.getTotal();
213
    mViews = new RubikDialogScoresView[mNumTabs];
214
    mViewPager = viewPager;
215
    mIsSubmitting = isSubmitting;
216

    
217
    viewPager.setAdapter(this);
218
    viewPager.setOffscreenPageLimit(mNumTabs-1);
219
    }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
  @Override
224
  @NonNull
225
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
226
    {
227
    DisplayMetrics metrics = mAct.getResources().getDisplayMetrics();
228

    
229
    mViews[position] = new RubikDialogScoresView(mAct, metrics.heightPixels, mIsSubmitting);
230
    collection.addView(mViews[position]);
231

    
232
    boolean allCreated = true;
233

    
234
    for(int i=0; i<mNumTabs; i++)
235
      {
236
      if( mViews[i]==null )
237
        {
238
        allCreated = false;
239
        break;
240
        }
241
      }
242

    
243
    if( allCreated )
244
      {
245
      RubikNetwork network = RubikNetwork.getInstance();
246

    
247
      if( mIsSubmitting )  network.submit  ( this, mAct );
248
      else                 network.download( this, mAct );
249
      }
250

    
251
    return mViews[position];
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  @Override
257
  public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
258
    {
259
    collection.removeView((View) view);
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
  @Override
265
  public int getCount()
266
    {
267
    return mNumTabs;
268
    }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

    
272
  @Override
273
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
274
    {
275
    return view == object;
276
    }
277
  }
(11-11/18)