Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikScoresPagerAdapter.java @ beb325a0

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
package org.distorted.magic;
21
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
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30
31 946d9762 Leszek Koltunski
class RubikScoresPagerAdapter extends PagerAdapter implements RubikScoresDownloader.Receiver
32 cc5ec229 Leszek Koltunski
  {
33
  private FragmentActivity mAct;
34 946d9762 Leszek Koltunski
  private RubikScoresView[] mViews;
35 028f3e0d Leszek Koltunski
  private ViewPager mViewPager;
36 cc5ec229 Leszek Koltunski
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
39 946d9762 Leszek Koltunski
  public void receive(final int[][][] country, final String[][][] name, final String[][][] time)
40 028f3e0d Leszek Koltunski
    {
41
    prepareView();
42
43
    int c = mViewPager.getCurrentItem();
44
45 beb325a0 Leszek Koltunski
    addPage(mViews[c],country[c],name[c],time[c]);
46 028f3e0d Leszek Koltunski
47
    for(int i=0; i<RubikSize.LENGTH; i++)
48
      {
49
      if( i==c ) continue;
50
51 beb325a0 Leszek Koltunski
      addPage(mViews[i],country[i],name[i],time[i]);
52 028f3e0d Leszek Koltunski
      }
53
    }
54
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
57
  private void prepareView()
58 946d9762 Leszek Koltunski
    {
59
    mAct.runOnUiThread(new Runnable()
60
      {
61
      @Override
62
      public void run()
63
        {
64
        for(int i=0; i<RubikSize.LENGTH; i++)
65
          {
66
          mViews[i].prepareView(mAct);
67
          }
68
        }
69
      });
70
    try
71
      {
72
      Thread.sleep(200);
73
      }
74
    catch( InterruptedException ie)
75
      {
76
77
      }
78 028f3e0d Leszek Koltunski
    }
79 946d9762 Leszek Koltunski
80 028f3e0d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
81
82 beb325a0 Leszek Koltunski
  private void addPage(final RubikScoresView view, final int[][] country, final String[][] name, final String[][] time)
83 028f3e0d Leszek Koltunski
    {
84
    for(int section=0; section<RubikActivity.MAX_SCRAMBLE; section++)
85 946d9762 Leszek Koltunski
      {
86 028f3e0d Leszek Koltunski
      final int sec = section;
87
      final int[] c = country[section];
88
      final String[] n = name[section];
89
      final String[] t = time[section];
90 946d9762 Leszek Koltunski
91 028f3e0d Leszek Koltunski
      mAct.runOnUiThread(new Runnable()
92 946d9762 Leszek Koltunski
        {
93 028f3e0d Leszek Koltunski
        @Override
94
        public void run()
95 946d9762 Leszek Koltunski
          {
96 beb325a0 Leszek Koltunski
          view.addSection(mAct, sec, c, n, t);
97 946d9762 Leszek Koltunski
          }
98 028f3e0d Leszek Koltunski
        });
99
100
      try
101
        {
102 4918f19c Leszek Koltunski
        Thread.sleep(60);
103 028f3e0d Leszek Koltunski
        }
104
      catch( InterruptedException ie)
105
        {
106 946d9762 Leszek Koltunski
107
        }
108
      }
109
    }
110
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
113
  public void exception(final String exce)
114
    {
115
    mAct.runOnUiThread(new Runnable()
116
      {
117
      @Override
118
      public void run()
119
        {
120
        for(int i=0; i<RubikSize.LENGTH; i++)
121
          {
122
          mViews[i].exception(exce);
123
          }
124
        }
125
      });
126
    }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130
  RubikScoresPagerAdapter(FragmentActivity act, ViewPager viewPager)
131 cc5ec229 Leszek Koltunski
    {
132
    mAct = act;
133 946d9762 Leszek Koltunski
    mViews = new RubikScoresView[RubikSize.LENGTH];
134 028f3e0d Leszek Koltunski
    mViewPager = viewPager;
135 cc5ec229 Leszek Koltunski
136
    viewPager.setAdapter(this);
137
    viewPager.setOffscreenPageLimit( RubikSize.LENGTH-1 );
138
    }
139
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
142
  @Override
143
  @NonNull
144
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
145
    {
146 946d9762 Leszek Koltunski
    mViews[position] = new RubikScoresView(mAct);
147
    collection.addView(mViews[position]);
148
149
    boolean allCreated = true;
150
151
    for(int i=0; i<RubikSize.LENGTH; i++)
152
      {
153
      if( mViews[i]==null )
154
        {
155
        allCreated = false;
156
        break;
157
        }
158
      }
159
160
    if( allCreated )
161
      {
162
      RubikScoresDownloader downloader = new RubikScoresDownloader();
163
      downloader.download(this, mAct.getResources(), mAct.getPackageName());
164
      }
165
166
    return mViews[position];
167 cc5ec229 Leszek Koltunski
    }
168
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
171
  @Override
172
  public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
173
    {
174
    collection.removeView((View) view);
175
    }
176
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
179
  @Override
180
  public int getCount()
181
    {
182
    return RubikSize.LENGTH;
183
    }
184
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
187
  @Override
188
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
189
    {
190
    return view == object;
191
    }
192
  }