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