Project

General

Profile

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

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

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 static org.distorted.object.RubikObject.LENGTH;
32
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

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

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

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

    
48
    int c = mViewPager.getCurrentItem();
49

    
50
    addPage(mViews[c],country[c],name[c],time[c]);
51

    
52
    for(int i=0; i<LENGTH; i++)
53
      {
54
      if( i==c ) continue;
55

    
56
      addPage(mViews[i],country[i],name[i],time[i]);
57
      }
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

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

    
82
      }
83
    }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

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

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

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

    
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
        for(int i=0; i<LENGTH; i++)
123
          {
124
          mViews[i].exception(exce);
125
          }
126
        }
127
      });
128
    }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
  RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager)
133
    {
134
    mAct = act;
135
    mViews = new RubikDialogScoresView[LENGTH];
136
    mViewPager = viewPager;
137

    
138
    viewPager.setAdapter(this);
139
    viewPager.setOffscreenPageLimit(LENGTH-1);
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

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

    
151
    boolean allCreated = true;
152

    
153
    for(int i=0; i<LENGTH; i++)
154
      {
155
      if( mViews[i]==null )
156
        {
157
        allCreated = false;
158
        break;
159
        }
160
      }
161

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

    
168
    return mViews[position];
169
    }
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
    return LENGTH;
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

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