Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresView.java @ 05c044a5

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 79e752b3 Leszek Koltunski
import android.annotation.SuppressLint;
13 f2a9be6d Leszek Koltunski
import android.content.Context;
14 7aff6aa7 Leszek Koltunski
import android.content.res.Resources;
15 66e777b0 Leszek Koltunski
import androidx.fragment.app.FragmentActivity;
16 f2a9be6d Leszek Koltunski
import android.util.AttributeSet;
17 362807f0 Leszek Koltunski
import android.util.TypedValue;
18 f2a9be6d Leszek Koltunski
import android.view.View;
19
import android.widget.FrameLayout;
20 cc5ec229 Leszek Koltunski
import android.widget.ImageView;
21 b8b38548 Leszek Koltunski
import android.widget.LinearLayout;
22
import android.widget.TextView;
23 f2a9be6d Leszek Koltunski
24 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
25 362807f0 Leszek Koltunski
import org.distorted.main.RubikActivity;
26 acabdd83 Leszek Koltunski
import org.distorted.external.RubikScores;
27 1c90c64a Leszek Koltunski
28 acabdd83 Leszek Koltunski
import static org.distorted.external.RubikNetwork.MAX_PLACES;
29 211b48f2 Leszek Koltunski
30 f2a9be6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
31
32 0fd3ac1f Leszek Koltunski
public class RubikDialogScoresView extends FrameLayout
33 f2a9be6d Leszek Koltunski
  {
34 362807f0 Leszek Koltunski
  private LinearLayout mLayout;
35 5b4eaf7e Leszek Koltunski
  private int mHeight;
36 f2a9be6d Leszek Koltunski
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
39 0fd3ac1f Leszek Koltunski
  public RubikDialogScoresView(Context context, AttributeSet attrs, int defStyle)
40 f2a9be6d Leszek Koltunski
    {
41
    super(context, attrs, defStyle);
42
    }
43
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46 0fd3ac1f Leszek Koltunski
  public RubikDialogScoresView(Context context, AttributeSet attrs)
47 f2a9be6d Leszek Koltunski
    {
48
    super(context, attrs);
49
    }
50
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53 5b4eaf7e Leszek Koltunski
  public RubikDialogScoresView(Context context, int height, boolean isSubmitting)
54 f2a9be6d Leszek Koltunski
    {
55
    super(context);
56
57 5b4eaf7e Leszek Koltunski
    mHeight = height;
58 362807f0 Leszek Koltunski
59 0fd3ac1f Leszek Koltunski
    View view = inflate(context, R.layout.dialog_scores_downloading, null);
60 b8b38548 Leszek Koltunski
    addView(view);
61 4895fff6 Leszek Koltunski
    TextView text = findViewById(R.id.message_text);
62
    text.setText( context.getString(isSubmitting ? R.string.submitting : R.string.downloading) );
63 f2a9be6d Leszek Koltunski
    }
64
65 a675474f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
66
67 79e752b3 Leszek Koltunski
  @SuppressLint("DefaultLocale")
68 00af5060 Leszek Koltunski
  LinearLayout createSection(FragmentActivity act, int tab, String title, int level, final String[] country, final String[] name, final float[] time)
69 a675474f Leszek Koltunski
    {
70 85b09df4 Leszek Koltunski
    LinearLayout levelLayout = (LinearLayout)inflate(act, R.layout.dialog_scores_scramble_title, null);
71
    TextView text = levelLayout.findViewById(R.id.scoresScrambleTitle);
72 00af5060 Leszek Koltunski
    text.setText(title);
73 7aff6aa7 Leszek Koltunski
74 5b4eaf7e Leszek Koltunski
    int size = (int)(mHeight*RubikActivity.SCORES_LEVEL_TEXT);
75 362807f0 Leszek Koltunski
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
76
77 7aff6aa7 Leszek Koltunski
    Resources res = act.getResources();
78
    String packageName = act.getPackageName();
79 1c90c64a Leszek Koltunski
    RubikScores scores = RubikScores.getInstance();
80 7aff6aa7 Leszek Koltunski
81 1c90c64a Leszek Koltunski
    boolean inserted = false;
82 7ac0ee88 Leszek Koltunski
    long myRecordInMillis = scores.getRecord(tab, level);
83 79e752b3 Leszek Koltunski
    float myRecordInSeconds = (myRecordInMillis/10)/100.0f;
84 1c90c64a Leszek Koltunski
    String myName = scores.getName();
85 f895e77a Leszek Koltunski
    if( myName.length()==0 ) myName = act.getString(R.string.you);
86 82ce8e64 Leszek Koltunski
    int myCountryID = res.getIdentifier( scores.getCountry(), "drawable", packageName);
87 79e752b3 Leszek Koltunski
    String myRecord = ( myRecordInMillis<RubikScores.NO_RECORD ) ? String.format("%.2f", myRecordInSeconds) : "??";
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 14f14138 Leszek Koltunski
        if( myRecordInSeconds<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 79e752b3 Leszek Koltunski
          theirTime = String.format("%.2f", 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 1c90c64a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
130
131 362807f0 Leszek Koltunski
  private View createRow(FragmentActivity act, int countryID, String name, String time, int height, int color)
132 1c90c64a Leszek Koltunski
    {
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 05c044a5 Leszek Koltunski
    imgCoun.setImageResource(countryID!=0 ? countryID : org.distorted.flags.R.drawable.unknown);
140 1c90c64a Leszek Koltunski
    textName.setText(name);
141
    textTime.setText(time);
142
143
    textName.setTextColor(color);
144
    textTime.setTextColor(color);
145
146 362807f0 Leszek Koltunski
    textName.setTextSize(TypedValue.COMPLEX_UNIT_PX, height);
147
    textTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, height);
148
149 1c90c64a Leszek Koltunski
    return row;
150
    }
151
152 f2a9be6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
153 7aff6aa7 Leszek Koltunski
// needs to run on UI thread
154 f2a9be6d Leszek Koltunski
155 946d9762 Leszek Koltunski
  void prepareView(FragmentActivity act)
156 b8b38548 Leszek Koltunski
    {
157
    removeAllViews();
158
159 e108b57e Leszek Koltunski
    View tab = inflate(act, R.layout.dialog_scores_tab, null);
160 946d9762 Leszek Koltunski
    mLayout = tab.findViewById(R.id.tabLayout);
161 b8b38548 Leszek Koltunski
    addView(tab);
162 946d9762 Leszek Koltunski
    }
163 b8b38548 Leszek Koltunski
164 946d9762 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
165 7aff6aa7 Leszek Koltunski
// needs to run on UI thread
166 b8b38548 Leszek Koltunski
167 d5fb0e7e Leszek Koltunski
  void addSection(LinearLayout section)
168
    {
169
    mLayout.addView(section);
170
    }
171
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173 7aff6aa7 Leszek Koltunski
// needs to run on UI thread
174 d5fb0e7e Leszek Koltunski
175 4895fff6 Leszek Koltunski
  void message(final String mess)
176 946d9762 Leszek Koltunski
    {
177 4895fff6 Leszek Koltunski
    TextView text = findViewById(R.id.message_text);
178 e319fe78 Leszek Koltunski
179
    if( text!=null )
180
      {
181
      text.setText(mess);
182
      }
183 f2a9be6d Leszek Koltunski
    }
184
  }