Project

General

Profile

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

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

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.objectlib.main.ObjectList;
34

    
35
import org.distorted.main.R;
36
import org.distorted.network.RubikScores;
37
import org.distorted.network.RubikNetwork;
38
import org.distorted.screens.RubikScreenPlay;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
class RubikDialogScoresPagerAdapter extends PagerAdapter implements RubikNetwork.Receiver
43
  {
44
  private final FragmentActivity mAct;
45
  private final RubikDialogScores mDialog;
46
  private final RubikDialogScoresView[] mViews;
47
  private final ViewPager mViewPager;
48
  private final int mNumTabs;
49
  private final boolean mIsSubmitting;
50
  private int mToDoTab, mToDoLvl;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  private void addSection(int tab, int level, int lastLevel, final RubikDialogScoresView view, final String[] country, final String[] name, final float[] time)
55
    {
56
    String title = level==lastLevel-1 ?
57
                   mAct.getString(R.string.level_full) :
58
                   mAct.getString(R.string.lv_placeholder,level+1);
59

    
60
    final LinearLayout section = view.createSection(mAct, tab, title, level, country, name, time);
61

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

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

    
78
      }
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

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

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

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

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

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

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

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

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

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

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

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

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

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

    
173
                mDialog.dismiss();
174

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

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

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

    
204
      }
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

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

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

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

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

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

    
233
    boolean allCreated = true;
234

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

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

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

    
252
    return mViews[position];
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

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

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

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

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

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