1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2020 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.dialogs;
|
11
|
|
12
|
import android.content.Context;
|
13
|
import android.content.res.Resources;
|
14
|
import androidx.fragment.app.FragmentActivity;
|
15
|
import android.util.AttributeSet;
|
16
|
import android.util.TypedValue;
|
17
|
import android.view.View;
|
18
|
import android.widget.FrameLayout;
|
19
|
import android.widget.ImageView;
|
20
|
import android.widget.LinearLayout;
|
21
|
import android.widget.TextView;
|
22
|
|
23
|
import org.distorted.main.R;
|
24
|
import org.distorted.main.RubikActivity;
|
25
|
import org.distorted.external.RubikScores;
|
26
|
|
27
|
import static org.distorted.external.RubikNetwork.MAX_PLACES;
|
28
|
|
29
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
30
|
|
31
|
public class RubikDialogScoresView extends FrameLayout
|
32
|
{
|
33
|
private LinearLayout mLayout;
|
34
|
private int mHeight;
|
35
|
|
36
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
37
|
|
38
|
public RubikDialogScoresView(Context context, AttributeSet attrs, int defStyle)
|
39
|
{
|
40
|
super(context, attrs, defStyle);
|
41
|
}
|
42
|
|
43
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44
|
|
45
|
public RubikDialogScoresView(Context context, AttributeSet attrs)
|
46
|
{
|
47
|
super(context, attrs);
|
48
|
}
|
49
|
|
50
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
51
|
|
52
|
public RubikDialogScoresView(Context context, int height, boolean isSubmitting)
|
53
|
{
|
54
|
super(context);
|
55
|
|
56
|
mHeight = height;
|
57
|
|
58
|
View view = inflate(context, R.layout.dialog_scores_downloading, null);
|
59
|
addView(view);
|
60
|
TextView text = findViewById(R.id.message_text);
|
61
|
text.setText( context.getString(isSubmitting ? R.string.submitting : R.string.downloading) );
|
62
|
}
|
63
|
|
64
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
65
|
|
66
|
LinearLayout createSection(FragmentActivity act, int tab, String title, int level, final String[] country, final String[] name, final int[] time)
|
67
|
{
|
68
|
LinearLayout levelLayout = (LinearLayout)inflate(act, R.layout.dialog_scores_scramble_title, null);
|
69
|
TextView text = levelLayout.findViewById(R.id.scoresScrambleTitle);
|
70
|
text.setText(title);
|
71
|
|
72
|
int size = (int)(mHeight*RubikActivity.SCORES_LEVEL_TEXT);
|
73
|
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
|
74
|
|
75
|
Resources res = act.getResources();
|
76
|
String packageName = act.getPackageName();
|
77
|
RubikScores scores = RubikScores.getInstance();
|
78
|
|
79
|
boolean inserted = false;
|
80
|
int myRecordInMillis = scores.getRecord(tab, level);
|
81
|
String myRecord = ( myRecordInMillis<RubikScores.NO_RECORD ) ? formatRecord(myRecordInMillis) : "??";
|
82
|
String myName = scores.getName();
|
83
|
if( myName.length()==0 ) myName = act.getString(R.string.you);
|
84
|
int myCountryID = res.getIdentifier( scores.getCountry(), "drawable", packageName);
|
85
|
String theirTime;
|
86
|
int theirCountryID;
|
87
|
|
88
|
int height = (int)(mHeight* RubikActivity.SCORES_ITEM_TEXT);
|
89
|
int white = res.getColor(R.color.white);
|
90
|
int red = res.getColor(R.color.red);
|
91
|
boolean equals;
|
92
|
|
93
|
for(int j=0; j<MAX_PLACES-1; j++)
|
94
|
{
|
95
|
if( name[j] != null )
|
96
|
{
|
97
|
if( myRecordInMillis<time[j] && !inserted )
|
98
|
{
|
99
|
inserted = true;
|
100
|
View row = createRow(act, myCountryID, myName, myRecord, height, red);
|
101
|
levelLayout.addView(row);
|
102
|
}
|
103
|
|
104
|
equals = name[j].equals(myName);
|
105
|
|
106
|
if( !inserted || !equals )
|
107
|
{
|
108
|
if( equals ) inserted=true;
|
109
|
theirCountryID = res.getIdentifier( country[j], "drawable", packageName);
|
110
|
theirTime = formatRecord(time[j]);
|
111
|
View row = createRow(act, theirCountryID, name[j], theirTime, height, equals ? red:white);
|
112
|
levelLayout.addView(row);
|
113
|
}
|
114
|
}
|
115
|
}
|
116
|
|
117
|
if( !inserted )
|
118
|
{
|
119
|
View row = createRow(act, myCountryID, myName, myRecord, height, red);
|
120
|
levelLayout.addView(row);
|
121
|
}
|
122
|
|
123
|
return levelLayout;
|
124
|
}
|
125
|
|
126
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
127
|
|
128
|
private String formatRecord(int time)
|
129
|
{
|
130
|
time /= 10;
|
131
|
int millis = time%100;
|
132
|
time /= 100;
|
133
|
int seconds = time%60;
|
134
|
int minutes = time/60;
|
135
|
|
136
|
String ret = minutes + (seconds<10 ? ":0" : ":") + seconds + (millis<10 ? ".0" : ".") + millis;
|
137
|
return minutes<10 ? "0"+ret : ret;
|
138
|
}
|
139
|
|
140
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
141
|
|
142
|
private View createRow(FragmentActivity act, int countryID, String name, String time, int height, int color)
|
143
|
{
|
144
|
View row = inflate(act, R.layout.dialog_scores_scramble_row, null);
|
145
|
|
146
|
ImageView imgCoun = row.findViewById(R.id.scoresScrambleRowCountry);
|
147
|
TextView textName = row.findViewById(R.id.scoresScrambleRowName);
|
148
|
TextView textTime = row.findViewById(R.id.scoresScrambleRowTime);
|
149
|
|
150
|
imgCoun.setImageResource(countryID!=0 ? countryID : org.distorted.flags.R.drawable.unknown);
|
151
|
textName.setText(name);
|
152
|
textTime.setText(time);
|
153
|
|
154
|
textName.setTextColor(color);
|
155
|
textTime.setTextColor(color);
|
156
|
|
157
|
textName.setTextSize(TypedValue.COMPLEX_UNIT_PX, height);
|
158
|
textTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, height);
|
159
|
|
160
|
return row;
|
161
|
}
|
162
|
|
163
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
164
|
// needs to run on UI thread
|
165
|
|
166
|
void prepareView(FragmentActivity act)
|
167
|
{
|
168
|
removeAllViews();
|
169
|
|
170
|
View tab = inflate(act, R.layout.dialog_scores_tab, null);
|
171
|
mLayout = tab.findViewById(R.id.tabLayout);
|
172
|
addView(tab);
|
173
|
}
|
174
|
|
175
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
176
|
// needs to run on UI thread
|
177
|
|
178
|
void addSection(LinearLayout section)
|
179
|
{
|
180
|
mLayout.addView(section);
|
181
|
}
|
182
|
|
183
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
184
|
// needs to run on UI thread
|
185
|
|
186
|
void message(final String mess)
|
187
|
{
|
188
|
TextView text = findViewById(R.id.message_text);
|
189
|
if( text!=null ) text.setText(mess);
|
190
|
}
|
191
|
}
|