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