Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialog / RubikDialogScoresPagerAdapter.java @ 4c0cd600

1 cc5ec229 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 211b48f2 Leszek Koltunski
package org.distorted.dialog;
21 cc5ec229 Leszek Koltunski
22 f895e77a Leszek Koltunski
import android.os.Bundle;
23 cc5ec229 Leszek Koltunski
import android.support.annotation.NonNull;
24
import android.support.v4.app.FragmentActivity;
25
import android.support.v4.view.PagerAdapter;
26 946d9762 Leszek Koltunski
import android.support.v4.view.ViewPager;
27 cc5ec229 Leszek Koltunski
import android.view.View;
28
import android.view.ViewGroup;
29 d5fb0e7e Leszek Koltunski
import android.widget.LinearLayout;
30 cc5ec229 Leszek Koltunski
31 f895e77a Leszek Koltunski
import org.distorted.scores.RubikScores;
32 f0e87514 Leszek Koltunski
import org.distorted.scores.RubikScoresDownloader;
33 4888e97c Leszek Koltunski
import org.distorted.object.RubikObjectList;
34
35 0333d81e Leszek Koltunski
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
36 211b48f2 Leszek Koltunski
37 cc5ec229 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
38
39 0fd3ac1f Leszek Koltunski
class RubikDialogScoresPagerAdapter extends PagerAdapter implements RubikScoresDownloader.Receiver
40 cc5ec229 Leszek Koltunski
  {
41
  private FragmentActivity mAct;
42 f895e77a Leszek Koltunski
  private RubikDialogScores mDialog;
43 0fd3ac1f Leszek Koltunski
  private RubikDialogScoresView[] mViews;
44 028f3e0d Leszek Koltunski
  private ViewPager mViewPager;
45 4888e97c Leszek Koltunski
  private int mNumTabs;
46 4895fff6 Leszek Koltunski
  private boolean mIsSubmitting;
47 cc5ec229 Leszek Koltunski
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50 1c90c64a Leszek Koltunski
  public void receive(final String[][][] country, final String[][][] name, final float[][][] time)
51 028f3e0d Leszek Koltunski
    {
52
    prepareView();
53
54
    int c = mViewPager.getCurrentItem();
55
56 1c90c64a Leszek Koltunski
    addPage(c, mViews[c],country[c],name[c],time[c]);
57 028f3e0d Leszek Koltunski
58 4888e97c Leszek Koltunski
    for(int i=0; i<mNumTabs; i++)
59 028f3e0d Leszek Koltunski
      {
60
      if( i==c ) continue;
61
62 1c90c64a Leszek Koltunski
      addPage(i, mViews[i],country[i],name[i],time[i]);
63 028f3e0d Leszek Koltunski
      }
64
    }
65
66 17f9a695 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 4c0cd600 Leszek Koltunski
  public void error(final String error)
86 17f9a695 Leszek Koltunski
    {
87
    char errorNumber = error.charAt(0);
88
89
    switch(errorNumber)
90
      {
91 4c0cd600 Leszek Koltunski
      case '1': message("Client error");
92
                break;
93 f895e77a Leszek Koltunski
      case '2': RubikScores scores = RubikScores.getInstance();
94
                Bundle bundle = new Bundle();
95
                bundle.putString("name", scores.getName() );
96
97
                RubikDialogSetName nameDiag = new RubikDialogSetName();
98
                nameDiag.setArguments(bundle);
99
                nameDiag.show(mAct.getSupportFragmentManager(), null);
100
101
                mDialog.dismiss();
102
103 17f9a695 Leszek Koltunski
                break;
104
      case '3': message("Server error");
105
                break;
106 4c0cd600 Leszek Koltunski
      default : message("Unexpected error");
107 17f9a695 Leszek Koltunski
      }
108
    }
109
110 028f3e0d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
111
112
  private void prepareView()
113 946d9762 Leszek Koltunski
    {
114
    mAct.runOnUiThread(new Runnable()
115
      {
116
      @Override
117
      public void run()
118
        {
119 4888e97c Leszek Koltunski
        for(int i=0; i<mNumTabs; i++)
120 946d9762 Leszek Koltunski
          {
121
          mViews[i].prepareView(mAct);
122
          }
123
        }
124
      });
125
    try
126
      {
127 7aff6aa7 Leszek Koltunski
      Thread.sleep(150);
128 946d9762 Leszek Koltunski
      }
129
    catch( InterruptedException ie)
130
      {
131
132
      }
133 028f3e0d Leszek Koltunski
    }
134 946d9762 Leszek Koltunski
135 028f3e0d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
136
137 1c90c64a Leszek Koltunski
  private void addPage(int tab, final RubikDialogScoresView view, final String[][] country, final String[][] name, final float[][] time)
138 028f3e0d Leszek Koltunski
    {
139 0333d81e Leszek Koltunski
    for(int i=0; i<MAX_SCRAMBLE; i++)
140 946d9762 Leszek Koltunski
      {
141 1c90c64a Leszek Koltunski
      final LinearLayout section = view.createSection(mAct, tab, i, country[i], name[i], time[i]);
142 d5fb0e7e Leszek Koltunski
143 028f3e0d Leszek Koltunski
      mAct.runOnUiThread(new Runnable()
144 946d9762 Leszek Koltunski
        {
145 028f3e0d Leszek Koltunski
        @Override
146
        public void run()
147 946d9762 Leszek Koltunski
          {
148 9f5e08ac Leszek Koltunski
          view.addSection(section);
149 946d9762 Leszek Koltunski
          }
150 028f3e0d Leszek Koltunski
        });
151
152
      try
153
        {
154 7aff6aa7 Leszek Koltunski
        Thread.sleep(50);
155 028f3e0d Leszek Koltunski
        }
156
      catch( InterruptedException ie)
157
        {
158 946d9762 Leszek Koltunski
159
        }
160
      }
161
    }
162
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
165 f895e77a Leszek Koltunski
  RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager, boolean isSubmitting, RubikDialogScores diag)
