Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikScoresViewPager.java @ 36e2cbdd

1 fdec60a3 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
package org.distorted.magic;
21
22
import android.support.annotation.NonNull;
23 f3e12931 Leszek Koltunski
import android.support.v4.app.FragmentActivity;
24 fdec60a3 Leszek Koltunski
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
///////////////////////////////////////////////////////////////////////////////////////////////////
30
31 b8b38548 Leszek Koltunski
class RubikScoresViewPager extends PagerAdapter implements RubikScoresDownloader.Receiver
32 fdec60a3 Leszek Koltunski
  {
33 f3e12931 Leszek Koltunski
  private FragmentActivity mAct;
34 b8b38548 Leszek Koltunski
  private RubikScoresView[] mViews;
35 fdec60a3 Leszek Koltunski
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37
38 f3e12931 Leszek Koltunski
  RubikScoresViewPager(FragmentActivity act, ViewPager viewPager)
39 fdec60a3 Leszek Koltunski
    {
40 f3e12931 Leszek Koltunski
    mAct = act;
41 b8b38548 Leszek Koltunski
    mViews = new RubikScoresView[getCount()];
42 fdec60a3 Leszek Koltunski
    viewPager.setAdapter(this);
43 dd73fdab Leszek Koltunski
    viewPager.setOffscreenPageLimit( RubikSize.LENGTH-1 );
44 fdec60a3 Leszek Koltunski
    }
45
46 f3e12931 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48 d8aa4ba8 Leszek Koltunski
  public void receive(final String[][][][] scores)
49 f3e12931 Leszek Koltunski
    {
50 b8b38548 Leszek Koltunski
    mAct.runOnUiThread(new Runnable()
51
      {
52
      @Override
53
      public void run()
54
        {
55
        for(int i=0; i<RubikSize.LENGTH; i++)
56
          {
57 d8aa4ba8 Leszek Koltunski
          mViews[i].prepareView(mAct,scores[i]);
58 b8b38548 Leszek Koltunski
          }
59
        }
60
      });
61
    }
62
63 fdec60a3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65
  @Override
66
  @NonNull
67
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
68
    {
69 b8b38548 Leszek Koltunski
    mViews[position] = new RubikScoresView(mAct,position);
70
    collection.addView(mViews[position]);
71
72
    boolean allCreated = true;
73
74
    for(int i=0; i<RubikSize.LENGTH; i++)
75
      {
76
      if( mViews[i]==null )
77
        {
78
        allCreated = false;
79
        break;
80
        }
81
      }
82
83
    if( allCreated )
84
      {
85
      RubikScoresDownloader downloader = new RubikScoresDownloader();
86
      downloader.download(this);
87
      }
88
89
    return mViews[position];
90 fdec60a3 Leszek Koltunski
    }
91
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
94
  @Override
95
  public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
96
    {
97
    collection.removeView((View) view);
98
    }
99
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
102
  @Override
103
  public int getCount()
104
    {
105 dd73fdab Leszek Koltunski
    return RubikSize.LENGTH;
106 fdec60a3 Leszek Koltunski
    }
107
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
110
  @Override
111
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
112
    {
113
    return view == object;
114
    }
115
  }