Project

General

Profile

Download (6.7 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresView.java @ 4bd09fe2

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