Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialog / RubikDialogScoresPagerAdapter.java @ 17f9a695

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.dialog;
21

    
22
import android.support.annotation.NonNull;
23
import android.support.v4.app.FragmentActivity;
24
import android.support.v4.view.PagerAdapter;
25
import android.support.v4.view.ViewPager;
26
import android.view.View;
27
import android.view.ViewGroup;
28
import android.widget.LinearLayout;
29

    
30
import org.distorted.scores.RubikScoresDownloader;
31
import org.distorted.object.RubikObjectList;
32

    
33
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
34

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

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

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

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

    
51
    int c = mViewPager.getCurrentItem();
52

    
53
    addPage(c, mViews[c],country[c],name[c],time[c]);
54

    
55
    for(int i=0; i<mNumTabs; i++)
56
      {
57
      if( i==c ) continue;
58

    
59
      addPage(i, mViews[i],country[i],name[i],time[i]);
60
      }
61
    }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  public void message(final String mess)
66
    {
67
    mAct.runOnUiThread(new Runnable()
68
      {
69
      @Override
70
      public void run()
71
        {
72
        for(int i=0; i<mNumTabs; i++)
73
          {
74
          mViews[i].message(mess);
75
          }
76
        }
77
      });
78
    }
79

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

    
82
  public void serverError(final String error)
83
    {
84
    char errorNumber = error.charAt(0);
85

    
86
    switch(errorNumber)
87
      {
88
      case '2': message("Name Taken");   // TODO
89
                break;
90
      case '3': message("Server error");
91
                break;
92
      }
93
    }
94

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

    
97
  private void prepareView()
98
    {
99
    mAct.runOnUiThread(new Runnable()
100
      {
101
      @Override
102
      public void run()
103
        {
104
        for(int i=0; i<mNumTabs; i++)
105
          {
106
          mViews[i].prepareView(mAct);
107
          }
108
        }
109
      });
110
    try
111
      {
112
      Thread.sleep(150);
113
      }
114
    catch( InterruptedException ie)
115
      {
116

    
117
      }
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  private void addPage(int tab, final RubikDialogScoresView view, final String[][] country, final String[][] name, final float[][] time)
123
    {
124
    for(int i=0; i<MAX_SCRAMBLE; i++)
125
      {
126
      final LinearLayout section = view.createSection(mAct, tab, i, country[i], name[i], time[i]);
127

    
128
      mAct.runOnUiThread(new Runnable()
129
        {
130
        @Override
131
        public void run()
132
          {
133
          view.addSection(section);
134
          }
135
        });
136

    
137
      try
138
        {
139
        Thread.sleep(50);
140
        }
141
      catch( InterruptedException ie)
142
        {
143

    
144
        }
145
      }
146
    }
147

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

    
150
  RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager, boolean isSubmitting)
151
    {
152
    mAct = act;
153
    mNumTabs = RubikObjectList.getTotal();
154
    mViews = new RubikDialogScoresView[mNumTabs];
155
    mViewPager = viewPager;
156
    mIsSubmitting = isSubmitting;
157

    
158
    viewPager.setAdapter(this);
159
    viewPager.setOffscreenPageLimit(mNumTabs-1);
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
  @Override
165
  @NonNull
166
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
167
    {
168
    mViews[position] = new RubikDialogScoresView(mAct, mIsSubmitting);
169
    collection.addView(mViews[position]);
170

    
171
    boolean allCreated = true;
172

    
173
    for(int i=0; i<mNumTabs; i++)
174
      {
175
      if( mViews[i]==null )
176
        {
177
        allCreated = false;
178
        break;
179
        }
180
      }
181

    
182
    if( allCreated )
183
      {
184
      RubikScoresDownloader downloader = RubikScoresDownloader.getInstance();
185

    
186
      if( mIsSubmitting )  downloader.submit  ( this, mAct );
187
      else                 downloader.download( this, mAct );
188
      }
189

    
190
    return mViews[position];
191
    }
192

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

    
195
  @Override
196
  public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
197
    {
198
    collection.removeView((View) view);
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  @Override
204
  public int getCount()
205
    {
206
    return mNumTabs;
207
    }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
  @Override
212
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
213
    {
214
    return view == object;
215
    }
216
  }
(5-5/8)