Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikScoresPagerAdapter.java @ 4918f19c

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

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
class RubikScoresPagerAdapter extends PagerAdapter implements RubikScoresDownloader.Receiver
32
  {
33
  private FragmentActivity mAct;
34
  private RubikScoresView[] mViews;
35
  private ViewPager mViewPager;
36

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

    
39
  public void receive(final int[][][] country, final String[][][] name, final String[][][] time)
40
    {
41
    prepareView();
42

    
43
    int c = mViewPager.getCurrentItem();
44

    
45
    addPage(c,country[c],name[c],time[c]);
46

    
47
    for(int i=0; i<RubikSize.LENGTH; i++)
48
      {
49
      if( i==c ) continue;
50

    
51
      addPage(i,country[i],name[i],time[i]);
52
      }
53
    }
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  private void prepareView()
58
    {
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
    }
79

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

    
82
  private void addPage(final int page, final int[][] country, final String[][] name, final String[][] time)
83
    {
84
    for(int section=0; section<RubikActivity.MAX_SCRAMBLE; section++)
85
      {
86
      final int sec = section;
87
      final int[] c = country[section];
88
      final String[] n = name[section];
89
      final String[] t = time[section];
90

    
91
      mAct.runOnUiThread(new Runnable()
92
        {
93
        @Override
94
        public void run()
95
          {
96
          mViews[page].addSection(mAct, sec, c, n, t);
97
          }
98
        });
99

    
100
      try
101
        {
102
        Thread.sleep(60);
103
        }
104
      catch( InterruptedException ie)
105
        {
106

    
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
    {
132
    mAct = act;
133
    mViews = new RubikScoresView[RubikSize.LENGTH];
134
    mViewPager = viewPager;
135

    
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
    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
    }
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
  }
(7-7/11)