Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialog / RubikDialogScoresPagerAdapter.java @ 1c90c64a

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
import android.support.annotation.NonNull;
23
import android.support.v4.app.FragmentActivity;
24
import android.support.v4.view.PagerAdapter;
25 946d9762 Leszek Koltunski
import android.support.v4.view.ViewPager;
26 cc5ec229 Leszek Koltunski
import android.view.View;
27
import android.view.ViewGroup;
28 d5fb0e7e Leszek Koltunski
import android.widget.LinearLayout;
29 cc5ec229 Leszek Koltunski
30 714292f1 Leszek Koltunski
import org.distorted.magic.R;
31 c3ffcf58 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 0fd3ac1f Leszek Koltunski
  private RubikDialogScoresView[] mViews;
43 028f3e0d Leszek Koltunski
  private ViewPager mViewPager;
44 4888e97c Leszek Koltunski
  private int mNumTabs;
45 cc5ec229 Leszek Koltunski
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48 1c90c64a Leszek Koltunski
  public void receive(final String[][][] country, final String[][][] name, final float[][][] time)
49 028f3e0d Leszek Koltunski
    {
50
    prepareView();
51
52
    int c = mViewPager.getCurrentItem();
53
54 1c90c64a Leszek Koltunski
    addPage(c, mViews[c],country[c],name[c],time[c]);
55 028f3e0d Leszek Koltunski
56 4888e97c Leszek Koltunski
    for(int i=0; i<mNumTabs; i++)
57 028f3e0d Leszek Koltunski
      {
58
      if( i==c ) continue;
59
60 1c90c64a Leszek Koltunski
      addPage(i, mViews[i],country[i],name[i],time[i]);
61 028f3e0d Leszek Koltunski
      }
62
    }
63
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66
  private void prepareView()
67 946d9762 Leszek Koltunski
    {
68
    mAct.runOnUiThread(new Runnable()
69
      {
70
      @Override
71
      public void run()
72
        {
73 4888e97c Leszek Koltunski
        for(int i=0; i<mNumTabs; i++)
74 946d9762 Leszek Koltunski
          {
75
          mViews[i].prepareView(mAct);
76
          }
77
        }
78
      });
79
    try
80
      {
81 7aff6aa7 Leszek Koltunski
      Thread.sleep(150);
82 946d9762 Leszek Koltunski
      }
83
    catch( InterruptedException ie)
84
      {
85
86
      }
87 028f3e0d Leszek Koltunski
    }
88 946d9762 Leszek Koltunski
89 028f3e0d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
90
91 1c90c64a Leszek Koltunski
  private void addPage(int tab, final RubikDialogScoresView view, final String[][] country, final String[][] name, final float[][] time)
92 028f3e0d Leszek Koltunski
    {
93 0333d81e Leszek Koltunski
    for(int i=0; i<MAX_SCRAMBLE; i++)
94 946d9762 Leszek Koltunski
      {
95 1c90c64a Leszek Koltunski
      final LinearLayout section = view.createSection(mAct, tab, i, country[i], name[i], time[i]);
96 d5fb0e7e Leszek Koltunski
97 028f3e0d Leszek Koltunski
      mAct.runOnUiThread(new Runnable()
98 946d9762 Leszek Koltunski
        {
99 028f3e0d Leszek Koltunski
        @Override
100
        public void run()
101 946d9762 Leszek Koltunski
          {
102 9f5e08ac Leszek Koltunski
          view.addSection(section);
103 946d9762 Leszek Koltunski
          }
104 028f3e0d Leszek Koltunski
        });
105
106
      try
107
        {
108 7aff6aa7 Leszek Koltunski
        Thread.sleep(50);
109 028f3e0d Leszek Koltunski
        }
110
      catch( InterruptedException ie)
111
        {
112 946d9762 Leszek Koltunski
113
        }
114
      }
115
    }
116
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
119
  public void exception(final String exce)
120
    {
121
    mAct.runOnUiThread(new Runnable()
122
      {
123
      @Override
124
      public void run()
125
        {
126 4888e97c Leszek Koltunski
        for(int i=0; i<mNumTabs; i++)
127 946d9762 Leszek Koltunski
          {
128
          mViews[i].exception(exce);
129
          }
130
        }
131
      });
132
    }
133
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
136 0fd3ac1f Leszek Koltunski
  RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager)
137 cc5ec229 Leszek Koltunski
    {
138
    mAct = act;
139 4888e97c Leszek Koltunski
    mNumTabs = RubikObjectList.getTotal();
140
    mViews = new RubikDialogScoresView[mNumTabs];
141 028f3e0d Leszek Koltunski
    mViewPager = viewPager;
142 cc5ec229 Leszek Koltunski
143
    viewPager.setAdapter(this);
144 4888e97c Leszek Koltunski
    viewPager.setOffscreenPageLimit(mNumTabs-1);
145 cc5ec229 Leszek Koltunski
    }
146
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
149
  @Override
150
  @NonNull
151
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
152
    {
153 0fd3ac1f Leszek Koltunski
    mViews[position] = new RubikDialogScoresView(mAct);
154 946d9762 Leszek Koltunski
    collection.addView(mViews[position]);
155
156
    boolean allCreated = true;
157
158 4888e97c Leszek Koltunski
    for(int i=0; i<mNumTabs; i++)
159 946d9762 Leszek Koltunski
      {
160
      if( mViews[i]==null )
161
        {
162
        allCreated = false;
163
        break;
164
        }
165
      }
166
167
    if( allCreated )
168
      {
169 c3ffcf58 Leszek Koltunski
      RubikScores scores = RubikScores.getInstance();
170 946d9762 Leszek Koltunski
      RubikScoresDownloader downloader = new RubikScoresDownloader();
171 c3ffcf58 Leszek Koltunski
      downloader.download( this, scores.getName(), mAct.getString(R.string.app_version), scores.getNumRuns() );
172 946d9762 Leszek Koltunski
      }
173
174
    return mViews[position];
175 cc5ec229 Leszek Koltunski
    }
176
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
179
  @Override
180
  public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
181
    {
182
    collection.removeView((View) view);
183
    }
184
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
187
  @Override
188
  public int getCount()
189
    {
190 4888e97c Leszek Koltunski
    return mNumTabs;
191 cc5ec229 Leszek Koltunski
    }
192
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194
195
  @Override
196
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
197
    {
198
    return view == object;
199
    }
200
  }