Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresPagerAdapter.java @ 13ed61b5

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
import android.view.View;
28
import android.view.ViewGroup;
29
import android.widget.LinearLayout;
30

    
31
import org.distorted.scores.RubikScores;
32
import org.distorted.scores.RubikScoresDownloader;
33
import org.distorted.objects.RubikObjectList;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
class RubikDialogScoresPagerAdapter extends PagerAdapter implements RubikScoresDownloader.Receiver
38
  {
39
  private FragmentActivity mAct;
40
  private RubikDialogScores mDialog;
41
  private RubikDialogScoresView[] mViews;
42
  private ViewPager mViewPager;
43
  private int mNumTabs;
44
  private boolean mIsSubmitting;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
  public void receive(final String[][][] country, final String[][][] name, final float[][][] time)
49
    {
50
    prepareView();
51

    
52
    boolean[] tabDone = new boolean[mNumTabs];
53
    for(int i=0; i<mNumTabs; i++) tabDone[i] = false;
54
    int toDo, doneTabs=0;
55

    
56
    while( doneTabs<mNumTabs )
57
      {
58
      toDo = mViewPager.getCurrentItem();
59

    
60
      if( tabDone[toDo] )
61
        {
62
        for(int i=0; i<mNumTabs; i++)
63
          {
64
          if( !tabDone[i] )
65
            {
66
            toDo = i;
67
            break;
68
            }
69
          }
70
        }
71

    
72
      addPage(toDo, mViews[toDo],country[toDo],name[toDo],time[toDo]);
73

    
74
      doneTabs++;
75
      tabDone[toDo] = true;
76
      }
77
    }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
  public void message(final String mess)
82
    {
83
    mAct.runOnUiThread(new Runnable()
84
      {
85
      @Override
86
      public void run()
87
        {
88
        for(int i=0; i<mNumTabs; i++)
89
          {
90
          mViews[i].message(mess);
91
          }
92
        }
93
      });
94
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
  public void error(final String error)
99
    {
100
    char errorNumber = error.charAt(0);
101

    
102
    switch(errorNumber)
103
      {
104
      case '1': message("Network error");
105
                break;
106
      case '2': RubikScores scores = RubikScores.getInstance();
107
                Bundle bundle = new Bundle();
108
                bundle.putString("name", scores.getName() );
109

    
110
                RubikDialogSetName nameDiag = new RubikDialogSetName();
111
                nameDiag.setArguments(bundle);
112
                nameDiag.show(mAct.getSupportFragmentManager(), null);
113

    
114
                mDialog.dismiss();
115

    
116
                break;
117
      case '3': message("Server error");
118
                break;
119
      default : message("Unexpected error");
120
      }
121
    }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
  private void prepareView()
126
    {
127
    mAct.runOnUiThread(new Runnable()
128
      {
129
      @Override
130
      public void run()
131
        {
132
        for(int i=0; i<mNumTabs; i++)
133
          {
134
          mViews[i].prepareView(mAct);
135
          }
136
        }
137
      });
138
    try
139
      {
140
      Thread.sleep(150);
141
      }
142
    catch( InterruptedException ie)
143
      {
144

    
145
      }
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
  private void addPage(int tab, final RubikDialogScoresView view, final String[][] country, final String[][] name, final float[][] time)
151
    {
152
    int object   = RubikObjectList.unpackObject(tab);
153
    int sizeIndex= RubikObjectList.unpackSizeIndex(tab);
154
    int maxLevel  = RubikObjectList.getMaxLevel(object, sizeIndex);
155

    
156
    for(int i=0; i<maxLevel; i++)
157
      {
158
      final LinearLayout section = view.createSection(mAct, tab, i, country[i], name[i], time[i]);
159

    
160
      mAct.runOnUiThread(new Runnable()
161
        {
162
        @Override
163
        public void run()
164
          {
165
          view.addSection(section);
166
          }
167
        });
168

    
169
      try
170
        {
171
        Thread.sleep(50);
172
        }
173
      catch( InterruptedException ie)
174
        {
175

    
176
        }
177
      }
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager, boolean isSubmitting, RubikDialogScores diag)
183
    {
184
    mAct = act;
185
    mDialog = diag;
186
    mNumTabs = RubikObjectList.getTotal();
187
    mViews = new RubikDialogScoresView[mNumTabs];
188
    mViewPager = viewPager;
189
    mIsSubmitting = isSubmitting;
190

    
191
    viewPager.setAdapter(this);
192
    viewPager.setOffscreenPageLimit(mNumTabs-1);
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
  @Override
198
  @NonNull
199
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
200
    {
201
    mViews[position] = new RubikDialogScoresView(mAct, mIsSubmitting);
202
    collection.addView(mViews[position]);
203

    
204
    boolean allCreated = true;
205

    
206
    for(int i=0; i<mNumTabs; i++)
207
      {
208
      if( mViews[i]==null )
209
        {
210
        allCreated = false;
211
        break;
212
        }
213
      }
214

    
215
    if( allCreated )
216
      {
217
      RubikScoresDownloader downloader = RubikScoresDownloader.getInstance();
218

    
219
      if( mIsSubmitting )  downloader.submit  ( this, mAct );
220
      else                 downloader.download( this, mAct );
221
      }
222

    
223
    return mViews[position];
224
    }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  @Override
229
  public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
230
    {
231
    collection.removeView((View) view);
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  @Override
237
  public int getCount()
238
    {
239
    return mNumTabs;
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
  @Override
245
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
246
    {
247
    return view == object;
248
    }
249
  }
(9-9/13)