Project

General

Profile

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

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

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);
58
          }
59

    
60
        int begin=-1 ,end, len = scores.length();
61

    
62
        while( begin<len )
63
          {
64
          end = scores.indexOf('\n', begin+1);
65
          if( end<0 ) end = len;
66
          sendRow(scores.substring(begin+1,end));
67
          begin = end;
68
          }
69
        }
70
      });
71
    }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

    
75
  private void sendRow(String row)
76
    {
77
    int s1 = row.indexOf(' ');
78
    int s2 = row.indexOf(' ',s1+1);
79
    int s3 = row.indexOf(' ',s2+1);
80
    int s4 = row.indexOf(' ',s3+1);
81
    int s5 = row.indexOf(' ',s4+1);
82
    int s6 = row.length();
83

    
84
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
85
      {
86
      int size       = Integer.valueOf( row.substring(   0,s1) );
87
      int levl       = Integer.valueOf( row.substring(s1+1,s2) );
88
      String place   = row.substring(s2+1, s3);
89
      String name    = row.substring(s3+1, s4);
90
      int time       = Integer.valueOf( row.substring(s4+1,s5) );
91
      String country = row.substring(s5+1, s6);
92

    
93
      if( size>=0 && size< RubikSize.LENGTH )
94
        {
95
        mViews[size].fillRow(mAct, levl, place, time, name, country);
96
        }
97
      else
98
        {
99
        android.util.Log.e("receive", "row invalid: size invalid: "+row);
100
        }
101
      }
102
    else
103
      {
104
      android.util.Log.e("receive", "row invalid: "+row);
105
      }
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
  @Override
111
  @NonNull
112
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
113
    {
114
    mViews[position] = new RubikScoresView(mAct,position);
115
    collection.addView(mViews[position]);
116

    
117
    boolean allCreated = true;
118

    
119
    for(int i=0; i<RubikSize.LENGTH; i++)
120
      {
121
      if( mViews[i]==null )
122
        {
123
        allCreated = false;
124
        break;
125
        }
126
      }
127

    
128
    if( allCreated )
129
      {
130
      RubikScoresDownloader downloader = new RubikScoresDownloader();
131
      downloader.download(this);
132
      }
133

    
134
    return mViews[position];
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  @Override
140
  public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
141
    {
142
    collection.removeView((View) view);
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  @Override
148
  public int getCount()
149
    {
150
    return RubikSize.LENGTH;
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  @Override
156
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
157
    {
158
    return view == object;
159
    }
160
  }
(8-8/11)