Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresPagerAdapter.java @ 83018ac4

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.dialogs;
11

    
12
import android.os.Bundle;
13
import androidx.annotation.NonNull;
14
import androidx.fragment.app.FragmentActivity;
15
import androidx.viewpager.widget.PagerAdapter;
16
import androidx.viewpager.widget.ViewPager;
17

    
18
import android.util.DisplayMetrics;
19
import android.view.View;
20
import android.view.ViewGroup;
21
import android.widget.LinearLayout;
22

    
23
import org.distorted.main.R;
24
import org.distorted.external.RubikScores;
25
import org.distorted.external.RubikNetwork;
26
import org.distorted.objects.RubikObjectList;
27
import org.distorted.screens.RubikScreenPlay;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
class RubikDialogScoresPagerAdapter extends PagerAdapter implements RubikNetwork.ScoresReceiver
32
  {
33
  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
  private int mToDoTab, mToDoLvl;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
  private void addSection(int tab, int level, final RubikDialogScoresView view, final String[] country, final String[] name, final float[] time)
44
    {
45
    if( level>RubikScreenPlay.LEVELS_SHOWN ) level=RubikScreenPlay.LEVELS_SHOWN;
46

    
47
    String title = level==RubikScreenPlay.LEVELS_SHOWN ?
48
                   mAct.getString(R.string.levelM) :
49
                   mAct.getString(R.string.lv_placeholder,level+1);
50

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

    
53
    mAct.runOnUiThread(new Runnable()
54
      {
55
      @Override
56
      public void run()
57
        {
58
        view.addSection(section);
59
        }
60
      });
61

    
62
    try
63
      {
64
      Thread.sleep(50);
65
      }
66
    catch( InterruptedException ignored)
67
      {
68

    
69
      }
70
    }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
  private void getNext(int currentTab, int[] toDoTab, int[] lastTab)
75
    {
76
    int MAX = RubikScreenPlay.LEVELS_SHOWN+1;
77

    
78
    if( toDoTab[currentTab]<MAX )
79
      {
80
      mToDoTab = currentTab;
81
      mToDoLvl = toDoTab[currentTab]<MAX-1 ? toDoTab[currentTab] : lastTab[currentTab]-1;
82
      toDoTab[currentTab]++;
83
      }
84
    else
85
      {
86
      for(int tab=0; tab<mNumTabs; tab++)
87
        {
88
        if( toDoTab[tab]<MAX )
89
          {
90
          mToDoTab = tab;
91
          mToDoLvl = toDoTab[tab]<MAX-1 ? toDoTab[tab] : lastTab[tab]-1;
92
          toDoTab[tab]++;
93
          break;
94
          }
95
        }
96
      }
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  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[] lastTab = new int[mNumTabs];
108

    
109
    for(int i=0; i<mNumTabs; i++)
110
      {
111
      lastTab[i]= RubikObjectList.getDBLevel(i);
112
      toDoTab[i]= 0;
113
      toDo += (RubikScreenPlay.LEVELS_SHOWN+1);
114
      }
115

    
116
    while( toDo>0 )
117
      {
118
      toDo--;
119
      getNext(mViewPager.getCurrentItem(), toDoTab, lastTab);
120
      addSection( mToDoTab, mToDoLvl, mViews[mToDoTab], country[mToDoTab][mToDoLvl], name[mToDoTab][mToDoLvl], time[mToDoTab][mToDoLvl]);
121
      }
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  public void message(final String mess)
127
    {
128
    mAct.runOnUiThread(new Runnable()
129
      {
130
      @Override
131
      public void run()
132
        {
133
        for(int i=0; i<mNumTabs; i++)
134
          {
135
          mViews[i].message(mess);
136
          }
137
        }
138
      });
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
  public void error(final String error)
144
    {
145
    char errorNumber = error.charAt(0);
146

    
147
    switch(errorNumber)
148
      {
149
      case '1': message(mAct.getString(R.string.networkError));
150
                break;
151
      case '2': RubikScores scores = RubikScores.getInstance();
152
                Bundle bundle = new Bundle();
153
                bundle.putString("name", scores.getName() );
154

    
155
                RubikDialogSetName nameDiag = new RubikDialogSetName();
156
                nameDiag.setArguments(bundle);
157
                nameDiag.show(mAct.getSupportFragmentManager(), null);
158

    
159
                mDialog.dismiss();
160

    
161
                break;
162
      case '3': message("Server error");
163
                break;
164
      default : message("Unexpected error");
165
      }
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  private void prepareView()
171
    {
172
    mAct.runOnUiThread(new Runnable()
173
      {
174
      @Override
175
      public void run()
176
        {
177
        for(int i=0; i<mNumTabs; i++)
178
          {
179
          mViews[i].prepareView(mAct);
180
          }
181
        }
182
      });
183
    try
184
      {
185
      Thread.sleep(150);
186
      }
187
    catch( InterruptedException ignored)
188
      {
189

    
190
      }
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager, boolean isSubmitting, RubikDialogScores diag)
196
    {
197
    mAct = act;
198
    mDialog = diag;
199
    mNumTabs = RubikObjectList.getNumObjects();
200
    mViews = new RubikDialogScoresView[mNumTabs];
201
    mViewPager = viewPager;
202
    mIsSubmitting = isSubmitting;
203

    
204
    viewPager.setAdapter(this);
205
    viewPager.setOffscreenPageLimit(mNumTabs-1);
206
    }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
  @Override
211
  @NonNull
212
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
213
    {
214
    DisplayMetrics metrics = mAct.getResources().getDisplayMetrics();
215

    
216
    mViews[position] = new RubikDialogScoresView(mAct, metrics.heightPixels, mIsSubmitting);
217
    collection.addView(mViews[position]);
218

    
219
    boolean allCreated = true;
220

    
221
    for(int i=0; i<mNumTabs; i++)
222
      {
223
      if( mViews[i]==null )
224
        {
225
        allCreated = false;
226
        break;
227
        }
228
      }
229

    
230
    if( allCreated )
231
      {
232
      RubikNetwork network = RubikNetwork.getInstance();
233

    
234
      if( mIsSubmitting )  network.submit  ( this, mAct );
235
      else                 network.download( this, mAct );
236
      }
237

    
238
    return mViews[position];
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  @Override
244
  public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
245
    {
246
    collection.removeView((View) view);
247
    }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
  @Override
252
  public int getCount()
253
    {
254
    return mNumTabs;
255
    }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
  @Override
260
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
261
    {
262
    return view == object;
263
    }
264
  }
(14-14/23)