Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialog / RubikDialogScoresPagerAdapter.java @ 4f9f99a2

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

    
29
import org.distorted.network.RubikScoresDownloader;
30
import org.distorted.object.RubikObject;
31
import org.distorted.uistate.RubikStatePlay;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

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

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

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

    
47
    int c = mViewPager.getCurrentItem();
48

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

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

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

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

    
81
      }
82
    }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

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

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

    
104
      try
105
        {
106
        Thread.sleep(60);
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< RubikObject.LENGTH; 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
    mViews = new RubikDialogScoresView[RubikObject.LENGTH];
138
    mViewPager = viewPager;
139

    
140
    viewPager.setAdapter(this);
141
    viewPager.setOffscreenPageLimit( RubikObject.LENGTH-1 );
142
    }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

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

    
153
    boolean allCreated = true;
154

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

    
164
    if( allCreated )
165
      {
166
      RubikScoresDownloader downloader = new RubikScoresDownloader();
167
      downloader.download(this, mAct.getResources(), mAct.getPackageName());
168
      }
169

    
170
    return mViews[position];
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

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

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  @Override
184
  public int getCount()
185
    {
186
    return RubikObject.LENGTH;
187
    }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

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