Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialog / RubikDialogScoresPagerAdapter.java @ 4888e97c

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

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

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

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

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

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

    
50
    int c = mViewPager.getCurrentItem();
51

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

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

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

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

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

    
84
      }
85
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

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

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

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

    
111
        }
112
      }
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

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

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

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

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

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

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

    
154
    boolean allCreated = true;
155

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

    
165
    if( allCreated )
166
      {
167
      RubikScoresDownloader downloader = new RubikScoresDownloader();
168
      downloader.download(this, "distorted", "1.0.0", 1);                        // TODO
169
      }
170

    
171
    return mViews[position];
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  @Override
177
  public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
178
    {
179
    collection.removeView((View) view);
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
  @Override
185
  public int getCount()
186
    {
187
    return mNumTabs;
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

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