166 cc5ec229 Leszek Koltunski
    {
167
    mAct = act;
168 f895e77a Leszek Koltunski
    mDialog = diag;
169 4888e97c Leszek Koltunski
    mNumTabs = RubikObjectList.getTotal();
170
    mViews = new RubikDialogScoresView[mNumTabs];
171 028f3e0d Leszek Koltunski
    mViewPager = viewPager;
172 4895fff6 Leszek Koltunski
    mIsSubmitting = isSubmitting;
173 cc5ec229 Leszek Koltunski
174
    viewPager.setAdapter(this);
175 4888e97c Leszek Koltunski
    viewPager.setOffscreenPageLimit(mNumTabs-1);
176 cc5ec229 Leszek Koltunski
    }
177
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
180
  @Override
181
  @NonNull
182
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
183
    {
184 4895fff6 Leszek Koltunski
    mViews[position] = new RubikDialogScoresView(mAct, mIsSubmitting);
185 946d9762 Leszek Koltunski
    collection.addView(mViews[position]);
186
187
    boolean allCreated = true;
188
189 4888e97c Leszek Koltunski
    for(int i=0; i<mNumTabs; i++)
190 946d9762 Leszek Koltunski
      {
191
      if( mViews[i]==null )
192
        {
193
        allCreated = false;
194
        break;
195
        }
196
      }
197
198
    if( allCreated )
199
      {
200 bc511ea5 Leszek Koltunski
      RubikScoresDownloader downloader = RubikScoresDownloader.getInstance();
201 4895fff6 Leszek Koltunski
202
      if( mIsSubmitting )  downloader.submit  ( this, mAct );
203
      else                 downloader.download( this, mAct );
204 946d9762 Leszek Koltunski
      }
205
206
    return mViews[position];
207 cc5ec229 Leszek Koltunski
    }
208
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210
211
  @Override
212
  public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
213
    {
214
    collection.removeView((View) view);
215
    }
216
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218
219
  @Override
220
  public int getCount()
221
    {
222 4888e97c Leszek Koltunski
    return mNumTabs;
223 cc5ec229 Leszek Koltunski
    }
224
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226
227
  @Override
228
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
229
    {
230
    return view == object;
231
    }
232
  }