Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresView.java @ b710a574

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.main_old.RubikActivity;
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 LinearLayout mLayout=null;
34
  private int mHeight;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

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

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

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

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

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

    
56
    mHeight = height;
57

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

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

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

    
75
    int size = (int)(mHeight*RubikActivity.SCORES_LEVEL_TEXT);
76
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
77

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

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

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

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

    
107
        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
          theirTime = formatRecord(time[j]);
114
          View row = createRow(act, theirCountryID, name[j], theirTime, height, equals ? red:white);
115
          levelLayout.addView(row);
116
          }
117
        }
118
      }
119

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

    
126
    return levelLayout;
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

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

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

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

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

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

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

    
157
    textName.setTextColor(color);
158
    textTime.setTextColor(color);
159

    
160
    textName.setTextSize(TypedValue.COMPLEX_UNIT_PX, height);
161
    textTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, height);
162

    
163
    return row;
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
// needs to run on UI thread
168

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

    
179
    mLayout.addView(section);
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
// needs to run on UI thread
184

    
185
  void message(final String mess)
186
    {
187
    TextView text = findViewById(R.id.message_text);
188
    if( text!=null ) text.setText(mess);
189
    }
190
  }
(20-20/33)