Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialog / RubikDialogScoresPagerAdapter.java @ 714292f1

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.RubikScoresDownloader;
32
import org.distorted.object.RubikObjectList;
33

    
34
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
class RubikDialogScoresPagerAdapter extends PagerAdapter implements RubikScoresDownloader.Receiver
39
  {
40
  private FragmentActivity mAct;
41
  private RubikDialogScoresView[] mViews;
42
  private ViewPager mViewPager;
43
  private int mNumTabs;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

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

    
51
    int c = mViewPager.getCurrentItem();
52

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

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

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

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

    
85
      }
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

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

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

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

    
112
        }
113
      }
114
    }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
  public void exception(final String exce)
119
    {
120
    mAct.runOnUiThread(new Runnable()
121
      {
122
      @Override
123
      public void run()
124
        {
125
        for(int i=0; i<mNumTabs; i++)
126
          {
127
          mViews[i].exception(exce);
128
          }
129
        }
130
      });
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

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

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

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

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

    
155
    boolean allCreated = true;
156

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

    
166
    if( allCreated )
167
      {
168
      RubikScoresDownloader downloader = new RubikScoresDownloader();
169
      downloader.download( this,
170
                           "distorted",  // TODO
171
                           mAct.getString(R.string.app_version),
172
                           1 );  // TODO
173
      }
174

    
175
    return mViews[position];
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  @Override
181
  public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
182
    {
183
    collection.removeView((View) view);
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  @Override
189
  public int getCount()
190
    {
191
    return mNumTabs;
192
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

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