Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresView.java @ 988e3d33

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.R;
24
import org.distorted.external.RubikScores;
25

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

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

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

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

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

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

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

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

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

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

    
58
    mHeight = height;
59

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

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

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

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

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

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

    
93
    int height = (int)(mHeight*SCORES_ITEM_TEXT);
94
    int white = res.getColor(R.color.white);
95
    int red   = res.getColor(R.color.red);
96
    boolean equals;
97

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

    
109
        equals = name[j].equals(myName);
110

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

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

    
128
    return levelLayout;
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

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

    
141
    String ret = minutes + (seconds<10 ? ":0" : ":") + seconds + (millis<10 ? ".0" : ".") + millis;
142
    return minutes<10 ? "0"+ret : ret;
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

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

    
151
    ImageView imgCoun = row.findViewById(R.id.scoresScrambleRowCountry);
152
    TextView textName = row.findViewById(R.id.scoresScrambleRowName);
153
    TextView textTime = row.findViewById(R.id.scoresScrambleRowTime);
154

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

    
159
    textName.setTextColor(color);
160
    textTime.setTextColor(color);
161

    
162
    textName.setTextSize(TypedValue.COMPLEX_UNIT_PX, height);
163
    textTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, height);
164

    
165
    return row;
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
// needs to run on UI thread
170

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

    
181
    mLayout.addView(section);
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
// needs to run on UI thread
186

    
187
  void message(final String mess)
188
    {
189
    TextView text = findViewById(R.id.message_text);
190
    if( text!=null ) text.setText(mess);
191
    }
192
  }
(16-16/24)