Project

General

Profile

« Previous | Next » 

Revision b8b38548

Added by Leszek Koltunski over 4 years ago

Downloading High Scores

View differences:

src/main/java/org/distorted/magic/RubikScoresViewPager.java
28 28

  
29 29
///////////////////////////////////////////////////////////////////////////////////////////////////
30 30

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

  
35 36
///////////////////////////////////////////////////////////////////////////////////////////////////
36 37

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

  
44 46
///////////////////////////////////////////////////////////////////////////////////////////////////
45
// TODO
46 47

  
47
  public void receive(String scores)
48
  public void receive(final String scores)
48 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
///////////////////////////////////////////////////////////////////////////////////////////////////
49 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
      }
50 106
    }
51 107

  
52 108
///////////////////////////////////////////////////////////////////////////////////////////////////
......
55 111
  @NonNull
56 112
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
57 113
    {
58
    RubikScoresView view = new RubikScoresView(mAct,position);
59
    collection.addView(view);
60

  
61
    return view;
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];
62 135
    }
63 136

  
64 137
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff