1 |
f2a9be6d
|
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 |
211b48f2
|
Leszek Koltunski
|
package org.distorted.dialog;
|
21 |
f2a9be6d
|
Leszek Koltunski
|
|
22 |
|
|
import android.content.Context;
|
23 |
7aff6aa7
|
Leszek Koltunski
|
import android.content.res.Resources;
|
24 |
b8b38548
|
Leszek Koltunski
|
import android.support.v4.app.FragmentActivity;
|
25 |
f2a9be6d
|
Leszek Koltunski
|
import android.util.AttributeSet;
|
26 |
|
|
import android.view.View;
|
27 |
|
|
import android.widget.FrameLayout;
|
28 |
cc5ec229
|
Leszek Koltunski
|
import android.widget.ImageView;
|
29 |
b8b38548
|
Leszek Koltunski
|
import android.widget.LinearLayout;
|
30 |
|
|
import android.widget.TextView;
|
31 |
f2a9be6d
|
Leszek Koltunski
|
|
32 |
211b48f2
|
Leszek Koltunski
|
import org.distorted.magic.R;
|
33 |
1c90c64a
|
Leszek Koltunski
|
import org.distorted.object.RubikObjectList;
|
34 |
|
|
import org.distorted.scores.RubikScores;
|
35 |
|
|
|
36 |
f0e87514
|
Leszek Koltunski
|
import static org.distorted.scores.RubikScoresDownloader.MAX_PLACES;
|
37 |
211b48f2
|
Leszek Koltunski
|
|
38 |
f2a9be6d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
39 |
|
|
|
40 |
0fd3ac1f
|
Leszek Koltunski
|
public class RubikDialogScoresView extends FrameLayout
|
41 |
f2a9be6d
|
Leszek Koltunski
|
{
|
42 |
946d9762
|
Leszek Koltunski
|
LinearLayout mLayout;
|
43 |
f2a9be6d
|
Leszek Koltunski
|
|
44 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
45 |
|
|
|
46 |
0fd3ac1f
|
Leszek Koltunski
|
public RubikDialogScoresView(Context context, AttributeSet attrs, int defStyle)
|
47 |
f2a9be6d
|
Leszek Koltunski
|
{
|
48 |
|
|
super(context, attrs, defStyle);
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
52 |
|
|
|
53 |
0fd3ac1f
|
Leszek Koltunski
|
public RubikDialogScoresView(Context context, AttributeSet attrs)
|
54 |
f2a9be6d
|
Leszek Koltunski
|
{
|
55 |
|
|
super(context, attrs);
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
59 |
|
|
|
60 |
4895fff6
|
Leszek Koltunski
|
public RubikDialogScoresView(Context context, boolean isSubmitting)
|
61 |
f2a9be6d
|
Leszek Koltunski
|
{
|
62 |
|
|
super(context);
|
63 |
|
|
|
64 |
0fd3ac1f
|
Leszek Koltunski
|
View view = inflate(context, R.layout.dialog_scores_downloading, null);
|
65 |
b8b38548
|
Leszek Koltunski
|
addView(view);
|
66 |
4895fff6
|
Leszek Koltunski
|
TextView text = findViewById(R.id.message_text);
|
67 |
|
|
text.setText( context.getString(isSubmitting ? R.string.submitting : R.string.downloading) );
|
68 |
f2a9be6d
|
Leszek Koltunski
|
}
|
69 |
|
|
|
70 |
a675474f
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
71 |
|
|
|
72 |
1c90c64a
|
Leszek Koltunski
|
LinearLayout createSection(FragmentActivity act, int tab, int scramble, final String[] country, final String[] name, final float[] time)
|
73 |
a675474f
|
Leszek Koltunski
|
{
|
74 |
7aff6aa7
|
Leszek Koltunski
|
LinearLayout level = (LinearLayout)inflate(act, R.layout.dialog_scores_scramble_title, null);
|
75 |
|
|
TextView text = level.findViewById(R.id.scoresScrambleTitle);
|
76 |
|
|
text.setText(act.getString(R.string.sc_placeholder,(scramble+1)));
|
77 |
|
|
|
78 |
|
|
Resources res = act.getResources();
|
79 |
|
|
String packageName = act.getPackageName();
|
80 |
|
|
|
81 |
1c90c64a
|
Leszek Koltunski
|
int object = RubikObjectList.unpackObject(tab);
|
82 |
|
|
int size = RubikObjectList.unpackSize(tab);
|
83 |
|
|
RubikScores scores = RubikScores.getInstance();
|
84 |
7aff6aa7
|
Leszek Koltunski
|
|
85 |
1c90c64a
|
Leszek Koltunski
|
boolean inserted = false;
|
86 |
286d73ae
|
Leszek Koltunski
|
long myRecordInMillis = scores.getRecord(object, size, scramble);
|
87 |
|
|
float myRecordInSeconds = (myRecordInMillis/100)/10.0f;
|
88 |
|
|
boolean mySubmitted = scores.isSubmitted(object, size, scramble);
|
89 |
1c90c64a
|
Leszek Koltunski
|
String myName = scores.getName();
|
90 |
f895e77a
|
Leszek Koltunski
|
if( myName.length()==0 ) myName = act.getString(R.string.you);
|
91 |
82ce8e64
|
Leszek Koltunski
|
int myCountryID = res.getIdentifier( scores.getCountry(), "drawable", packageName);
|
92 |
286d73ae
|
Leszek Koltunski
|
String myRecord = ( myRecordInMillis<RubikScores.NO_RECORD ) ? Float.toString(myRecordInSeconds) : "??";
|
93 |
1c90c64a
|
Leszek Koltunski
|
String theirTime;
|
94 |
|
|
int theirCountryID;
|
95 |
7aff6aa7
|
Leszek Koltunski
|
|
96 |
1c90c64a
|
Leszek Koltunski
|
int white = res.getColor(R.color.white);
|
97 |
|
|
int red = res.getColor(R.color.red);
|
98 |
2dfe43d3
|
Leszek Koltunski
|
boolean equals;
|
99 |
7aff6aa7
|
Leszek Koltunski
|
|
100 |
1c90c64a
|
Leszek Koltunski
|
for(int j=0; j<MAX_PLACES-1; j++)
|
101 |
|
|
{
|
102 |
|
|
if( name[j] != null )
|
103 |
|
|
{
|
104 |
286d73ae
|
Leszek Koltunski
|
if( !mySubmitted && myRecordInSeconds<time[j] && !inserted )
|
105 |
1c90c64a
|
Leszek Koltunski
|
{
|
106 |
|
|
inserted = true;
|
107 |
|
|
View row = createRow(act, myCountryID, myName, myRecord, red);
|
108 |
|
|
level.addView(row);
|
109 |
|
|
}
|
110 |
|
|
|
111 |
2dfe43d3
|
Leszek Koltunski
|
equals = name[j].equals(myName);
|
112 |
|
|
|
113 |
|
|
if( !inserted || !equals )
|
114 |
|
|
{
|
115 |
|
|
if( equals ) inserted=true;
|
116 |
|
|
theirCountryID = res.getIdentifier( country[j], "drawable", packageName);
|
117 |
|
|
theirTime = Float.toString(time[j]);
|
118 |
|
|
View row = createRow(act, theirCountryID, name[j], theirTime, equals ? red:white);
|
119 |
|
|
level.addView(row);
|
120 |
|
|
}
|
121 |
7aff6aa7
|
Leszek Koltunski
|
}
|
122 |
|
|
}
|
123 |
|
|
|
124 |
1c90c64a
|
Leszek Koltunski
|
if( !inserted )
|
125 |
|
|
{
|
126 |
|
|
View row = createRow(act, myCountryID, myName, myRecord, red);
|
127 |
|
|
level.addView(row);
|
128 |
|
|
}
|
129 |
|
|
|
130 |
7aff6aa7
|
Leszek Koltunski
|
return level;
|
131 |
a675474f
|
Leszek Koltunski
|
}
|
132 |
|
|
|
133 |
1c90c64a
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134 |
|
|
|
135 |
|
|
private View createRow(FragmentActivity act, int countryID, String name, String time, int color)
|
136 |
|
|
{
|
137 |
|
|
View row = inflate(act, R.layout.dialog_scores_scramble_row, null);
|
138 |
|
|
|
139 |
|
|
ImageView imgCoun = row.findViewById(R.id.scoresScrambleRowCountry);
|
140 |
|
|
TextView textName = row.findViewById(R.id.scoresScrambleRowName);
|
141 |
|
|
TextView textTime = row.findViewById(R.id.scoresScrambleRowTime);
|
142 |
|
|
|
143 |
286d73ae
|
Leszek Koltunski
|
imgCoun.setImageResource(countryID!=0 ? countryID : R.drawable.un);
|
144 |
1c90c64a
|
Leszek Koltunski
|
textName.setText(name);
|
145 |
|
|
textTime.setText(time);
|
146 |
|
|
|
147 |
|
|
textName.setTextColor(color);
|
148 |
|
|
textTime.setTextColor(color);
|
149 |
|
|
|
150 |
|
|
return row;
|
151 |
|
|
}
|
152 |
|
|
|
153 |
f2a9be6d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
154 |
7aff6aa7
|
Leszek Koltunski
|
// needs to run on UI thread
|
155 |
f2a9be6d
|
Leszek Koltunski
|
|
156 |
946d9762
|
Leszek Koltunski
|
void prepareView(FragmentActivity act)
|
157 |
b8b38548
|
Leszek Koltunski
|
{
|
158 |
|
|
removeAllViews();
|
159 |
|
|
|
160 |
0fd3ac1f
|
Leszek Koltunski
|
View tab = inflate(act, R.layout.dialog_scores_tab, null);
|
161 |
946d9762
|
Leszek Koltunski
|
mLayout = tab.findViewById(R.id.tabLayout);
|
162 |
b8b38548
|
Leszek Koltunski
|
addView(tab);
|
163 |
946d9762
|
Leszek Koltunski
|
}
|
164 |
b8b38548
|
Leszek Koltunski
|
|
165 |
946d9762
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166 |
7aff6aa7
|
Leszek Koltunski
|
// needs to run on UI thread
|
167 |
b8b38548
|
Leszek Koltunski
|
|
168 |
d5fb0e7e
|
Leszek Koltunski
|
void addSection(LinearLayout section)
|
169 |
|
|
{
|
170 |
|
|
mLayout.addView(section);
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
174 |
7aff6aa7
|
Leszek Koltunski
|
// needs to run on UI thread
|
175 |
d5fb0e7e
|
Leszek Koltunski
|
|
176 |
4895fff6
|
Leszek Koltunski
|
void message(final String mess)
|
177 |
946d9762
|
Leszek Koltunski
|
{
|
178 |
4895fff6
|
Leszek Koltunski
|
TextView text = findViewById(R.id.message_text);
|
179 |
|
|
text.setText(mess);
|
180 |
f2a9be6d
|
Leszek Koltunski
|
}
|
181 |
|
|
}
|