Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresView.java @ 6a083c6a

1 f2a9be6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 1f9772f3 Leszek Koltunski
package org.distorted.dialogs;
21 f2a9be6d Leszek Koltunski
22 79e752b3 Leszek Koltunski
import android.annotation.SuppressLint;
23 f2a9be6d Leszek Koltunski
import android.content.Context;
24 7aff6aa7 Leszek Koltunski
import android.content.res.Resources;
25 66e777b0 Leszek Koltunski
import androidx.fragment.app.FragmentActivity;
26 f2a9be6d Leszek Koltunski
import android.util.AttributeSet;
27 362807f0 Leszek Koltunski
import android.util.TypedValue;
28 f2a9be6d Leszek Koltunski
import android.view.View;
29
import android.widget.FrameLayout;
30 cc5ec229 Leszek Koltunski
import android.widget.ImageView;
31 b8b38548 Leszek Koltunski
import android.widget.LinearLayout;
32
import android.widget.TextView;
33 f2a9be6d Leszek Koltunski
34 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
35 362807f0 Leszek Koltunski
import org.distorted.main.RubikActivity;
36 9c2f0c91 Leszek Koltunski
import org.distorted.objects.ObjectList;
37 6a083c6a Leszek Koltunski
import org.distorted.network.RubikScores;
38 1c90c64a Leszek Koltunski
39 6a083c6a Leszek Koltunski
import static org.distorted.network.RubikNetwork.MAX_PLACES;
40 211b48f2 Leszek Koltunski
41 f2a9be6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
42
43 0fd3ac1f Leszek Koltunski
public class RubikDialogScoresView extends FrameLayout
44 f2a9be6d Leszek Koltunski
  {
45 362807f0 Leszek Koltunski
  private LinearLayout mLayout;
46 5b4eaf7e Leszek Koltunski
  private int mHeight;
47 f2a9be6d Leszek Koltunski
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50 0fd3ac1f Leszek Koltunski
  public RubikDialogScoresView(Context context, AttributeSet attrs, int defStyle)
51 f2a9be6d Leszek Koltunski
    {
52
    super(context, attrs, defStyle);
53
    }
54
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
57 0fd3ac1f Leszek Koltunski
  public RubikDialogScoresView(Context context, AttributeSet attrs)
58 f2a9be6d Leszek Koltunski
    {
59
    super(context, attrs);
60
    }
61
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64 5b4eaf7e Leszek Koltunski
  public RubikDialogScoresView(Context context, int height, boolean isSubmitting)
65 f2a9be6d Leszek Koltunski
    {
66
    super(context);
67
68 5b4eaf7e Leszek Koltunski
    mHeight = height;
69 362807f0 Leszek Koltunski
70 0fd3ac1f Leszek Koltunski
    View view = inflate(context, R.layout.dialog_scores_downloading, null);
71 b8b38548 Leszek Koltunski
    addView(view);
72 4895fff6 Leszek Koltunski
    TextView text = findViewById(R.id.message_text);
73
    text.setText( context.getString(isSubmitting ? R.string.submitting : R.string.downloading) );
74 f2a9be6d Leszek Koltunski
    }
75
76 a675474f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
77
78 79e752b3 Leszek Koltunski
  @SuppressLint("DefaultLocale")
79 00af5060 Leszek Koltunski
  LinearLayout createSection(FragmentActivity act, int tab, String title, int level, final String[] country, final String[] name, final float[] time)
80 a675474f Leszek Koltunski
    {
81 85b09df4 Leszek Koltunski
    LinearLayout levelLayout = (LinearLayout)inflate(act, R.layout.dialog_scores_scramble_title, null);
82
    TextView text = levelLayout.findViewById(R.id.scoresScrambleTitle);
83 00af5060 Leszek Koltunski
    text.setText(title);
84 7aff6aa7 Leszek Koltunski
85 5b4eaf7e Leszek Koltunski
    int size = (int)(mHeight*RubikActivity.SCORES_LEVEL_TEXT);
86 362807f0 Leszek Koltunski
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
87
88 7aff6aa7 Leszek Koltunski
    Resources res = act.getResources();
89
    String packageName = act.getPackageName();
90
91 9c2f0c91 Leszek Koltunski
    int object   = ObjectList.unpackObject(tab);
92
    int sizeIndex= ObjectList.unpackSizeIndex(tab);
93 1c90c64a Leszek Koltunski
    RubikScores scores = RubikScores.getInstance();
94 7aff6aa7 Leszek Koltunski
95 1c90c64a Leszek Koltunski
    boolean inserted = false;
96 8e3898c8 Leszek Koltunski
    long myRecordInMillis = scores.getRecord(object, sizeIndex, level);
97 79e752b3 Leszek Koltunski
    float myRecordInSeconds = (myRecordInMillis/10)/100.0f;
98 8e3898c8 Leszek Koltunski
    boolean mySubmitted = scores.isSubmitted(object, sizeIndex, level);
99 1c90c64a Leszek Koltunski
    String myName = scores.getName();
100 f895e77a Leszek Koltunski
    if( myName.length()==0 ) myName = act.getString(R.string.you);
101 82ce8e64 Leszek Koltunski
    int myCountryID = res.getIdentifier( scores.getCountry(), "drawable", packageName);
102 79e752b3 Leszek Koltunski
    String myRecord = ( myRecordInMillis<RubikScores.NO_RECORD ) ? String.format("%.2f", myRecordInSeconds) : "??";
103 1c90c64a Leszek Koltunski
    String theirTime;
104
    int theirCountryID;
105 7aff6aa7 Leszek Koltunski
106 5b4eaf7e Leszek Koltunski
    int height = (int)(mHeight* RubikActivity.SCORES_ITEM_TEXT);
107 1c90c64a Leszek Koltunski
    int white = res.getColor(R.color.white);
108
    int red   = res.getColor(R.color.red);
109 2dfe43d3 Leszek Koltunski
    boolean equals;
110 7aff6aa7 Leszek Koltunski
111 1c90c64a Leszek Koltunski
    for(int j=0; j<MAX_PLACES-1; j++)
112
      {
113
      if( name[j] != null )
114
        {
115 286d73ae Leszek Koltunski
        if( !mySubmitted && myRecordInSeconds<time[j] && !inserted )
116 1c90c64a Leszek Koltunski
          {
117
          inserted = true;
118 362807f0 Leszek Koltunski
          View row = createRow(act, myCountryID, myName, myRecord, height, red);
119 85b09df4 Leszek Koltunski
          levelLayout.addView(row);
120 1c90c64a Leszek Koltunski
          }
121
122 2dfe43d3 Leszek Koltunski
        equals = name[j].equals(myName);
123
124
        if( !inserted || !equals )
125
          {
126
          if( equals ) inserted=true;
127
          theirCountryID = res.getIdentifier( country[j], "drawable", packageName);
128 79e752b3 Leszek Koltunski
          theirTime = String.format("%.2f", time[j]);
129 362807f0 Leszek Koltunski
          View row = createRow(act, theirCountryID, name[j], theirTime, height, equals ? red:white);
130 85b09df4 Leszek Koltunski
          levelLayout.addView(row);
131 2dfe43d3 Leszek Koltunski
          }
132 7aff6aa7 Leszek Koltunski
        }
133
      }
134
135 1c90c64a Leszek Koltunski
    if( !inserted )
136
      {
137 362807f0 Leszek Koltunski
      View row = createRow(act, myCountryID, myName, myRecord, height, red);
138 85b09df4 Leszek Koltunski
      levelLayout.addView(row);
139 1c90c64a Leszek Koltunski
      }
140
141 85b09df4 Leszek Koltunski
    return levelLayout;
142 a675474f Leszek Koltunski
    }
143
144 1c90c64a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
145
146 362807f0 Leszek Koltunski
  private View createRow(FragmentActivity act, int countryID, String name, String time, int height, int color)
147 1c90c64a Leszek Koltunski
    {
148
    View row = inflate(act, R.layout.dialog_scores_scramble_row, null);
149
150
    ImageView imgCoun = row.findViewById(R.id.scoresScrambleRowCountry);
151
    TextView textName = row.findViewById(R.id.scoresScrambleRowName);
152
    TextView textTime = row.findViewById(R.id.scoresScrambleRowTime);
153
154 286d73ae Leszek Koltunski
    imgCoun.setImageResource(countryID!=0 ? countryID : R.drawable.un);
155 1c90c64a Leszek Koltunski
    textName.setText(name);
156
    textTime.setText(time);
157
158
    textName.setTextColor(color);
159
    textTime.setTextColor(color);
160
161 362807f0 Leszek Koltunski
    textName.setTextSize(TypedValue.COMPLEX_UNIT_PX, height);
162
    textTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, height);
163
164 1c90c64a Leszek Koltunski
    return row;
165
    }
166
167 f2a9be6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
168 7aff6aa7 Leszek Koltunski
// needs to run on UI thread
169 f2a9be6d Leszek Koltunski
170 946d9762 Leszek Koltunski
  void prepareView(FragmentActivity act)
171 b8b38548 Leszek Koltunski
    {
172
    removeAllViews();
173
174 e108b57e Leszek Koltunski
    View tab = inflate(act, R.layout.dialog_scores_tab, null);
175 946d9762 Leszek Koltunski
    mLayout = tab.findViewById(R.id.tabLayout);
176 b8b38548 Leszek Koltunski
    addView(tab);
177 946d9762 Leszek Koltunski
    }
178 b8b38548 Leszek Koltunski
179 946d9762 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
180 7aff6aa7 Leszek Koltunski
// needs to run on UI thread
181 b8b38548 Leszek Koltunski
182 d5fb0e7e Leszek Koltunski
  void addSection(LinearLayout section)
183
    {
184
    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 e319fe78 Leszek Koltunski
194
    if( text!=null )
195
      {
196
      text.setText(mess);
197
      }
198 f2a9be6d Leszek Koltunski
    }
199
  }