Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresView.java @ ff07f079

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