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