Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialog / RubikDialogScoresPagerAdapter.java @ f895e77a

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.os.Bundle;
23
import android.support.annotation.NonNull;
24
import android.support.v4.app.FragmentActivity;
25
import android.support.v4.view.PagerAdapter;
26
import android.support.v4.view.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.object.RubikObjectList;
34

    
35
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

    
54
    int c = mViewPager.getCurrentItem();
55

    
56
    addPage(c, mViews[c],country[c],name[c],time[c]);
57

    
58
    for(int i=0; i<mNumTabs; i++)
59
      {
60
      if( i==c ) continue;
61

    
62
      addPage(i, mViews[i],country[i],name[i],time[i]);
63
      }
64
    }
65

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

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

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
  public void serverError(final String error)
86
    {
87
    char errorNumber = error.charAt(0);
88

    
89
    switch(errorNumber)
90
      {
91
      case '2': RubikScores scores = RubikScores.getInstance();
92
                Bundle bundle = new Bundle();
93
                bundle.putString("name", scores.getName() );
94

    
95
                RubikDialogSetName nameDiag = new RubikDialogSetName();
96
                nameDiag.setArguments(bundle);
97
                nameDiag.show(mAct.getSupportFragmentManager(), null);
98

    
99
                mDialog.dismiss();
100

    
101
                break;
102
      case '3': message("Server error");
103
                break;
104
      }
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
  private void prepareView()
110
    {
111
    mAct.runOnUiThread(new Runnable()
112
      {
113
      @Override
114
      public void run()
115
        {
116
        for(int i=0; i<mNumTabs; i++)
117
          {
118
          mViews[i].prepareView(mAct);
119
          }
120
        }
121
      });
122
    try
123
      {
124
      Thread.sleep(150);
125
      }
126
    catch( InterruptedException ie)
127
      {
128

    
129
      }
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  private void addPage(int tab, final RubikDialogScoresView view, final String[][] country, final String[][] name, final float[][] time)
135
    {
136
    for(int i=0; i<MAX_SCRAMBLE; i++)
137
      {
138
      final LinearLayout section = view.createSection(mAct, tab, i, country[i], name[i], time[i]);
139

    
140
      mAct.runOnUiThread(new Runnable()
141
        {
142
        @Override
143
        public void run()
144
          {
145
          view.addSection(section);
146
          }
147
        });
148

    
149
      try
150
        {
151
        Thread.sleep(50);
152
        }
153
      catch( InterruptedException ie)
154
        {
155

    
156
        }
157
      }
158
    }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
  RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager, boolean isSubmitting, RubikDialogScores diag)
163
    {
164
    mAct = act;
165
    mDialog = diag;
166
    mNumTabs = RubikObjectList.getTotal();
167
    mViews = new RubikDialogScoresView[mNumTabs];
168
    mViewPager = viewPager;
169
    mIsSubmitting = isSubmitting;
170

    
171
    viewPager.setAdapter(this);
172
    viewPager.setOffscreenPageLimit(mNumTabs-1);
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
  @Override
178
  @NonNull
179
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
180
    {
181
    mViews[position] = new RubikDialogScoresView(mAct, mIsSubmitting);
182
    collection.addView(mViews[position]);
183

    
184
    boolean allCreated = true;
185

    
186
    for(int i=0; i<mNumTabs; i++)
187
      {
188
      if( mViews[i]==null )
189
        {
190
        allCreated = false;
191
        break;
192
        }
193
      }
194

    
195
    if( allCreated )
196
      {
197
      RubikScoresDownloader downloader = RubikScoresDownloader.getInstance();
198

    
199
      if( mIsSubmitting )  downloader.submit  ( this, mAct );
200
      else                 downloader.download( this, mAct );
201
      }
202

    
203
    return mViews[position];
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  @Override
209
  public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
210
    {
211
    collection.removeView((View) view);
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
  @Override
217
  public int getCount()
218
    {
219
    return mNumTabs;
220
    }
221

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

    
224
  @Override
225
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
226
    {
227
    return view == object;
228
    }
229
  }
(5-5/9)