Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikScoresViewPager.java @ a675474f

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.magic;
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
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
class RubikScoresViewPager extends PagerAdapter implements RubikScoresDownloader.Receiver
32
  {
33
  private FragmentActivity mAct;
34
  private RubikScoresView[] mViews;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
  RubikScoresViewPager(FragmentActivity act, ViewPager viewPager)
39
    {
40
    mAct = act;
41
    mViews = new RubikScoresView[getCount()];
42
    viewPager.setAdapter(this);
43
    viewPager.setOffscreenPageLimit( RubikSize.LENGTH-1 );
44
    }
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
  public void receive(final String[][][][] scores)
49
    {
50
    mAct.runOnUiThread(new Runnable()
51
      {
52
      @Override
53
      public void run()
54
        {
55
        for(int i=0; i<RubikSize.LENGTH; i++)
56
          {
57
          mViews[i].prepareView(mAct,scores[i]);
58
          }
59
        }
60
      });
61
    }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  public void exception(final String exce)
66
    {
67
    mAct.runOnUiThread(new Runnable()
68
      {
69
      @Override
70
      public void run()
71
        {
72
        for(int i=0; i<RubikSize.LENGTH; i++)
73
          {
74
          mViews[i].exception(exce);
75
          }
76
        }
77
      });
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  @Override
83
  @NonNull
84
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
85
    {
86
    mViews[position] = new RubikScoresView(mAct,position);
87
    collection.addView(mViews[position]);
88

    
89
    boolean allCreated = true;
90

    
91
    for(int i=0; i<RubikSize.LENGTH; i++)
92
      {
93
      if( mViews[i]==null )
94
        {
95
        allCreated = false;
96
        break;
97
        }
98
      }
99

    
100
    if( allCreated )
101
      {
102
      RubikScoresDownloader downloader = new RubikScoresDownloader();
103
      downloader.download(this);
104
      }
105

    
106
    return mViews[position];
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  @Override
112
  public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
113
    {
114
    collection.removeView((View) view);
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  @Override
120
  public int getCount()
121
    {
122
    return RubikSize.LENGTH;
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  @Override
128
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
129
    {
130
    return view == object;
131
    }
132
  }
(8-8/11)