Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialog / RubikDialogScoresPagerAdapter.java @ d5fb0e7e

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.RubikObject;
32
import org.distorted.uistate.RubikStatePlay;
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 int[][][] 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< RubikObject.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< RubikObject.LENGTH; i++)
70
          {
71
          mViews[i].prepareView(mAct);
72
          }
73
        }
74
      });
75
    try
76
      {
77
      Thread.sleep(200);
78
      }
79
    catch( InterruptedException ie)
80
      {
81

    
82
      }
83
    }
84

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

    
87
  private void addPage(final RubikDialogScoresView view, final int[][] country, final String[][] name, final String[][] time)
88
    {
89
    for(int section = 0; section< RubikStatePlay.MAX_SCRAMBLE; section++)
90
      {
91
      final int sec = section;
92
      final int[] c = country[section];
93
      final String[] n = name[section];
94
      final String[] t = time[section];
95

    
96
      final LinearLayout layout = view.createSection(mAct, sec, c, n, t);
97

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

    
107
      try
108
        {
109
        Thread.sleep(60);
110
        }
111
      catch( InterruptedException ie)
112
        {
113

    
114
        }
115
      }
116
    }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

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

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
  RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager)
138
    {
139
    mAct = act;
140
    mViews = new RubikDialogScoresView[RubikObject.LENGTH];
141
    mViewPager = viewPager;
142

    
143
    viewPager.setAdapter(this);
144
    viewPager.setOffscreenPageLimit( RubikObject.LENGTH-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);
154
    collection.addView(mViews[position]);
155

    
156
    boolean allCreated = true;
157

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

    
167
    if( allCreated )
168
      {
169
      RubikScoresDownloader downloader = new RubikScoresDownloader();
170
      downloader.download(this, mAct.getResources(), mAct.getPackageName());
171
      }
172

    
173
    return mViews[position];
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

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

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  @Override
187
  public int getCount()
188
    {
189
    return RubikObject.LENGTH;
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

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