Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialog / RubikDialogScoresPagerAdapter.java @ 329c0aeb

1 cc5ec229 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 211b48f2 Leszek Koltunski
package org.distorted.dialog;
21 cc5ec229 Leszek Koltunski
22
import android.support.annotation.NonNull;
23
import android.support.v4.app.FragmentActivity;
24
import android.support.v4.view.PagerAdapter;
25 946d9762 Leszek Koltunski
import android.support.v4.view.ViewPager;
26 cc5ec229 Leszek Koltunski
import android.view.View;
27
import android.view.ViewGroup;
28 d5fb0e7e Leszek Koltunski
import android.widget.LinearLayout;
29 cc5ec229 Leszek Koltunski
30 4f9f99a2 Leszek Koltunski
import org.distorted.network.RubikScoresDownloader;
31 0333d81e Leszek Koltunski
import static org.distorted.object.RubikObject.LENGTH;
32
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
33 211b48f2 Leszek Koltunski
34 cc5ec229 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
35
36 0fd3ac1f Leszek Koltunski
class RubikDialogScoresPagerAdapter extends PagerAdapter implements RubikScoresDownloader.Receiver
37 cc5ec229 Leszek Koltunski
  {
38
  private FragmentActivity mAct;
39 0fd3ac1f Leszek Koltunski
  private RubikDialogScoresView[] mViews;
40 028f3e0d Leszek Koltunski
  private ViewPager mViewPager;
41 cc5ec229 Leszek Koltunski
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44 7aff6aa7 Leszek Koltunski
  public void receive(final String[][][] country, final String[][][] name, final String[][][] time)
45 028f3e0d Leszek Koltunski
    {
46
    prepareView();
47
48
    int c = mViewPager.getCurrentItem();
49
50 beb325a0 Leszek Koltunski
    addPage(mViews[c],country[c],name[c],time[c]);
51 028f3e0d Leszek Koltunski
52 0333d81e Leszek Koltunski
    for(int i=0; i<LENGTH; i++)
53 028f3e0d Leszek Koltunski
      {
54
      if( i==c ) continue;
55
56 beb325a0 Leszek Koltunski
      addPage(mViews[i],country[i],name[i],time[i]);
57 028f3e0d Leszek Koltunski
      }
58
    }
59
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62
  private void prepareView()
63 946d9762 Leszek Koltunski
    {
64
    mAct.runOnUiThread(new Runnable()
65
      {
66
      @Override
67
      public void run()
68
        {
69 0333d81e Leszek Koltunski
        for(int i=0; i<LENGTH; i++)
70 946d9762 Leszek Koltunski
          {
71
          mViews[i].prepareView(mAct);
72
          }
73
        }
74
      });
75
    try
76
      {
77 7aff6aa7 Leszek Koltunski
      Thread.sleep(150);
78 946d9762 Leszek Koltunski
      }
79
    catch( InterruptedException ie)
80
      {
81
82
      }
83 028f3e0d Leszek Koltunski
    }
84 946d9762 Leszek Koltunski
85 028f3e0d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87 7aff6aa7 Leszek Koltunski
  private void addPage(final RubikDialogScoresView view, final String[][] country, final String[][] name, final String[][] time)
88 028f3e0d Leszek Koltunski
    {
89 0333d81e Leszek Koltunski
    for(int i=0; i<MAX_SCRAMBLE; i++)
90 946d9762 Leszek Koltunski
      {
91 9f5e08ac Leszek Koltunski
      final LinearLayout section = view.createSection(mAct, i, country[i], name[i], time[i]);
92 d5fb0e7e Leszek Koltunski
93 028f3e0d Leszek Koltunski
      mAct.runOnUiThread(new Runnable()
94 946d9762 Leszek Koltunski
        {
95 028f3e0d Leszek Koltunski
        @Override
96
        public void run()
97 946d9762 Leszek Koltunski
          {
98 9f5e08ac Leszek Koltunski
          view.addSection(section);
99 946d9762 Leszek Koltunski
          }
100 028f3e0d Leszek Koltunski
        });
101
102
      try
103
        {
104 7aff6aa7 Leszek Koltunski
        Thread.sleep(50);
105 028f3e0d Leszek Koltunski
        }
106
      catch( InterruptedException ie)
107
        {
108 946d9762 Leszek Koltunski
109
        }
110
      }
111
    }
112
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
115
  public void exception(final String exce)
116
    {
117
    mAct.runOnUiThread(new Runnable()
118
      {
119
      @Override
120
      public void run()
121
        {
122 0333d81e Leszek Koltunski
        for(int i=0; i<LENGTH; i++)
123 946d9762 Leszek Koltunski
          {
124
          mViews[i].exception(exce);
125
          }
126
        }
127
      });
128
    }
129
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
132 0fd3ac1f Leszek Koltunski
  RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager)
133 cc5ec229 Leszek Koltunski
    {
134
    mAct = act;
135 0333d81e Leszek Koltunski
    mViews = new RubikDialogScoresView[LENGTH];
136 028f3e0d Leszek Koltunski
    mViewPager = viewPager;
137 cc5ec229 Leszek Koltunski
138
    viewPager.setAdapter(this);
139 0333d81e Leszek Koltunski
    viewPager.setOffscreenPageLimit(LENGTH-1);
140 cc5ec229 Leszek Koltunski
    }
141
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
144
  @Override
145
  @NonNull
146
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
147
    {
148 0fd3ac1f Leszek Koltunski
    mViews[position] = new RubikDialogScoresView(mAct);
149 946d9762 Leszek Koltunski
    collection.addView(mViews[position]);
150
151
    boolean allCreated = true;
152
153 0333d81e Leszek Koltunski
    for(int i=0; i<LENGTH; i++)
154 946d9762 Leszek Koltunski
      {
155
      if( mViews[i]==null )
156
        {
157
        allCreated = false;
158
        break;
159
        }
160
      }
161
162
    if( allCreated )
163
      {
164
      RubikScoresDownloader downloader = new RubikScoresDownloader();
165 329c0aeb Leszek Koltunski
      downloader.download(this, "distorted", "1.0.0", 1);                        // TODO
166 946d9762 Leszek Koltunski
      }
167
168
    return mViews[position];
169 cc5ec229 Leszek Koltunski
    }
170
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
173
  @Override
174
  public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
175
    {
176
    collection.removeView((View) view);
177
    }
178
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
181
  @Override
182
  public int getCount()
183
    {
184 0333d81e Leszek Koltunski
    return LENGTH;
185 cc5ec229 Leszek Koltunski
    }
186
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
189
  @Override
190
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
191
    {
192
    return view == object;
193
    }
194
  }