Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresView.java @ 0a9adc31

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.dialogs;
11

    
12
import android.content.Context;
13
import android.content.res.Resources;
14
import androidx.fragment.app.FragmentActivity;
15
import android.util.AttributeSet;
16
import android.util.TypedValue;
17
import android.view.View;
18
import android.widget.FrameLayout;
19
import android.widget.ImageView;
20
import android.widget.LinearLayout;
21
import android.widget.TextView;
22

    
23
import org.distorted.main.MainActivity;
24
import org.distorted.main.R;
25
import org.distorted.external.RubikScores;
26

    
27
import static org.distorted.external.RubikNetwork.MAX_PLACES;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
public class RubikDialogScoresView extends FrameLayout
32
  {
33
  private static final float SCORES_LEVEL_TEXT = 0.035f;
34
  private static final float SCORES_ITEM_TEXT  = 0.025f;
35

    
36
  private LinearLayout mLayout=null;
37
  private int mHeight;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
  public RubikDialogScoresView(Context context, AttributeSet attrs, int defStyle)
42
    {
43
    super(context, attrs, defStyle);
44
    }
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
  public RubikDialogScoresView(Context context, AttributeSet attrs)
49
    {
50
    super(context, attrs);
51
    }
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
  public RubikDialogScoresView(Context context, int height, boolean isSubmitting, boolean downloadedAlready )
56
    {
57
    super(context);
58

    
59
    mHeight = height;
60

    
61
    if( !downloadedAlready )
62
      {
63
      View view = inflate(context, R.layout.dialog_scores_downloading, null);
64
      addView(view);
65
      TextView text = findViewById(R.id.message_text);
66
      text.setText(context.getString(isSubmitting ? R.string.submitting : R.string.downloading));
67
      }
68
    }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  LinearLayout createSection(FragmentActivity act, int tab, String title, int level, final String[] country, final String[] name, final int[] time)
73
    {
74
    LinearLayout levelLayout = (LinearLayout)inflate(act, R.layout.dialog_scores_scramble_title, null);
75
    TextView text = levelLayout.findViewById(R.id.scoresScrambleTitle);
76
    text.setText(title);
77

    
78
    int size = (int)(mHeight*SCORES_LEVEL_TEXT);
79
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
80

    
81
    Resources res = act.getResources();
82
    String packageName = act.getPackageName();
83
    RubikScores scores = RubikScores.getInstance();
84

    
85
    boolean inserted = false;
86
    int myRecordInMillis = scores.getRecord(tab, level);
87
    String myRecord = ( myRecordInMillis<RubikScores.NO_RECORD ) ? formatRecord(myRecordInMillis) : "??";
88
    String myName = scores.getName();
89
    if( myName.length()==0 ) myName = act.getString(R.string.you);
90
    int myCountryID = res.getIdentifier( scores.getCountry(), "drawable", packageName);
91
    String theirTime;
92
    int theirCountryID;
93

    
94
    MainActivity mact = (MainActivity) act;
95
    int selected = mact.getSelectedColor();
96
    int height = (int)(mHeight*SCORES_ITEM_TEXT);
97
    int white = res.getColor(R.color.white);
98
    int red   = res.getColor(selected);
99
    boolean equals;
100

    
101
    for(int j=0; j<MAX_PLACES-1; j++)
102
      {
103
      if( name[j] != null )
104
        {
105
        if( myRecordInMillis<time[j] && !inserted )
106
          {
107
          inserted = true;
108
          View row = createRow(act, myCountryID, myName, myRecord, height, red);
109
          levelLayout.addView(row);
110
          }
111

    
112
        equals = name[j].equals(myName);
113

    
114
        if( !inserted || !equals )
115
          {
116
          if( equals ) inserted=true;
117
          theirCountryID = res.getIdentifier( country[j], "drawable", packageName);
118
          theirTime = formatRecord(time[j]);
119
          View row = createRow(act, theirCountryID, name[j], theirTime, height, equals ? red:white);
120
          levelLayout.addView(row);
121
          }
122
        }
123
      }
124

    
125
    if( !inserted )
126
      {
127
      View row = createRow(act, myCountryID, myName, myRecord, height, red);
128
      levelLayout.addView(row);
129
      }
130

    
131
    return levelLayout;
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  public static String formatRecord(int time)
137
    {
138
    time /= 10;
139
    int millis = time%100;
140
    time /= 100;
141
    int seconds = time%60;
142
    int minutes = time/60;
143

    
144
    String ret = minutes + (seconds<10 ? ":0" : ":") + seconds + (millis<10 ? ".0" : ".") + millis;
145
    return minutes<10 ? "0"+ret : ret;
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
  private View createRow(FragmentActivity act, int countryID, String name, String time, int height, int color)
151
    {
152
    View row = inflate(act, R.layout.dialog_scores_scramble_row, null);
153

    
154
    ImageView imgCoun = row.findViewById(R.id.scoresScrambleRowCountry);
155
    TextView textName = row.findViewById(R.id.scoresScrambleRowName);
156
    TextView textTime = row.findViewById(R.id.scoresScrambleRowTime);
157

    
158
    imgCoun.setImageResource(countryID!=0 ? countryID : org.distorted.flags.R.drawable.unknown);
159
    textName.setText(name);
160
    textTime.setText(time);
161

    
162
    textName.setTextColor(color);
163
    textTime.setTextColor(color);
164

    
165
    textName.setTextSize(TypedValue.COMPLEX_UNIT_PX, height);
166
    textTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, height);
167

    
168
    return row;
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
// needs to run on UI thread
173

    
174
  void addSection(FragmentActivity act, LinearLayout section)
175
    {
176
    if( mLayout==null )
177
      {
178
      removeAllViews();
179
      View tab = inflate(act, R.layout.dialog_scores_tab, null);
180
      mLayout = tab.findViewById(R.id.tabLayout);
181
      addView(tab);
182
      }
183

    
184
    mLayout.addView(section);
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
// needs to run on UI thread
189

    
190
  void message(final String mess)
191
    {
192
    TextView text = findViewById(R.id.message_text);
193
    if( text!=null ) text.setText(mess);
194
    }
195
  }
(17-17/25)