Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresPagerAdapter.java @ 5bda8973

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 prepareView()
44
    {
45
    mAct.runOnUiThread(new Runnable()
46
      {
47
      @Override
48
      public void run()
49
        {
50
        for(int i=0; i<mNumTabs; i++)
51
          {
52
          mViews[i].prepareView(mAct);
53
          }
54
        }
55
      });
56
    try
57
      {
58
      Thread.sleep(150);
59
      }
60
    catch( InterruptedException ignored)
61
      {
62

    
63
      }
64
    }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
  private void addSection(int tab, int level, final RubikDialogScoresView view, final String[] country, final String[] name, final float[] time)
69
    {
70
    String title = level==RubikScreenPlay.LEVELS_SHOWN ?
71
                   mAct.getString(R.string.levelM) :
72
                   mAct.getString(R.string.lv_placeholder,level+1);
73

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

    
76
    mAct.runOnUiThread(new Runnable()
77
      {
78
      @Override
79
      public void run()
80
        {
81
        view.addSection(section);
82
        }
83
      });
84

    
85
    try
86
      {
87
      Thread.sleep(50);
88
      }
89
    catch( InterruptedException ignored)
90
      {
91

    
92
      }
93
    }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

    
97
  private void getNext(int currentTab, int[] toDoTab)
98
    {
99
    int MAX = RubikScreenPlay.LEVELS_SHOWN;
100

    
101
    if( toDoTab[currentTab]<=MAX )
102
      {
103
      mToDoTab = currentTab;
104
      mToDoLvl = toDoTab[currentTab];
105
      toDoTab[currentTab]++;
106
      }
107
    else
108
      {
109
      for(int tab=0; tab<mNumTabs; tab++)
110
        {
111
        if( toDoTab[tab]<=MAX )
112
          {
113
          mToDoTab = tab;
114
          mToDoLvl = toDoTab[tab];
115
          toDoTab[tab]++;
116
          break;
117
          }
118
        }
119
      }
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  public void receive(final String[][][] country, final String[][][] name, final float[][][] time)
125
    {
126
    prepareView();
127
    int toDo=0;
128
    int[] toDoTab = new int[mNumTabs];
129

    
130
    for(int i=0; i<mNumTabs; i++)
131
      {
132
      toDoTab[i]= 0;
133
      toDo += (RubikScreenPlay.LEVELS_SHOWN+1);
134
      }
135

    
136
    while( toDo>0 )
137
      {
138
      toDo--;
139
      getNext(mViewPager.getCurrentItem(), toDoTab);
140
      addSection( mToDoTab, mToDoLvl, mViews[mToDoTab], country[mToDoTab][mToDoLvl], name[mToDoTab][mToDoLvl], time[mToDoTab][mToDoLvl]);
141
      }
142
    }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
  public void message(final String mess)
147
    {
148
    mAct.runOnUiThread(new Runnable()
149
      {
150
      @Override
151
      public void run()
152
        {
153
        for(int i=0; i<mNumTabs; i++)
154
          {
155
          mViews[i].message(mess);
156
          }
157
        }
158
      });
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  public void error(final String error)
164
    {
165
    char errorNumber = error.charAt(0);
166

    
167
    switch(errorNumber)
168
      {
169
      case '1': message(mAct.getString(R.string.networkError));
170
                break;
171
      case '2': RubikScores scores = RubikScores.getInstance();
172
                Bundle bundle = new Bundle();
173
                bundle.putString("name", scores.getName() );
174

    
175
                RubikDialogSetName nameDiag = new RubikDialogSetName();
176
                nameDiag.setArguments(bundle);
177
                nameDiag.show(mAct.getSupportFragmentManager(), null);
178

    
179
                mDialog.dismiss();
180

    
181
                break;
182
      case '3': message("Server error");
183
                break;
184
      default : message("Unexpected error");
185
      }
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
  RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager, boolean isSubmitting, RubikDialogScores diag)
191
    {
192
    mAct = act;
193
    mDialog = diag;
194
    mNumTabs = RubikObjectList.getNumObjects();
195
    mViews = new RubikDialogScoresView[mNumTabs];
196
    mViewPager = viewPager;
197
    mIsSubmitting = isSubmitting;
198

    
199
    viewPager.setAdapter(this);
200
    viewPager.setOffscreenPageLimit(mNumTabs-1);
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
  @Override
206
  @NonNull
207
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
208
    {
209
    DisplayMetrics metrics = mAct.getResources().getDisplayMetrics();
210

    
211
    mViews[position] = new RubikDialogScoresView(mAct, metrics.heightPixels, mIsSubmitting);
212
    collection.addView(mViews[position]);
213

    
214
    boolean allCreated = true;
215

    
216
    for(int i=0; i<mNumTabs; i++)
217
      {
218
      if( mViews[i]==null )
219
        {
220
        allCreated = false;
221
        break;
222
        }
223
      }
224

    
225
    if( allCreated )
226
      {
227
      RubikNetwork network = RubikNetwork.getInstance();
228

    
229
      if( mIsSubmitting )  network.submit  ( this, mAct );
230
      else                 network.download( this, mAct );
231
      }
232

    
233
    return mViews[position];
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
  @Override
239
  public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
240
    {
241
    collection.removeView((View) view);
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
  @Override
247
  public int getCount()
248
    {
249
    return mNumTabs;
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
  @Override
255
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
256
    {
257
    return view == object;
258
    }
259
  }
(14-14/23)