Project

General

Profile

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

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

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.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
import android.widget.LinearLayout;
29

    
30
import org.distorted.magic.R;
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 RubikDialogScoresView[] mViews;
43
  private ViewPager mViewPager;
44
  private int mNumTabs;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

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

    
52
    int c = mViewPager.getCurrentItem();
53

    
54
    addPage(c, mViews[c],country[c],name[c],time[c]);
55

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

    
60
      addPage(i, mViews[i],country[i],name[i],time[i]);
61
      }
62
    }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  private void prepareView()
67
    {
68
    mAct.runOnUiThread(new Runnable()
69
      {
70
      @Override
71
      public void run()
72
        {
73
        for(int i=0; i<mNumTabs; i++)
74
          {
75
          mViews[i].prepareView(mAct);
76
          }
77
        }
78
      });
79
    try
80
      {
81
      Thread.sleep(150);
82
      }
83
    catch( InterruptedException ie)
84
      {
85

    
86
      }
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
  private void addPage(int tab, final RubikDialogScoresView view, final String[][] country, final String[][] name, final float[][] time)
92
    {
93
    for(int i=0; i<MAX_SCRAMBLE; i++)
94
      {
95
      final LinearLayout section = view.createSection(mAct, tab, i, country[i], name[i], time[i]);
96

    
97
      mAct.runOnUiThread(new Runnable()
98
        {
99
        @Override
100
        public void run()
101
          {
102
          view.addSection(section);
103
          }
104
        });
105

    
106
      try
107
        {
108
        Thread.sleep(50);
109
        }
110
      catch( InterruptedException ie)
111
        {
112

    
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
        for(int i=0; i<mNumTabs; i++)
127
          {
128
          mViews[i].exception(exce);
129
          }
130
        }
131
      });
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager)
137
    {
138
    mAct = act;
139
    mNumTabs = RubikObjectList.getTotal();
140
    mViews = new RubikDialogScoresView[mNumTabs];
141
    mViewPager = viewPager;
142

    
143
    viewPager.setAdapter(this);
144
    viewPager.setOffscreenPageLimit(mNumTabs-1);
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  @Override
150
  @NonNull
151
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
152
    {
153
    mViews[position] = new RubikDialogScoresView(mAct);
154
    collection.addView(mViews[position]);
155

    
156
    boolean allCreated = true;
157

    
158
    for(int i=0; i<mNumTabs; i++)
159
      {
160
      if( mViews[i]==null )
161
        {
162
        allCreated = false;
163
        break;
164
        }
165
      }
166

    
167
    if( allCreated )
168
      {
169
      RubikScores scores = RubikScores.getInstance();
170
      RubikScoresDownloader downloader = new RubikScoresDownloader();
171
      downloader.download( this, scores.getName(), mAct.getString(R.string.app_version), scores.getNumRuns() );
172
      }
173

    
174
    return mViews[position];
175
    }
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
    return mNumTabs;
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  @Override
196
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
197
    {
198
    return view == object;
199
    }
200
  }
(4-4/6)