Revision 946d9762
Added by Leszek Koltunski over 4 years ago
src/main/java/org/distorted/magic/RubikScoresPagerAdapter.java | ||
---|---|---|
22 | 22 |
import android.support.annotation.NonNull; |
23 | 23 |
import android.support.v4.app.FragmentActivity; |
24 | 24 |
import android.support.v4.view.PagerAdapter; |
25 |
import android.support.v4.view.ViewPager; |
|
25 | 26 |
import android.view.View; |
26 | 27 |
import android.view.ViewGroup; |
27 | 28 |
|
28 | 29 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
29 | 30 |
|
30 |
class RubikScoresPagerAdapter extends PagerAdapter |
|
31 |
class RubikScoresPagerAdapter extends PagerAdapter implements RubikScoresDownloader.Receiver
|
|
31 | 32 |
{ |
32 | 33 |
private FragmentActivity mAct; |
33 |
private RubikScoresViewPager mPager;
|
|
34 |
private RubikScoresView[] mViews;
|
|
34 | 35 |
|
35 | 36 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
36 | 37 |
|
37 |
RubikScoresPagerAdapter(FragmentActivity act, RubikScoresViewPager viewPager) |
|
38 |
public void receive(final int[][][] country, final String[][][] name, final String[][][] time) |
|
39 |
{ |
|
40 |
mAct.runOnUiThread(new Runnable() |
|
41 |
{ |
|
42 |
@Override |
|
43 |
public void run() |
|
44 |
{ |
|
45 |
for(int i=0; i<RubikSize.LENGTH; i++) |
|
46 |
{ |
|
47 |
mViews[i].prepareView(mAct); |
|
48 |
} |
|
49 |
} |
|
50 |
}); |
|
51 |
try |
|
52 |
{ |
|
53 |
Thread.sleep(200); |
|
54 |
} |
|
55 |
catch( InterruptedException ie) |
|
56 |
{ |
|
57 |
|
|
58 |
} |
|
59 |
|
|
60 |
for(int i=0; i<RubikSize.LENGTH; i++) |
|
61 |
{ |
|
62 |
final RubikScoresView view = mViews[i]; |
|
63 |
|
|
64 |
for(int j=0; j<RubikActivity.MAX_SCRAMBLE; j++) |
|
65 |
{ |
|
66 |
final int scramble = j; |
|
67 |
final int[] c = country[i][j]; |
|
68 |
final String[] n = name[i][j]; |
|
69 |
final String[] t = time[i][j]; |
|
70 |
|
|
71 |
mAct.runOnUiThread(new Runnable() |
|
72 |
{ |
|
73 |
@Override |
|
74 |
public void run() |
|
75 |
{ |
|
76 |
view.addScramble(mAct, scramble, c, n, t); |
|
77 |
} |
|
78 |
}); |
|
79 |
|
|
80 |
try |
|
81 |
{ |
|
82 |
Thread.sleep(50); |
|
83 |
} |
|
84 |
catch( InterruptedException ie) |
|
85 |
{ |
|
86 |
|
|
87 |
} |
|
88 |
} |
|
89 |
} |
|
90 |
} |
|
91 |
|
|
92 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
93 |
|
|
94 |
public void exception(final String exce) |
|
95 |
{ |
|
96 |
mAct.runOnUiThread(new Runnable() |
|
97 |
{ |
|
98 |
@Override |
|
99 |
public void run() |
|
100 |
{ |
|
101 |
for(int i=0; i<RubikSize.LENGTH; i++) |
|
102 |
{ |
|
103 |
mViews[i].exception(exce); |
|
104 |
} |
|
105 |
} |
|
106 |
}); |
|
107 |
} |
|
108 |
|
|
109 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
110 |
|
|
111 |
RubikScoresPagerAdapter(FragmentActivity act, ViewPager viewPager) |
|
38 | 112 |
{ |
39 | 113 |
mAct = act; |
40 |
mPager = viewPager;
|
|
114 |
mViews = new RubikScoresView[RubikSize.LENGTH];
|
|
41 | 115 |
|
42 | 116 |
viewPager.setAdapter(this); |
43 | 117 |
viewPager.setOffscreenPageLimit( RubikSize.LENGTH-1 ); |
... | ... | |
49 | 123 |
@NonNull |
50 | 124 |
public Object instantiateItem(@NonNull ViewGroup collection, int position) |
51 | 125 |
{ |
52 |
RubikScoresView view = new RubikScoresView(mAct); |
|
53 |
mPager.setView(position,view); |
|
54 |
collection.addView(view); |
|
55 |
|
|
56 |
return view; |
|
126 |
mViews[position] = new RubikScoresView(mAct); |
|
127 |
collection.addView(mViews[position]); |
|
128 |
|
|
129 |
boolean allCreated = true; |
|
130 |
|
|
131 |
for(int i=0; i<RubikSize.LENGTH; i++) |
|
132 |
{ |
|
133 |
if( mViews[i]==null ) |
|
134 |
{ |
|
135 |
allCreated = false; |
|
136 |
break; |
|
137 |
} |
|
138 |
} |
|
139 |
|
|
140 |
if( allCreated ) |
|
141 |
{ |
|
142 |
RubikScoresDownloader downloader = new RubikScoresDownloader(); |
|
143 |
downloader.download(this, mAct.getResources(), mAct.getPackageName()); |
|
144 |
} |
|
145 |
|
|
146 |
return mViews[position]; |
|
57 | 147 |
} |
58 | 148 |
|
59 | 149 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
Also available in: Unified diff
Downloading High Scores: more dynamic way of displaying scores