Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialog / RubikDialogScoresPagerAdapter.java @ 4895fff6

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.scores.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
  private boolean mIsSubmitting;
44

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

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

    
51
    int c = mViewPager.getCurrentItem();
52

    
53
    addPage(c, 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(i, 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(int tab, final RubikDialogScoresView view, final String[][] country, final String[][] name, final float[][] time)
91
    {
92
    for(int i=0; i<MAX_SCRAMBLE; i++)
93
      {
94
      final LinearLayout section = view.createSection(mAct, tab, 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 message(final String mess)
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].message(mess);
128
          }
129
        }
130
      });
131
    }
132

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

    
135
  RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager, boolean isSubmitting)
136
    {
137
    mAct = act;
138
    mNumTabs = RubikObjectList.getTotal();
139
    mViews = new RubikDialogScoresView[mNumTabs];
140
    mViewPager = viewPager;
141
    mIsSubmitting = isSubmitting;
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, mIsSubmitting);
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
      RubikScoresDownloader downloader = RubikScoresDownloader.getInstance();
170

    
171
      if( mIsSubmitting )  downloader.submit  ( this, mAct );
172
      else                 downloader.download( this, mAct );
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
  }
(5-5/8)