Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresView.java @ 65bc1da3

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